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
7aeec57f
Commit
7aeec57f
authored
Sep 04, 2017
by
Sasank Chilamkurthy
Browse files
Asserts for functions
parent
bf38166d
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
1 deletion
+26
-1
torchvision/transforms.py
torchvision/transforms.py
+26
-1
No files found.
torchvision/transforms.py
View file @
7aeec57f
...
@@ -13,7 +13,24 @@ import types
...
@@ -13,7 +13,24 @@ import types
import
collections
import
collections
def
_is_pil_image
(
img
):
if
accimage
is
not
None
:
return
isinstance
(
img
,
(
Image
.
Image
,
accimage
.
Image
))
else
:
return
isinstance
(
img
,
Image
.
Image
)
def
_is_tensor_image
(
img
):
return
torch
.
is_tensor
(
img
)
and
img
.
ndimension
()
==
3
def
_is_numpy_image
(
img
):
return
isinstance
(
img
,
np
.
ndarray
)
and
(
img
.
ndim
in
{
2
,
3
})
def
to_tensor
(
pic
):
def
to_tensor
(
pic
):
assert
_is_pil_image
(
pic
)
or
_is_numpy_image
(
pic
),
'pic should be PIL Image or ndarray'
if
isinstance
(
pic
,
np
.
ndarray
):
if
isinstance
(
pic
,
np
.
ndarray
):
# handle numpy array
# handle numpy array
img
=
torch
.
from_numpy
(
pic
.
transpose
((
2
,
0
,
1
)))
img
=
torch
.
from_numpy
(
pic
.
transpose
((
2
,
0
,
1
)))
...
@@ -50,13 +67,15 @@ def to_tensor(pic):
...
@@ -50,13 +67,15 @@ def to_tensor(pic):
def
to_pilimage
(
pic
):
def
to_pilimage
(
pic
):
assert
_is_numpy_image
(
pic
)
or
_is_tensor_image
(
pic
),
'pic should be Tensor or ndarray'
npimg
=
pic
npimg
=
pic
mode
=
None
mode
=
None
if
isinstance
(
pic
,
torch
.
FloatTensor
):
if
isinstance
(
pic
,
torch
.
FloatTensor
):
pic
=
pic
.
mul
(
255
).
byte
()
pic
=
pic
.
mul
(
255
).
byte
()
if
torch
.
is_tensor
(
pic
):
if
torch
.
is_tensor
(
pic
):
npimg
=
np
.
transpose
(
pic
.
numpy
(),
(
1
,
2
,
0
))
npimg
=
np
.
transpose
(
pic
.
numpy
(),
(
1
,
2
,
0
))
assert
isinstance
(
npimg
,
np
.
ndarray
)
,
'pic should be Tensor or ndarray'
assert
isinstance
(
npimg
,
np
.
ndarray
)
if
npimg
.
shape
[
2
]
==
1
:
if
npimg
.
shape
[
2
]
==
1
:
npimg
=
npimg
[:,
:,
0
]
npimg
=
npimg
[:,
:,
0
]
...
@@ -76,6 +95,7 @@ def to_pilimage(pic):
...
@@ -76,6 +95,7 @@ def to_pilimage(pic):
def
normalize
(
tensor
,
mean
,
std
):
def
normalize
(
tensor
,
mean
,
std
):
assert
_is_tensor_image
(
tensor
)
# TODO: make efficient
# TODO: make efficient
for
t
,
m
,
s
in
zip
(
tensor
,
mean
,
std
):
for
t
,
m
,
s
in
zip
(
tensor
,
mean
,
std
):
t
.
sub_
(
m
).
div_
(
s
)
t
.
sub_
(
m
).
div_
(
s
)
...
@@ -83,6 +103,7 @@ def normalize(tensor, mean, std):
...
@@ -83,6 +103,7 @@ def normalize(tensor, mean, std):
def
scale
(
img
,
size
,
interpolation
=
Image
.
BILINEAR
):
def
scale
(
img
,
size
,
interpolation
=
Image
.
BILINEAR
):
assert
_is_pil_image
(
img
),
'img should be PIL Image'
assert
isinstance
(
size
,
int
)
or
(
isinstance
(
size
,
collections
.
Iterable
)
and
len
(
size
)
==
2
)
assert
isinstance
(
size
,
int
)
or
(
isinstance
(
size
,
collections
.
Iterable
)
and
len
(
size
)
==
2
)
if
isinstance
(
size
,
int
):
if
isinstance
(
size
,
int
):
w
,
h
=
img
.
size
w
,
h
=
img
.
size
...
@@ -101,6 +122,7 @@ def scale(img, size, interpolation=Image.BILINEAR):
...
@@ -101,6 +122,7 @@ def scale(img, size, interpolation=Image.BILINEAR):
def
pad
(
img
,
padding
,
fill
=
0
):
def
pad
(
img
,
padding
,
fill
=
0
):
assert
_is_pil_image
(
img
),
'img should be PIL Image'
assert
isinstance
(
padding
,
(
numbers
.
Number
,
tuple
))
assert
isinstance
(
padding
,
(
numbers
.
Number
,
tuple
))
assert
isinstance
(
fill
,
(
numbers
.
Number
,
str
,
tuple
))
assert
isinstance
(
fill
,
(
numbers
.
Number
,
str
,
tuple
))
if
isinstance
(
padding
,
collections
.
Sequence
)
and
len
(
padding
)
not
in
[
2
,
4
]:
if
isinstance
(
padding
,
collections
.
Sequence
)
and
len
(
padding
)
not
in
[
2
,
4
]:
...
@@ -111,15 +133,18 @@ def pad(img, padding, fill=0):
...
@@ -111,15 +133,18 @@ def pad(img, padding, fill=0):
def
crop
(
img
,
x
,
y
,
w
,
h
):
def
crop
(
img
,
x
,
y
,
w
,
h
):
assert
_is_pil_image
(
img
),
'img should be PIL Image'
return
img
.
crop
((
x
,
y
,
x
+
w
,
y
+
h
))
return
img
.
crop
((
x
,
y
,
x
+
w
,
y
+
h
))
def
scaled_crop
(
img
,
x
,
y
,
w
,
h
,
size
,
interpolation
=
Image
.
BILINEAR
):
def
scaled_crop
(
img
,
x
,
y
,
w
,
h
,
size
,
interpolation
=
Image
.
BILINEAR
):
assert
_is_pil_image
(
img
),
'img should be PIL Image'
img
=
crop
(
img
,
x
,
y
,
w
,
h
)
img
=
crop
(
img
,
x
,
y
,
w
,
h
)
img
=
scale
(
img
,
size
,
interpolation
)
img
=
scale
(
img
,
size
,
interpolation
)
def
hflip
(
img
):
def
hflip
(
img
):
assert
_is_pil_image
(
img
),
'img should be PIL Image'
return
img
.
transpose
(
Image
.
FLIP_LEFT_RIGHT
)
return
img
.
transpose
(
Image
.
FLIP_LEFT_RIGHT
)
...
...
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