Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
vision
Commits
fbe4ad58
Commit
fbe4ad58
authored
Sep 26, 2017
by
Alykhan Tejani
Committed by
GitHub
Sep 26, 2017
Browse files
add RandomVerticalFlip transform (#262)
add RandomVerticalFlip transform
parent
459dc59e
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
49 additions
and
0 deletions
+49
-0
test/test_transforms.py
test/test_transforms.py
+33
-0
torchvision/transforms.py
torchvision/transforms.py
+16
-0
No files found.
test/test_transforms.py
View file @
fbe4ad58
...
@@ -9,6 +9,11 @@ try:
...
@@ -9,6 +9,11 @@ try:
except
ImportError
:
except
ImportError
:
accimage
=
None
accimage
=
None
try
:
from
scipy
import
stats
except
ImportError
:
stats
=
None
GRACE_HOPPER
=
'assets/grace_hopper_517x606.jpg'
GRACE_HOPPER
=
'assets/grace_hopper_517x606.jpg'
...
@@ -327,6 +332,34 @@ class Tester(unittest.TestCase):
...
@@ -327,6 +332,34 @@ class Tester(unittest.TestCase):
assert
img
.
mode
==
'I'
assert
img
.
mode
==
'I'
assert
np
.
allclose
(
img
,
img_data
[:,
:,
0
])
assert
np
.
allclose
(
img
,
img_data
[:,
:,
0
])
@
unittest
.
skipIf
(
stats
is
None
,
'scipy.stats not available'
)
def
test_random_vertical_flip
(
self
):
img
=
transforms
.
ToPILImage
()(
torch
.
rand
(
3
,
10
,
10
))
vimg
=
img
.
transpose
(
Image
.
FLIP_TOP_BOTTOM
)
num_vertical
=
0
for
_
in
range
(
100
):
out
=
transforms
.
RandomVerticalFlip
()(
img
)
if
out
==
vimg
:
num_vertical
+=
1
p_value
=
stats
.
binom_test
(
num_vertical
,
100
,
p
=
0.5
)
assert
p_value
>
0.05
@
unittest
.
skipIf
(
stats
is
None
,
'scipy.stats not available'
)
def
test_random_horizontal_flip
(
self
):
img
=
transforms
.
ToPILImage
()(
torch
.
rand
(
3
,
10
,
10
))
himg
=
img
.
transpose
(
Image
.
FLIP_LEFT_RIGHT
)
num_horizontal
=
0
for
_
in
range
(
100
):
out
=
transforms
.
RandomHorizontalFlip
()(
img
)
if
out
==
himg
:
num_horizontal
+=
1
p_value
=
stats
.
binom_test
(
num_horizontal
,
100
,
p
=
0.5
)
assert
p_value
>
0.05
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
torchvision/transforms.py
View file @
fbe4ad58
...
@@ -547,6 +547,22 @@ class RandomHorizontalFlip(object):
...
@@ -547,6 +547,22 @@ class RandomHorizontalFlip(object):
return
img
return
img
class
RandomVerticalFlip
(
object
):
"""Vertically flip the given PIL.Image randomly with a probability of 0.5"""
def
__call__
(
self
,
img
):
"""
Args:
img (PIL.Image): Image to be flipped.
Returns:
PIL.Image: Randomly flipped image.
"""
if
random
.
random
()
<
0.5
:
return
img
.
transpose
(
Image
.
FLIP_TOP_BOTTOM
)
return
img
class
RandomSizedCrop
(
object
):
class
RandomSizedCrop
(
object
):
"""Crop the given PIL.Image to random size and aspect ratio.
"""Crop the given PIL.Image to random size and aspect ratio.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment