Skip to content

Commit

Permalink
[fbsync] port rotate (#7713)
Browse files Browse the repository at this point in the history
Reviewed By: matteobettini

Differential Revision: D48642318

fbshipit-source-id: dfcb7ca6a837104bc0acfe3ba2f66bb9ecef267b
  • Loading branch information
NicolasHug authored and facebook-github-bot committed Aug 25, 2023
1 parent 2e5762b commit de45a8b
Show file tree
Hide file tree
Showing 4 changed files with 433 additions and 428 deletions.
74 changes: 0 additions & 74 deletions test/test_transforms_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,80 +541,6 @@ def test__transform_image_mask(self, fill, mocker):
fn.assert_has_calls(calls)


class TestRandomRotation:
def test_assertions(self):
with pytest.raises(ValueError, match="is a single number, it must be positive"):
transforms.RandomRotation(-0.7)

for d in [[-0.7], [-0.7, 0, 0.7]]:
with pytest.raises(ValueError, match="degrees should be a sequence of length 2"):
transforms.RandomRotation(d)

with pytest.raises(TypeError, match="Got inappropriate fill arg"):
transforms.RandomRotation(12, fill="abc")

with pytest.raises(TypeError, match="center should be a sequence of length"):
transforms.RandomRotation(12, center=12)

with pytest.raises(ValueError, match="center should be a sequence of length"):
transforms.RandomRotation(12, center=[1, 2, 3])

def test__get_params(self):
angle_bound = 34
transform = transforms.RandomRotation(angle_bound)

params = transform._get_params(None)
assert -angle_bound <= params["angle"] <= angle_bound

angle_bounds = [12, 34]
transform = transforms.RandomRotation(angle_bounds)

params = transform._get_params(None)
assert angle_bounds[0] <= params["angle"] <= angle_bounds[1]

@pytest.mark.parametrize("degrees", [23, [0, 45], (0, 45)])
@pytest.mark.parametrize("expand", [False, True])
@pytest.mark.parametrize("fill", [0, [1, 2, 3], (2, 3, 4)])
@pytest.mark.parametrize("center", [None, [2.0, 3.0]])
def test__transform(self, degrees, expand, fill, center, mocker):
interpolation = InterpolationMode.BILINEAR
transform = transforms.RandomRotation(
degrees, interpolation=interpolation, expand=expand, fill=fill, center=center
)

if isinstance(degrees, (tuple, list)):
assert transform.degrees == [float(degrees[0]), float(degrees[1])]
else:
assert transform.degrees == [float(-degrees), float(degrees)]

fn = mocker.patch("torchvision.transforms.v2.functional.rotate")
inpt = mocker.MagicMock(spec=datapoints.Image)
# vfdev-5, Feature Request: let's store params as Transform attribute
# This could be also helpful for users
# Otherwise, we can mock transform._get_params
torch.manual_seed(12)
_ = transform(inpt)
torch.manual_seed(12)
params = transform._get_params(inpt)

fill = transforms._utils._convert_fill_arg(fill)
fn.assert_called_once_with(inpt, **params, interpolation=interpolation, expand=expand, fill=fill, center=center)

@pytest.mark.parametrize("angle", [34, -87])
@pytest.mark.parametrize("expand", [False, True])
def test_boundingbox_spatial_size(self, angle, expand):
# Specific test for BoundingBox.rotate
bbox = datapoints.BoundingBox(
torch.tensor([1, 2, 3, 4]), format=datapoints.BoundingBoxFormat.XYXY, spatial_size=(32, 32)
)
img = datapoints.Image(torch.rand(1, 3, 32, 32))

out_img = img.rotate(angle, expand=expand)
out_bbox = bbox.rotate(angle, expand=expand)

assert out_img.spatial_size == out_bbox.spatial_size


class TestRandomCrop:
def test_assertions(self):
with pytest.raises(ValueError, match="Please provide only two dimensions"):
Expand Down
Loading

0 comments on commit de45a8b

Please sign in to comment.