Home > Backend Development > Python Tutorial > CenterCrop in PyTorch

CenterCrop in PyTorch

Susan Sarandon
Release: 2025-01-20 12:15:13
Original
317 people have browsed it

Buy Me a Coffee☕

*Memos:

  • My post explains OxfordIIITPet().

CenterCrop() can crop zero or more images, centering on them as shown below:

*Memos:

  • The 1st argument for initialization is size(Required-Type:int, float or tuple/list(int or float) or size()): *Memos:
    • It's [height, width].
    • It must be 0 <= x.
    • A tuple/list must be the 1D with 1 or 2 elements.
    • A single value(int, float or tuple/list(int or float) means [size, size].
  • The 1st argument is img(Required-Type:PIL Image or tensor(int, float, complex or bool)): *Memos:
    • A tensor must be the 2D or more D of zero 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 CenterCrop

centercrop = CenterCrop(size=100)

centercrop
# CenterCrop(size=(100, 100))

centercrop.size
# (100, 100)

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

p600_data = OxfordIIITPet(
    root="data",
    transform=CenterCrop(size=600)
    # transform=CenterCrop(size=[600])
    # transform=CenterCrop(size=[600, 600])
)

p400_data = OxfordIIITPet(
    root="data",
    transform=CenterCrop(size=400)
)

p200_data = OxfordIIITPet(
    root="data",
    transform=CenterCrop(size=200)
)

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

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

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

p200p300_data = OxfordIIITPet(
    root="data",
    transform=CenterCrop(size=[200, 300])
)

p300p200_data = OxfordIIITPet(
    root="data",
    transform=CenterCrop(size=[300, 200])
)

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=p600_data, main_title="p600_data")
show_images1(data=p400_data, main_title="p400_data")
show_images1(data=p200_data, main_title="p200_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=p200p300_data, main_title="p200p300_data")
show_images1(data=p300p200_data, main_title="p300p200_data")

# ↓ ↓ ↓ ↓ ↓ ↓ The code below is identical to the code above. ↓ ↓ ↓ ↓ ↓ ↓
def show_images2(data, main_title=None, s=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[1], im.size[0]]
        cc = CenterCrop(size=s) # Here
        plt.imshow(X=cc(im)) # Here
    plt.tight_layout()
    plt.show()

show_images2(data=origin_data, main_title="origin_data")
show_images2(data=origin_data, main_title="p600_data", s=600)
show_images2(data=origin_data, main_title="p400_data", s=400)
show_images2(data=origin_data, main_title="p200_data", s=200)
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="p200p300_data", s=[200, 300])
show_images2(data=origin_data, main_title="p300p200_data", s=[300, 200])
Copy after login

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 CenterCrop 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