Skip to content

Commit

Permalink
Added sizes check and a test (pytorch#2816)
Browse files Browse the repository at this point in the history
  • Loading branch information
vfdev-5 committed Dec 4, 2020
1 parent 36b09b9 commit cdd750c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions test/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,11 @@ def test_random_crop(self):
self.assertEqual(result.size(1), height + 1)
self.assertEqual(result.size(2), width + 1)

t = transforms.RandomCrop(48)
img = torch.ones(3, 32, 32)
with self.assertRaisesRegex(ValueError, r"Required crop size .+ is larger then input image size .+"):
t(img)

def test_pad(self):
height = random.randint(10, 32) * 2
width = random.randint(10, 32) * 2
Expand Down
6 changes: 6 additions & 0 deletions torchvision/transforms/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,12 @@ def get_params(img: Tensor, output_size: Tuple[int, int]) -> Tuple[int, int, int
"""
w, h = F._get_image_size(img)
th, tw = output_size

if h + 1 < th or w + 1 < tw:
raise ValueError(
"Required crop size {} is larger then input image size {}".format((th, tw), (h, w))
)

if w == tw and h == th:
return 0, 0, h, w

Expand Down

0 comments on commit cdd750c

Please sign in to comment.