@@ -29,7 +29,8 @@ def _is_numpy_image(img):
29
29
30
30
31
31
def to_tensor (pic ):
32
- assert _is_pil_image (pic ) or _is_numpy_image (pic ), 'pic should be PIL Image or ndarray'
32
+ if not (_is_pil_image (pic ) or _is_numpy_image (pic )):
33
+ raise TypeError ('pic should be PIL Image or ndarray. Got {}' .format (type (pic )))
33
34
34
35
if isinstance (pic , np .ndarray ):
35
36
# handle numpy array
@@ -67,7 +68,8 @@ def to_tensor(pic):
67
68
68
69
69
70
def to_pilimage (pic ):
70
- assert _is_numpy_image (pic ) or _is_tensor_image (pic ), 'pic should be Tensor or ndarray'
71
+ if not (_is_numpy_image (pic ) or _is_tensor_image (pic )):
72
+ raise TypeError ('pic should be Tensor or ndarray. Got {}.' .format (type (pic )))
71
73
72
74
npimg = pic
73
75
mode = None
@@ -95,16 +97,20 @@ def to_pilimage(pic):
95
97
96
98
97
99
def normalize (tensor , mean , std ):
98
- assert _is_tensor_image (tensor )
100
+ if not _is_tensor_image (tensor ):
101
+ raise TypeError ('tensor is not a torch image.' )
99
102
# TODO: make efficient
100
103
for t , m , s in zip (tensor , mean , std ):
101
104
t .sub_ (m ).div_ (s )
102
105
return tensor
103
106
104
107
105
108
def scale (img , size , interpolation = Image .BILINEAR ):
106
- assert _is_pil_image (img ), 'img should be PIL Image'
107
- assert isinstance (size , int ) or (isinstance (size , collections .Iterable ) and len (size ) == 2 )
109
+ if not _is_pil_image (img ):
110
+ raise TypeError ('img should be PIL Image. Got {}' .format (type (img )))
111
+ if not (isinstance (size , int ) or (isinstance (size , collections .Iterable ) and len (size ) == 2 )):
112
+ raise TypeError ('Got inappropriate size arg: {}' .format (size ))
113
+
108
114
if isinstance (size , int ):
109
115
w , h = img .size
110
116
if (w <= h and w == size ) or (h <= w and h == size ):
@@ -122,9 +128,14 @@ def scale(img, size, interpolation=Image.BILINEAR):
122
128
123
129
124
130
def pad (img , padding , fill = 0 ):
125
- assert _is_pil_image (img ), 'img should be PIL Image'
126
- assert isinstance (padding , (numbers .Number , tuple ))
127
- assert isinstance (fill , (numbers .Number , str , tuple ))
131
+ if not _is_pil_image (img ):
132
+ raise TypeError ('img should be PIL Image. Got {}' .format (type (img )))
133
+
134
+ if not isinstance (padding , (numbers .Number , tuple )):
135
+ raise TypeError ('Got inappropriate padding arg' )
136
+ if not isinstance (fill , (numbers .Number , str , tuple )):
137
+ raise TypeError ('Got inappropriate fill arg' )
138
+
128
139
if isinstance (padding , collections .Sequence ) and len (padding ) not in [2 , 4 ]:
129
140
raise ValueError ("Padding must be an int or a 2, or 4 element tuple, not a " +
130
141
"{} element tuple" .format (len (padding )))
@@ -133,7 +144,9 @@ def pad(img, padding, fill=0):
133
144
134
145
135
146
def crop (img , x , y , w , h ):
136
- assert _is_pil_image (img ), 'img should be PIL Image'
147
+ if not _is_pil_image (img ):
148
+ raise TypeError ('img should be PIL Image. Got {}' .format (type (img )))
149
+
137
150
return img .crop ((x , y , x + w , y + h ))
138
151
139
152
@@ -144,7 +157,9 @@ def scaled_crop(img, x, y, w, h, size, interpolation=Image.BILINEAR):
144
157
145
158
146
159
def hflip (img ):
147
- assert _is_pil_image (img ), 'img should be PIL Image'
160
+ if not _is_pil_image (img ):
161
+ raise TypeError ('img should be PIL Image. Got {}' .format (type (img )))
162
+
148
163
return img .transpose (Image .FLIP_LEFT_RIGHT )
149
164
150
165
0 commit comments