Skip to content

Commit 342a3d8

Browse files
vfdev-5fmassa
authored andcommitted
Added sizes check and a test (#2816)
1 parent b5db5e8 commit 342a3d8

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

test/test_transforms.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,11 @@ def test_random_crop(self):
298298
self.assertEqual(result.size(1), height + 1)
299299
self.assertEqual(result.size(2), width + 1)
300300

301+
t = transforms.RandomCrop(48)
302+
img = torch.ones(3, 32, 32)
303+
with self.assertRaisesRegex(ValueError, r"Required crop size .+ is larger then input image size .+"):
304+
t(img)
305+
301306
def test_pad(self):
302307
height = random.randint(10, 32) * 2
303308
width = random.randint(10, 32) * 2

torchvision/transforms/transforms.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,12 @@ def get_params(img: Tensor, output_size: Tuple[int, int]) -> Tuple[int, int, int
532532
"""
533533
w, h = F._get_image_size(img)
534534
th, tw = output_size
535+
536+
if h + 1 < th or w + 1 < tw:
537+
raise ValueError(
538+
"Required crop size {} is larger then input image size {}".format((th, tw), (h, w))
539+
)
540+
535541
if w == tw and h == th:
536542
return 0, 0, h, w
537543

0 commit comments

Comments
 (0)