Home > Backend Development > Python Tutorial > RandomVerticalFlip in PyTorch

RandomVerticalFlip in PyTorch

Susan Sarandon
Release: 2024-12-28 14:27:18
Original
605 people have browsed it

Buy Me a Coffee☕

*Memos:

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

RandomVerticalFlip() can flip zero or more images vertically as shown below:

*Memos:

  • The 1st argument for initialization is p(Optional-Default:0.5-Type:float). *It's the probability which each image is flipped. *It's the probability which each image is flipped.
  • The 1st argument is img(Required-Type:PIL Image or tensor, tuple or list of int): *Memos:
    • It must be 2D.
    • 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 RandomVerticalFlip

RandomVerticalFlip()
# RandomVerticalFlip(p=0.5)

RandomVerticalFlip().p
# 0.5

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

trans100_data = OxfordIIITPet(
    root="data",
    transform=RandomVerticalFlip(p=1.0)
)

trans50_data = OxfordIIITPet(
    root="data",
    transform=RandomVerticalFlip(p=0.5)
)

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=trans100_data, main_title="trans100_data")
show_images(data=trans50_data, main_title="trans50_data")
Copy after login

RandomVerticalFlip in PyTorch

RandomVerticalFlip in PyTorch

RandomVerticalFlip in PyTorch

from torchvision.datasets import OxfordIIITPet
from torchvision.transforms.v2 import RandomVerticalFlip

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

import matplotlib.pyplot as plt

def show_images(data, main_title=None, prob=0.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)
        rvf = RandomVerticalFlip(p=prob)
        plt.imshow(X=rvf(im))
        plt.xticks(ticks=[])
        plt.yticks(ticks=[])
    plt.tight_layout()
    plt.show()

show_images(data=my_data, main_title="origin_data")
show_images(data=my_data, main_title="trans100_data", prob=1.0)
show_images(data=my_data, main_title="trans50_data", prob=0.5)
Copy after login

RandomVerticalFlip in PyTorch

RandomVerticalFlip in PyTorch

RandomVerticalFlip in PyTorch

The above is the detailed content of RandomVerticalFlip 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