
/home/.../anaconda3/lib/python3.7/site-packages/torchvision/transforms/transforms.py:281: UserWarning: Argument interpolation should be of type InterpolationMode instead of int. Please, use InterpolationMode enum. "Argument interpolation should be of type InterpolationMode instead of int. "
错误原因: 原生代码与本地pytorch版本不一致
2020-11-28 nightly release (4ab46e5f7585b86fb2befdc32d22d13635868c4e) · be02dfa3b9 - vision - uniartisan: Git with group
解决方法:修改只需transforms.py 文件中的方法以及相关调用 (成功解决)
一:如 将文件中的 Image.BICUBIC 改为 InterpolationMode.BICUBIC
源代码:
_pil_interpolation_to_str = {
Image.NEAREST: 'PIL.Image.NEAREST',
Image.BILINEAR: 'PIL.Image.BILINEAR',
Image.BICUBIC: 'PIL.Image.BICUBIC',
Image.LANCZOS: 'PIL.Image.LANCZOS',
Image.HAMMING: 'PIL.Image.HAMMING',
Image.BOX: 'PIL.Image.BOX',
}
def _pil_interp(method):
if method == 'bicubic':
return Image.BICUBIC
elif method == 'lanczos':
return Image.LANCZOS
elif method == 'hamming':
return Image.HAMMING
else:
# default bilinear, do we want to allow nearest?
return Image.BILINEAR
_RANDOM_INTERPOLATION = (Image.BILINEAR, Image.BICUBIC)
修改后代码:
_pil_interpolation_to_str = {
InterpolationMode.NEAREST: 'InterpolationMode.NEAREST',
InterpolationMode.BILINEAR: 'InterpolationMode.BILINEAR',
InterpolationMode.BICUBIC: 'InterpolationMode.BICUBIC',
InterpolationMode.LANCZOS: 'InterpolationMode.LANCZOS',
InterpolationMode.HAMMING: 'InterpolationMode.HAMMING',
InterpolationMode.BOX: 'InterpolationMode.BOX',
}
def _pil_interp_torch10(method):
if method == 'bicubic':
return InterpolationMode.BICUBIC
elif method == 'lanczos':
return InterpolationMode.LANCZOS
elif method == 'hamming':
return InterpolationMode.HAMMING
else:
# default bilinear, do we want to allow nearest?
return InterpolationMode.BILINEAR
_RANDOM_INTERPOLATION = (InterpolationMode.BILINEAR, InterpolationMode.BICUBIC)
并且添加 InterpolationMode 类(torchvision.transforms.functional — Torchvision 0.10.0 documentationhttps://pytorch.org/vision/stable/_modules/torchvision/transforms/functional.html#InterpolationMode)
二: 其他文件如rand_augment_transform都会调用_pil_interp(method):故仍需同时保留原_pil_interp(method)文件。
def _pil_interp(method):
if method == 'bicubic':
return Image.BICUBIC
elif method == 'lanczos':
return Image.LANCZOS
elif method == 'hamming':
return Image.HAMMING
else:
# default bilinear, do we want to allow nearest?
return Image.BILINEAR