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
34a8a37c
Unverified
Commit
34a8a37c
authored
Jun 15, 2021
by
Anirudh
Committed by
GitHub
Jun 15, 2021
Browse files
Refactor test_transforms.py into classes (#4030)
parent
b74366b8
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
336 additions
and
356 deletions
+336
-356
test/test_transforms.py
test/test_transforms.py
+336
-356
No files found.
test/test_transforms.py
View file @
34a8a37c
...
@@ -195,8 +195,10 @@ class TestAccImage:
...
@@ -195,8 +195,10 @@ class TestAccImage:
torch
.
testing
.
assert_close
(
output
,
expected_output
)
torch
.
testing
.
assert_close
(
output
,
expected_output
)
@
pytest
.
mark
.
parametrize
(
'channels'
,
[
1
,
3
,
4
])
class
TestToTensor
:
def
test_to_tensor
(
channels
):
@
pytest
.
mark
.
parametrize
(
'channels'
,
[
1
,
3
,
4
])
def
test_to_tensor
(
self
,
channels
):
height
,
width
=
4
,
4
height
,
width
=
4
,
4
trans
=
transforms
.
ToTensor
()
trans
=
transforms
.
ToTensor
()
...
@@ -221,8 +223,7 @@ def test_to_tensor(channels):
...
@@ -221,8 +223,7 @@ def test_to_tensor(channels):
output
=
trans
(
img
)
output
=
trans
(
img
)
torch
.
testing
.
assert_close
(
input_data
,
output
,
check_dtype
=
False
,
check_stride
=
False
)
torch
.
testing
.
assert_close
(
input_data
,
output
,
check_dtype
=
False
,
check_stride
=
False
)
def
test_to_tensor_errors
(
self
):
def
test_to_tensor_errors
():
height
,
width
=
4
,
4
height
,
width
=
4
,
4
trans
=
transforms
.
ToTensor
()
trans
=
transforms
.
ToTensor
()
...
@@ -235,9 +236,8 @@ def test_to_tensor_errors():
...
@@ -235,9 +236,8 @@ def test_to_tensor_errors():
with
pytest
.
raises
(
ValueError
):
with
pytest
.
raises
(
ValueError
):
trans
(
np
.
random
.
rand
(
1
,
1
,
height
,
width
))
trans
(
np
.
random
.
rand
(
1
,
1
,
height
,
width
))
@
pytest
.
mark
.
parametrize
(
'dtype'
,
[
torch
.
float16
,
torch
.
float
,
torch
.
double
])
@
pytest
.
mark
.
parametrize
(
'dtype'
,
[
torch
.
float16
,
torch
.
float
,
torch
.
double
])
def
test_to_tensor_with_other_default_dtypes
(
self
,
dtype
):
def
test_to_tensor_with_other_default_dtypes
(
dtype
):
current_def_dtype
=
torch
.
get_default_dtype
()
current_def_dtype
=
torch
.
get_default_dtype
()
t
=
transforms
.
ToTensor
()
t
=
transforms
.
ToTensor
()
...
@@ -250,9 +250,8 @@ def test_to_tensor_with_other_default_dtypes(dtype):
...
@@ -250,9 +250,8 @@ def test_to_tensor_with_other_default_dtypes(dtype):
torch
.
set_default_dtype
(
current_def_dtype
)
torch
.
set_default_dtype
(
current_def_dtype
)
@
pytest
.
mark
.
parametrize
(
'channels'
,
[
1
,
3
,
4
])
@
pytest
.
mark
.
parametrize
(
'channels'
,
[
1
,
3
,
4
])
def
test_pil_to_tensor
(
self
,
channels
):
def
test_pil_to_tensor
(
channels
):
height
,
width
=
4
,
4
height
,
width
=
4
,
4
trans
=
transforms
.
PILToTensor
()
trans
=
transforms
.
PILToTensor
()
...
@@ -279,8 +278,7 @@ def test_pil_to_tensor(channels):
...
@@ -279,8 +278,7 @@ def test_pil_to_tensor(channels):
output
=
trans
(
img
).
view
(
torch
.
uint8
).
bool
().
to
(
torch
.
uint8
)
output
=
trans
(
img
).
view
(
torch
.
uint8
).
bool
().
to
(
torch
.
uint8
)
torch
.
testing
.
assert_close
(
input_data
,
output
,
check_stride
=
False
)
torch
.
testing
.
assert_close
(
input_data
,
output
,
check_stride
=
False
)
def
test_pil_to_tensor_errors
(
self
):
def
test_pil_to_tensor_errors
():
height
,
width
=
4
,
4
height
,
width
=
4
,
4
trans
=
transforms
.
PILToTensor
()
trans
=
transforms
.
PILToTensor
()
...
@@ -537,7 +535,9 @@ def test_randomness(fn, trans, config, p):
...
@@ -537,7 +535,9 @@ def test_randomness(fn, trans, config, p):
assert
p_value
>
0.0001
assert
p_value
>
0.0001
def
_get_1_channel_tensor_various_types
():
class
TestToPil
:
def
_get_1_channel_tensor_various_types
():
img_data_float
=
torch
.
Tensor
(
1
,
4
,
4
).
uniform_
()
img_data_float
=
torch
.
Tensor
(
1
,
4
,
4
).
uniform_
()
expected_output
=
img_data_float
.
mul
(
255
).
int
().
float
().
div
(
255
).
numpy
()
expected_output
=
img_data_float
.
mul
(
255
).
int
().
float
().
div
(
255
).
numpy
()
yield
img_data_float
,
expected_output
,
'L'
yield
img_data_float
,
expected_output
,
'L'
...
@@ -554,10 +554,26 @@ def _get_1_channel_tensor_various_types():
...
@@ -554,10 +554,26 @@ def _get_1_channel_tensor_various_types():
expected_output
=
img_data_int
.
numpy
()
expected_output
=
img_data_int
.
numpy
()
yield
img_data_int
,
expected_output
,
'I'
yield
img_data_int
,
expected_output
,
'I'
def
_get_2d_tensor_various_types
():
img_data_float
=
torch
.
Tensor
(
4
,
4
).
uniform_
()
expected_output
=
img_data_float
.
mul
(
255
).
int
().
float
().
div
(
255
).
numpy
()
yield
img_data_float
,
expected_output
,
'L'
img_data_byte
=
torch
.
ByteTensor
(
4
,
4
).
random_
(
0
,
255
)
expected_output
=
img_data_byte
.
float
().
div
(
255.0
).
numpy
()
yield
img_data_byte
,
expected_output
,
'L'
img_data_short
=
torch
.
ShortTensor
(
4
,
4
).
random_
()
expected_output
=
img_data_short
.
numpy
()
yield
img_data_short
,
expected_output
,
'I;16'
img_data_int
=
torch
.
IntTensor
(
4
,
4
).
random_
()
expected_output
=
img_data_int
.
numpy
()
yield
img_data_int
,
expected_output
,
'I'
@
pytest
.
mark
.
parametrize
(
'with_mode'
,
[
False
,
True
])
@
pytest
.
mark
.
parametrize
(
'with_mode'
,
[
False
,
True
])
@
pytest
.
mark
.
parametrize
(
'img_data, expected_output, expected_mode'
,
_get_1_channel_tensor_various_types
())
@
pytest
.
mark
.
parametrize
(
'img_data, expected_output, expected_mode'
,
_get_1_channel_tensor_various_types
())
def
test_1_channel_tensor_to_pil_image
(
with_mode
,
img_data
,
expected_output
,
expected_mode
):
def
test_1_channel_tensor_to_pil_image
(
self
,
with_mode
,
img_data
,
expected_output
,
expected_mode
):
transform
=
transforms
.
ToPILImage
(
mode
=
expected_mode
)
if
with_mode
else
transforms
.
ToPILImage
()
transform
=
transforms
.
ToPILImage
(
mode
=
expected_mode
)
if
with_mode
else
transforms
.
ToPILImage
()
to_tensor
=
transforms
.
ToTensor
()
to_tensor
=
transforms
.
ToTensor
()
...
@@ -565,8 +581,7 @@ def test_1_channel_tensor_to_pil_image(with_mode, img_data, expected_output, exp
...
@@ -565,8 +581,7 @@ def test_1_channel_tensor_to_pil_image(with_mode, img_data, expected_output, exp
assert
img
.
mode
==
expected_mode
assert
img
.
mode
==
expected_mode
torch
.
testing
.
assert_close
(
expected_output
,
to_tensor
(
img
).
numpy
(),
check_stride
=
False
)
torch
.
testing
.
assert_close
(
expected_output
,
to_tensor
(
img
).
numpy
(),
check_stride
=
False
)
def
test_1_channel_float_tensor_to_pil_image
(
self
):
def
test_1_channel_float_tensor_to_pil_image
():
img_data
=
torch
.
Tensor
(
1
,
4
,
4
).
uniform_
()
img_data
=
torch
.
Tensor
(
1
,
4
,
4
).
uniform_
()
# 'F' mode for torch.FloatTensor
# 'F' mode for torch.FloatTensor
img_F_mode
=
transforms
.
ToPILImage
(
mode
=
'F'
)(
img_data
)
img_F_mode
=
transforms
.
ToPILImage
(
mode
=
'F'
)(
img_data
)
...
@@ -575,15 +590,14 @@ def test_1_channel_float_tensor_to_pil_image():
...
@@ -575,15 +590,14 @@ def test_1_channel_float_tensor_to_pil_image():
np
.
array
(
Image
.
fromarray
(
img_data
.
squeeze
(
0
).
numpy
(),
mode
=
'F'
)),
np
.
array
(
img_F_mode
)
np
.
array
(
Image
.
fromarray
(
img_data
.
squeeze
(
0
).
numpy
(),
mode
=
'F'
)),
np
.
array
(
img_F_mode
)
)
)
@
pytest
.
mark
.
parametrize
(
'with_mode'
,
[
False
,
True
])
@
pytest
.
mark
.
parametrize
(
'with_mode'
,
[
False
,
True
])
@
pytest
.
mark
.
parametrize
(
'img_data, expected_mode'
,
[
@
pytest
.
mark
.
parametrize
(
'img_data, expected_mode'
,
[
(
torch
.
Tensor
(
4
,
4
,
1
).
uniform_
().
numpy
(),
'F'
),
(
torch
.
Tensor
(
4
,
4
,
1
).
uniform_
().
numpy
(),
'F'
),
(
torch
.
ByteTensor
(
4
,
4
,
1
).
random_
(
0
,
255
).
numpy
(),
'L'
),
(
torch
.
ByteTensor
(
4
,
4
,
1
).
random_
(
0
,
255
).
numpy
(),
'L'
),
(
torch
.
ShortTensor
(
4
,
4
,
1
).
random_
().
numpy
(),
'I;16'
),
(
torch
.
ShortTensor
(
4
,
4
,
1
).
random_
().
numpy
(),
'I;16'
),
(
torch
.
IntTensor
(
4
,
4
,
1
).
random_
().
numpy
(),
'I'
),
(
torch
.
IntTensor
(
4
,
4
,
1
).
random_
().
numpy
(),
'I'
),
])
])
def
test_1_channel_ndarray_to_pil_image
(
with_mode
,
img_data
,
expected_mode
):
def
test_1_channel_ndarray_to_pil_image
(
self
,
with_mode
,
img_data
,
expected_mode
):
transform
=
transforms
.
ToPILImage
(
mode
=
expected_mode
)
if
with_mode
else
transforms
.
ToPILImage
()
transform
=
transforms
.
ToPILImage
(
mode
=
expected_mode
)
if
with_mode
else
transforms
.
ToPILImage
()
img
=
transform
(
img_data
)
img
=
transform
(
img_data
)
assert
img
.
mode
==
expected_mode
assert
img
.
mode
==
expected_mode
...
@@ -591,9 +605,8 @@ def test_1_channel_ndarray_to_pil_image(with_mode, img_data, expected_mode):
...
@@ -591,9 +605,8 @@ def test_1_channel_ndarray_to_pil_image(with_mode, img_data, expected_mode):
# and otherwise assert_close wouldn't be able to construct a tensor from the uint16 array
# and otherwise assert_close wouldn't be able to construct a tensor from the uint16 array
torch
.
testing
.
assert_close
(
img_data
[:,
:,
0
],
np
.
asarray
(
img
).
astype
(
img_data
.
dtype
))
torch
.
testing
.
assert_close
(
img_data
[:,
:,
0
],
np
.
asarray
(
img
).
astype
(
img_data
.
dtype
))
@
pytest
.
mark
.
parametrize
(
'expected_mode'
,
[
None
,
'LA'
])
@
pytest
.
mark
.
parametrize
(
'expected_mode'
,
[
None
,
'LA'
])
def
test_2_channel_ndarray_to_pil_image
(
self
,
expected_mode
):
def
test_2_channel_ndarray_to_pil_image
(
expected_mode
):
img_data
=
torch
.
ByteTensor
(
4
,
4
,
2
).
random_
(
0
,
255
).
numpy
()
img_data
=
torch
.
ByteTensor
(
4
,
4
,
2
).
random_
(
0
,
255
).
numpy
()
if
expected_mode
is
None
:
if
expected_mode
is
None
:
...
@@ -606,8 +619,7 @@ def test_2_channel_ndarray_to_pil_image(expected_mode):
...
@@ -606,8 +619,7 @@ def test_2_channel_ndarray_to_pil_image(expected_mode):
for
i
in
range
(
2
):
for
i
in
range
(
2
):
torch
.
testing
.
assert_close
(
img_data
[:,
:,
i
],
np
.
asarray
(
split
[
i
]),
check_stride
=
False
)
torch
.
testing
.
assert_close
(
img_data
[:,
:,
i
],
np
.
asarray
(
split
[
i
]),
check_stride
=
False
)
def
test_2_channel_ndarray_to_pil_image_error
(
self
):
def
test_2_channel_ndarray_to_pil_image_error
():
img_data
=
torch
.
ByteTensor
(
4
,
4
,
2
).
random_
(
0
,
255
).
numpy
()
img_data
=
torch
.
ByteTensor
(
4
,
4
,
2
).
random_
(
0
,
255
).
numpy
()
transforms
.
ToPILImage
().
__repr__
()
transforms
.
ToPILImage
().
__repr__
()
...
@@ -619,9 +631,8 @@ def test_2_channel_ndarray_to_pil_image_error():
...
@@ -619,9 +631,8 @@ def test_2_channel_ndarray_to_pil_image_error():
with
pytest
.
raises
(
ValueError
,
match
=
r
"Only modes \['LA'\] are supported for 2D inputs"
):
with
pytest
.
raises
(
ValueError
,
match
=
r
"Only modes \['LA'\] are supported for 2D inputs"
):
transforms
.
ToPILImage
(
mode
=
'RGB'
)(
img_data
)
transforms
.
ToPILImage
(
mode
=
'RGB'
)(
img_data
)
@
pytest
.
mark
.
parametrize
(
'expected_mode'
,
[
None
,
'LA'
])
@
pytest
.
mark
.
parametrize
(
'expected_mode'
,
[
None
,
'LA'
])
def
test_2_channel_tensor_to_pil_image
(
self
,
expected_mode
):
def
test_2_channel_tensor_to_pil_image
(
expected_mode
):
img_data
=
torch
.
Tensor
(
2
,
4
,
4
).
uniform_
()
img_data
=
torch
.
Tensor
(
2
,
4
,
4
).
uniform_
()
expected_output
=
img_data
.
mul
(
255
).
int
().
float
().
div
(
255
)
expected_output
=
img_data
.
mul
(
255
).
int
().
float
().
div
(
255
)
if
expected_mode
is
None
:
if
expected_mode
is
None
:
...
@@ -635,8 +646,7 @@ def test_2_channel_tensor_to_pil_image(expected_mode):
...
@@ -635,8 +646,7 @@ def test_2_channel_tensor_to_pil_image(expected_mode):
for
i
in
range
(
2
):
for
i
in
range
(
2
):
torch
.
testing
.
assert_close
(
expected_output
[
i
].
numpy
(),
F
.
to_tensor
(
split
[
i
]).
squeeze
(
0
).
numpy
())
torch
.
testing
.
assert_close
(
expected_output
[
i
].
numpy
(),
F
.
to_tensor
(
split
[
i
]).
squeeze
(
0
).
numpy
())
def
test_2_channel_tensor_to_pil_image_error
(
self
):
def
test_2_channel_tensor_to_pil_image_error
():
img_data
=
torch
.
Tensor
(
2
,
4
,
4
).
uniform_
()
img_data
=
torch
.
Tensor
(
2
,
4
,
4
).
uniform_
()
# should raise if we try a mode for 4 or 1 or 3 channel images
# should raise if we try a mode for 4 or 1 or 3 channel images
...
@@ -647,28 +657,9 @@ def test_2_channel_tensor_to_pil_image_error():
...
@@ -647,28 +657,9 @@ def test_2_channel_tensor_to_pil_image_error():
with
pytest
.
raises
(
ValueError
,
match
=
r
"Only modes \['LA'\] are supported for 2D inputs"
):
with
pytest
.
raises
(
ValueError
,
match
=
r
"Only modes \['LA'\] are supported for 2D inputs"
):
transforms
.
ToPILImage
(
mode
=
'RGB'
)(
img_data
)
transforms
.
ToPILImage
(
mode
=
'RGB'
)(
img_data
)
@
pytest
.
mark
.
parametrize
(
'with_mode'
,
[
False
,
True
])
def
_get_2d_tensor_various_types
():
@
pytest
.
mark
.
parametrize
(
'img_data, expected_output, expected_mode'
,
_get_2d_tensor_various_types
())
img_data_float
=
torch
.
Tensor
(
4
,
4
).
uniform_
()
def
test_2d_tensor_to_pil_image
(
self
,
with_mode
,
img_data
,
expected_output
,
expected_mode
):
expected_output
=
img_data_float
.
mul
(
255
).
int
().
float
().
div
(
255
).
numpy
()
yield
img_data_float
,
expected_output
,
'L'
img_data_byte
=
torch
.
ByteTensor
(
4
,
4
).
random_
(
0
,
255
)
expected_output
=
img_data_byte
.
float
().
div
(
255.0
).
numpy
()
yield
img_data_byte
,
expected_output
,
'L'
img_data_short
=
torch
.
ShortTensor
(
4
,
4
).
random_
()
expected_output
=
img_data_short
.
numpy
()
yield
img_data_short
,
expected_output
,
'I;16'
img_data_int
=
torch
.
IntTensor
(
4
,
4
).
random_
()
expected_output
=
img_data_int
.
numpy
()
yield
img_data_int
,
expected_output
,
'I'
@
pytest
.
mark
.
parametrize
(
'with_mode'
,
[
False
,
True
])
@
pytest
.
mark
.
parametrize
(
'img_data, expected_output, expected_mode'
,
_get_2d_tensor_various_types
())
def
test_2d_tensor_to_pil_image
(
with_mode
,
img_data
,
expected_output
,
expected_mode
):
transform
=
transforms
.
ToPILImage
(
mode
=
expected_mode
)
if
with_mode
else
transforms
.
ToPILImage
()
transform
=
transforms
.
ToPILImage
(
mode
=
expected_mode
)
if
with_mode
else
transforms
.
ToPILImage
()
to_tensor
=
transforms
.
ToTensor
()
to_tensor
=
transforms
.
ToTensor
()
...
@@ -676,23 +667,21 @@ def test_2d_tensor_to_pil_image(with_mode, img_data, expected_output, expected_m
...
@@ -676,23 +667,21 @@ def test_2d_tensor_to_pil_image(with_mode, img_data, expected_output, expected_m
assert
img
.
mode
==
expected_mode
assert
img
.
mode
==
expected_mode
torch
.
testing
.
assert_close
(
expected_output
,
to_tensor
(
img
).
numpy
()[
0
])
torch
.
testing
.
assert_close
(
expected_output
,
to_tensor
(
img
).
numpy
()[
0
])
@
pytest
.
mark
.
parametrize
(
'with_mode'
,
[
False
,
True
])
@
pytest
.
mark
.
parametrize
(
'with_mode'
,
[
False
,
True
])
@
pytest
.
mark
.
parametrize
(
'img_data, expected_mode'
,
[
@
pytest
.
mark
.
parametrize
(
'img_data, expected_mode'
,
[
(
torch
.
Tensor
(
4
,
4
).
uniform_
().
numpy
(),
'F'
),
(
torch
.
Tensor
(
4
,
4
).
uniform_
().
numpy
(),
'F'
),
(
torch
.
ByteTensor
(
4
,
4
).
random_
(
0
,
255
).
numpy
(),
'L'
),
(
torch
.
ByteTensor
(
4
,
4
).
random_
(
0
,
255
).
numpy
(),
'L'
),
(
torch
.
ShortTensor
(
4
,
4
).
random_
().
numpy
(),
'I;16'
),
(
torch
.
ShortTensor
(
4
,
4
).
random_
().
numpy
(),
'I;16'
),
(
torch
.
IntTensor
(
4
,
4
).
random_
().
numpy
(),
'I'
),
(
torch
.
IntTensor
(
4
,
4
).
random_
().
numpy
(),
'I'
),
])
])
def
test_2d_ndarray_to_pil_image
(
with_mode
,
img_data
,
expected_mode
):
def
test_2d_ndarray_to_pil_image
(
self
,
with_mode
,
img_data
,
expected_mode
):
transform
=
transforms
.
ToPILImage
(
mode
=
expected_mode
)
if
with_mode
else
transforms
.
ToPILImage
()
transform
=
transforms
.
ToPILImage
(
mode
=
expected_mode
)
if
with_mode
else
transforms
.
ToPILImage
()
img
=
transform
(
img_data
)
img
=
transform
(
img_data
)
assert
img
.
mode
==
expected_mode
assert
img
.
mode
==
expected_mode
np
.
testing
.
assert_allclose
(
img_data
,
img
)
np
.
testing
.
assert_allclose
(
img_data
,
img
)
@
pytest
.
mark
.
parametrize
(
'expected_mode'
,
[
None
,
'RGB'
,
'HSV'
,
'YCbCr'
])
@
pytest
.
mark
.
parametrize
(
'expected_mode'
,
[
None
,
'RGB'
,
'HSV'
,
'YCbCr'
])
def
test_3_channel_tensor_to_pil_image
(
self
,
expected_mode
):
def
test_3_channel_tensor_to_pil_image
(
expected_mode
):
img_data
=
torch
.
Tensor
(
3
,
4
,
4
).
uniform_
()
img_data
=
torch
.
Tensor
(
3
,
4
,
4
).
uniform_
()
expected_output
=
img_data
.
mul
(
255
).
int
().
float
().
div
(
255
)
expected_output
=
img_data
.
mul
(
255
).
int
().
float
().
div
(
255
)
...
@@ -706,8 +695,7 @@ def test_3_channel_tensor_to_pil_image(expected_mode):
...
@@ -706,8 +695,7 @@ def test_3_channel_tensor_to_pil_image(expected_mode):
for
i
in
range
(
3
):
for
i
in
range
(
3
):
torch
.
testing
.
assert_close
(
expected_output
[
i
].
numpy
(),
F
.
to_tensor
(
split
[
i
]).
squeeze
(
0
).
numpy
())
torch
.
testing
.
assert_close
(
expected_output
[
i
].
numpy
(),
F
.
to_tensor
(
split
[
i
]).
squeeze
(
0
).
numpy
())
def
test_3_channel_tensor_to_pil_image_error
(
self
):
def
test_3_channel_tensor_to_pil_image_error
():
img_data
=
torch
.
Tensor
(
3
,
4
,
4
).
uniform_
()
img_data
=
torch
.
Tensor
(
3
,
4
,
4
).
uniform_
()
error_message_3d
=
r
"Only modes \['RGB', 'YCbCr', 'HSV'\] are supported for 3D inputs"
error_message_3d
=
r
"Only modes \['RGB', 'YCbCr', 'HSV'\] are supported for 3D inputs"
# should raise if we try a mode for 4 or 1 or 2 channel images
# should raise if we try a mode for 4 or 1 or 2 channel images
...
@@ -721,9 +709,8 @@ def test_3_channel_tensor_to_pil_image_error():
...
@@ -721,9 +709,8 @@ def test_3_channel_tensor_to_pil_image_error():
with
pytest
.
raises
(
ValueError
,
match
=
r
'pic should be 2/3 dimensional. Got \d+ dimensions.'
):
with
pytest
.
raises
(
ValueError
,
match
=
r
'pic should be 2/3 dimensional. Got \d+ dimensions.'
):
transforms
.
ToPILImage
()(
torch
.
Tensor
(
1
,
3
,
4
,
4
).
uniform_
())
transforms
.
ToPILImage
()(
torch
.
Tensor
(
1
,
3
,
4
,
4
).
uniform_
())
@
pytest
.
mark
.
parametrize
(
'expected_mode'
,
[
None
,
'RGB'
,
'HSV'
,
'YCbCr'
])
@
pytest
.
mark
.
parametrize
(
'expected_mode'
,
[
None
,
'RGB'
,
'HSV'
,
'YCbCr'
])
def
test_3_channel_ndarray_to_pil_image
(
self
,
expected_mode
):
def
test_3_channel_ndarray_to_pil_image
(
expected_mode
):
img_data
=
torch
.
ByteTensor
(
4
,
4
,
3
).
random_
(
0
,
255
).
numpy
()
img_data
=
torch
.
ByteTensor
(
4
,
4
,
3
).
random_
(
0
,
255
).
numpy
()
if
expected_mode
is
None
:
if
expected_mode
is
None
:
...
@@ -736,8 +723,7 @@ def test_3_channel_ndarray_to_pil_image(expected_mode):
...
@@ -736,8 +723,7 @@ def test_3_channel_ndarray_to_pil_image(expected_mode):
for
i
in
range
(
3
):
for
i
in
range
(
3
):
torch
.
testing
.
assert_close
(
img_data
[:,
:,
i
],
np
.
asarray
(
split
[
i
]),
check_stride
=
False
)
torch
.
testing
.
assert_close
(
img_data
[:,
:,
i
],
np
.
asarray
(
split
[
i
]),
check_stride
=
False
)
def
test_3_channel_ndarray_to_pil_image_error
(
self
):
def
test_3_channel_ndarray_to_pil_image_error
():
img_data
=
torch
.
ByteTensor
(
4
,
4
,
3
).
random_
(
0
,
255
).
numpy
()
img_data
=
torch
.
ByteTensor
(
4
,
4
,
3
).
random_
(
0
,
255
).
numpy
()
# Checking if ToPILImage can be printed as string
# Checking if ToPILImage can be printed as string
...
@@ -752,9 +738,8 @@ def test_3_channel_ndarray_to_pil_image_error():
...
@@ -752,9 +738,8 @@ def test_3_channel_ndarray_to_pil_image_error():
with
pytest
.
raises
(
ValueError
,
match
=
error_message_3d
):
with
pytest
.
raises
(
ValueError
,
match
=
error_message_3d
):
transforms
.
ToPILImage
(
mode
=
'LA'
)(
img_data
)
transforms
.
ToPILImage
(
mode
=
'LA'
)(
img_data
)
@
pytest
.
mark
.
parametrize
(
'expected_mode'
,
[
None
,
'RGBA'
,
'CMYK'
,
'RGBX'
])
@
pytest
.
mark
.
parametrize
(
'expected_mode'
,
[
None
,
'RGBA'
,
'CMYK'
,
'RGBX'
])
def
test_4_channel_tensor_to_pil_image
(
self
,
expected_mode
):
def
test_4_channel_tensor_to_pil_image
(
expected_mode
):
img_data
=
torch
.
Tensor
(
4
,
4
,
4
).
uniform_
()
img_data
=
torch
.
Tensor
(
4
,
4
,
4
).
uniform_
()
expected_output
=
img_data
.
mul
(
255
).
int
().
float
().
div
(
255
)
expected_output
=
img_data
.
mul
(
255
).
int
().
float
().
div
(
255
)
...
@@ -769,8 +754,7 @@ def test_4_channel_tensor_to_pil_image(expected_mode):
...
@@ -769,8 +754,7 @@ def test_4_channel_tensor_to_pil_image(expected_mode):
for
i
in
range
(
4
):
for
i
in
range
(
4
):
torch
.
testing
.
assert_close
(
expected_output
[
i
].
numpy
(),
F
.
to_tensor
(
split
[
i
]).
squeeze
(
0
).
numpy
())
torch
.
testing
.
assert_close
(
expected_output
[
i
].
numpy
(),
F
.
to_tensor
(
split
[
i
]).
squeeze
(
0
).
numpy
())
def
test_4_channel_tensor_to_pil_image_error
(
self
):
def
test_4_channel_tensor_to_pil_image_error
():
img_data
=
torch
.
Tensor
(
4
,
4
,
4
).
uniform_
()
img_data
=
torch
.
Tensor
(
4
,
4
,
4
).
uniform_
()
error_message_4d
=
r
"Only modes \['RGBA', 'CMYK', 'RGBX'\] are supported for 4D inputs"
error_message_4d
=
r
"Only modes \['RGBA', 'CMYK', 'RGBX'\] are supported for 4D inputs"
...
@@ -782,9 +766,8 @@ def test_4_channel_tensor_to_pil_image_error():
...
@@ -782,9 +766,8 @@ def test_4_channel_tensor_to_pil_image_error():
with
pytest
.
raises
(
ValueError
,
match
=
error_message_4d
):
with
pytest
.
raises
(
ValueError
,
match
=
error_message_4d
):
transforms
.
ToPILImage
(
mode
=
'LA'
)(
img_data
)
transforms
.
ToPILImage
(
mode
=
'LA'
)(
img_data
)
@
pytest
.
mark
.
parametrize
(
'expected_mode'
,
[
None
,
'RGBA'
,
'CMYK'
,
'RGBX'
])
@
pytest
.
mark
.
parametrize
(
'expected_mode'
,
[
None
,
'RGBA'
,
'CMYK'
,
'RGBX'
])
def
test_4_channel_ndarray_to_pil_image
(
self
,
expected_mode
):
def
test_4_channel_ndarray_to_pil_image
(
expected_mode
):
img_data
=
torch
.
ByteTensor
(
4
,
4
,
4
).
random_
(
0
,
255
).
numpy
()
img_data
=
torch
.
ByteTensor
(
4
,
4
,
4
).
random_
(
0
,
255
).
numpy
()
if
expected_mode
is
None
:
if
expected_mode
is
None
:
...
@@ -797,8 +780,7 @@ def test_4_channel_ndarray_to_pil_image(expected_mode):
...
@@ -797,8 +780,7 @@ def test_4_channel_ndarray_to_pil_image(expected_mode):
for
i
in
range
(
4
):
for
i
in
range
(
4
):
torch
.
testing
.
assert_close
(
img_data
[:,
:,
i
],
np
.
asarray
(
split
[
i
]),
check_stride
=
False
)
torch
.
testing
.
assert_close
(
img_data
[:,
:,
i
],
np
.
asarray
(
split
[
i
]),
check_stride
=
False
)
def
test_4_channel_ndarray_to_pil_image_error
(
self
):
def
test_4_channel_ndarray_to_pil_image_error
():
img_data
=
torch
.
ByteTensor
(
4
,
4
,
4
).
random_
(
0
,
255
).
numpy
()
img_data
=
torch
.
ByteTensor
(
4
,
4
,
4
).
random_
(
0
,
255
).
numpy
()
error_message_4d
=
r
"Only modes \['RGBA', 'CMYK', 'RGBX'\] are supported for 4D inputs"
error_message_4d
=
r
"Only modes \['RGBA', 'CMYK', 'RGBX'\] are supported for 4D inputs"
...
@@ -810,8 +792,7 @@ def test_4_channel_ndarray_to_pil_image_error():
...
@@ -810,8 +792,7 @@ def test_4_channel_ndarray_to_pil_image_error():
with
pytest
.
raises
(
ValueError
,
match
=
error_message_4d
):
with
pytest
.
raises
(
ValueError
,
match
=
error_message_4d
):
transforms
.
ToPILImage
(
mode
=
'LA'
)(
img_data
)
transforms
.
ToPILImage
(
mode
=
'LA'
)(
img_data
)
def
test_ndarray_bad_types_to_pil_image
(
self
):
def
test_ndarray_bad_types_to_pil_image
():
trans
=
transforms
.
ToPILImage
()
trans
=
transforms
.
ToPILImage
()
reg_msg
=
r
'Input type \w+ is not supported'
reg_msg
=
r
'Input type \w+ is not supported'
with
pytest
.
raises
(
TypeError
,
match
=
reg_msg
):
with
pytest
.
raises
(
TypeError
,
match
=
reg_msg
):
...
@@ -828,8 +809,7 @@ def test_ndarray_bad_types_to_pil_image():
...
@@ -828,8 +809,7 @@ def test_ndarray_bad_types_to_pil_image():
with
pytest
.
raises
(
ValueError
,
match
=
r
'pic should not have > 4 channels. Got \d+ channels.'
):
with
pytest
.
raises
(
ValueError
,
match
=
r
'pic should not have > 4 channels. Got \d+ channels.'
):
transforms
.
ToPILImage
()(
np
.
ones
([
4
,
4
,
6
]))
transforms
.
ToPILImage
()(
np
.
ones
([
4
,
4
,
6
]))
def
test_tensor_bad_types_to_pil_image
(
self
):
def
test_tensor_bad_types_to_pil_image
():
with
pytest
.
raises
(
ValueError
,
match
=
r
'pic should be 2/3 dimensional. Got \d+ dimensions.'
):
with
pytest
.
raises
(
ValueError
,
match
=
r
'pic should be 2/3 dimensional. Got \d+ dimensions.'
):
transforms
.
ToPILImage
()(
torch
.
ones
(
1
,
3
,
4
,
4
))
transforms
.
ToPILImage
()(
torch
.
ones
(
1
,
3
,
4
,
4
))
with
pytest
.
raises
(
ValueError
,
match
=
r
'pic should not have > 4 channels. Got \d+ channels.'
):
with
pytest
.
raises
(
ValueError
,
match
=
r
'pic should not have > 4 channels. Got \d+ channels.'
):
...
...
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