*Memos:
- My post explains RandomAffine() about
degrees
,translate
,fill
andcenter
argument. - My post explains RandomPerspective().
- My post explains OxfordIIITPet().
RandomRotation() can randomly rotate an image as shown below:
*Memos:
- The 1st argument for initialization is
degrees
(Required-Type:int
,float
ortuple
/list
(int
orfloat
)): *Memos:- It can do rotation.
- It's the range of the degrees
[min, max]
so it must bemin <= max
. - A tuple/list must be the 1D with 2 elements.
- A single value must be
0 <= x
. - A single value means
[-degrees, +degrees]
.
- The 2nd argument for initialization is
interpolation
(Optional-Default:InterpolationMode.NEAREST
-Type:InterpolationMode): *Memos:-
NEAREST
,NEAREST_EXACT
,BILINEAR
andBICUBIC
modes can be used. - My post explains InterpolationMode with and without anti-aliasing.
-
- The 3rd argument for initialization is
expand
(Optional-Default:False
-Type:bool
). - The 4th argument for initialization is
center
(Optional-Default:None
-Type:tuple
/list
(int
orfloat
)): *Memos:- It can change the center position of an image.
- It must be the 1D with 2 elements.
- If at least one value is
x <= 0
, an image isn't shown, only showing the background of the image.
- The 5th 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 rotating an image.
- A tuple/list must be the 1D with 1 or 3 elements.
- If all values are
x <= 0
, it's black. - If all values are
255 <= x
, it's white.
- The 1st argument is
img
(Required-Type:PIL Image
ortensor
(int
/float
/complex
/bool
)): *Memos:- A tensor must be 3D or more D.
- 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 RandomRotation from torchvision.transforms.functional import InterpolationMode rr = RandomRotation(degrees=90) rr = RandomRotation(degrees=[-90, 90], interpolation=InterpolationMode.NEAREST, expand=False, center=None, fill=0) rr # RandomRotation(degrees=[-90.0, 90.0], # interpolation=InterpolationMode.NEAREST, # expand=False, # fill=0) rr.degrees # [-90.0, 90.0] rr.interpolation # <InterpolationMode.NEAREST: 'nearest'> rr.expand # False print(rr.center) # None rr.fill # 0 origin_data = OxfordIIITPet( root="data", transform=None ) d0origin_data = OxfordIIITPet( # `d` is degrees. root="data", transform=RandomRotation(degrees=0) # transform=RandomRotation(degrees=[0, 0]) ) d180_data = OxfordIIITPet( root="data", transform=RandomRotation(degrees=180) # transform=RandomRotation(degrees=[-180, 180]) # transform=RandomRotation(degrees=[-360, 0]) # transform=RandomRotation(degrees=[0, 360]) ) dn180_0_data = OxfordIIITPet( # `n` is negative. root="data", transform=RandomRotation(degrees=[-180, 0]) # transform=RandomRotation(degrees=[180, 360]) ) d0_180_data = OxfordIIITPet( root="data", transform=RandomRotation(degrees=[0, 180]) # transform=RandomRotation(degrees=[-360, -180]) ) d15_15_data = OxfordIIITPet( root="data", transform=RandomRotation(degrees=[15, 15]) # transform=RandomRotation(degrees=[-345, -345]) ) d30_30_data = OxfordIIITPet( root="data", transform=RandomRotation(degrees=[30, 30]) # transform=RandomRotation(degrees=[-330, -330]) ) d45_45_data = OxfordIIITPet( root="data", transform=RandomRotation(degrees=[45, 45]) # transform=RandomRotation(degrees=[-315, -315]) ) d60_60_data = OxfordIIITPet( root="data", transform=RandomRotation(degrees=[60, 60]) # transform=RandomRotation(degrees=[-300, -300]) ) d75_75_data = OxfordIIITPet( root="data", transform=RandomRotation(degrees=[75, 75]) # transform=RandomRotation(degrees=[-285, -285]) ) d90_90_data = OxfordIIITPet( root="data", transform=RandomRotation(degrees=[90, 90]) # transform=RandomRotation(degrees=[-270, -270]) ) d105_105_data = OxfordIIITPet( root="data", transform=RandomRotation(degrees=[105, 105]) # transform=RandomRotation(degrees=[-255, -255]) ) d120_120_data = OxfordIIITPet( root="data", transform=RandomRotation(degrees=[120, 120]) # transform=RandomRotation(degrees=[-240, -240]) ) d135_135_data = OxfordIIITPet( root="data", transform=RandomRotation(degrees=[135, 135]) # transform=RandomRotation(degrees=[-225, -225]) ) d150_150_data = OxfordIIITPet( root="data", transform=RandomRotation(degrees=[150, 150]) # transform=RandomRotation(degrees=[-210, -210]) ) d165_165_data = OxfordIIITPet( root="data", transform=RandomRotation(degrees=[165, 165]) # transform=RandomRotation(degrees=[-195, -195]) ) d180_180_data = OxfordIIITPet( root="data", transform=RandomRotation(degrees=[180, 180]) # transform=RandomRotation(degrees=[-180, -180]) ) dn15n15_data = OxfordIIITPet( root="data", transform=RandomRotation(degrees=[-15, -15]) # transform=RandomRotation(degrees=[345, 345]) ) dn30n30_data = OxfordIIITPet( root="data", transform=RandomRotation(degrees=[-30, -30]) # transform=RandomRotation(degrees=[330, 330]) ) dn45n45_data = OxfordIIITPet( root="data", transform=RandomRotation(degrees=[-45, -45]) # transform=RandomRotation(degrees=[315, 315]) ) dn60n60_data = OxfordIIITPet( root="data", transform=RandomRotation(degrees=[-60, -60]) # transform=RandomRotation(degrees=[300, 300]) ) dn75n75_data = OxfordIIITPet( root="data", transform=RandomRotation(degrees=[-75, -75]) # transform=RandomRotation(degrees=[285, 285]) ) dn90n90_data = OxfordIIITPet( root="data", transform=RandomRotation(degrees=[-90, -90]) # transform=RandomRotation(degrees=[270, 270]) ) dn105n105_data = OxfordIIITPet( root="data", transform=RandomRotation(degrees=[-105, -105]) # transform=RandomRotation(degrees=[255, 255]) ) dn120n120_data = OxfordIIITPet( root="data", transform=RandomRotation(degrees=[-120, -120]) # transform=RandomRotation(degrees=[240, 240]) ) dn135n135_data = OxfordIIITPet( root="data", transform=RandomRotation(degrees=[-135, -135]) # transform=RandomRotation(degrees=[225, 225]) ) dn150n150_data = OxfordIIITPet( root="data", transform=RandomRotation(degrees=[-150, -150]) # transform=RandomRotation(degrees=[210, 210]) ) dn165n165_data = OxfordIIITPet( root="data", transform=RandomRotation(degrees=[-165, -165]) # transform=RandomRotation(degrees=[195, 195]) ) dn180n180_data = OxfordIIITPet( root="data", transform=RandomRotation(degrees=[-180, -180]) # transform=RandomRotation(degrees=[180, 180]) ) dn90n90expand_data = OxfordIIITPet( root="data", transform=RandomRotation(degrees=[-90, -90], expand=True) ) d180_180c270_200_data = OxfordIIITPet( # `c` is center. root="data", transform=RandomRotation(degrees=[180, 180], center=[270, 200]) ) dn45n45fgray_data = OxfordIIITPet( # `f` is fill. root="data", transform=RandomRotation(degrees=[-45, -45], fill=150) # transform=RandomRotation(degrees=[-45, -45], fill=[150]) ) d135_135fpurple_data = OxfordIIITPet( root="data", transform=RandomRotation(degrees=[135, 135], fill=[160, 32, 240]) ) 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") print() show_images1(data=d0origin_data, main_title="d0origin_data") show_images1(data=d180_data, main_title="d180_data") show_images1(data=dn180_0_data, main_title="dn180_0_data") show_images1(data=d0_180_data, main_title="d0_180_data") print() show_images1(data=d0origin_data, main_title="d0origin_data") show_images1(data=d15_15_data, main_title="d15_15_data") show_images1(data=d30_30_data, main_title="d30_30_data") show_images1(data=d45_45_data, main_title="d45_45_data") show_images1(data=d60_60_data, main_title="d60_60_data") show_images1(data=d75_75_data, main_title="d75_75_data") show_images1(data=d90_90_data, main_title="d90_90_data") show_images1(data=d105_105_data, main_title="d105_105_data") show_images1(data=d120_120_data, main_title="d120_120_data") show_images1(data=d135_135_data, main_title="d135_135_data") show_images1(data=d150_150_data, main_title="d150_150_data") show_images1(data=d165_165_data, main_title="d165_165_data") show_images1(data=d180_180_data, main_title="d180_180_data") print() show_images1(data=d0origin_data, main_title="d0origin_data") show_images1(data=dn15n15_data, main_title="dn15n15_data") show_images1(data=dn30n30_data, main_title="dn30n30_data") show_images1(data=dn45n45_data, main_title="dn45n45_data") show_images1(data=dn60n60_data, main_title="dn60n60_data") show_images1(data=dn75n75_data, main_title="dn75n75_data") show_images1(data=dn90n90_data, main_title="dn90n90_data") show_images1(data=dn105n105_data, main_title="dn105n105_data") show_images1(data=dn120n120_data, main_title="dn120n120_data") show_images1(data=dn135n135_data, main_title="dn135n135_data") show_images1(data=dn150n150_data, main_title="dn150n150_data") show_images1(data=dn165n165_data, main_title="dn165n165_data") show_images1(data=dn180n180_data, main_title="dn180n180_data") print() show_images1(data=dn90n90expand_data, main_title="dn90n90expand_data") show_images1(data=d180_180c270_200_data, main_title="d180_180c270_200_data") show_images1(data=dn45n45fgray_data, main_title="dn45n45fgray_data") show_images1(data=d135_135fpurple_data, main_title="d135_135fpurple_data") # ↓ ↓ ↓ ↓ ↓ ↓ The code below is identical to the code above. ↓ ↓ ↓ ↓ ↓ ↓ def show_images2(data, main_title=None, d=None, ip=InterpolationMode.NEAREST, e=False, c=None, f=0): plt.figure(figsize=[10, 5]) plt.suptitle(t=main_title, y=0.8, fontsize=14) if main_title != "origin_data": for i, (im, _) in zip(range(1, 6), data): plt.subplot(1, 5, i) rr = RandomRotation(degrees=d, interpolation=ip, expand=e, center=c, fill=f) plt.imshow(X=rr(im)) plt.xticks(ticks=[]) plt.yticks(ticks=[]) else: 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_images2(data=origin_data, main_title="origin_data") print() show_images2(data=origin_data, main_title="d0origin_data", d=0) show_images2(data=origin_data, main_title="d180_data", d=180) show_images2(data=origin_data, main_title="dn180_0_data", d=[-180, 0]) show_images2(data=origin_data, main_title="d0_180_data", d=[0, 180]) print() show_images2(data=origin_data, main_title="d0origin_data", d=0) show_images2(data=origin_data, main_title="d15_15_data", d=[15, 15]) show_images2(data=origin_data, main_title="d30_30_data", d=[30, 30]) show_images2(data=origin_data, main_title="d45_45_data", d=[45, 45]) show_images2(data=origin_data, main_title="d60_60_data", d=[60, 60]) show_images2(data=origin_data, main_title="d75_75_data", d=[75, 75]) show_images2(data=origin_data, main_title="d90_90_data", d=[90, 90]) show_images2(data=origin_data, main_title="d105_105_data", d=[105, 105]) show_images2(data=origin_data, main_title="d120_120_data", d=[120, 120]) show_images2(data=origin_data, main_title="d135_135_data", d=[135, 135]) show_images2(data=origin_data, main_title="d150_150_data", d=[150, 150]) show_images2(data=origin_data, main_title="d165_165_data", d=[165, 165]) show_images2(data=origin_data, main_title="d180_180_data", d=[180, 180]) print() show_images2(data=origin_data, main_title="d0origin_data", d=0) show_images2(data=origin_data, main_title="dn15n15_data", d=[-15, -15]) show_images2(data=origin_data, main_title="dn30n30_data", d=[-30, -30]) show_images2(data=origin_data, main_title="dn45n45_data", d=[-45, -45]) show_images2(data=origin_data, main_title="dn60n60_data", d=[-60, -60]) show_images2(data=origin_data, main_title="dn75n75_data", d=[-75, -75]) show_images2(data=origin_data, main_title="dn90n90_data", d=[-90, -90]) show_images2(data=origin_data, main_title="dn105n105_data", d=[-105, -105]) show_images2(data=origin_data, main_title="dn120n120_data", d=[-120, -120]) show_images2(data=origin_data, main_title="dn135n135_data", d=[-135, -135]) show_images2(data=origin_data, main_title="dn150n150_data", d=[-150, -150]) show_images2(data=origin_data, main_title="dn165n165_data", d=[-165, -165]) show_images2(data=origin_data, main_title="dn180n180_data", d=[-180, -180]) print() show_images2(data=origin_data, main_title="dn90n90expand_data", d=[-90, -90], e=True) show_images2(data=origin_data, main_title="d180_180c270_200_data", d=[180, 180], c=[270, 200]) show_images2(data=origin_data, main_title="dn45n45fgray_data", d=[-45, -45], f=150) show_images2(data=origin_data, main_title="d135_135fpurple_data", d=[135, 135], f=[160, 32, 240])
Top comments (0)