Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dy2St] RandomHorizontalFlip #49590

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions python/paddle/tests/test_transforms_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ def set_trans_api(self):
self.api = transforms.RandomVerticalFlip(prob=1)


class TestRandomHorizontalFlip0(TestTransformUnitTestBase):
def set_trans_api(self):
self.api = transforms.RandomHorizontalFlip(0)


class TestRandomHorizontalFlip1(TestTransformUnitTestBase):
def set_trans_api(self):
self.api = transforms.RandomHorizontalFlip(1)


class TestRandomCrop_random(TestTransformUnitTestBase):
def get_shape(self):
return (3, 240, 240)
Expand Down
13 changes: 13 additions & 0 deletions python/paddle/vision/transforms/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,10 +611,23 @@ def __init__(self, prob=0.5, keys=None):
self.prob = prob

def _apply_image(self, img):
if paddle.in_dynamic_mode():
return self._dynamic_apply_image(img)
else:
return self._static_apply_image(img)

def _dynamic_apply_image(self, img):
if random.random() < self.prob:
return F.hflip(img)
return img

def _static_apply_image(self, img):
return paddle.static.nn.cond(
paddle.rand(shape=(1,)) < self.prob,
lambda: F.hflip(img),
lambda: img,
)


class RandomVerticalFlip(BaseTransform):
"""Vertically flip the input data randomly with a given probability.
Expand Down