DEV Community

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

Posted on • Edited on

CenterCrop in PyTorch

Buy Me a Coffee

*Memos:

CenterCrop() can crop an image, centering on it as shown below:

*Memos:

  • The 1st argument for initialization is size(Required-Type:int or tuple/list(int) or size()): *Memos:
    • It's [height, width].
    • 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)) means [size, size].
  • The 1st argument is img(Required-Type:PIL Image or tensor(int)): *Memos:
    • A tensor must be 2D or 3D.
    • 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
)

s600_data = OxfordIIITPet( # `s` is size.
    root="data",
    transform=CenterCrop(size=600)
    # transform=CenterCrop(size=[600])
    # transform=CenterCrop(size=[600, 600])
)

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

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

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

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

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

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

s300_200_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=s600_data, main_title="s600_data")
show_images1(data=s400_data, main_title="s400_data")
show_images1(data=s200_data, main_title="s200_data")
show_images1(data=s100_data, main_title="s100_data")
show_images1(data=s50_data, main_title="s50_data")
show_images1(data=s10_data, main_title="s10_data")
print()
show_images1(data=origin_data, main_title="origin_data")
show_images1(data=s200_300_data, main_title="s200_300_data")
show_images1(data=s300_200_data, main_title="s300_200_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)
    temp_s = s
    for i, (im, _) in zip(range(1, 6), data):
        plt.subplot(1, 5, i)
        if not temp_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="s600_data", s=600)
show_images2(data=origin_data, main_title="s400_data", s=400)
show_images2(data=origin_data, main_title="s200_data", s=200)
show_images2(data=origin_data, main_title="s100_data", s=100)
show_images2(data=origin_data, main_title="s50_data", s=50)
show_images2(data=origin_data, main_title="s10_data", s=10)
print()
show_images2(data=origin_data, main_title="origin_data")
show_images2(data=origin_data, main_title="s200_300_data", s=[200, 300])
show_images2(data=origin_data, main_title="s300_200_data", s=[300, 200])
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

Billboard image

Synthetic monitoring. Built for developers.

Join Vercel, Render, and thousands of other teams that trust Checkly to streamline monitor creation and configuration with Monitoring as Code.

Start Monitoring

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay