This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)
*Memos:
-
My post explains RandomCrop() about
padding
,fill
andpadding_mode
argument. -
My post explains RandomCrop() about
pad_if_needed
argument. - 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])
)
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")
# ↓ ↓ ↓ ↓ ↓ ↓ 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])
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-02-08T23:25:17+00:00) RandomCrop in PyTorch (1). Retrieved from https://www.scien.cx/2025/02/08/randomcrop-in-pytorch-1-2/
" » RandomCrop in PyTorch (1)." Super Kai (Kazuya Ito) | Sciencx - Saturday February 8, 2025, https://www.scien.cx/2025/02/08/randomcrop-in-pytorch-1-2/
HARVARDSuper Kai (Kazuya Ito) | Sciencx Saturday February 8, 2025 » RandomCrop in PyTorch (1)., viewed ,<https://www.scien.cx/2025/02/08/randomcrop-in-pytorch-1-2/>
VANCOUVERSuper Kai (Kazuya Ito) | Sciencx - » RandomCrop in PyTorch (1). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/02/08/randomcrop-in-pytorch-1-2/
CHICAGO" » RandomCrop in PyTorch (1)." Super Kai (Kazuya Ito) | Sciencx - Accessed . https://www.scien.cx/2025/02/08/randomcrop-in-pytorch-1-2/
IEEE" » RandomCrop in PyTorch (1)." Super Kai (Kazuya Ito) | Sciencx [Online]. Available: https://www.scien.cx/2025/02/08/randomcrop-in-pytorch-1-2/. [Accessed: ]
rf:citation » RandomCrop in PyTorch (1) | Super Kai (Kazuya Ito) | Sciencx | https://www.scien.cx/2025/02/08/randomcrop-in-pytorch-1-2/ |
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.