Skip to content

Commit c97421c

Browse files
NicolasHugfacebook-github-bot
authored andcommitted
[fbsync] port tests for transforms.RandomZoomOut (#7975)
Reviewed By: matteobettini Differential Revision: D49600770 fbshipit-source-id: 14a0a02554b441c8d359955538ef115543931908
1 parent 7bd5976 commit c97421c

File tree

3 files changed

+65
-30
lines changed

3 files changed

+65
-30
lines changed

test/test_transforms_v2.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ class TestSmoke:
136136
(transforms.RandomRotation(degrees=30), None),
137137
(transforms.RandomShortestSize(min_size=10, antialias=True), None),
138138
(transforms.RandomVerticalFlip(p=1.0), None),
139-
(transforms.RandomZoomOut(p=1.0), None),
140139
(transforms.Resize([16, 16], antialias=True), None),
141140
(transforms.ScaleJitter((16, 16), scale_range=(0.8, 1.2), antialias=True), None),
142141
(transforms.ClampBoundingBoxes(), None),
@@ -388,34 +387,6 @@ def was_applied(output, inpt):
388387
assert transform.was_applied(output, input)
389388

390389

391-
class TestRandomZoomOut:
392-
def test_assertions(self):
393-
with pytest.raises(TypeError, match="Got inappropriate fill arg"):
394-
transforms.RandomZoomOut(fill="abc")
395-
396-
with pytest.raises(TypeError, match="should be a sequence of length"):
397-
transforms.RandomZoomOut(0, side_range=0)
398-
399-
with pytest.raises(ValueError, match="Invalid canvas side range"):
400-
transforms.RandomZoomOut(0, side_range=[4.0, 1.0])
401-
402-
@pytest.mark.parametrize("fill", [0, [1, 2, 3], (2, 3, 4)])
403-
@pytest.mark.parametrize("side_range", [(1.0, 4.0), [2.0, 5.0]])
404-
def test__get_params(self, fill, side_range):
405-
transform = transforms.RandomZoomOut(fill=fill, side_range=side_range)
406-
407-
h, w = size = (24, 32)
408-
image = make_image(size)
409-
410-
params = transform._get_params([image])
411-
412-
assert len(params["padding"]) == 4
413-
assert 0 <= params["padding"][0] <= (side_range[1] - 1) * w
414-
assert 0 <= params["padding"][1] <= (side_range[1] - 1) * h
415-
assert 0 <= params["padding"][2] <= (side_range[1] - 1) * w
416-
assert 0 <= params["padding"][3] <= (side_range[1] - 1) * h
417-
418-
419390
class TestElasticTransform:
420391
def test_assertions(self):
421392

test/test_transforms_v2_refactored.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4000,3 +4000,67 @@ def test_random_transform_correctness(self, num_input_channels):
40004000
expected = F.to_image(F.rgb_to_grayscale(F.to_pil_image(image), num_output_channels=num_input_channels))
40014001

40024002
assert_equal(actual, expected, rtol=0, atol=1)
4003+
4004+
4005+
class TestRandomZoomOut:
4006+
# Tests are light because this largely relies on the already tested `pad` kernels.
4007+
4008+
@pytest.mark.parametrize(
4009+
"make_input",
4010+
[
4011+
make_image_tensor,
4012+
make_image_pil,
4013+
make_image,
4014+
make_bounding_boxes,
4015+
make_segmentation_mask,
4016+
make_detection_mask,
4017+
make_video,
4018+
],
4019+
)
4020+
def test_transform(self, make_input):
4021+
check_transform(transforms.RandomZoomOut(p=1), make_input())
4022+
4023+
def test_transform_error(self):
4024+
for side_range in [None, 1, [1, 2, 3]]:
4025+
with pytest.raises(
4026+
ValueError if isinstance(side_range, list) else TypeError, match="should be a sequence of length 2"
4027+
):
4028+
transforms.RandomZoomOut(side_range=side_range)
4029+
4030+
for side_range in [[0.5, 1.5], [2.0, 1.0]]:
4031+
with pytest.raises(ValueError, match="Invalid side range"):
4032+
transforms.RandomZoomOut(side_range=side_range)
4033+
4034+
@pytest.mark.parametrize("side_range", [(1.0, 4.0), [2.0, 5.0]])
4035+
@pytest.mark.parametrize(
4036+
"make_input",
4037+
[
4038+
make_image_tensor,
4039+
make_image_pil,
4040+
make_image,
4041+
make_bounding_boxes,
4042+
make_segmentation_mask,
4043+
make_detection_mask,
4044+
make_video,
4045+
],
4046+
)
4047+
@pytest.mark.parametrize("device", cpu_and_cuda())
4048+
def test_transform_params_correctness(self, side_range, make_input, device):
4049+
if make_input is make_image_pil and device != "cpu":
4050+
pytest.skip("PIL image tests with parametrization device!='cpu' will degenerate to that anyway.")
4051+
4052+
transform = transforms.RandomZoomOut(side_range=side_range)
4053+
4054+
input = make_input()
4055+
height, width = F.get_size(input)
4056+
4057+
params = transform._get_params([input])
4058+
assert "padding" in params
4059+
4060+
padding = params["padding"]
4061+
assert len(padding) == 4
4062+
4063+
assert 0 <= padding[0] <= (side_range[1] - 1) * width
4064+
assert 0 <= padding[1] <= (side_range[1] - 1) * height
4065+
assert 0 <= padding[2] <= (side_range[1] - 1) * width
4066+
assert 0 <= padding[3] <= (side_range[1] - 1) * height

torchvision/transforms/v2/_geometry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ def __init__(
546546

547547
self.side_range = side_range
548548
if side_range[0] < 1.0 or side_range[0] > side_range[1]:
549-
raise ValueError(f"Invalid canvas side range provided {side_range}.")
549+
raise ValueError(f"Invalid side range provided {side_range}.")
550550

551551
def _get_params(self, flat_inputs: List[Any]) -> Dict[str, Any]:
552552
orig_h, orig_w = query_size(flat_inputs)

0 commit comments

Comments
 (0)