*Memos:
- My post explains OxfordIIITPet().
RandomCrop() can crop an image randomly as shown below:
*Memos:
- The 1st argument for initialization is
size
(Required-Type:int
ortuple/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
ortuple/list
(int
)) means[size, size]
.
- It's
- The 2nd argument for initialization is
padding
(Optional-Default:None
-Type:int
ortuple
/list
(int
)): *Memos:- It's
[left, top, right, bottom]
which can be converted from[left-right, top-bottom]
or[left-top-right-bottom]
. - A tuple/list must be the 1D with 1, 2 or 4 elements.
- A single value(
int
ortuple/list
(int
)) means[padding, padding, padding, padding]
. - Double values(
tuple/list
(int
)) means[padding[0], padding[1], padding[0], padding[1]]
.
- It's
- The 3rd argument for initialization is
pad_if_needed
(Optional-Default:False
-Type:bool
):- If it's
False
andsize
is smaller than an original image or the padded image bypadding
, there is error. - If it's
True
andsize
is smaller than an original image or the padded image bypadding
, there is no error, then the image is randomly padded to becomesize
.
- If it's
- The 4th 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 an image is positively padded.
- A tuple/list must be the 1D with 1 or 3 elements.
- The 5th 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 RandomCrop
randomcrop = RandomCrop(size=100)
randomcrop = RandomCrop(size=100,
padding=None,
pad_if_needed=False,
fill=0,
padding_mode='constant')
randomcrop
# RandomCrop(size=(100, 100),
# pad_if_needed=False,
# fill=0,
# padding_mode=constant)
randomcrop.size
# (100, 100)
print(randomcrop.padding)
# None
randomcrop.pad_if_needed
# False
randomcrop.fill
# 0
randomcrop.padding_mode
# 'constant'
origin_data = OxfordIIITPet(
root="data",
transform=None
)
s300_data = OxfordIIITPet( # `s` is size.
root="data",
transform=RandomCrop(size=300)
# transform=RandomCrop(size=[300, 300])
)
s200_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=200)
)
s100_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=100)
)
s50_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=50)
)
s10_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=10)
)
s1_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=1)
)
s200_300_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[200, 300])
)
s300_200_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[300, 200])
)
s300p100_data = OxfordIIITPet( # `p` is padding.
root="data",
transform=RandomCrop(size=300, padding=100)
# transform=RandomCrop(size=300, padding=[100, 100])
# transform=RandomCrop(size=300, padding=[100, 100, 100, 100])
)
s300p200_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=300, padding=200)
)
s700_594p100origin_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[700, 594], padding=100)
)
s300p100_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=300, padding=100)
)
s600_594p100_50origin_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[600, 594], padding=[100, 50])
)
s300p100_50_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=300, padding=[100, 50])
)
s650_494p25_50_75_100origin_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[650, 494], padding=[25, 50, 75, 100])
)
s300p25_50_75_100_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=300, padding=[25, 50, 75, 100])
)
s300_194pn100origin_data = OxfordIIITPet( # `n` is negative.
root="data",
transform=RandomCrop(size=[300, 194], padding=-100)
)
s150pn100_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=150, padding=-100)
)
s300_294pn50n100origin_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[300, 294], padding=[-50, -100])
)
s150pn50n100_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=150, padding=[-50, -100])
)
s350_294pn25n50n75n100origin_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[350, 294], padding=[-25, -50, -75, -100])
)
s150pn25n50n75n100_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=150, padding=[-25, -50, -75, -100])
)
s600_444p25_50origin_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[600, 444], padding=[25, 50])
)
s200p25_50_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=200, padding=[25, 50])
)
s400_344pn25n50origin_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[400, 344], padding=[-25, -50])
)
s200pn25n50_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=200, padding=[-25, -50])
)
s400_444p25n50origin_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[400, 444], padding=[25, -50])
)
s200p25n50_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=200, padding=[25, -50])
)
s600_344pn25_50origin_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[600, 344], padding=[-25, 50])
)
s200pn25_50_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=200, padding=[-25, 50])
)
s700_594p100fgrayorigin_data = OxfordIIITPet( # `f` is fill.
root="data",
transform=RandomCrop(size=[700, 594], padding=100, fill=150)
# transform=RandomCrop(size=[700, 594], padding=100, fill=[150])
)
s300p100fgray_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=300, padding=100, fill=150)
)
s700_594p100fpurpleorigin_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[700, 594], padding=100, fill=[160, 32, 240])
)
s300p100fpurple_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=300, padding=100, fill=[160, 32, 240])
)
s700_594p100pmconstorigin_data = OxfordIIITPet( # `pm` is padding_mode.
root="data", # `const` is constant.
transform=RandomCrop(size=[700, 594], padding=100, padding_mode='constant')
)
s300p100pmconst_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=300, padding=100, padding_mode='constant')
)
s700_594p100pmedgeorigin_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[700, 594], padding=100, padding_mode='edge')
)
s300p100pmedge_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=300, padding=100, padding_mode='edge')
)
s700_594p100pmrefleorigin_data = OxfordIIITPet( # `refle` is reflect.
root="data",
transform=RandomCrop(size=[700, 594], padding=100, padding_mode='reflect')
)
s300p100pmrefle_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=300, padding=100, padding_mode='reflect')
)
s700_594p100pmsymmeorigin_data = OxfordIIITPet( # `symme` is symmetric.
root="data",
transform=RandomCrop(size=[700, 594], padding=100,
padding_mode='symmetric')
)
s300p100pmsymme_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=300, 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 in range(1, 6):
plt.subplot(1, 5, i)
plt.imshow(X=data[0][0])
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(data=origin_data, main_title="s500_394origin_data")
show_images1(data=s300_data, main_title="s300_data")
show_images1(data=s200_data, main_title="s200_data")
show_images1(data=s100_data, main_title="s100_data")
show_images1(data=s50_data, main_title="s50_data")
show_images1(data=s10_data, main_title="s10_data")
show_images1(data=s1_data, main_title="s1_data")
show_images1(data=s200_300_data, main_title="s200_300_data")
show_images1(data=s300_200_data, main_title="s300_200_data")
print()
show_images1(data=s700_594p100origin_data,
main_title="s700_594p100origin_data")
show_images1(data=s300p100_data, main_title="s300p100_data")
print()
show_images1(data=s600_594p100_50origin_data,
main_title="s600_594p100_50origin_data")
show_images1(data=s300p100_50_data, main_title="s300p100_50_data")
print()
show_images1(data=s650_494p25_50_75_100origin_data,
main_title="s650_494p25_50_75_100origin_data")
show_images1(data=s300p25_50_75_100_data,
main_title="s300p25_50_75_100_data")
print()
show_images1(data=s300_194pn100origin_data,
main_title="s300_194pn100origin_data")
show_images1(data=s150pn100_data,
main_title="s150pn100_data")
print()
show_images1(data=s300_294pn50n100origin_data,
main_title="s300_294pn50n100origin_data")
show_images1(data=s150pn50n100_data,
main_title="s150pn50n100_data")
print()
show_images1(data=s350_294pn25n50n75n100origin_data,
main_title="s350_294pn25n50n75n100origin_data")
show_images1(data=s150pn25n50n75n100_data,
main_title="s150pn25n50n75n100_data")
print()
show_images1(data=s600_444p25_50origin_data,
main_title="s600_444p25_50origin_data")
show_images1(data=s200p25_50_data,
main_title="s200p25_50_data")
print()
show_images1(data=s400_344pn25n50origin_data,
main_title="s400_344pn25n50origin_data")
show_images1(data=s200pn25n50_data,
main_title="s200pn25n50_data")
print()
show_images1(data=s400_444p25n50origin_data,
main_title="s400_444p25n50origin_data")
show_images1(data=s200p25n50_data,
main_title="s200p25n50_data")
print()
show_images1(data=s600_344pn25_50origin_data,
main_title="s600_344pn25_50origin_data")
show_images1(data=s200pn25_50_data,
main_title="s200pn25_50_data")
print()
show_images1(data=s700_594p100fgrayorigin_data,
main_title="s700_594p100fgrayorigin_data")
show_images1(data=s300p100fgray_data, main_title="s300p100fgray_data")
print()
show_images1(data=s700_594p100fpurpleorigin_data,
main_title="s700_594p100fpurpleorigin_data")
show_images1(data=s300p100fpurple_data, main_title="s300p100fpurple_data")
print()
show_images1(data=s700_594p100pmconstorigin_data,
main_title="s700_594p100pmconstorigin_data")
show_images1(data=s300p100pmconst_data, main_title="s300p100pmconst_data")
print()
show_images1(data=s700_594p100pmedgeorigin_data,
main_title="s700_594p100pmedgeorigin_data")
show_images1(data=s300p100pmedge_data, main_title="s300p100pmedge_data")
print()
show_images1(data=s700_594p100pmrefleorigin_data,
main_title="s700_594p100pmrefleorigin_data")
show_images1(data=s300p100pmrefle_data, main_title="s300p100pmrefle_data")
print()
show_images1(data=s700_594p100pmsymmeorigin_data,
main_title="s700_594p100pmsymmeorigin_data")
show_images1(data=s300p100pmsymme_data, main_title="s300p100pmsymme_data")
# ↓ ↓ ↓ ↓ ↓ ↓ The code below is identical to the code above. ↓ ↓ ↓ ↓ ↓ ↓
def show_images2(data, main_title=None, s=None, p=None,
pin=False, f=0, pm='constant'):
plt.figure(figsize=(10, 5))
plt.suptitle(t=main_title, y=0.8, fontsize=14)
temp_s = s
im = data[0][0]
for i in range(1, 6):
plt.subplot(1, 5, i)
if not temp_s:
s = [im.size[1], im.size[0]]
rc = RandomCrop(size=s, padding=p, # Here
pad_if_needed=pin, fill=f, padding_mode=pm)
plt.imshow(X=rc(im)) # 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(data=origin_data, main_title="s500_394origin_data")
show_images2(data=origin_data, main_title="s300_data", s=300)
show_images2(data=origin_data, main_title="s200_data", s=200)
show_images2(data=origin_data, main_title="s100_data", s=100)
show_images2(data=origin_data, main_title="s50_data", s=50)
show_images2(data=origin_data, main_title="s10_data", s=10)
show_images2(data=origin_data, main_title="s1_data", s=1)
show_images2(data=origin_data, main_title="s200_300_data", s=[200, 300])
show_images2(data=origin_data, main_title="s300_200_data", s=[300, 200])
print()
show_images2(data=origin_data, main_title="s700_594p100origin_data",
s=[700, 594], p=100)
show_images2(data=origin_data, main_title="s300p100_data", s=300, p=100)
print()
show_images2(data=origin_data, main_title="s600_594p100_50origin_data",
s=[600, 594], p=[100, 50])
show_images2(data=origin_data, main_title="s300p100_50_data", s=300,
p=[100, 50])
print()
show_images2(data=origin_data, main_title="s650_494p25_50_75_100origin_data",
s=[650, 494], p=[25, 50, 75, 100])
show_images2(data=origin_data, main_title="s300p25_50_75_100_data", s=300,
p=[25, 50, 75, 100])
print()
show_images2(data=origin_data, main_title="s300_194pn100origin_data",
s=[300, 194], p=-100)
show_images2(data=origin_data, main_title="s150pn100_data", s=150, p=-100)
print()
show_images2(data=origin_data, main_title="s300_294pn50n100origin_data",
s=[300, 294], p=[-50, -100])
show_images2(data=origin_data, main_title="s150pn50n100_data", s=150,
p=[-50, -100])
print()
show_images2(data=origin_data, main_title="s350_294pn25n50n75n100origin_data",
s=[350, 294], p=[-25, -50, -75, -100])
show_images2(data=origin_data, main_title="s150pn25n50n75n100_data", s=150,
p=[-25, -50, -75, -100])
print()
show_images2(data=origin_data, main_title="s600_444p25_50origin_data",
s=[600, 444], p=[25, 50])
show_images2(data=origin_data, main_title="s200p25_50_data", s=200,
p=[25, 50])
print()
show_images2(data=origin_data, main_title="s400_344pn25n50origin_data",
s=[400, 344], p=[-25, -50])
show_images2(data=origin_data, main_title="s200pn25n50_data", s=200,
p=[-25, -50])
print()
show_images2(data=origin_data, main_title="s400_444p25n50origin_data",
s=[400, 444], p=[25, -50])
show_images2(data=origin_data, main_title="s200p25n50_data", s=200,
p=[25, -50])
print()
show_images2(data=origin_data, main_title="s600_344pn25_50origin_data",
s=[600, 344], p=[-25, 50])
show_images2(data=origin_data, main_title="s200pn25_50_data", s=200,
p=[-25, 50])
print()
show_images2(data=origin_data, main_title="s700_594p100fgrayorigin_data",
s=[700, 594], p=100, f=150)
show_images2(data=origin_data, main_title="s300p100fgray_data", s=300,
p=100, f=150)
print()
show_images2(data=origin_data, main_title="s700_594p100fpurpleorigin_data",
s=[700, 594], p=100, f=[160, 32, 240])
show_images2(data=origin_data, main_title="s300p100fpurple_data", s=300,
p=100, f=[160, 32, 240])
print()
show_images2(data=origin_data, main_title="s700_594p100pmconstorigin_data",
s=[700, 594], p=100, pm='constant')
show_images2(data=origin_data, main_title="s300p100pmconst_data", s=300,
p=100, pm='constant')
print()
show_images2(data=origin_data, main_title="s700_594p100pmedgeorigin_data",
s=[700, 594], p=100, pm='edge')
show_images2(data=origin_data, main_title="s300p100pmedge_data", s=300,
p=100, pm='edge')
print()
show_images2(data=origin_data, main_title="s700_594p100pmrefleorigin_data",
s=[700, 594], p=100, pm='reflect')
show_images2(data=origin_data, main_title="s300p100pmrefle_data", s=300,
p=100, pm='reflect')
print()
show_images2(data=origin_data, main_title="s700_594p100pmsymmeorigin_data",
s=[700, 594], p=100, pm='symmetric')
show_images2(data=origin_data, main_title="s300p100pmsymme_data", s=300,
p=100, pm='symmetric')
Top comments (0)