Home > Backend Development > Python Tutorial > RandomRotation in PyTorch

RandomRotation in PyTorch

Susan Sarandon
Release: 2024-12-29 02:47:11
Original
673 people have browsed it

Buy Me a Coffee☕

*Memos:

  • My post explains RandomHorizontalFlip().
  • My post explains RandomVerticalFlip().
  • My post explains OxfordIIITPet().

RandomRotation() can rotate zero or more images as shown below:

*Memos:

  • The 1st argument for initialization is degrees(Required-Type:int, float or tuple/list(int or float)): *Memos:
    • A single value must be 0 <= x.
    • A tuple or list must be the 1D with 2 elements. *The 1st element must be less than or equal to the 2nd element.
  • The 2nd argument for initialization is interpolation(Optional-Default:InterpolationMode.NEAREST-Type:InterpolationMode).
  • 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 or float)). *It must be the 1D with 2 elements.
  • The 5th argument for initialization is fill(Optional-Default:0-Type:int, float or tuple/list(int or float)): *Memos:
    • A tuple or list must be the 1D with 3 elements.
  • The 1st argument is img(Required-Type:PIL Image or tensor/tuple/list(int or float)): *Memos:
    • It must be 2D or 3D. For 3D, the deepest D must have one element.
    • 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

randomrotation = RandomRotation(degrees=90.0)
randomrotation = RandomRotation(degrees=[-90.0, 90.0], 
                                interpolation=InterpolationMode.NEAREST,
                                expand=False,
                                center=None,
                                fill=0)
randomrotation
# RandomRotation(degrees=[-90.0, 90.0],
#                interpolation=InterpolationMode.NEAREST,
#                expand=False,
#                fill=0)

randomrotation.degrees
# [-90.0, 90.0]

randomrotation.interpolation
# <InterpolationMode.NEAREST: 'nearest'>

randomrotation.expand
# False

print(randomrotation.center)
# None

randomrotation.fill
# 0

origin_data = OxfordIIITPet(
    root="data",
    transform=None
)

p90_data = OxfordIIITPet( # `p` is plus.
    root="data",
    transform=RandomRotation(degrees=90.0)
)

p90p90_data = OxfordIIITPet(
    root="data",
    transform=RandomRotation(degrees=(90.0, 90.0))
)

m90m90expand_data = OxfordIIITPet( # `m` is minus.
    root="data",
    transform=RandomRotation(degrees=(-90.0, -90.0), expand=True)
)

p180p180offcenter_data = OxfordIIITPet(
    root="data",
    transform=RandomRotation(degrees=(180.0, 180.0), center=(270, 200))
)

m45m45fillgray_data = OxfordIIITPet(
    root="data",
    transform=RandomRotation(degrees=(-45.0, -45.0), fill=150)
)

p135p135fillpurple_data = OxfordIIITPet(
    root="data",
    transform=RandomRotation(degrees=(135.0, 135.0), fill=(160, 32, 240))
)

import matplotlib.pyplot as plt

def show_images(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_images(data=origin_data, main_title="origin_data")
show_images(data=p90_data, main_title="p90_data")
show_images(data=p90p90_data, main_title="p90p90_data")
show_images(data=m90m90expand_data, main_title="m90m90expand_data")
show_images(data=p180p180offcenter_data, main_title="p180p180offcenter_data")
show_images(data=m45m45fillgray_data, main_title="m45m45fillgray_data")
show_images(data=p135p135fillpurple_data, main_title="p135p135fillpurple_data")




<p><img src="https://img.php.cn/upload/article/000/000/000/173541163355044.jpg" alt="RandomRotation in PyTorch"></p>

<p><img src="https://img.php.cn/upload/article/000/000/000/173541163528488.jpg" alt="RandomRotation in PyTorch"></p>

<p><img src="https://img.php.cn/upload/article/000/000/000/173541163621429.jpg" alt="RandomRotation in PyTorch"></p>

<p><img src="https://img.php.cn/upload/article/000/000/000/173541163867638.jpg" alt="RandomRotation in PyTorch"></p>

<p><img src="https://img.php.cn/upload/article/000/000/000/173541164089048.jpg" alt="RandomRotation in PyTorch"></p>

<p><img src="https://img.php.cn/upload/article/000/000/000/173541164120249.jpg" alt="RandomRotation in PyTorch"></p>

<p><img src="https://img.php.cn/upload/article/000/000/000/173541164382128.jpg" alt="RandomRotation in PyTorch"><br>
</p>

<pre class="brush:php;toolbar:false">from torchvision.datasets import OxfordIIITPet
from torchvision.transforms.v2 import RandomRotation

my_data = OxfordIIITPet(
    root="data",
    transform=None
)

import matplotlib.pyplot as plt

def show_images(data, main_title=None, d=0.0, e=False, c=None, f=0):
    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)
        rr = RandomRotation(degrees=d, expand=e, center=c, fill=f) # Here
        plt.imshow(X=rr(im)) # Here
        plt.xticks(ticks=[])
        plt.yticks(ticks=[])
    plt.tight_layout()
    plt.show()

show_images(data=my_data, main_title="my_data")
show_images(data=my_data, main_title="p90_data", d=90.0)
show_images(data=my_data, main_title="p90p90_data", d=(90.0, 90.0))
show_images(data=my_data, main_title="m90m90expand_data", d=(-90, -90))
show_images(data=my_data, main_title="p180p180offcenter_data",
            d=(180.0, 180.0), c=(270, 200))
show_images(data=my_data, main_title="m45m45fillgray_data",
            d=(-45.0, -45.0), f=150)
show_images(data=my_data, main_title="p135p135fillpurple_data",
            d=(135.0, 135.0), f=(160, 32, 240))
Copy after login

RandomRotation in PyTorch

RandomRotation in PyTorch

RandomRotation in PyTorch

RandomRotation in PyTorch

RandomRotation in PyTorch

RandomRotation in PyTorch

RandomRotation in PyTorch

The above is the detailed content of RandomRotation in PyTorch. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template