RandomHorizontalFlip in PyTorch

Buy Me a Coffee

*Memos:

RandomHorizontalFlip() can flip zero or more images horizontally as shown below:

*Memos:

  • The 1st argument for initialization is p(Optional-Default:0.5-Type:int or float): *Memos:
    • It’s the probability of whether each image is flipped or not.
    • It must be 0 <= x <= 1.
  • The 1st argument is img(Required-Type:PIL Image or tensor(int, float, complex or bool)): *Memos:
    • The 0D or more D tensor of zero or more elements can be set to it.
    • 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 RandomHorizontalFlip

randomhorizontalflip = RandomHorizontalFlip()
randomhorizontalflip = RandomHorizontalFlip(p=0.5)

randomhorizontalflip
# RandomHorizontalFlip(p=0.5) 
randomhorizontalflip.p
# 0.5 
origin_data = OxfordIIITPet(
    root="data",
    transform=None
    # transform=RandomHorizontalFlip(p=0) )

p1_data = OxfordIIITPet(
    root="data",
    transform=RandomHorizontalFlip(p=1)
)

p05_data = OxfordIIITPet(
    root="data",
    transform=RandomHorizontalFlip(p=0.5)
)

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")
show_images1(data=p1_data, main_title="p1_data")
show_images1(data=p05_data, main_title="p05_data")

# ↓ ↓ ↓ ↓ ↓ ↓ The code below is identical to the code above. ↓ ↓ ↓ ↓ ↓ ↓ def show_images2(data, main_title=None, prob=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)
        rhf = RandomHorizontalFlip(p=prob)
        plt.imshow(X=rhf(im))
        plt.xticks(ticks=[])
        plt.yticks(ticks=[])
    plt.tight_layout()
    plt.show()

show_images2(data=origin_data, main_title="origin_data")
show_images2(data=origin_data, main_title="p1_data", prob=1)
show_images2(data=origin_data, main_title="p05_data", prob=0.5)

Enter fullscreen mode Exit fullscreen mode

原文链接:RandomHorizontalFlip in PyTorch

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容