Skip to content

Commit

Permalink
Introduced vertical flipping in RandomFlip. (facebookresearch#643)
Browse files Browse the repository at this point in the history
Summary:
Fixes facebookresearch#507.
Pull Request resolved: facebookresearch#643

Differential Revision: D19314245

Pulled By: ppwwyyxx

fbshipit-source-id: 57f4665ebf1532a77197f75174332d34383733bb
  • Loading branch information
maxfrei750 authored and facebook-github-bot committed Jan 9, 2020
1 parent e0bffda commit 7667b16
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions detectron2/data/transforms/transform_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
NoOpTransform,
Transform,
TransformList,
VFlipTransform,
)
from PIL import Image

Expand Down Expand Up @@ -113,31 +114,32 @@ def __repr__(self):

class RandomFlip(TransformGen):
"""
Flip the image horizontally with the given probability.
TODO Vertical flip to be implemented.
Flip the image horizontally or vertically with the given probability.
"""

def __init__(self, prob=0.5):
def __init__(self, prob=0.5, *, horizontal=True, vertical=False):
"""
Args:
prob (float): probability of flip.
horizontal (boolean): whether to apply horizontal flipping
vertical (boolean): whether to apply vertical flipping
"""
horiz, vert = True, False
# TODO implement vertical flip when we need it
super().__init__()

if horiz and vert:
if horizontal and vertical:
raise ValueError("Cannot do both horiz and vert. Please use two Flip instead.")
if not horiz and not vert:
if not horizontal and not vertical:
raise ValueError("At least one of horiz or vert has to be True!")
self._init(locals())

def get_transform(self, img):
_, w = img.shape[:2]
h, w = img.shape[:2]
do = self._rand_range() < self.prob
if do:
return HFlipTransform(w)
if self.horizontal:
return HFlipTransform(w)
elif self.vertical:
return VFlipTransform(h)
else:
return NoOpTransform()

Expand Down

0 comments on commit 7667b16

Please sign in to comment.