Skip to content

Commit f27ecce

Browse files
KovenYufmassa
authored andcommitted
fix a bug described in issue #488 (#489)
* fix a bug described in issue #488 * improve doc described in issue #488 * add arguments in RandomCrop as proposed by vfdev-5 in PR #489
1 parent 9aff567 commit f27ecce

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

torchvision/transforms/functional.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def scale(*args, **kwargs):
213213

214214

215215
def pad(img, padding, fill=0, padding_mode='constant'):
216-
r"""Pad the given PIL Image on all sides with speficified padding mode and fill value.
216+
r"""Pad the given PIL Image on all sides with specified padding mode and fill value.
217217
218218
Args:
219219
img (PIL Image): Image to be padded.

torchvision/transforms/transforms.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -372,20 +372,42 @@ class RandomCrop(object):
372372
int instead of sequence like (h, w), a square crop (size, size) is
373373
made.
374374
padding (int or sequence, optional): Optional padding on each border
375-
of the image. Default is 0, i.e no padding. If a sequence of length
375+
of the image. Default is None, i.e no padding. If a sequence of length
376376
4 is provided, it is used to pad left, top, right, bottom borders
377-
respectively.
377+
respectively. If a sequence of length 2 is provided, it is used to
378+
pad left/right, top/bottom borders, respectively.
378379
pad_if_needed (boolean): It will pad the image if smaller than the
379380
desired size to avoid raising an exception.
381+
fill: Pixel fill value for constant fill. Default is 0. If a tuple of
382+
length 3, it is used to fill R, G, B channels respectively.
383+
This value is only used when the padding_mode is constant
384+
padding_mode: Type of padding. Should be: constant, edge, reflect or symmetric. Default is constant.
385+
386+
- constant: pads with a constant value, this value is specified with fill
387+
388+
- edge: pads with the last value on the edge of the image
389+
390+
- reflect: pads with reflection of image (without repeating the last value on the edge)
391+
392+
padding [1, 2, 3, 4] with 2 elements on both sides in reflect mode
393+
will result in [3, 2, 1, 2, 3, 4, 3, 2]
394+
395+
- symmetric: pads with reflection of image (repeating the last value on the edge)
396+
397+
padding [1, 2, 3, 4] with 2 elements on both sides in symmetric mode
398+
will result in [2, 1, 1, 2, 3, 4, 4, 3]
399+
380400
"""
381401

382-
def __init__(self, size, padding=0, pad_if_needed=False):
402+
def __init__(self, size, padding=None, pad_if_needed=False, fill=0, padding_mode='constant'):
383403
if isinstance(size, numbers.Number):
384404
self.size = (int(size), int(size))
385405
else:
386406
self.size = size
387407
self.padding = padding
388408
self.pad_if_needed = pad_if_needed
409+
self.fill = fill
410+
self.padding_mode = padding_mode
389411

390412
@staticmethod
391413
def get_params(img, output_size):
@@ -415,15 +437,15 @@ def __call__(self, img):
415437
Returns:
416438
PIL Image: Cropped image.
417439
"""
418-
if self.padding > 0:
419-
img = F.pad(img, self.padding)
440+
if self.padding is not None:
441+
img = F.pad(img, self.padding, self.fill, self.padding_mode)
420442

421443
# pad the width if needed
422444
if self.pad_if_needed and img.size[0] < self.size[1]:
423-
img = F.pad(img, (int((1 + self.size[1] - img.size[0]) / 2), 0))
445+
img = F.pad(img, (int((1 + self.size[1] - img.size[0]) / 2), 0), self.fill, self.padding_mode)
424446
# pad the height if needed
425447
if self.pad_if_needed and img.size[1] < self.size[0]:
426-
img = F.pad(img, (0, int((1 + self.size[0] - img.size[1]) / 2)))
448+
img = F.pad(img, (0, int((1 + self.size[0] - img.size[1]) / 2)), self.fill, self.padding_mode)
427449

428450
i, j, h, w = self.get_params(img, self.size)
429451

0 commit comments

Comments
 (0)