Skip to content
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

fix: add appropriate error message when validating padding argument. #8959

Merged
merged 5 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions test/test_transforms_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3022,12 +3022,18 @@ def test_errors(self):
with pytest.raises(ValueError, match="Please provide only two dimensions"):
transforms.RandomCrop([10, 12, 14])

with pytest.raises(TypeError, match="Got inappropriate padding arg"):
with pytest.raises(ValueError, match="Padding must be an int or a 1, 2, or 4"):
transforms.RandomCrop([10, 12], padding="abc")

with pytest.raises(ValueError, match="Padding must be an int or a 1, 2, or 4"):
transforms.RandomCrop([10, 12], padding=[-0.7, 0, 0.7])

with pytest.raises(ValueError, match="Padding must be an int or a 1, 2, or 4"):
transforms.RandomCrop([10, 12], padding=0.5)

with pytest.raises(ValueError, match="Padding must be an int or a 1, 2, or 4"):
transforms.RandomCrop([10, 12], padding=[0.5, 0.5])

with pytest.raises(TypeError, match="Got inappropriate fill arg"):
transforms.RandomCrop([10, 12], padding=1, fill="abc")

Expand Down Expand Up @@ -3892,12 +3898,18 @@ def test_transform(self, make_input):
check_transform(transforms.Pad(padding=[1]), make_input())

def test_transform_errors(self):
with pytest.raises(TypeError, match="Got inappropriate padding arg"):
with pytest.raises(ValueError, match="Padding must be"):
transforms.Pad("abc")

with pytest.raises(ValueError, match="Padding must be an int or a 1, 2, or 4"):
with pytest.raises(ValueError, match="Padding must be an int or a 1, 2, or 4 element of tuple or list"):
transforms.Pad([-0.7, 0, 0.7])

with pytest.raises(ValueError, match="Padding must be an int or a 1, 2, or 4 element of tuple or list"):
transforms.Pad(0.5)

with pytest.raises(ValueError, match="Padding must be an int or a 1, 2, or 4 element of tuple or list"):
transforms.Pad(padding=[0.5, 0.5])

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

Expand Down
10 changes: 6 additions & 4 deletions torchvision/transforms/v2/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ def _get_fill(fill_dict, inpt_type):


def _check_padding_arg(padding: Union[int, Sequence[int]]) -> None:
if not isinstance(padding, (numbers.Number, tuple, list)):
raise TypeError("Got inappropriate padding arg")

if isinstance(padding, (tuple, list)) and len(padding) not in [1, 2, 4]:
raise ValueError(f"Padding must be an int or a 1, 2, or 4 element tuple, not a {len(padding)} element tuple")
err_msg = f"Padding must be an int or a 1, 2, or 4 element of tuple or list, got {padding}."
if isinstance(padding, (tuple, list)):
if len(padding) not in [1, 2, 4] or not all(isinstance(p, int) for p in padding):
raise ValueError(err_msg)
elif not isinstance(padding, int):
raise ValueError(err_msg)


# TODO: let's use torchvision._utils.StrEnum to have the best of both worlds (strings and enums)
Expand Down