FiveCrop in PyTorch

Barbara Streisand
Barbara StreisandOriginal
2025-01-21 12:12:09807browse

Buy Me a Coffee☕

*Memos:

  • My post explains OxfordIIITPet().

FiveCrop() can crop an image into 5 parts(Top-left, Top-right, bottom-left, bottom-right and center) 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 the 2D or 3D 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 FiveCrop

fivecrop = FiveCrop(size=100)

fivecrop
# FiveCrop(size=(100, 100))

fivecrop.size
# (100, 100)

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

p500p394origin_data = OxfordIIITPet(
    root="data",
    transform=FiveCrop(size=[500, 394])
    # transform=FiveCrop(size=[600])
    # transform=FiveCrop(size=[600, 600])
)

p300_data = OxfordIIITPet(
    root="data",
    transform=FiveCrop(size=300)
)

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

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

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

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

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

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

import matplotlib.pyplot as plt

def show_images1(fcims, main_title=None):
    plt.figure(figsize=(10, 5))
    plt.suptitle(t=main_title, y=0.8, fontsize=14)
    titles = ['Top-left', 'Top-right', 'bottom-left',
              'bottom-right', 'center']
    for i, fcim in zip(range(1, 6), fcims):
        plt.subplot(1, 5, i)
        plt.title(label=titles[i-1], fontsize=14)
        plt.imshow(X=fcim)
    plt.tight_layout()
    plt.show()

plt.figure(figsize=(7, 9))
plt.title(label="Origin_data", fontsize=14)
plt.imshow(X=origin_data[0][0])
show_images1(fcims=p500p394origin_data[0][0], main_title="p500p394origin_data")
show_images1(fcims=p300_data[0][0], main_title="p300_data")
show_images1(fcims=p200_data[0][0], main_title="p200_data")
show_images1(fcims=p100_data[0][0], main_title="p100_data")
show_images1(fcims=p50_data[0][0], main_title="p50_data")
show_images1(fcims=p10_data[0][0], main_title="p10_data")
show_images1(fcims=p200p300_data[0][0], main_title="p200p300_data")
show_images1(fcims=p300p200_data[0][0], main_title="p300p200_data")

# ↓ ↓ ↓ ↓ ↓ ↓ The code below is identical to the code above. ↓ ↓ ↓ ↓ ↓ ↓
def show_images2(im, main_title=None, s=None):
    plt.figure(figsize=(10, 5))
    plt.suptitle(t=main_title, y=0.8, fontsize=14)
    titles = ['Top-left', 'Top-right', 'bottom-left',
              'bottom-right', 'center']
    if not s:
        s = [im.size[1], im.size[0]] 
    fc = FiveCrop(size=s) # Here
    for i, fcim in zip(range(1, 6), fc(im)):
        plt.subplot(1, 5, i)
        plt.title(label=titles[i-1], fontsize=14)
        plt.imshow(X=fcim) # Here
    plt.tight_layout()
    plt.show()

plt.figure(figsize=(7, 9))
plt.title(label="Origin_data", fontsize=14)
plt.imshow(X=origin_data[0][0])
show_images2(im=origin_data[0][0], main_title="p500p394origin_data")
# show_images2(im=origin_data[0][0], main_title="p500p394origin_data",
#              s=[500, 394])
show_images2(im=origin_data[0][0], main_title="p300_data", s=300)
show_images2(im=origin_data[0][0], main_title="p200_data", s=200)
show_images2(im=origin_data[0][0], main_title="p100_data", s=100)
show_images2(im=origin_data[0][0], main_title="p50_data", s=50)
show_images2(im=origin_data[0][0], main_title="p10_data", s=10)
show_images2(im=origin_data[0][0], main_title="p200p300_data", s=[200, 300])
show_images2(im=origin_data[0][0], main_title="p300p200_data", s=[300, 200])

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 FiveCrop in PyTorch. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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