diff --git a/test/test_transforms_v2.py b/test/test_transforms_v2.py index 0db64dec2d1..a9fd3bc5ec9 100644 --- a/test/test_transforms_v2.py +++ b/test/test_transforms_v2.py @@ -3758,12 +3758,18 @@ def test_transform_errors_warnings(self): with pytest.raises(ValueError, match="provide only two dimensions"): transforms.RandomResizedCrop(size=(1, 2, 3)) - with pytest.raises(TypeError, match="Scale should be a sequence"): + with pytest.raises(TypeError, match="Scale should be a sequence of two floats."): transforms.RandomResizedCrop(size=self.INPUT_SIZE, scale=123) - with pytest.raises(TypeError, match="Ratio should be a sequence"): + with pytest.raises(TypeError, match="Ratio should be a sequence of two floats."): transforms.RandomResizedCrop(size=self.INPUT_SIZE, ratio=123) + with pytest.raises(TypeError, match="Ratio should be a sequence of two floats."): + transforms.RandomResizedCrop(size=self.INPUT_SIZE, ratio=[1, 2, 3]) + + with pytest.raises(TypeError, match="Scale should be a sequence of two floats."): + transforms.RandomResizedCrop(size=self.INPUT_SIZE, scale=[1, 2, 3]) + for param in ["scale", "ratio"]: with pytest.warns(match="Scale and ratio should be of kind"): transforms.RandomResizedCrop(size=self.INPUT_SIZE, **{param: [1, 0]}) diff --git a/torchvision/transforms/v2/_geometry.py b/torchvision/transforms/v2/_geometry.py index c2461418a42..c615515b943 100644 --- a/torchvision/transforms/v2/_geometry.py +++ b/torchvision/transforms/v2/_geometry.py @@ -254,10 +254,10 @@ def __init__( super().__init__() self.size = _setup_size(size, error_msg="Please provide only two dimensions (h, w) for size.") - if not isinstance(scale, Sequence): - raise TypeError("Scale should be a sequence") - if not isinstance(ratio, Sequence): - raise TypeError("Ratio should be a sequence") + if not isinstance(scale, Sequence) or len(scale) != 2: + raise TypeError("Scale should be a sequence of two floats.") + if not isinstance(ratio, Sequence) or len(ratio) != 2: + raise TypeError("Ratio should be a sequence of two floats.") if (scale[0] > scale[1]) or (ratio[0] > ratio[1]): warnings.warn("Scale and ratio should be of kind (min, max)")