Skip to content

Commit f91a182

Browse files
ssnlsoumith
authored andcommitted
fix py37 warning (#600)
1 parent fc7911c commit f91a182

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

torchvision/transforms/functional.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import division
22
import torch
3+
import sys
34
import math
45
import random
56
from PIL import Image, ImageOps, ImageEnhance, PILLOW_VERSION
@@ -13,6 +14,13 @@
1314
import collections
1415
import warnings
1516

17+
if sys.version_info < (3, 3):
18+
Sequence = collections.Sequence
19+
Iterable = collections.Iterable
20+
else:
21+
Sequence = collections.abc.Sequence
22+
Iterable = collections.abc.Iterable
23+
1624

1725
def _is_pil_image(img):
1826
if accimage is not None:
@@ -191,7 +199,7 @@ def resize(img, size, interpolation=Image.BILINEAR):
191199
"""
192200
if not _is_pil_image(img):
193201
raise TypeError('img should be PIL Image. Got {}'.format(type(img)))
194-
if not (isinstance(size, int) or (isinstance(size, collections.Iterable) and len(size) == 2)):
202+
if not (isinstance(size, int) or (isinstance(size, Iterable) and len(size) == 2)):
195203
raise TypeError('Got inappropriate size arg: {}'.format(size))
196204

197205
if isinstance(size, int):
@@ -258,7 +266,7 @@ def pad(img, padding, fill=0, padding_mode='constant'):
258266
if not isinstance(padding_mode, str):
259267
raise TypeError('Got inappropriate padding_mode arg')
260268

261-
if isinstance(padding, collections.Sequence) and len(padding) not in [2, 4]:
269+
if isinstance(padding, Sequence) and len(padding) not in [2, 4]:
262270
raise ValueError("Padding must be an int or a 2, or 4 element tuple, not a " +
263271
"{} element tuple".format(len(padding)))
264272

@@ -270,10 +278,10 @@ def pad(img, padding, fill=0, padding_mode='constant'):
270278
else:
271279
if isinstance(padding, int):
272280
pad_left = pad_right = pad_top = pad_bottom = padding
273-
if isinstance(padding, collections.Sequence) and len(padding) == 2:
281+
if isinstance(padding, Sequence) and len(padding) == 2:
274282
pad_left = pad_right = padding[0]
275283
pad_top = pad_bottom = padding[1]
276-
if isinstance(padding, collections.Sequence) and len(padding) == 4:
284+
if isinstance(padding, Sequence) and len(padding) == 4:
277285
pad_left = padding[0]
278286
pad_top = padding[1]
279287
pad_right = padding[2]

torchvision/transforms/transforms.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import division
22
import torch
33
import math
4+
import sys
45
import random
56
from PIL import Image, ImageOps, ImageEnhance
67
try:
@@ -15,6 +16,14 @@
1516

1617
from . import functional as F
1718

19+
if sys.version_info < (3, 3):
20+
Sequence = collections.Sequence
21+
Iterable = collections.Iterable
22+
else:
23+
Sequence = collections.abc.Sequence
24+
Iterable = collections.abc.Iterable
25+
26+
1827
__all__ = ["Compose", "ToTensor", "ToPILImage", "Normalize", "Resize", "Scale", "CenterCrop", "Pad",
1928
"Lambda", "RandomApply", "RandomChoice", "RandomOrder", "RandomCrop", "RandomHorizontalFlip",
2029
"RandomVerticalFlip", "RandomResizedCrop", "RandomSizedCrop", "FiveCrop", "TenCrop", "LinearTransformation",
@@ -163,7 +172,7 @@ class Resize(object):
163172
"""
164173

165174
def __init__(self, size, interpolation=Image.BILINEAR):
166-
assert isinstance(size, int) or (isinstance(size, collections.Iterable) and len(size) == 2)
175+
assert isinstance(size, int) or (isinstance(size, Iterable) and len(size) == 2)
167176
self.size = size
168177
self.interpolation = interpolation
169178

@@ -255,7 +264,7 @@ def __init__(self, padding, fill=0, padding_mode='constant'):
255264
assert isinstance(padding, (numbers.Number, tuple))
256265
assert isinstance(fill, (numbers.Number, str, tuple))
257266
assert padding_mode in ['constant', 'edge', 'reflect', 'symmetric']
258-
if isinstance(padding, collections.Sequence) and len(padding) not in [2, 4]:
267+
if isinstance(padding, Sequence) and len(padding) not in [2, 4]:
259268
raise ValueError("Padding must be an int or a 2, or 4 element tuple, not a " +
260269
"{} element tuple".format(len(padding)))
261270

0 commit comments

Comments
 (0)