From 256495fe4106713a5c106b5cf5454adea2c36180 Mon Sep 17 00:00:00 2001 From: Sasank Chilamkurthy Date: Tue, 26 Sep 2017 23:12:09 +0530 Subject: [PATCH] VerticalFlip converted to follow refactor #240 --- torchvision/transforms.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/torchvision/transforms.py b/torchvision/transforms.py index 4b0675a644f..c8a2911e74e 100644 --- a/torchvision/transforms.py +++ b/torchvision/transforms.py @@ -266,6 +266,21 @@ def hflip(img): return img.transpose(Image.FLIP_LEFT_RIGHT) +def vflip(img): + """Vertically flip the given PIL.Image. + + Args: + img (PIL.Image): Image to be flipped. + + Returns: + PIL.Image: Vertically flipped image. + """ + if not _is_pil_image(img): + raise TypeError('img should be PIL Image. Got {}'.format(type(img))) + + return img.transpose(Image.FLIP_TOP_BOTTOM) + + class Compose(object): """Composes several transforms together. @@ -548,7 +563,7 @@ def __call__(self, img): class RandomVerticalFlip(object): - """Vertically flip the given PIL.Image randomly with a probability of 0.5""" + """Vertically flip the given PIL.Image randomly with a probability of 0.5.""" def __call__(self, img): """ @@ -559,7 +574,7 @@ def __call__(self, img): PIL.Image: Randomly flipped image. """ if random.random() < 0.5: - return img.transpose(Image.FLIP_TOP_BOTTOM) + return vflip(img) return img