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
154283b1
"docs/vscode:/vscode.git/clone" did not exist on "5b380b89b5104ded935efd86dae0bfe94e106d03"
Unverified
Commit
154283b1
authored
May 26, 2021
by
Zhiqiang Wang
Committed by
GitHub
May 25, 2021
Browse files
Port test/test_utils.py to pytest (#3917)
parent
1b6fe682
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
124 additions
and
115 deletions
+124
-115
test/test_utils.py
test/test_utils.py
+124
-115
No files found.
test/test_utils.py
View file @
154283b1
...
@@ -5,7 +5,7 @@ import sys
...
@@ -5,7 +5,7 @@ import sys
import
tempfile
import
tempfile
import
torch
import
torch
import
torchvision.utils
as
utils
import
torchvision.utils
as
utils
import
unittest
from
io
import
BytesIO
from
io
import
BytesIO
import
torchvision.transforms.functional
as
F
import
torchvision.transforms.functional
as
F
from
PIL
import
Image
,
__version__
as
PILLOW_VERSION
,
ImageColor
from
PIL
import
Image
,
__version__
as
PILLOW_VERSION
,
ImageColor
...
@@ -18,9 +18,7 @@ boxes = torch.tensor([[0, 0, 20, 20], [0, 0, 0, 0],
...
@@ -18,9 +18,7 @@ boxes = torch.tensor([[0, 0, 20, 20], [0, 0, 0, 0],
[
10
,
15
,
30
,
35
],
[
23
,
35
,
93
,
95
]],
dtype
=
torch
.
float
)
[
10
,
15
,
30
,
35
],
[
23
,
35
,
93
,
95
]],
dtype
=
torch
.
float
)
class
Tester
(
unittest
.
TestCase
):
def
test_make_grid_not_inplace
():
def
test_make_grid_not_inplace
(
self
):
t
=
torch
.
rand
(
5
,
3
,
10
,
10
)
t
=
torch
.
rand
(
5
,
3
,
10
,
10
)
t_clone
=
t
.
clone
()
t_clone
=
t
.
clone
()
...
@@ -33,7 +31,8 @@ class Tester(unittest.TestCase):
...
@@ -33,7 +31,8 @@ class Tester(unittest.TestCase):
utils
.
make_grid
(
t
,
normalize
=
True
,
scale_each
=
True
)
utils
.
make_grid
(
t
,
normalize
=
True
,
scale_each
=
True
)
assert_equal
(
t
,
t_clone
,
msg
=
'make_grid modified tensor in-place'
)
assert_equal
(
t
,
t_clone
,
msg
=
'make_grid modified tensor in-place'
)
def
test_normalize_in_make_grid
(
self
):
def
test_normalize_in_make_grid
():
t
=
torch
.
rand
(
5
,
3
,
10
,
10
)
*
255
t
=
torch
.
rand
(
5
,
3
,
10
,
10
)
*
255
norm_max
=
torch
.
tensor
(
1.0
)
norm_max
=
torch
.
tensor
(
1.0
)
norm_min
=
torch
.
tensor
(
0.0
)
norm_min
=
torch
.
tensor
(
0.0
)
...
@@ -50,22 +49,25 @@ class Tester(unittest.TestCase):
...
@@ -50,22 +49,25 @@ class Tester(unittest.TestCase):
assert_equal
(
norm_max
,
rounded_grid_max
,
msg
=
'Normalized max is not equal to 1'
)
assert_equal
(
norm_max
,
rounded_grid_max
,
msg
=
'Normalized max is not equal to 1'
)
assert_equal
(
norm_min
,
rounded_grid_min
,
msg
=
'Normalized min is not equal to 0'
)
assert_equal
(
norm_min
,
rounded_grid_min
,
msg
=
'Normalized min is not equal to 0'
)
@
unittest
.
skipIf
(
sys
.
platform
in
(
'win32'
,
'cygwin'
),
'temporarily disabled on Windows'
)
def
test_save_image
(
self
):
@
pytest
.
mark
.
skipif
(
sys
.
platform
in
(
'win32'
,
'cygwin'
),
reason
=
'temporarily disabled on Windows'
)
def
test_save_image
():
with
tempfile
.
NamedTemporaryFile
(
suffix
=
'.png'
)
as
f
:
with
tempfile
.
NamedTemporaryFile
(
suffix
=
'.png'
)
as
f
:
t
=
torch
.
rand
(
2
,
3
,
64
,
64
)
t
=
torch
.
rand
(
2
,
3
,
64
,
64
)
utils
.
save_image
(
t
,
f
.
name
)
utils
.
save_image
(
t
,
f
.
name
)
self
.
assertTrue
(
os
.
path
.
exists
(
f
.
name
),
'The image is not present after save'
)
assert
os
.
path
.
exists
(
f
.
name
),
'The image is not present after save'
@
unittest
.
skip
I
f
(
sys
.
platform
in
(
'win32'
,
'cygwin'
),
'temporarily disabled on Windows'
)
@
pytest
.
mark
.
skip
i
f
(
sys
.
platform
in
(
'win32'
,
'cygwin'
),
reason
=
'temporarily disabled on Windows'
)
def
test_save_image_single_pixel
(
self
):
def
test_save_image_single_pixel
():
with
tempfile
.
NamedTemporaryFile
(
suffix
=
'.png'
)
as
f
:
with
tempfile
.
NamedTemporaryFile
(
suffix
=
'.png'
)
as
f
:
t
=
torch
.
rand
(
1
,
3
,
1
,
1
)
t
=
torch
.
rand
(
1
,
3
,
1
,
1
)
utils
.
save_image
(
t
,
f
.
name
)
utils
.
save_image
(
t
,
f
.
name
)
self
.
assertTrue
(
os
.
path
.
exists
(
f
.
name
),
'The pixel image is not present after save'
)
assert
os
.
path
.
exists
(
f
.
name
),
'The pixel image is not present after save'
@
unittest
.
skip
I
f
(
sys
.
platform
in
(
'win32'
,
'cygwin'
),
'temporarily disabled on Windows'
)
@
pytest
.
mark
.
skip
i
f
(
sys
.
platform
in
(
'win32'
,
'cygwin'
),
reason
=
'temporarily disabled on Windows'
)
def
test_save_image_file_object
(
self
):
def
test_save_image_file_object
():
with
tempfile
.
NamedTemporaryFile
(
suffix
=
'.png'
)
as
f
:
with
tempfile
.
NamedTemporaryFile
(
suffix
=
'.png'
)
as
f
:
t
=
torch
.
rand
(
2
,
3
,
64
,
64
)
t
=
torch
.
rand
(
2
,
3
,
64
,
64
)
utils
.
save_image
(
t
,
f
.
name
)
utils
.
save_image
(
t
,
f
.
name
)
...
@@ -75,8 +77,9 @@ class Tester(unittest.TestCase):
...
@@ -75,8 +77,9 @@ class Tester(unittest.TestCase):
img_bytes
=
Image
.
open
(
fp
)
img_bytes
=
Image
.
open
(
fp
)
assert_equal
(
F
.
to_tensor
(
img_orig
),
F
.
to_tensor
(
img_bytes
),
msg
=
'Image not stored in file object'
)
assert_equal
(
F
.
to_tensor
(
img_orig
),
F
.
to_tensor
(
img_bytes
),
msg
=
'Image not stored in file object'
)
@
unittest
.
skipIf
(
sys
.
platform
in
(
'win32'
,
'cygwin'
),
'temporarily disabled on Windows'
)
def
test_save_image_single_pixel_file_object
(
self
):
@
pytest
.
mark
.
skipif
(
sys
.
platform
in
(
'win32'
,
'cygwin'
),
reason
=
'temporarily disabled on Windows'
)
def
test_save_image_single_pixel_file_object
():
with
tempfile
.
NamedTemporaryFile
(
suffix
=
'.png'
)
as
f
:
with
tempfile
.
NamedTemporaryFile
(
suffix
=
'.png'
)
as
f
:
t
=
torch
.
rand
(
1
,
3
,
1
,
1
)
t
=
torch
.
rand
(
1
,
3
,
1
,
1
)
utils
.
save_image
(
t
,
f
.
name
)
utils
.
save_image
(
t
,
f
.
name
)
...
@@ -86,7 +89,8 @@ class Tester(unittest.TestCase):
...
@@ -86,7 +89,8 @@ class Tester(unittest.TestCase):
img_bytes
=
Image
.
open
(
fp
)
img_bytes
=
Image
.
open
(
fp
)
assert_equal
(
F
.
to_tensor
(
img_orig
),
F
.
to_tensor
(
img_bytes
),
msg
=
'Image not stored in file object'
)
assert_equal
(
F
.
to_tensor
(
img_orig
),
F
.
to_tensor
(
img_bytes
),
msg
=
'Image not stored in file object'
)
def
test_draw_boxes
(
self
):
def
test_draw_boxes
():
img
=
torch
.
full
((
3
,
100
,
100
),
255
,
dtype
=
torch
.
uint8
)
img
=
torch
.
full
((
3
,
100
,
100
),
255
,
dtype
=
torch
.
uint8
)
img_cp
=
img
.
clone
()
img_cp
=
img
.
clone
()
boxes_cp
=
boxes
.
clone
()
boxes_cp
=
boxes
.
clone
()
...
@@ -108,7 +112,8 @@ class Tester(unittest.TestCase):
...
@@ -108,7 +112,8 @@ class Tester(unittest.TestCase):
assert_equal
(
boxes
,
boxes_cp
)
assert_equal
(
boxes
,
boxes_cp
)
assert_equal
(
img
,
img_cp
)
assert_equal
(
img
,
img_cp
)
def
test_draw_boxes_vanilla
(
self
):
def
test_draw_boxes_vanilla
():
img
=
torch
.
full
((
3
,
100
,
100
),
0
,
dtype
=
torch
.
uint8
)
img
=
torch
.
full
((
3
,
100
,
100
),
0
,
dtype
=
torch
.
uint8
)
img_cp
=
img
.
clone
()
img_cp
=
img
.
clone
()
boxes_cp
=
boxes
.
clone
()
boxes_cp
=
boxes
.
clone
()
...
@@ -125,15 +130,19 @@ class Tester(unittest.TestCase):
...
@@ -125,15 +130,19 @@ class Tester(unittest.TestCase):
assert_equal
(
boxes
,
boxes_cp
)
assert_equal
(
boxes
,
boxes_cp
)
assert_equal
(
img
,
img_cp
)
assert_equal
(
img
,
img_cp
)
def
test_draw_invalid_boxes
(
self
):
def
test_draw_invalid_boxes
():
img_tp
=
((
1
,
1
,
1
),
(
1
,
2
,
3
))
img_tp
=
((
1
,
1
,
1
),
(
1
,
2
,
3
))
img_wrong1
=
torch
.
full
((
3
,
5
,
5
),
255
,
dtype
=
torch
.
float
)
img_wrong1
=
torch
.
full
((
3
,
5
,
5
),
255
,
dtype
=
torch
.
float
)
img_wrong2
=
torch
.
full
((
1
,
3
,
5
,
5
),
255
,
dtype
=
torch
.
uint8
)
img_wrong2
=
torch
.
full
((
1
,
3
,
5
,
5
),
255
,
dtype
=
torch
.
uint8
)
boxes
=
torch
.
tensor
([[
0
,
0
,
20
,
20
],
[
0
,
0
,
0
,
0
],
boxes
=
torch
.
tensor
([[
0
,
0
,
20
,
20
],
[
0
,
0
,
0
,
0
],
[
10
,
15
,
30
,
35
],
[
23
,
35
,
93
,
95
]],
dtype
=
torch
.
float
)
[
10
,
15
,
30
,
35
],
[
23
,
35
,
93
,
95
]],
dtype
=
torch
.
float
)
self
.
assertRaises
(
TypeError
,
utils
.
draw_bounding_boxes
,
img_tp
,
boxes
)
with
pytest
.
raises
(
TypeError
,
match
=
"Tensor expected"
):
self
.
assertRaises
(
ValueError
,
utils
.
draw_bounding_boxes
,
img_wrong1
,
boxes
)
utils
.
draw_bounding_boxes
(
img_tp
,
boxes
)
self
.
assertRaises
(
ValueError
,
utils
.
draw_bounding_boxes
,
img_wrong2
,
boxes
)
with
pytest
.
raises
(
ValueError
,
match
=
"Tensor uint8 expected"
):
utils
.
draw_bounding_boxes
(
img_wrong1
,
boxes
)
with
pytest
.
raises
(
ValueError
,
match
=
"Pass individual images, not batches"
):
utils
.
draw_bounding_boxes
(
img_wrong2
,
boxes
)
@
pytest
.
mark
.
parametrize
(
'colors'
,
[
@
pytest
.
mark
.
parametrize
(
'colors'
,
[
...
@@ -218,5 +227,5 @@ def test_draw_segmentation_masks_errors():
...
@@ -218,5 +227,5 @@ def test_draw_segmentation_masks_errors():
utils
.
draw_segmentation_masks
(
image
=
img
,
masks
=
masks
,
colors
=
bad_colors
)
utils
.
draw_segmentation_masks
(
image
=
img
,
masks
=
masks
,
colors
=
bad_colors
)
if
__name__
==
'
__main__
'
:
if
__name__
==
"
__main__
"
:
unit
test
.
main
()
py
test
.
main
(
[
__file__
]
)
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