Skip to content

Commit 8b18f52

Browse files
committed
raise TypeErrors instead of assertins
1 parent 7aeec57 commit 8b18f52

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

torchvision/transforms.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ def _is_numpy_image(img):
2929

3030

3131
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)))
3334

3435
if isinstance(pic, np.ndarray):
3536
# handle numpy array
@@ -67,7 +68,8 @@ def to_tensor(pic):
6768

6869

6970
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)))
7173

7274
npimg = pic
7375
mode = None
@@ -95,16 +97,20 @@ def to_pilimage(pic):
9597

9698

9799
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.')
99102
# TODO: make efficient
100103
for t, m, s in zip(tensor, mean, std):
101104
t.sub_(m).div_(s)
102105
return tensor
103106

104107

105108
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+
108114
if isinstance(size, int):
109115
w, h = img.size
110116
if (w <= h and w == size) or (h <= w and h == size):
@@ -122,9 +128,14 @@ def scale(img, size, interpolation=Image.BILINEAR):
122128

123129

124130
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+
128139
if isinstance(padding, collections.Sequence) and len(padding) not in [2, 4]:
129140
raise ValueError("Padding must be an int or a 2, or 4 element tuple, not a " +
130141
"{} element tuple".format(len(padding)))
@@ -133,7 +144,9 @@ def pad(img, padding, fill=0):
133144

134145

135146
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+
137150
return img.crop((x, y, x + w, y + h))
138151

139152

@@ -144,7 +157,9 @@ def scaled_crop(img, x, y, w, h, size, interpolation=Image.BILINEAR):
144157

145158

146159
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+
148163
return img.transpose(Image.FLIP_LEFT_RIGHT)
149164

150165

0 commit comments

Comments
 (0)