Add SanitizeKeyPoints
transform to remove keypoints outside of the image area
#9235
+671
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context
This PR proposes to add a
SanitizeKeyPoints
transform, similar to the existingSanitizeBoundingBoxes
(#7246). This transform removes keypoints lying outside of the valid image area, which can happen after geometric transformations with the new defaultclamping_mode
proposed in #9234, which allows for disabling automatic clamping of keypoints.This implementation follows the proposal in issue #9223 as a solution for the issue that the previous default clamping of keypoints to the image edges modifies their position and creates a misalignment with the actual locations in the transformed image.
This PR hence only makes sense in combination with new clamping modes such as the one proposed in #9234.
Implementation details
To understand the behavior of the proposed
SanitizeKeyPoints
transform, we need to distinguish two cases of keypoint formats:tv_tensors.KeyPoints
contains a set of keypoints of shape[N_points, 2]
or[N_points, 1, 2]
. In this case, the transform will remove all keypoints lying outside of the valid image region.tv_tensors.KeyPoints
contains groups of keypoints, i.e., several objects, each consisting of a certain number of keypoints (e.g., polygons, polygonal chains, skeletons etc.). It is a tensor of shape[N_objects, N_points, 2]
or, in general,[N_objects, ..., 2]
. In this case, the transform will remove all objects (first dimension) that have at least a certain number of keypoints lying outside of the valid image region.The behavior of the transform can be controlled with the following arguments:
min_valid_edge_distance
(int): The minimum distance that keypoints need to be away from the closest image edge along any axis in order to be considered valid. For example, setting this to 0 will only invalidate/remove keypoints outside of the image area, while a value of 1 will also remove keypoints lying exactly on the edge. Default is 0.min_invalid_points
(int or float): Minimum number or fraction of invalid keypoints required for a group of keypoints to be removed. For example, setting this to 1 will remove a group of keypoints if any of its keypoints is invalid, while setting it to 2 will only remove groups with at least 2 invalid keypoints. If a float in(0.0, 1.0]
is passed, it represents a fraction of the total number of keypoints in the group. For example, setting this to 0.3 will remove groups of keypoints with at least 30% invalid keypoints. Note that a value of1
(integer) is very different from1.0
(float). The former will remove groups with any invalid keypoint, while the latter will only remove groups where all keypoints are invalid. Default is 1 (int).In addition, the transform can also remove labels associated with the keypoints (or elements from any other tensors with the same first dimension as the keypoints). This can be achieved by setting the
labels_getter
argument, which follows the same logic as the homonymous argument ofSanitizeBoundingBoxes
. The only difference is, that the default forSanitizeKeyPoints
isNone
, in order to avoid accidental conflicts with any additionally present bounding box labels.Illustration of the changes
The following example additionally requires PR #9234.
Unsanitized keypoint coordinates:
Sanitization:
Testing
Please run the following unit tests: