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
685799bf
Commit
685799bf
authored
Nov 27, 2016
by
Soumith Chintala
Browse files
adding padding to RandomCrop, as well as transforms.Pad
parent
4d247b08
Changes
4
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
279 additions
and
3 deletions
+279
-3
README.md
README.md
+8
-1
test/sanity_checks.ipynb
test/sanity_checks.ipynb
+234
-0
test/test_transforms.py
test/test_transforms.py
+23
-0
torchvision/transforms.py
torchvision/transforms.py
+14
-2
No files found.
README.md
View file @
685799bf
...
@@ -177,10 +177,11 @@ Crops the given PIL.Image at the center to have a region of
...
@@ -177,10 +177,11 @@ Crops the given PIL.Image at the center to have a region of
the given size. size can be a tuple (target_height, target_width)
the given size. size can be a tuple (target_height, target_width)
or an integer, in which case the target will be of a square shape (size, size)
or an integer, in which case the target will be of a square shape (size, size)
### `RandomCrop(size)`
### `RandomCrop(size
, padding=0
)`
Crops the given PIL.Image at a random location to have a region of
Crops the given PIL.Image at a random location to have a region of
the given size. size can be a tuple (target_height, target_width)
the given size. size can be a tuple (target_height, target_width)
or an integer, in which case the target will be of a square shape (size, size)
or an integer, in which case the target will be of a square shape (size, size)
If
`padding`
is non-zero, then the image is first zero-padded on each side with
`padding`
pixels.
### `RandomHorizontalFlip()`
### `RandomHorizontalFlip()`
Randomly horizontally flips the given PIL.Image with a probability of 0.5
Randomly horizontally flips the given PIL.Image with a probability of 0.5
...
@@ -193,6 +194,12 @@ This is popularly used to train the Inception networks
...
@@ -193,6 +194,12 @@ This is popularly used to train the Inception networks
-
size: size of the smaller edge
-
size: size of the smaller edge
-
interpolation: Default: PIL.Image.BILINEAR
-
interpolation: Default: PIL.Image.BILINEAR
### `Pad(padding, fill=0)`
Pads the given image on each side with
`padding`
number of pixels, and the padding pixels are filled with
pixel value
`fill`
.
If a
`5x5`
image is padded with
`padding=1`
then it becomes
`7x7`
## Transforms on torch.*Tensor
## Transforms on torch.*Tensor
### `Normalize(mean, std)`
### `Normalize(mean, std)`
...
...
test/sanity_checks.ipynb
0 → 100644
View file @
685799bf
This diff is collapsed.
Click to expand it.
test/test_transforms.py
View file @
685799bf
...
@@ -81,6 +81,29 @@ class Tester(unittest.TestCase):
...
@@ -81,6 +81,29 @@ class Tester(unittest.TestCase):
assert
result
.
size
(
1
)
==
oheight
assert
result
.
size
(
1
)
==
oheight
assert
result
.
size
(
2
)
==
owidth
assert
result
.
size
(
2
)
==
owidth
padding
=
random
.
randint
(
1
,
20
)
result
=
transforms
.
Compose
([
transforms
.
ToPILImage
(),
transforms
.
RandomCrop
((
oheight
,
owidth
),
padding
=
padding
),
transforms
.
ToTensor
(),
])(
img
)
assert
result
.
size
(
1
)
==
oheight
assert
result
.
size
(
2
)
==
owidth
def
test_pad
(
self
):
height
=
random
.
randint
(
10
,
32
)
*
2
width
=
random
.
randint
(
10
,
32
)
*
2
img
=
torch
.
ones
(
3
,
height
,
width
)
padding
=
random
.
randint
(
1
,
20
)
result
=
transforms
.
Compose
([
transforms
.
ToPILImage
(),
transforms
.
Pad
(
padding
),
transforms
.
ToTensor
(),
])(
img
)
print
(
height
,
width
,
padding
)
print
(
result
.
size
(
1
),
result
.
size
(
2
))
assert
result
.
size
(
1
)
==
height
+
2
*
padding
assert
result
.
size
(
2
)
==
width
+
2
*
padding
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
...
...
torchvision/transforms.py
View file @
685799bf
...
@@ -2,7 +2,7 @@ from __future__ import division
...
@@ -2,7 +2,7 @@ from __future__ import division
import
torch
import
torch
import
math
import
math
import
random
import
random
from
PIL
import
Image
from
PIL
import
Image
,
ImageOps
import
numpy
as
np
import
numpy
as
np
import
numbers
import
numbers
...
@@ -115,6 +115,18 @@ class CenterCrop(object):
...
@@ -115,6 +115,18 @@ class CenterCrop(object):
return
img
.
crop
((
x1
,
y1
,
x1
+
tw
,
y1
+
th
))
return
img
.
crop
((
x1
,
y1
,
x1
+
tw
,
y1
+
th
))
class
Pad
(
object
):
"""Pads the given PIL.Image on all sides with the given "pad" value"""
def
__init__
(
self
,
padding
,
fill
=
0
):
assert
isinstance
(
padding
,
numbers
.
Number
)
assert
isinstance
(
fill
,
numbers
.
Number
)
self
.
padding
=
padding
self
.
fill
=
fill
def
__call__
(
self
,
img
):
return
ImageOps
.
expand
(
img
,
border
=
self
.
padding
,
fill
=
self
.
fill
)
class
RandomCrop
(
object
):
class
RandomCrop
(
object
):
"""Crops the given PIL.Image at a random location to have a region of
"""Crops the given PIL.Image at a random location to have a region of
the given size. size can be a tuple (target_height, target_width)
the given size. size can be a tuple (target_height, target_width)
...
@@ -129,7 +141,7 @@ class RandomCrop(object):
...
@@ -129,7 +141,7 @@ class RandomCrop(object):
def
__call__
(
self
,
img
):
def
__call__
(
self
,
img
):
if
self
.
padding
>
0
:
if
self
.
padding
>
0
:
raise
NotImplementedError
(
)
img
=
ImageOps
.
expand
(
img
,
border
=
self
.
padding
,
fill
=
0
)
w
,
h
=
img
.
size
w
,
h
=
img
.
size
th
,
tw
=
self
.
size
th
,
tw
=
self
.
size
...
...
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