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
37a0d8d6
Unverified
Commit
37a0d8d6
authored
Jun 04, 2020
by
Francisco Massa
Committed by
GitHub
Jun 04, 2020
Browse files
[BC-breaking] Fix for integer fill value in constant padding (#2284)
* Bugfix in pad * Address review comments * Fix lint
parent
39021408
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
16 additions
and
1 deletion
+16
-1
test/test_transforms.py
test/test_transforms.py
+10
-1
torchvision/transforms/functional.py
torchvision/transforms/functional.py
+6
-0
No files found.
test/test_transforms.py
View file @
37a0d8d6
...
...
@@ -299,13 +299,22 @@ class Tester(unittest.TestCase):
width
=
random
.
randint
(
10
,
32
)
*
2
img
=
torch
.
ones
(
3
,
height
,
width
)
padding
=
random
.
randint
(
1
,
20
)
fill
=
random
.
randint
(
1
,
50
)
result
=
transforms
.
Compose
([
transforms
.
ToPILImage
(),
transforms
.
Pad
(
padding
),
transforms
.
Pad
(
padding
,
fill
=
fill
),
transforms
.
ToTensor
(),
])(
img
)
self
.
assertEqual
(
result
.
size
(
1
),
height
+
2
*
padding
)
self
.
assertEqual
(
result
.
size
(
2
),
width
+
2
*
padding
)
# check that all elements in the padded region correspond
# to the pad value
fill_v
=
fill
/
255
eps
=
1e-5
self
.
assertTrue
((
result
[:,
:
padding
,
:]
-
fill_v
).
abs
().
max
()
<
eps
)
self
.
assertTrue
((
result
[:,
:,
:
padding
]
-
fill_v
).
abs
().
max
()
<
eps
)
self
.
assertRaises
(
ValueError
,
transforms
.
Pad
(
padding
,
fill
=
(
1
,
2
)),
transforms
.
ToPILImage
()(
img
))
def
test_pad_with_tuple_of_pad_values
(
self
):
height
=
random
.
randint
(
10
,
32
)
*
2
...
...
torchvision/transforms/functional.py
View file @
37a0d8d6
...
...
@@ -329,6 +329,12 @@ def pad(img, padding, fill=0, padding_mode='constant'):
'Padding mode should be either constant, edge, reflect or symmetric'
if
padding_mode
==
'constant'
:
if
isinstance
(
fill
,
numbers
.
Number
):
fill
=
(
fill
,)
*
len
(
img
.
getbands
())
if
len
(
fill
)
!=
len
(
img
.
getbands
()):
raise
ValueError
(
'fill should have the same number of elements '
'as the number of channels in the image '
'({}), got {} instead'
.
format
(
len
(
img
.
getbands
()),
len
(
fill
)))
if
img
.
mode
==
'P'
:
palette
=
img
.
getpalette
()
image
=
ImageOps
.
expand
(
img
,
border
=
padding
,
fill
=
fill
)
...
...
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