*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 1, 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 padding.
root="data",
transform=Pad(padding=50)
)
p100_data = OxfordIIITPet(
root="data",
transform=Pad(padding=100)
)
p150_data = OxfordIIITPet(
root="data",
transform=Pad(padding=150)
)
pn50_data = OxfordIIITPet( # `n` is negative.
root="data",
transform=Pad(padding=-50)
)
pn100_data = OxfordIIITPet(
root="data",
transform=Pad(padding=-100)
)
pn150_data = OxfordIIITPet(
root="data",
transform=Pad(padding=-150)
)
p100_50_data = OxfordIIITPet(
root="data",
transform=Pad(padding=[100, 50])
)
pn100n50_data = OxfordIIITPet(
root="data",
transform=Pad(padding=[-100, -50])
)
p100n50_data = OxfordIIITPet(
root="data",
transform=Pad(padding=[100, -50])
)
p25_50_75_100_data = OxfordIIITPet(
root="data",
transform=Pad(padding=[25, 50, 75, 100])
)
pn25n50n75n100_data = OxfordIIITPet(
root="data",
transform=Pad(padding=[-25, -50, -75, -100])
)
p25n50_75n100_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])
)
p100pmconstant_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")
)
p100pmreflect_data = OxfordIIITPet(
root="data",
transform=Pad(padding=100, padding_mode="reflect")
)
p100pmsymmetric_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.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=pn50_data, main_title='pn50_data')
show_images1(data=pn100_data, main_title='pn100_data')
show_images1(data=pn150_data, main_title='pn150_data')
print()
show_images1(data=origin_data, main_title='origin_data')
show_images1(data=p100_50_data, main_title='p100_50_data')
show_images1(data=pn100n50_data, main_title='pn100n50_data')
show_images1(data=p100n50_data, main_title='p100n50_data')
print()
show_images1(data=origin_data, main_title='origin_data')
show_images1(data=p25_50_75_100_data, main_title='p25_50_75_100_data')
show_images1(data=pn25n50n75n100_data, main_title='pn25n50n75n100_data')
show_images1(data=p25n50_75n100_data, main_title='p25n50_75n100_data')
print()
show_images1(data=p100fgray_data, main_title='p100fgray_data')
show_images1(data=p100fpurple_data, main_title='p100fpurple_data')
print()
show_images1(data=p100pmconstant_data, main_title='p100pmconstant_data')
show_images1(data=p100pmedge_data, main_title='p100pmedge_data')
show_images1(data=p100pmreflect_data, main_title='p100pmreflect_data')
show_images1(data=p100pmsymmetric_data, main_title='p100pmsymmetric_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='pn50_data', p=-50)
show_images2(data=origin_data, main_title='pn100_data', p=-100)
show_images2(data=origin_data, main_title='pn150_data', p=-150)
print()
show_images2(data=origin_data, main_title='origin_data')
show_images2(data=origin_data, main_title='p100_50_data', p=[100, 50])
show_images2(data=origin_data, main_title='pn100n50_data', p=[-100, -50])
show_images2(data=origin_data, main_title='p100n50_data', p=[100, -50])
print()
show_images2(data=origin_data, main_title='origin_data')
show_images2(data=origin_data, main_title='p25_50_75_100_data',
p=[25, 50, 75, 100])
show_images2(data=origin_data, main_title='pn25n50n75n100_data',
p=[-25, -50, -75, -100])
show_images2(data=origin_data, main_title='p25n50_75n100_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='p100pmconstant_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='p100pmreflect_data', p=100,
pm='reflect')
show_images2(data=origin_data, main_title='p100pmsymmetric_data', p=100,
pm='symmetric')
Top comments (0)