DEV Community

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

Posted on

CenterCrop in PyTorch

Buy Me a Coffee

*Memos:

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)
    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="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])
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

Top comments (0)