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
dd53c974
Commit
dd53c974
authored
Sep 08, 2017
by
Aiden Nibali
Committed by
Francisco Massa
Sep 08, 2017
Browse files
Add RGBA support to ToPILImage (#189)
parent
7a54c6be
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
0 deletions
+19
-0
test/test_transforms.py
test/test_transforms.py
+16
-0
torchvision/transforms.py
torchvision/transforms.py
+3
-0
No files found.
test/test_transforms.py
View file @
dd53c974
...
@@ -264,6 +264,22 @@ class Tester(unittest.TestCase):
...
@@ -264,6 +264,22 @@ class Tester(unittest.TestCase):
assert
np
.
allclose
(
img_data_short
.
numpy
(),
to_tensor
(
img_short
).
numpy
())
assert
np
.
allclose
(
img_data_short
.
numpy
(),
to_tensor
(
img_short
).
numpy
())
assert
np
.
allclose
(
img_data_int
.
numpy
(),
to_tensor
(
img_int
).
numpy
())
assert
np
.
allclose
(
img_data_int
.
numpy
(),
to_tensor
(
img_int
).
numpy
())
def
test_tensor_rgba_to_pil_image
(
self
):
trans
=
transforms
.
ToPILImage
()
to_tensor
=
transforms
.
ToTensor
()
img_data
=
torch
.
Tensor
(
4
,
4
,
4
).
uniform_
()
img
=
trans
(
img_data
)
assert
img
.
mode
==
'RGBA'
assert
img
.
getbands
()
==
(
'R'
,
'G'
,
'B'
,
'A'
)
r
,
g
,
b
,
a
=
img
.
split
()
expected_output
=
img_data
.
mul
(
255
).
int
().
float
().
div
(
255
)
assert
np
.
allclose
(
expected_output
[
0
].
numpy
(),
to_tensor
(
r
).
numpy
())
assert
np
.
allclose
(
expected_output
[
1
].
numpy
(),
to_tensor
(
g
).
numpy
())
assert
np
.
allclose
(
expected_output
[
2
].
numpy
(),
to_tensor
(
b
).
numpy
())
assert
np
.
allclose
(
expected_output
[
3
].
numpy
(),
to_tensor
(
a
).
numpy
())
def
test_ndarray_to_pil_image
(
self
):
def
test_ndarray_to_pil_image
(
self
):
trans
=
transforms
.
ToPILImage
()
trans
=
transforms
.
ToPILImage
()
img_data
=
torch
.
ByteTensor
(
4
,
4
,
3
).
random_
(
0
,
255
).
numpy
()
img_data
=
torch
.
ByteTensor
(
4
,
4
,
3
).
random_
(
0
,
255
).
numpy
()
...
...
torchvision/transforms.py
View file @
dd53c974
...
@@ -119,6 +119,9 @@ class ToPILImage(object):
...
@@ -119,6 +119,9 @@ class ToPILImage(object):
mode
=
'I'
mode
=
'I'
elif
npimg
.
dtype
==
np
.
float32
:
elif
npimg
.
dtype
==
np
.
float32
:
mode
=
'F'
mode
=
'F'
elif
npimg
.
shape
[
2
]
==
4
:
if
npimg
.
dtype
==
np
.
uint8
:
mode
=
'RGBA'
else
:
else
:
if
npimg
.
dtype
==
np
.
uint8
:
if
npimg
.
dtype
==
np
.
uint8
:
mode
=
'RGB'
mode
=
'RGB'
...
...
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