Pad in PyTorch

Buy Me a Coffee☕ *Memos: My post explains OxfordIIITPet(). Pad() can add padding to zero or more images as shown below: *Memos: The 1st argument for initialization is padding(Required-Type:int or tuple/list(int)): *Memos: It can do add padding. A tuple/list must be the 1D with 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 images. *The background can be seen when adding padding for images. A tuple/list must be the 1D with 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. There is the 1st argument(Required-Type:PIL Image or tensor(int)). *It must be a 3D or more D tensor. 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( 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( 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]) ) p100fillgray_data = OxfordIIITPet( root="data", transform=Pad(padding=100, fill=150) ) p100fillpurple_data = OxfordIIITPet( root="data", transform=Pad(padding=100, fill=[160, 32, 240]) ) p100edge_data = OxfordIIITPet( root="data", transform=Pad(padding=100, padding_mode="edge") ) p100reflect_data = OxfordIIITPet( root="data", transform=Pad(padding=100, padding_mode="reflect") ) p100symmetric_data = OxfordIIITPet( 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.xticks(ticks=[]) plt.yticks(ticks=[]) 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=p100fillgray_data, main_title='p100fillgray_data') show_images1(data=p100fillpurple_data, main_title='p100fillpurple_data') print() show_images1(data=p100edge_data, main_title='p100edge_data') show_images1(data=p100reflect_data, main_title='p100reflect_data') show_images1(data=p100symmetric_data, main_title='p100symmetric_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.xticks(ticks=[]) plt.yticks(ticks=[])

Jan 18, 2025 - 00:53
Pad in PyTorch

Buy Me a Coffee

*Memos:

Pad() can add padding to zero or more images as shown below:

*Memos:

  • The 1st argument for initialization is padding(Required-Type:int or tuple/list(int)): *Memos:
    • It can do add padding.
    • A tuple/list must be the 1D with 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 images. *The background can be seen when adding padding for images.
    • A tuple/list must be the 1D with 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.
  • There is the 1st argument(Required-Type:PIL Image or tensor(int)). *It must be a 3D or more D tensor.
  • 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(
    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(
    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])
)

p100fillgray_data = OxfordIIITPet(
    root="data",
    transform=Pad(padding=100, fill=150)
)

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

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

p100reflect_data = OxfordIIITPet(
    root="data",
    transform=Pad(padding=100, padding_mode="reflect")
)

p100symmetric_data = OxfordIIITPet(
    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.xticks(ticks=[])
        plt.yticks(ticks=[])
    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=p100fillgray_data, main_title='p100fillgray_data')
show_images1(data=p100fillpurple_data, main_title='p100fillpurple_data')
print()
show_images1(data=p100edge_data, main_title='p100edge_data')
show_images1(data=p100reflect_data, main_title='p100reflect_data')
show_images1(data=p100symmetric_data, main_title='p100symmetric_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.xticks(ticks=[])
        plt.yticks(ticks=[])
    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='p100fillgray_data', p=100,
             f=[150])
show_images2(data=origin_data, main_title='p100fillpurple_data', p=100,
             f=[160, 32, 240])
print()
show_images2(data=origin_data, main_title='p100edge_data', p=100, 
             pm='edge')
show_images2(data=origin_data, main_title='p100reflect_data', p=100,
             pm='reflect')
show_images2(data=origin_data, main_title='p100symmetric_data', p=100,
             pm='symmetric')

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