Pytorch의 Resize에 대해서 알아보자.
CLASS torchvision.transforms.Resize(size, interpolation=InterpolationMode.BILINEAR, max_size=None, antialias=True)
Resize the input image to the given size. If the image is torch Tensor, it is expected to have [..., H, W] shape, where ... means a maximum of two leading dimensions
size (sequence or int)-
Desired output size. If size is a sequence like (h,w), output size will be matched to this. if size is an int, smaller edge of the image will be matched to this number. i.e, if height > width, then image will be rescaled to (height * size / width, size).
🔥[주의]🔥 size 입력 시에 int로 입력하면 h, w 중 작은 값이 해당 size에 맞춰지고 다른 쪽은 기존 ratio에 맞게 resize됨.
Note
- In torchscript mode size as single int is not supported, use a sequence of length 1:
[size, ].
Interpolation (InterploationMode) - Desired interpolation enum defined by torchvision.transforms.InterpolationMode. Default is InterpolationMode.BILINEAR. If input is Tensor, only InterpolationMode.NEAREST,InterpolationMode.NEAREST_EXACT,InterpolationMode.BILINEAR and InterpolationMode.BICUBIC are supported. The corresponding Pillow integer constants, e.g. PIL.Image.BILINEAR are accepted as well.
🔥[주의]🔥 torchvision.transforms.InterpolationMode 는 이미지의 크기를 변경할 때, 새로운 픽셀의 값을 어떻게 채워 넣을지 결정하는 수학적인 방법임; 이미지를 늘리거나 줄이면 기존 픽셀 정보만으로는 부족해서 주변 픽셀을 참고해서 새 픽셀을 추정 해야하는데 이 방식을 보간(Interpolation) 이라고 한다.
- NEAREST : 가장 가까운 원본 픽셀의 값을 그대로 복사하여 사용함; 가장 빠르지만, 결과 이미지의 경계선이 거칠고 계단 현상(aliasing)이 뚜렷하게 나타남.
- NEAREST_EXACT : NEAREST와 유사한데, 좌표 정렬 방식에서 약간의 기술적 정밀도를 높인 버전; 일반적인 NEAREST보다 약간 더 정확한 픽셀 매핑이 필요할 때 사용됨.
- BILINEAR : 새로운 픽셀 주변의 4개 픽셀을 사용하여 가중 평균을 계산; 속도와 결과 품질의 균형이 가장 좋아서 이미지 모델 훈련에서 가장 일반적으로 사용되는 기본값.
- BICUBIC : 새로운 픽셀 주변의 16개 픽셀 (4x4격자)를 사용하여 훨씬 더 복잡한 곡선 방정식을 적용; 가장 부드럽고 고화질의 결과물을 제공하지만, 계산량이 많아 가장 느림.
max_size (int,optional) - The maximum allowed for the longer edge of the resized image. If the longer edge of the image is greater than max_size after being resized according to size, size will be overruled so that the longer edge is equal to max_size. As a result, the smaller edge may be shorter than size. This is only supported if size is an int (or a sequence of length 1 in torchscript mode).
antialias (bool,optional) - Whether to apply antialiasing. It only affects tensors with bilinear of bicubic modes and it is ignored otherwise: on PIL images, antialiasing is always applied on bilinear or bicubic modes; on ther modes (for PIL images and tensors), antialiasing makes no sense and this parameter is ignored. Possible values are:
- True (default) : will apply antialiasing for bilinear or bicubic modes. Other mode aren't affected. This is probably what you want to use.
- False : will not apply antialiasing for tensors on any model. PIL images are still antialiased on bilinear or bicubic modes, because PIL doesn't support no antialias.
- None : equivalent to False for tensors and True for PIL images. This Value exists for legacy reasons and you probably don't want to use it unless you really know what you are doing.
- The default value changed from None to True in v0.17, for the PIL and Tensor backends to be consistent.
🔥[주의]🔥 Antialias은 이미지를 resize할 때 발생하는 jagged edges 를 부드럽게 처리하여 이미지 품질을 개선하는 기술
-- 문제 : 이미지를 Downsampling할 때, 대각선이나 곡선의 날카로운 경계선에 있던 픽셀들이 뭉쳐지면서 깨진 것처럼 보이거나 계단 모양으로 거칠게 변화함.
-- 해결책 (Antialiasing) : 안티 얼라이어싱을 적용하면, 경계선 주변 픽셀들의 색상과 배경색을 미세하게 섞어서 (약간의 블러) 픽셀 경계를 부드럽게 만들고 시각적인 거슬림을 줄인다. 결과적으로 모델이 더 자연스로운 입력을받음.
-- 지원되는 보간모드는 BILINEAR and BICUBIC : 이 모드들은 이미 주변 픽셀들을 Blending 방식으로 새로운 픽셀을 추정. 안티앨리어싱은 이 블렌딩 과정을 경계선에서 더 정교하게 만들어서 화질을 높임.
-- NEAREST : 이 모드는 주변 픽셀 중 하나를 선택만하고 섞지 않아서, 안티앨리어싱을 적용하는 것이 무의미하거나 지원이 안됨.
https://docs.pytorch.org/vision/main/generated/torchvision.transforms.Resize.html