InterpolationMode in PyTorch (2)

Buy Me a Coffee☕ *My post explains InterpolationMode about image in PyTorch. You can set InterpolationMode as shown below. *It's about image tensor: from torchvision.datasets import OxfordIIITPet from torchvision.transforms.v2 import Resize, PILToTensor origin_data = OxfordIIITPet( root="data", transform=None ) def show_rimagetensor(im, s=None, ip=None, a=None): r = Resize(size=s, interpolation=ip, antialias=a) ptt = PILToTensor() print(ptt(r(im))) def show_rrimagetensor(im, d=None, ip=None): rr = RandomRotation(degrees=d, interpolation=ip) ptt = PILToTensor() print(ptt(rr(im))[0][279]) # Resize() show_rimagetensor(im=origin_data[0][0], s=50, ip=InterpolationMode.NEAREST, a=True) # tensor([[[35, 38, 49, ..., 255, 255, 251], # [24, 244, 228, ..., 255, 255, 255], # ..., # [6, 2, 1, ..., 59, 65, 69]], # [[18, 19, 24, ..., 255, 255, 251], # [10, 179, 168, ..., 255, 255, 255], # ..., # [9, 8, 6, ..., 95, 100, 118]], # [[10, 13, 19, ..., 255, 255, 253], # [7, 77, 58, ..., 255, 255, 255], # ..., # [24, 24, 25, ..., 218, 220, 247]]], dtype=torch.uint8) show_rimagetensor(im=origin_data[0][0], s=50, ip=InterpolationMode.NEAREST, a=False) # tensor([[[35, 38, 49, ..., 255, 255, 251], # [24, 244, 228, ..., 255, 255, 255], # ..., # [6, 2, 1, ..., 59, 65, 69]], # [[18, 19, 24, ..., 255, 255, 251], # [10, 179, 168, ..., 255, 255, 255], # ..., # [9, 8, 6, ..., 95, 100, 118]], # [[10, 13, 19, ..., 255, 255, 253], # [7, 77, 58, ..., 255, 255, 255], # ..., # [24, 24, 25, ..., 218, 220, 247]]], dtype=torch.uint8) print() show_rimagetensor(im=origin_data[0][0], s=50, ip=InterpolationMode.NEAREST_EXACT, a=True) # tensor([[[35, 38, 49, ..., 255, 255, 251], # [24, 244, 228, ..., 255, 255, 255], # ..., # [6, 2, 1, ..., 59, 65, 69]], # [[18, 19, 24, ..., 255, 255, 251], # [10, 179, 168, ..., 255, 255, 255], # ..., # [9, 8, 6, ..., 95, 100, 118]], # [[10, 13, 19, ..., 255, 255, 253], # [7, 77, 58, ..., 255, 255, 255], # ..., # [24, 24, 25, ..., 218, 220, 247]]], dtype=torch.uint8) show_rimagetensor(im=origin_data[0][0], s=50, ip=InterpolationMode.NEAREST_EXACT, a=False) # tensor([[[35, 38, 49, ..., 255, 255, 251], # [24, 244, 228, ..., 255, 255, 255], # ..., # [6, 2, 1, ..., 59, 65, 69]], # [[18, 19, 24, ..., 255, 255, 251], # [10, 179, 168, ..., 255, 255, 255], # ..., # [9, 8, 6, ..., 95, 100, 118]], # [[10, 13, 19, ..., 255, 255, 253], # [7, 77, 58, ..., 255, 255, 255], # ..., # [24, 24, 25, ..., 218, 220, 247]]], dtype=torch.uint8) print() show_rimagetensor(im=origin_data[0][0], s=50, ip=InterpolationMode.BILINEAR, a=True) # tensor([[[37, 42, 66, ..., 254, 254, 253], # [122, 163, 209, ..., 255, 255, 255], # ..., # [15, 29, 34, ..., 81, 61, 60]], # [[20, 23, 40, ..., 254, 254, 253], # [101, 134, 175, ..., 255, 255, 255], # ..., # [17, 30, 35, ..., 114, 96, 97]], # [[12, 13, 18, ..., 254, 254, 254], # [50, 53, 65, ..., 255, 255, 255], # ..., # [32, 43, 48, ..., 219, 218, 223]]], dtype=torch.uint8) show_rimagetensor(im=origin_data[0][0], s=50, ip=InterpolationMode.BILINEAR, a=False) # tensor([[[37, 42, 66, ..., 254, 254, 253], # [122, 163, 209, ..., 255, 255, 255], # ..., # [15, 29, 34, ..., 81, 61, 60]], # [[20, 23, 40, ..., 254, 254, 253], # [101, 134, 175, ..., 255, 255, 255], # ..., # [17, 30, 35, ..., 114, 96, 97]], # [[12, 13, 18, ..., 254, 254, 254], # [50, 53, 65, ..., 255, 255, 255], # ..., # [32, 43, 48, ..., 219, 218, 223]]], dtype=torch.uint8) print() show_rimagetensor(im=origin_data[0][0], s=50, ip=InterpolationMode.BICUBIC, a=True) # tensor([[[28, 30, 52, ..., 255, 254, 252], # [116, 162, 216, ..., 255, 255, 255], # ..., # [14, 28, 33, ..., 66, 52, 59]], # [[10, 12, 26, ..., 255, 254, 252], # [95, 131, 179, ..., 255, 255, 255], # ..., # [16, 29, 34, ..., 102, 88, 96]], # [[8, 9, 14, ..., 255, 254, 254], # [44, 48, 64, ..., 255, 255, 255], # ..., # [30, 42, 47, ..., 216, 216, 224]]], dtype=torch.uint8) show_rimagetensor(im=origin_data[0][0], s=50, ip=InterpolationMode.BICUBIC, a=False) # tensor([[[28, 30, 52, ..., 255, 254, 252], # [116, 162, 216, ..., 255, 255, 255], # ..., # [14, 28, 33, ..., 66, 52, 59]], # [[10, 12, 26, ..., 255, 254,

May 13, 2025 - 07:00
 0
InterpolationMode in PyTorch (2)

Buy Me a Coffee

*My post explains InterpolationMode about image in PyTorch.

You can set InterpolationMode as shown below. *It's about image tensor:

from torchvision.datasets import OxfordIIITPet
from torchvision.transforms.v2 import Resize, PILToTensor

origin_data = OxfordIIITPet(
    root="data",
    transform=None
)

def show_rimagetensor(im, s=None, ip=None, a=None):
    r = Resize(size=s, interpolation=ip, antialias=a)
    ptt = PILToTensor()
    print(ptt(r(im)))

def show_rrimagetensor(im, d=None, ip=None):
    rr = RandomRotation(degrees=d, interpolation=ip)
    ptt = PILToTensor()
    print(ptt(rr(im))[0][279])

# Resize()
show_rimagetensor(im=origin_data[0][0], s=50,
                  ip=InterpolationMode.NEAREST, a=True)
# tensor([[[35, 38, 49, ..., 255, 255, 251],
#          [24, 244, 228, ..., 255, 255, 255],
#          ...,
#          [6, 2, 1, ..., 59, 65, 69]],
#         [[18, 19, 24, ..., 255, 255, 251],
#          [10, 179, 168, ..., 255, 255, 255],
#          ...,
#          [9, 8, 6, ..., 95, 100, 118]],
#         [[10, 13, 19, ..., 255, 255, 253],
#          [7, 77, 58, ..., 255, 255, 255],
#          ...,
#          [24, 24, 25, ..., 218, 220, 247]]], dtype=torch.uint8)
show_rimagetensor(im=origin_data[0][0], s=50,
                  ip=InterpolationMode.NEAREST, a=False)
# tensor([[[35, 38, 49, ..., 255, 255, 251],
#          [24, 244, 228, ..., 255, 255, 255],
#          ...,
#          [6, 2, 1, ..., 59, 65, 69]],
#         [[18, 19, 24, ..., 255, 255, 251],
#          [10, 179, 168, ..., 255, 255, 255],
#          ...,
#          [9, 8, 6, ...,  95, 100, 118]],
#         [[10, 13, 19, ..., 255, 255, 253],
#          [7, 77, 58, ..., 255, 255, 255],
#          ...,
#          [24, 24, 25, ..., 218, 220, 247]]], dtype=torch.uint8)
print()
show_rimagetensor(im=origin_data[0][0], s=50,
                  ip=InterpolationMode.NEAREST_EXACT, a=True)
# tensor([[[35, 38, 49, ..., 255, 255, 251],
#          [24, 244, 228, ..., 255, 255, 255],
#          ...,
#          [6, 2, 1, ..., 59, 65, 69]],
#         [[18, 19, 24, ..., 255, 255, 251],
#          [10, 179, 168, ..., 255, 255, 255],
#          ...,
#          [9, 8, 6, ..., 95, 100, 118]],
#         [[10, 13, 19, ..., 255, 255, 253],
#          [7, 77, 58, ..., 255, 255, 255],
#          ...,
#          [24, 24, 25, ..., 218, 220, 247]]], dtype=torch.uint8)
show_rimagetensor(im=origin_data[0][0], s=50,
                  ip=InterpolationMode.NEAREST_EXACT, a=False)
# tensor([[[35, 38, 49, ..., 255, 255, 251],
#          [24, 244, 228, ..., 255, 255, 255],
#          ...,
#          [6, 2, 1, ..., 59, 65, 69]],
#         [[18, 19, 24, ..., 255, 255, 251],
#          [10, 179, 168, ..., 255, 255, 255],
#          ...,
#          [9, 8, 6, ..., 95, 100, 118]],
#         [[10, 13, 19, ..., 255, 255, 253],
#          [7, 77, 58, ..., 255, 255, 255],
#          ...,
#          [24, 24, 25, ..., 218, 220, 247]]], dtype=torch.uint8)
print()
show_rimagetensor(im=origin_data[0][0], s=50,
                  ip=InterpolationMode.BILINEAR, a=True)
# tensor([[[37, 42, 66, ..., 254, 254, 253],
#          [122, 163, 209, ..., 255, 255, 255],
#          ...,
#          [15, 29, 34, ..., 81, 61, 60]],
#         [[20, 23, 40, ..., 254, 254, 253],
#          [101, 134, 175, ..., 255, 255, 255],
#          ...,
#          [17, 30, 35, ..., 114, 96, 97]],
#         [[12, 13, 18, ..., 254, 254, 254],
#          [50, 53, 65, ..., 255, 255, 255],
#          ...,
#          [32, 43, 48, ..., 219, 218, 223]]], dtype=torch.uint8)
show_rimagetensor(im=origin_data[0][0], s=50,
                  ip=InterpolationMode.BILINEAR, a=False)
# tensor([[[37, 42, 66, ..., 254, 254, 253],
#          [122, 163, 209, ..., 255, 255, 255],
#          ...,
#          [15, 29, 34, ..., 81, 61, 60]],
#         [[20, 23, 40, ..., 254, 254, 253],
#          [101, 134, 175, ..., 255, 255, 255],
#          ...,
#          [17, 30, 35, ..., 114, 96, 97]],
#         [[12, 13, 18, ..., 254, 254, 254],
#          [50, 53, 65, ..., 255, 255, 255],
#          ...,
#          [32, 43, 48, ..., 219, 218, 223]]], dtype=torch.uint8)
print()
show_rimagetensor(im=origin_data[0][0], s=50,
                  ip=InterpolationMode.BICUBIC, a=True)
# tensor([[[28, 30, 52, ..., 255, 254, 252],
#          [116, 162, 216, ..., 255, 255, 255],
#          ...,
#          [14, 28, 33, ..., 66, 52, 59]],
#         [[10, 12, 26, ..., 255, 254, 252],
#          [95, 131, 179, ..., 255, 255, 255],
#          ...,
#          [16, 29, 34, ..., 102, 88, 96]],
#         [[8, 9, 14, ..., 255, 254, 254],
#          [44, 48, 64, ..., 255, 255, 255],
#          ...,
#          [30, 42, 47, ..., 216, 216, 224]]], dtype=torch.uint8)
show_rimagetensor(im=origin_data[0][0], s=50, 
                  ip=InterpolationMode.BICUBIC, a=False)
# tensor([[[28, 30, 52, ..., 255, 254, 252],
#          [116, 162, 216, ..., 255, 255, 255],
#          ...,
#          [14, 28, 33, ..., 66, 52, 59]],
#         [[10, 12, 26, ..., 255, 254, 252],
#          [95, 131, 179, ..., 255, 255, 255],
#          ...,
#          [16, 29, 34, ..., 102, 88, 96]],
#         [[8, 9, 14, ..., 255, 254, 254],
#          [44, 48, 64, ..., 255, 255, 255],
#          ...,
#          [30, 42, 47, ..., 216, 216, 224]]], dtype=torch.uint8)
print()
show_rimagetensor(im=origin_data[0][0], s=50,
                  ip=InterpolationMode.BOX, a=True)
# tensor([[[37, 37, 44, ..., 254, 254, 252],
#          [117, 169, 231, ..., 255, 255, 255],
#          ...,
#          [13, 30, 30, ..., 69, 59, 60]],
#         [[19, 20, 23, ..., 254, 254, 252],
#          [96, 137, 192, ..., 255, 255, 255],
#          ...,
#          [15, 32, 31, ..., 103, 91, 97]],
#         [[12, 12, 14, ..., 254, 254, 254],
#          [44, 48, 68, ..., 255, 255, 255],
#          ...,
#          [29, 44, 44, ..., 217, 215, 224]]], dtype=torch.uint8)
show_rimagetensor(im=origin_data[0][0], s=50,
                  ip=InterpolationMode.BOX, a=False)
# tensor([[[37, 37, 44, ..., 254, 254, 252],
#          [117, 169, 231, ..., 255, 255, 255],
#          ...,
#          [13, 30, 30, ..., 69, 59, 60]],
#         [[19, 20, 23, ..., 254, 254, 252],
#          [96, 137, 192, ..., 255, 255, 255],
#          ...,
#          [15, 32, 31, ..., 103, 91, 97]],
#         [[12, 12, 14, ..., 254, 254, 254],
#          [44, 48, 68, ..., 255, 255, 255],
#          ...,
#          [29, 44, 44, ..., 217, 215, 224]]], dtype=torch.uint8)
print()
show_rimagetensor(im=origin_data[0][0], s=50, 
                  ip=InterpolationMode.HAMMING, a=True)
# tensor([[[37, 37, 52, ..., 254, 254, 252],
#          [113, 167, 222, ..., 255, 255, 255],
#          ...,
#          [11, 25, 26, ..., 70, 59, 60]],
#         [[19, 20, 29, ..., 254, 254, 252],
#          [92, 134, 182, ..., 255, 255, 255],
#          ...,
#          [13, 27, 27, ..., 104, 93, 98]],
#         [[12, 12, 15, ..., 254, 255, 253],
#          [42, 49, 65, ..., 255, 255, 255],
#          ...,
#          [28, 40, 42, ..., 218, 216, 225]]], dtype=torch.uint8)
show_rimagetensor(im=origin_data[0][0], s=50,
                  ip=InterpolationMode.HAMMING, a=False)
# tensor([[[37, 37, 52, ..., 254, 254, 252],
#          [113, 167, 222, ..., 255, 255, 255],
#          ...,
#          [11, 25, 26, ..., 70, 59, 60]],
#         [[19, 20, 29, ..., 254, 254, 252],
#          [92, 134, 182, ..., 255, 255, 255],
#          ...,
#          [13, 27, 27, ..., 104, 93, 98]],
#         [[12, 12, 15, ..., 254, 255, 253],
#          [42, 49, 65, ..., 255, 255, 255],
#          ...,
#          [28, 40, 42, ..., 218, 216, 225]]], dtype=torch.uint8)
print()
show_rimagetensor(im=origin_data[0][0], s=50,
                  ip=InterpolationMode.LANCZOS, a=True)
# tensor([[[21, 23, 48, ..., 255, 254, 252],
#          [112, 160, 218, ..., 255, 255, 255],
#          ...,
#          [14, 27, 34, ..., 60, 48, 61]],
#         [[4, 5, 21, ..., 254, 254, 252],
#          [92, 130, 180, ..., 255, 255, 255],
#          ...,
#          [16, 28, 35, ..., 97, 85, 97]],
#         [[6, 8, 12, ..., 254, 254, 254],
#          [41, 46, 64, ..., 255, 255, 255],
#          ...,
#          [30, 41, 48, ..., 214, 216, 224]]], dtype=torch.uint8)
show_rimagetensor(im=origin_data[0][0], s=50,
                  ip=InterpolationMode.LANCZOS, a=False)
# tensor([[[21, 23, 48, ..., 255, 254, 252],
#          [112, 160, 218, ..., 255, 255, 255],
#          ...,
#          [14, 27, 34, ..., 60, 48, 61]],
#         [[4, 5, 21, ..., 254, 254, 252],
#          [92, 130, 180, ..., 255, 255, 255],
#          ...,
#          [16, 28, 35, ..., 97, 85, 97]],
#         [[6, 8, 12, ..., 254, 254, 254],
#          [41, 46, 64, ..., 255, 255, 255],
#          ...,
#          [30, 41, 48, ..., 214, 216, 224]]], dtype=torch.uint8)
print()
# RandomRotation()
show_rrimagetensor(im=origin_data[0][0], d=[45, 45],
                   ip=InterpolationMode.NEAREST)
# tensor([230, 250, 252, 253, 250, 251, 252, 253, 251, 249,
#         252, 255, 253, 253, 253, 255, 253, 254, 252, 251,
#         247, 249, 247, 239, 238, ...,  66, 126,  52], dtype=torch.uint8)
show_rrimagetensor(im=origin_data[0][0], d=[45, 45],
                   ip=InterpolationMode.NEAREST_EXACT)
# tensor([230, 250, 252, 253, 250, 251, 252, 253, 251, 249,
#         252, 255, 253, 253, 253, 255, 253, 254, 252, 251,
#         247, 249, 247, 239, 238, ...,  66, 126,  52], dtype=torch.uint8)
show_rrimagetensor(im=origin_data[0][0], d=[45, 45],
                   ip=InterpolationMode.BILINEAR)
# tensor([236, 252, 252, 252, 251, 251, 252, 252, 250, 249,
#         253, 253, 252, 252, 253, 253, 252, 253, 250, 251,
#         249, 247, 247, 242, 240, ...,  62,  99,  76], dtype=torch.uint8)
show_rrimagetensor(im=origin_data[0][0], d=[45, 45],
                   ip=InterpolationMode.BICUBIC)
# tensor([241, 253, 252, 251, 251, 250, 252, 251, 249, 248,
#         253, 253, 252, 251, 254, 253, 251, 254, 250, 251,
#         249, 246, 247, 241, 237, ...,  52, 102,  77], dtype=torch.uint8)