DEV Community

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

Posted on • Edited on

FiveCrop in PyTorch

Buy Me a Coffee

*Memos:

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

fivecrop = FiveCrop(size=100)

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

fivecrop.size
# (100, 100)

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

s500_394origin_data = OxfordIIITPet( # `s` is size.
    root="data",
    transform=FiveCrop(size=[500, 394])
)

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

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

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

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

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

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

s300_200_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="s500_394origin_data", fontsize=14)
plt.imshow(X=origin_data[0][0])
show_images1(fcims=s500_394origin_data[0][0], main_title="s500_394origin_data")
show_images1(fcims=s300_data[0][0], main_title="s300_data")
show_images1(fcims=s200_data[0][0], main_title="s200_data")
show_images1(fcims=s100_data[0][0], main_title="s100_data")
show_images1(fcims=s50_data[0][0], main_title="s50_data")
show_images1(fcims=s10_data[0][0], main_title="s10_data")
show_images1(fcims=s200_300_data[0][0], main_title="s200_300_data")
show_images1(fcims=s300_200_data[0][0], main_title="s300_200_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="s500_394origin_data", fontsize=14)
plt.imshow(X=origin_data[0][0])
show_images2(im=origin_data[0][0], main_title="s500_394origin_data")
# show_images2(im=origin_data[0][0], main_title="s500_394origin_data",
#              s=[500, 394])
show_images2(im=origin_data[0][0], main_title="s300_data", s=300)
show_images2(im=origin_data[0][0], main_title="s200_data", s=200)
show_images2(im=origin_data[0][0], main_title="s100_data", s=100)
show_images2(im=origin_data[0][0], main_title="s50_data", s=50)
show_images2(im=origin_data[0][0], main_title="s10_data", s=10)
show_images2(im=origin_data[0][0], main_title="s200_300_data", s=[200, 300])
show_images2(im=origin_data[0][0], 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

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

Imagine monitoring that's actually built for developers

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

Start Monitoring

👋 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