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
1610268e
Commit
1610268e
authored
Jan 31, 2017
by
Alykhan Tejani
Browse files
use integer division in tests for array slicing + various PEP-8 fixes
parent
6c7733f0
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
20 deletions
+19
-20
test/test_transforms.py
test/test_transforms.py
+19
-20
No files found.
test/test_transforms.py
View file @
1610268e
import
torch
import
torch
import
torchvision.transforms
as
transforms
import
torchvision.transforms
as
transforms
import
torchvision.datasets
as
datasets
import
numpy
as
np
import
unittest
import
unittest
import
random
import
random
class
Tester
(
unittest
.
TestCase
):
class
Tester
(
unittest
.
TestCase
):
def
test_crop
(
self
):
def
test_crop
(
self
):
height
=
random
.
randint
(
10
,
32
)
*
2
height
=
random
.
randint
(
10
,
32
)
*
2
width
=
random
.
randint
(
10
,
32
)
*
2
width
=
random
.
randint
(
10
,
32
)
*
2
oheight
=
random
.
randint
(
5
,
(
height
-
2
)
/
2
)
*
2
oheight
=
random
.
randint
(
5
,
(
height
-
2
)
/
2
)
*
2
owidth
=
random
.
randint
(
5
,
(
width
-
2
)
/
2
)
*
2
owidth
=
random
.
randint
(
5
,
(
width
-
2
)
/
2
)
*
2
img
=
torch
.
ones
(
3
,
height
,
width
)
img
=
torch
.
ones
(
3
,
height
,
width
)
oh1
=
(
height
-
oheight
)
/
2
oh1
=
(
height
-
oheight
)
/
/
2
ow1
=
(
width
-
owidth
)
/
2
ow1
=
(
width
-
owidth
)
/
/
2
imgnarrow
=
img
[:,
oh1
:
oh1
+
oheight
,
ow1
:
ow1
+
owidth
]
imgnarrow
=
img
[:,
oh1
:
oh1
+
oheight
,
ow1
:
ow1
+
owidth
]
imgnarrow
.
fill_
(
0
)
imgnarrow
.
fill_
(
0
)
result
=
transforms
.
Compose
([
result
=
transforms
.
Compose
([
transforms
.
ToPILImage
(),
transforms
.
ToPILImage
(),
...
@@ -23,7 +22,7 @@ class Tester(unittest.TestCase):
...
@@ -23,7 +22,7 @@ class Tester(unittest.TestCase):
transforms
.
ToTensor
(),
transforms
.
ToTensor
(),
])(
img
)
])(
img
)
assert
result
.
sum
()
==
0
,
"height: "
+
str
(
height
)
+
" width: "
\
assert
result
.
sum
()
==
0
,
"height: "
+
str
(
height
)
+
" width: "
\
+
str
(
width
)
+
" oheight: "
+
str
(
oheight
)
+
" owidth: "
+
str
(
owidth
)
+
str
(
width
)
+
" oheight: "
+
str
(
oheight
)
+
" owidth: "
+
str
(
owidth
)
oheight
+=
1
oheight
+=
1
owidth
+=
1
owidth
+=
1
result
=
transforms
.
Compose
([
result
=
transforms
.
Compose
([
...
@@ -33,9 +32,9 @@ class Tester(unittest.TestCase):
...
@@ -33,9 +32,9 @@ class Tester(unittest.TestCase):
])(
img
)
])(
img
)
sum1
=
result
.
sum
()
sum1
=
result
.
sum
()
assert
sum1
>
1
,
"height: "
+
str
(
height
)
+
" width: "
\
assert
sum1
>
1
,
"height: "
+
str
(
height
)
+
" width: "
\
+
str
(
width
)
+
" oheight: "
+
str
(
oheight
)
+
" owidth: "
+
str
(
owidth
)
+
str
(
width
)
+
" oheight: "
+
str
(
oheight
)
+
" owidth: "
+
str
(
owidth
)
oheight
+=
1
oheight
+=
1
owidth
+=
1
owidth
+=
1
result
=
transforms
.
Compose
([
result
=
transforms
.
Compose
([
transforms
.
ToPILImage
(),
transforms
.
ToPILImage
(),
transforms
.
CenterCrop
((
oheight
,
owidth
)),
transforms
.
CenterCrop
((
oheight
,
owidth
)),
...
@@ -43,15 +42,15 @@ class Tester(unittest.TestCase):
...
@@ -43,15 +42,15 @@ class Tester(unittest.TestCase):
])(
img
)
])(
img
)
sum2
=
result
.
sum
()
sum2
=
result
.
sum
()
assert
sum2
>
0
,
"height: "
+
str
(
height
)
+
" width: "
\
assert
sum2
>
0
,
"height: "
+
str
(
height
)
+
" width: "
\
+
str
(
width
)
+
" oheight: "
+
str
(
oheight
)
+
" owidth: "
+
str
(
owidth
)
+
str
(
width
)
+
" oheight: "
+
str
(
oheight
)
+
" owidth: "
+
str
(
owidth
)
assert
sum2
>
sum1
,
"height: "
+
str
(
height
)
+
" width: "
\
assert
sum2
>
sum1
,
"height: "
+
str
(
height
)
+
" width: "
\
+
str
(
width
)
+
" oheight: "
+
str
(
oheight
)
+
" owidth: "
+
str
(
owidth
)
+
str
(
width
)
+
" oheight: "
+
str
(
oheight
)
+
" owidth: "
+
str
(
owidth
)
def
test_scale
(
self
):
def
test_scale
(
self
):
height
=
random
.
randint
(
24
,
32
)
*
2
height
=
random
.
randint
(
24
,
32
)
*
2
width
=
random
.
randint
(
24
,
32
)
*
2
width
=
random
.
randint
(
24
,
32
)
*
2
osize
=
random
.
randint
(
5
,
12
)
*
2
osize
=
random
.
randint
(
5
,
12
)
*
2
img
=
torch
.
ones
(
3
,
height
,
width
)
img
=
torch
.
ones
(
3
,
height
,
width
)
result
=
transforms
.
Compose
([
result
=
transforms
.
Compose
([
transforms
.
ToPILImage
(),
transforms
.
ToPILImage
(),
...
@@ -63,7 +62,7 @@ class Tester(unittest.TestCase):
...
@@ -63,7 +62,7 @@ class Tester(unittest.TestCase):
# print result.size()
# print result.size()
assert
osize
in
result
.
size
()
assert
osize
in
result
.
size
()
if
height
<
width
:
if
height
<
width
:
assert
result
.
size
(
1
)
<=
result
.
size
(
2
)
assert
result
.
size
(
1
)
<=
result
.
size
(
2
)
elif
width
<
height
:
elif
width
<
height
:
assert
result
.
size
(
1
)
>=
result
.
size
(
2
)
assert
result
.
size
(
1
)
>=
result
.
size
(
2
)
...
@@ -71,7 +70,7 @@ class Tester(unittest.TestCase):
...
@@ -71,7 +70,7 @@ class Tester(unittest.TestCase):
height
=
random
.
randint
(
10
,
32
)
*
2
height
=
random
.
randint
(
10
,
32
)
*
2
width
=
random
.
randint
(
10
,
32
)
*
2
width
=
random
.
randint
(
10
,
32
)
*
2
oheight
=
random
.
randint
(
5
,
(
height
-
2
)
/
2
)
*
2
oheight
=
random
.
randint
(
5
,
(
height
-
2
)
/
2
)
*
2
owidth
=
random
.
randint
(
5
,
(
width
-
2
)
/
2
)
*
2
owidth
=
random
.
randint
(
5
,
(
width
-
2
)
/
2
)
*
2
img
=
torch
.
ones
(
3
,
height
,
width
)
img
=
torch
.
ones
(
3
,
height
,
width
)
result
=
transforms
.
Compose
([
result
=
transforms
.
Compose
([
transforms
.
ToPILImage
(),
transforms
.
ToPILImage
(),
...
@@ -100,20 +99,20 @@ class Tester(unittest.TestCase):
...
@@ -100,20 +99,20 @@ class Tester(unittest.TestCase):
transforms
.
Pad
(
padding
),
transforms
.
Pad
(
padding
),
transforms
.
ToTensor
(),
transforms
.
ToTensor
(),
])(
img
)
])(
img
)
assert
result
.
size
(
1
)
==
height
+
2
*
padding
assert
result
.
size
(
1
)
==
height
+
2
*
padding
assert
result
.
size
(
2
)
==
width
+
2
*
padding
assert
result
.
size
(
2
)
==
width
+
2
*
padding
def
test_lambda
(
self
):
def
test_lambda
(
self
):
trans
=
transforms
.
Lambda
(
lambda
x
:
x
.
add
(
10
))
trans
=
transforms
.
Lambda
(
lambda
x
:
x
.
add
(
10
))
x
=
torch
.
randn
(
10
)
x
=
torch
.
randn
(
10
)
y
=
trans
(
x
)
y
=
trans
(
x
)
assert
(
y
.
equal
(
torch
.
add
(
x
,
10
)))
assert
(
y
.
equal
(
torch
.
add
(
x
,
10
)))
trans
=
transforms
.
Lambda
(
lambda
x
:
x
.
add_
(
10
))
trans
=
transforms
.
Lambda
(
lambda
x
:
x
.
add_
(
10
))
x
=
torch
.
randn
(
10
)
x
=
torch
.
randn
(
10
)
y
=
trans
(
x
)
y
=
trans
(
x
)
assert
(
y
.
equal
(
x
))
assert
(
y
.
equal
(
x
))
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
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