DEV Community

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

Posted on • Edited on

Pad in PyTorch

Buy Me a Coffee

*Memos:

Pad() can add padding to an image as shown below:

*Memos:

  • The 1st argument for initialization is padding(Required-Type:int or tuple/list(int)). *A tuple/list must be the 1D with 1, 2 or 4 elements.
  • The 2nd argument for initialization is fill(Optional-Default:0-Type:int, float or tuple/list(int or float)): *Memos:
    • It can change the background of an image. *The background can be seen when adding padding for an image.
    • A tuple/list must be the 1D with 1 or 3 elements.
  • The 3rd argument for initialization is padding_mode(Optional-Default:'constant'-Type:str). *'constant', 'edge', 'reflect' or 'symmetric' can be set to it.
  • 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 Pad

pad = Pad(padding=100)
pad = Pad(padding=100, fill=0, padding_mode='constant')

pad
# Pad(padding=100, fill=0, padding_mode=constant)

pad.padding
# 100

pad.fill
# 0

pad.padding_mode
# 'constant'

origin_data = OxfordIIITPet(
    root="data",
    transform=None
    # transform=Pad(padding=0)
)

p50_data = OxfordIIITPet( # `p` is plus.
    root="data",
    transform=Pad(padding=50)
)

p100_data = OxfordIIITPet(
    root="data",
    transform=Pad(padding=100)
)

p150_data = OxfordIIITPet(
    root="data",
    transform=Pad(padding=150)
)

m50_data = OxfordIIITPet( # `m` is minus.
    root="data",
    transform=Pad(padding=-50)
)

m100_data = OxfordIIITPet(
    root="data",
    transform=Pad(padding=-100)
)

m150_data = OxfordIIITPet(
    root="data",
    transform=Pad(padding=-150)
)

p100p50_data = OxfordIIITPet(
    root="data",
    transform=Pad(padding=[100, 50])
)

m100m50_data = OxfordIIITPet(
    root="data",
    transform=Pad(padding=[-100, -50])
)

p100m50_data = OxfordIIITPet(
    root="data",
    transform=Pad(padding=[100, -50])
)

p25p50p75p100_data = OxfordIIITPet(
    root="data",
    transform=Pad(padding=[25, 50, 75, 100])
)

m25m50m75m100_data = OxfordIIITPet(
    root="data",
    transform=Pad(padding=[-25, -50, -75, -100])
)

p25m50p75m100_data = OxfordIIITPet(
    root="data",
    transform=Pad(padding=[25, -50, 75, -100])
)

p100fgray_data = OxfordIIITPet( # `f` is fill.
    root="data",
    transform=Pad(padding=100, fill=150)
)

p100fpurple_data = OxfordIIITPet(
    root="data",
    transform=Pad(padding=100, fill=[160, 32, 240])
)

p100pmconst_data = OxfordIIITPet( # `pm` is padding_mode.
    root="data",                  # `const` is constant.
    transform=Pad(padding=100, padding_mode="constant")
)

p100pmedge_data = OxfordIIITPet(
    root="data",
    transform=Pad(padding=100, padding_mode="edge")
)

p100pmrefle_data = OxfordIIITPet( # `refle` is reflect.
    root="data",
    transform=Pad(padding=100, padding_mode="reflect")
)

p100pmsymme_data = OxfordIIITPet( # `symme` is symmetric.
    root="data",
    transform=Pad(padding=100, padding_mode="symmetric")
)

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=p50_data, main_title='p50_data')
show_images1(data=p100_data, main_title='p100_data')
show_images1(data=p150_data, main_title='p150_data')
print()
show_images1(data=origin_data, main_title='origin_data')
show_images1(data=m50_data, main_title='m50_data')
show_images1(data=m100_data, main_title='m100_data')
show_images1(data=m150_data, main_title='m150_data')
print()
show_images1(data=origin_data, main_title='origin_data')
show_images1(data=p100p50_data, main_title='p100p50_data')
show_images1(data=m100m50_data, main_title='m100m50_data')
show_images1(data=p100m50_data, main_title='p100m50_data')
print()
show_images1(data=origin_data, main_title='origin_data')
show_images1(data=p25p50p75p100_data, main_title='p25p50p75p100_data')
show_images1(data=m25m50m75m100_data, main_title='m25m50m75m100_data')
show_images1(data=p25m50p75m100_data, main_title='p25m50p75m100_data')
print()
show_images1(data=p100fgray_data, main_title='p100fgray_data')
show_images1(data=p100fpurple_data, main_title='p100fpurple_data')
print()
show_images1(data=p100pmconst_data, main_title='p100pmconst_data')
show_images1(data=p100pmedge_data, main_title='p100pmedge_data')
show_images1(data=p100pmrefle_data, main_title='p100pmrefle_data')
show_images1(data=p100pmsymme_data, main_title='p100pmsymme_data')

# ↓ ↓ ↓ ↓ ↓ ↓ The code below is identical to the code above. ↓ ↓ ↓ ↓ ↓ ↓
def show_images2(data, main_title=None, p=0, f=0, pm='constant'):
    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)
        pad = Pad(padding=p, fill=f, padding_mode=pm) # Here
        plt.imshow(X=pad(im)) # Here
    plt.tight_layout()
    plt.show()

show_images2(data=origin_data, main_title='origin_data')
show_images2(data=origin_data, main_title='p50_data', p=50)
show_images2(data=origin_data, main_title='p100_data', p=100)
show_images2(data=origin_data, main_title='p150_data', p=150)
print()
show_images2(data=origin_data, main_title='origin_data')
show_images2(data=origin_data, main_title='m50_data', p=-50)
show_images2(data=origin_data, main_title='m100_data', p=-100)
show_images2(data=origin_data, main_title='m150_data', p=-150)
print()
show_images2(data=origin_data, main_title='origin_data')
show_images2(data=origin_data, main_title='p100p50_data', p=[100, 50])
show_images2(data=origin_data, main_title='m100m50_data', p=[-100, -50])
show_images2(data=origin_data, main_title='p100m50_data', p=[100, -50])
print()
show_images2(data=origin_data, main_title='origin_data')
show_images2(data=origin_data, main_title='p25p50p75p100_data',
             p=[25, 50, 75, 100])
show_images2(data=origin_data, main_title='m25m50m75m100_data',
             p=[-25, -50, -75, -100])
show_images2(data=origin_data, main_title='p25m50p75m100_data',
             p=[25, -50, 75, -100])
print()
show_images2(data=origin_data, main_title='p100fgray_data', p=100,
             f=150)
show_images2(data=origin_data, main_title='p100fpurple_data', p=100,
             f=[160, 32, 240])
print()
show_images2(data=origin_data, main_title='p100pmconst_data', p=100, 
             pm='constant')
show_images2(data=origin_data, main_title='p100pmedge_data', p=100, 
             pm='edge')
show_images2(data=origin_data, main_title='p100pmrefle_data', p=100,
             pm='reflect')
show_images2(data=origin_data, main_title='p100pmsymme_data', p=100,
             pm='symmetric')
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

Image description

Image description


Image description

Image description

Image description

Image description


Image description

Image description


Image description

Image description

Image description

Image description

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Get started

Top comments (0)

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

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay