From 69a1160e1e2201c275b07a44951984884523774c Mon Sep 17 00:00:00 2001 From: 2prime Date: Fri, 28 Jul 2017 13:46:55 +0800 Subject: [PATCH 1/2] Update transforms.py Adding different types of padding: symmetric, edge, reflect --- torchvision/transforms.py | 70 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/torchvision/transforms.py b/torchvision/transforms.py index 6d649ab18fa..952fc54c4a6 100644 --- a/torchvision/transforms.py +++ b/torchvision/transforms.py @@ -11,7 +11,7 @@ import numbers import types import collections - +import functools class Compose(object): """Composes several transforms together. @@ -369,3 +369,71 @@ def __call__(self, img): scale = Scale(self.size, interpolation=self.interpolation) crop = CenterCrop(self.size) return crop(scale(img)) + + def expand_reflect(image, border=0): + """ + Add border to the image(Symmetric padding) + + :param image: The image to expand. + :param border: Border width, in pixels. + :return: An image. + """ + img = np.asarray(image) + img = np.pad(img,pad_width=border,mode="reflect") + return Image.fromarray(np.uint8(img[:,:,2:5])) + +class Reflect_Pad(object): + """Pads the given PIL.Image on all sides with the given "pad" reflect""" + + def __init__(self, padding): + assert isinstance(padding, numbers.Number) + self.padding = padding + + def __call__(self, img): + return expand_reflect(img, border=self.padding) + + +def expand_edge(image, border=0): + """ + Add border to the image(Symmetric padding) + + :param image: The image to expand. + :param border: Border width, in pixels. + :return: An image. + """ + img = np.asarray(image) + img = np.pad(img,pad_width=border,mode="edge") + return Image.fromarray(np.uint8(img[:,:,2:5])) + +class Edge_Pad(object): + """Pads the given PIL.Image on all sides with the given "pad":edge pad """ + + def __init__(self, padding): + assert isinstance(padding, numbers.Number) + self.padding = padding + + def __call__(self, img): + return expand_edge(img, border=self.padding) + + +def expand_symmetric(image, border=0): + """ + Add border to the image(Symmetric padding) + + :param image: The image to expand. + :param border: Border width, in pixels. + :return: An image. + """ + img = np.asarray(image) + img = np.pad(img,pad_width=border,mode="symmetric") + return Image.fromarray(np.uint8(img[:,:,2:5])) + +class Symmetric_Pad(object): + """Pads the given PIL.Image on all sides with the given "pad":symmetric pad """ + + def __init__(self, padding): + assert isinstance(padding, numbers.Number) + self.padding = padding + + def __call__(self, img): + return expand_symmetric(img, border=self.padding) From ed1e0c8bfdefe95ece736d6bb2e1962cec100143 Mon Sep 17 00:00:00 2001 From: 2prime Date: Fri, 28 Jul 2017 13:59:01 +0800 Subject: [PATCH 2/2] Update transforms.py --- torchvision/transforms.py | 114 ++++++++++++++++---------------------- 1 file changed, 48 insertions(+), 66 deletions(-) diff --git a/torchvision/transforms.py b/torchvision/transforms.py index 952fc54c4a6..2d71e7fef17 100644 --- a/torchvision/transforms.py +++ b/torchvision/transforms.py @@ -236,13 +236,52 @@ class Pad(object): padding (int or sequence): Padding on each border. If a sequence of length 4, it is used to pad left, top, right and bottom borders respectively. fill: Pixel fill value. Default is 0. + type:padding type: constant,reflect,edge,symmetric """ - def __init__(self, padding, fill=0): + def __init__(self, padding, fill=0,type="constant"): assert isinstance(padding, numbers.Number) assert isinstance(fill, numbers.Number) or isinstance(fill, str) or isinstance(fill, tuple) self.padding = padding self.fill = fill + assert (type in ["constant","edge","symmetric","reflect"]) + self.type = type + + def __expand_reflect(slef,image, border=0): + """ + Add border to the image(Symmetric padding) + + :param image: The image to expand. + :param border: Border width, in pixels. + :return: An image. + """ + img = np.asarray(image) + img = np.pad(img, pad_width=border, mode="reflect") + return Image.fromarray(np.uint8(img)) + + def __expand_edge(self,image, border=0): + """ + Add border to the image(Symmetric padding) + + :param image: The image to expand. + :param border: Border width, in pixels. + :return: An image. + """ + img = np.asarray(image) + img = np.pad(img, pad_width=border, mode="edge") + return Image.fromarray(np.uint8(img)) + + def __expand_symmetric(self,image, border=0): + """ + Add border to the image(Symmetric padding) + + :param image: The image to expand. + :param border: Border width, in pixels. + :return: An image. + """ + img = np.asarray(image) + img = np.pad(img, pad_width=border, mode="symmetric") + return Image.fromarray(np.uint8(img)) def __call__(self, img): """ @@ -252,7 +291,14 @@ def __call__(self, img): Returns: PIL.Image: Padded image. """ - return ImageOps.expand(img, border=self.padding, fill=self.fill) + if self.type == "constant": + return ImageOps.expand(img, border=self.padding, fill=self.fill) + elif self.type == "symmetric": + return self.__expand_symmetric(img, border=self.padding) + elif self.type == "reflect": + return self.__expand_reflect(img, border=self.padding) + elif self.type == "edge": + return self.__expand_edge(img, border=self.padding) class Lambda(object): @@ -369,71 +415,7 @@ def __call__(self, img): scale = Scale(self.size, interpolation=self.interpolation) crop = CenterCrop(self.size) return crop(scale(img)) - - def expand_reflect(image, border=0): - """ - Add border to the image(Symmetric padding) - - :param image: The image to expand. - :param border: Border width, in pixels. - :return: An image. - """ - img = np.asarray(image) - img = np.pad(img,pad_width=border,mode="reflect") - return Image.fromarray(np.uint8(img[:,:,2:5])) -class Reflect_Pad(object): - """Pads the given PIL.Image on all sides with the given "pad" reflect""" - def __init__(self, padding): - assert isinstance(padding, numbers.Number) - self.padding = padding - def __call__(self, img): - return expand_reflect(img, border=self.padding) - -def expand_edge(image, border=0): - """ - Add border to the image(Symmetric padding) - - :param image: The image to expand. - :param border: Border width, in pixels. - :return: An image. - """ - img = np.asarray(image) - img = np.pad(img,pad_width=border,mode="edge") - return Image.fromarray(np.uint8(img[:,:,2:5])) - -class Edge_Pad(object): - """Pads the given PIL.Image on all sides with the given "pad":edge pad """ - - def __init__(self, padding): - assert isinstance(padding, numbers.Number) - self.padding = padding - - def __call__(self, img): - return expand_edge(img, border=self.padding) - - -def expand_symmetric(image, border=0): - """ - Add border to the image(Symmetric padding) - - :param image: The image to expand. - :param border: Border width, in pixels. - :return: An image. - """ - img = np.asarray(image) - img = np.pad(img,pad_width=border,mode="symmetric") - return Image.fromarray(np.uint8(img[:,:,2:5])) - -class Symmetric_Pad(object): - """Pads the given PIL.Image on all sides with the given "pad":symmetric pad """ - - def __init__(self, padding): - assert isinstance(padding, numbers.Number) - self.padding = padding - - def __call__(self, img): - return expand_symmetric(img, border=self.padding)