DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on

InterpolationMode in PyTorch

Buy Me a Coffee

*Memos:

You can set InterpolationMode as shown below:

*Memos:

from torchvision.datasets import OxfordIIITPet
from torchvision.transforms.v2 import Resize, RandomRotation
from torchvision.transforms.functional import InterpolationMode

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

import matplotlib.pyplot as plt

def show_rimages(im, s=None, ip=None):
    title = "s" + str(s) + "ip" + str(ip).split(".")[1] + "a"
    title1 = title + "True_data"
    title2 = title + "False_data"
    plt.figure(figsize=[10, 8])
    for i in range(1, 3):
        plt.subplot(1, 2, i)
        r = Resize(size=s, interpolation=ip,
                   antialias=True if i == 1 else False)
        plt.title(label=title1 if i == 1 else title2, y=1, fontsize=14)
        plt.imshow(X=r(im))
    plt.tight_layout()
    plt.show()

def show_rrimages(im, d=None, ip=None):
    plt.figure(figsize=[10, 8])
    for i in range(2):
        if isinstance(d, collections.abc.Sequence):
            d1 = str(d[0]) if d[0] >= 0 else "n" + str(-1*d[0])
            d2 = str(d[1]) if d[1] >= 0 else "n" + str(-1*d[1])
            dpart = d1 + d2 if "n" in d2 else d1 + "_" + d2
        else:
            dpart = str(d)
        title = "d" + dpart + "ip" + str(ip[i]).split(".")[1] + "_data"
        plt.subplot(1, 2, (i+1))
        rr = RandomRotation(degrees=d, interpolation=ip[i])
        plt.title(label=title, y=1, fontsize=14)
        plt.imshow(X=rr(im))
    plt.tight_layout()
    plt.show()

plt.figure(figsize=[7, 9])
plt.title(label="s500_394origin_data", fontsize=14)
plt.imshow(X=origin_data[0][0])
plt.show()
print()
show_rimages(im=origin_data[0][0], s=50, ip=InterpolationMode.NEAREST)
show_rimages(im=origin_data[0][0], s=50, ip=InterpolationMode.NEAREST_EXACT)
show_rimages(im=origin_data[0][0], s=50, ip=InterpolationMode.BILINEAR)
show_rimages(im=origin_data[0][0], s=50, ip=InterpolationMode.BICUBIC)
show_rimages(im=origin_data[0][0], s=50, ip=InterpolationMode.BOX)
show_rimages(im=origin_data[0][0], s=50, ip=InterpolationMode.HAMMING)
show_rimages(im=origin_data[0][0], s=50, ip=InterpolationMode.LANCZOS)
print()
show_rrimages(im=origin_data[0][0], d=[45, 45],
              ip=[InterpolationMode.NEAREST, InterpolationMode.NEAREST_EXACT])
show_rrimages(im=origin_data[0][0], d=[45, 45],
              ip=[InterpolationMode.BILINEAR, InterpolationMode.BICUBIC])
Enter fullscreen mode Exit fullscreen mode

Image description


Image description

Image description

Image description

Image description

Image description

Image description

Image description


Image description

Image description

Image of Stellar post

How a Hackathon Project became a Web3 Startup 🚀

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Watch the video

Top comments (0)

Sentry image

Make it make sense

Only the context you need to fix your broken code with Sentry.

Start debugging →

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay