Skip to content

Commit 8cc7bab

Browse files
committed
fix a bug described in issue #488
1 parent 4db0398 commit 8cc7bab

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

torchvision/transforms/transforms.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,8 @@ class RandomCrop(object):
367367
padding (int or sequence, optional): Optional padding on each border
368368
of the image. Default is 0, i.e no padding. If a sequence of length
369369
4 is provided, it is used to pad left, top, right, bottom borders
370-
respectively.
370+
respectively. If a sequence of length 2 is provided, it is used to pad
371+
top/bottom, left/right.
371372
pad_if_needed (boolean): It will pad the image if smaller than the
372373
desired size to avoid raising an exception.
373374
"""
@@ -377,6 +378,13 @@ def __init__(self, size, padding=0, pad_if_needed=False):
377378
self.size = (int(size), int(size))
378379
else:
379380
self.size = size
381+
382+
if isinstance(padding, numbers.Number):
383+
padding = (int(padding), int(padding), int(padding), int(padding))
384+
elif len(padding) == 2:
385+
padding = (int(padding[1]), int(padding[0]), int(padding[1]), int(padding[0]))
386+
elif len(padding) != 4:
387+
assert False, "The length of padding must be either 2 or 4, but got {}.".format(len(padding))
380388
self.padding = padding
381389
self.pad_if_needed = pad_if_needed
382390

@@ -408,7 +416,7 @@ def __call__(self, img):
408416
Returns:
409417
PIL Image: Cropped image.
410418
"""
411-
if self.padding > 0:
419+
if any(np.array(self.padding) > 0):
412420
img = F.pad(img, self.padding)
413421

414422
# pad the width if needed

0 commit comments

Comments
 (0)