Home > Backend Development > Python Tutorial > Resize in PyTorch

Resize in PyTorch

Mary-Kate Olsen
Release: 2025-01-19 22:12:10
Original
938 people have browsed it

Buy Me a Coffee☕

*Memos:

  • My post explains OxfordIIITPet().

Resize() can resize zero or more images as shown below:

*Memos:

  • The 1st argument for initialization is size(Required-Type:int or tuple/list(int)): *Memos:
    • It's [width, height].
    • It must be 1 <= x.
    • A tuple/list must be the 1D with 1 or 2 elements.
    • A single value(int or tuple/list(int`)) is applied to a smaller image's width or height edge, then the other larger width or height edge is also resized: *Memos:
    • If an image width is smaller than its height, it's [size, size * width / height].
    • If an image width is larger than its height, it's [size * width / height , size].
    • If an image width is equal to its height, it's [size, size].
  • The 2nd argument for initialization is interpolation(Optional-Default:InterpolationMode.BILINEAR-Type:InterpolationMode).
  • The 3rd argument for initialization is max_size(Optional-Default:None-Type:int): *Memos:
    • It's only supported if size is a single value(int or tuple/list(int`)).
    • After size is applied if a larger image's width or height edge exceeds it, it's applied to a larger image's width or height edge to limit the image size, then the other smaller image's width or height edge also becomes smaller than before.
  • The 4th argument for initialization is antialias(Optional-Default:True-Type:bool). *Even if setting False to it, it's always True if interpolation is InterpolationMode.BILINEAR or InterpolationMode.BICUBIC.
  • The 1st argument is img(Required-Type:PIL Image or tensor(int, float, complex or bool)): *Memos:
    • A tensor must be the 3D or more D tensor of one or more elements.
    • Don't use img=.
  • v2 is recommended to use according to V1 or V2? Which one should I use?.
from torchvision.datasets import OxfordIIITPet
from torchvision.transforms.v2 import Resize
from torchvision.transforms.functional import InterpolationMode

resize = Resize(size=100)
resize = Resize(size=100,
                interpolation=InterpolationMode.BILINEAR,
                max_size=None,
                antialias=True)
resize
# Resize(size=[100],
#        interpolation=InterpolationMode.BILINEAR,
#        antialias=True)

resize.size
# [100]

resize.interpolation
# 

print(resize.max_size)
# None

resize.antialias
# True

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

p1000_data = OxfordIIITPet(
    root="data",
    transform=Resize(size=1000)
    # transform=Resize(size=[1000])
)

p100_data = OxfordIIITPet(
    root="data",
    transform=Resize(size=100)
)

p50_data = OxfordIIITPet(
    root="data",
    transform=Resize(size=50)
)

p10_data = OxfordIIITPet(
    root="data",
    transform=Resize(size=10)
)

p100p180_data = OxfordIIITPet(
    root="data",
    transform=Resize(size=[100, 180])
)

p180p100_data = OxfordIIITPet(
    root="data",
    transform=Resize(size=[180, 100])
)

p100ms110_data = OxfordIIITPet(
    root="data",
    transform=Resize(size=100, max_size=110)
)

import matplotlib.pyplot as plt

def show_images1(data, main_title=None):
    plt.figure(figsize=(10, 5))
    plt.suptitle(t=main_title, y=0.8, fontsize=14)
    for i, (im, _) in zip(range(1, 6), data):
        plt.subplot(1, 5, i)
        plt.imshow(X=im)
    plt.tight_layout()
    plt.show()

show_images1(data=origin_data, main_title="origin_data")
show_images1(data=p1000_data, main_title="p1000_data")
show_images1(data=p100_data, main_title="p100_data")
show_images1(data=p50_data, main_title="p50_data")
show_images1(data=p10_data, main_title="p10_data")
print()
show_images1(data=origin_data, main_title="origin_data")
show_images1(data=p100p180_data, main_title="p100p180_data")
show_images1(data=p180p100_data, main_title="p180p100_data")
print()
show_images1(data=p100_data, main_title="p100_data")
show_images1(data=p100ms110_data, main_title='p100ms110_data')

# ↓ ↓ ↓ ↓ ↓ ↓ The code below is identical to the code above. ↓ ↓ ↓ ↓ ↓ ↓
def show_images2(data, main_title=None, s=None, ms=None):
    plt.figure(figsize=(10, 5))
    plt.suptitle(t=main_title, y=0.8, fontsize=14)
    for i, (im, _) in zip(range(1, 6), data):
        plt.subplot(1, 5, i)
        if not s:
            s = im.size
        resize = Resize(size=s, max_size=ms) # Here
        plt.imshow(X=resize(im)) # Here
    plt.tight_layout()
    plt.show()

show_images2(data=origin_data, main_title="origin_data")
show_images2(data=origin_data, main_title="p1000_data", s=1000)
show_images2(data=origin_data, main_title="p100_data", s=100)
show_images2(data=origin_data, main_title="p50_data", s=50)
show_images2(data=origin_data, main_title="p10_data", s=10)
print()
show_images2(data=origin_data, main_title="origin_data")
show_images2(data=origin_data, main_title="p100p180_data", s=[100, 180])
show_images2(data=origin_data, main_title="p180p100_data", s=[180, 100])
print()
show_images2(data=origin_data, main_title="p100_data", s=100)
show_images2(data=origin_data, main_title="p100ms110_data", s=100, ms=110)




Image description

Image description

Image description

Image description

Image description


Image description

Image description

Image description


Image description

Image description

The above is the detailed content of Resize in PyTorch. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template