This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)
*Memos:
- My post explains OxfordIIITPet().
Pad() can add padding to an image as shown below:
*Memos:
- The 1st argument for initialization is
padding
(Required-Type:int
ortuple
/list
(int
)). *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
ortuple
/list
(int
orfloat
)): *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
ortensor
(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')
This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)
Print
Share
Comment
Cite
Upload
Translate
Updates
There are no updates yet.
Click the Upload button above to add an update.
APA
MLA
Super Kai (Kazuya Ito) | Sciencx (2025-01-26T05:07:39+00:00) Pad in PyTorch. Retrieved from https://www.scien.cx/2025/01/26/pad-in-pytorch-3/
" » Pad in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Sunday January 26, 2025, https://www.scien.cx/2025/01/26/pad-in-pytorch-3/
HARVARDSuper Kai (Kazuya Ito) | Sciencx Sunday January 26, 2025 » Pad in PyTorch., viewed ,<https://www.scien.cx/2025/01/26/pad-in-pytorch-3/>
VANCOUVERSuper Kai (Kazuya Ito) | Sciencx - » Pad in PyTorch. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/01/26/pad-in-pytorch-3/
CHICAGO" » Pad in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Accessed . https://www.scien.cx/2025/01/26/pad-in-pytorch-3/
IEEE" » Pad in PyTorch." Super Kai (Kazuya Ito) | Sciencx [Online]. Available: https://www.scien.cx/2025/01/26/pad-in-pytorch-3/. [Accessed: ]
rf:citation » Pad in PyTorch | Super Kai (Kazuya Ito) | Sciencx | https://www.scien.cx/2025/01/26/pad-in-pytorch-3/ |
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.