-
Notifications
You must be signed in to change notification settings - Fork 7.2k
feat: add functional center crop on mask #5961
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
cb36ff6
9e2739b
9a94978
802b9b4
97cfbf8
f6984f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -10,11 +10,11 @@ | |||||
from torch import jit | ||||||
from torch.nn.functional import one_hot | ||||||
from torchvision.prototype import features | ||||||
from torchvision.prototype.transforms.functional._geometry import _center_crop_compute_padding | ||||||
from torchvision.prototype.transforms.functional._meta import convert_bounding_box_format | ||||||
from torchvision.transforms.functional import _get_perspective_coeffs | ||||||
from torchvision.transforms.functional_tensor import _max_value as get_max_value | ||||||
|
||||||
|
||||||
make_tensor = functools.partial(torch.testing.make_tensor, device="cpu") | ||||||
|
||||||
|
||||||
|
@@ -421,6 +421,14 @@ def center_crop_bounding_box(): | |||||
) | ||||||
|
||||||
|
||||||
def center_crop_segmentation_mask(): | ||||||
for mask, output_size in itertools.product( | ||||||
make_segmentation_masks(image_sizes=((16, 16), (7, 33), (31, 9)), extra_dims=((), (4,), (2, 3))), | ||||||
[[4, 3], [42, 70], [4]], # crop sizes < image sizes, crop_sizes > image sizes, single crop size | ||||||
): | ||||||
yield SampleInput(mask, output_size) | ||||||
|
||||||
|
||||||
@pytest.mark.parametrize( | ||||||
"kernel", | ||||||
[ | ||||||
|
@@ -1337,3 +1345,26 @@ def _compute_expected_bbox(bbox, output_size_): | |||||
else: | ||||||
expected_bboxes = expected_bboxes[0] | ||||||
torch.testing.assert_close(output_boxes, expected_bboxes) | ||||||
|
||||||
|
||||||
@pytest.mark.parametrize("device", cpu_and_gpu()) | ||||||
@pytest.mark.parametrize("output_size", [[4, 2], [4], [7, 6]]) | ||||||
def test_correctness_center_crop_segmentation_mask(device, output_size): | ||||||
def _compute_expected_segmentation_mask(mask, output_size): | ||||||
crop_height, crop_width = output_size if len(output_size) > 1 else [output_size[0], output_size[0]] | ||||||
|
||||||
_, image_height, image_width = mask.shape | ||||||
if crop_width > image_height or crop_height > image_width: | ||||||
padding = _center_crop_compute_padding(crop_height, crop_width, image_height, image_width) | ||||||
mask = F.pad_image_tensor(mask, padding, fill=0) | ||||||
|
||||||
left = round((image_width - crop_width) * 0.5) | ||||||
top = round((image_height - crop_height) * 0.5) | ||||||
Comment on lines
+1361
to
+1362
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The kernel has an additional vision/torchvision/prototype/transforms/functional/_geometry.py Lines 585 to 586 in b969cca
@vfdev-5 I recall we had issues with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pmeier I do not quite remember what was the issue with round (maybe jit behaves differently to eager mode). For me the code you mention is more like a definition of crop_top and crop_left. For example, for bboxes we could also keep these values as float but let's define that crop_top/left are rounded integers. |
||||||
|
||||||
return mask[:, top : top + crop_height, left : left + crop_width] | ||||||
|
||||||
mask = torch.randint(0, 2, size=(1, 6, 6), dtype=torch.long, device=device) | ||||||
actual = F.center_crop_segmentation_mask(mask, output_size) | ||||||
|
||||||
expected = _compute_expected_segmentation_mask(mask, output_size) | ||||||
torch.testing.assert_close(expected, actual) |
Uh oh!
There was an error while loading. Please reload this page.