PyTorch 中的随机水平翻转
请我喝杯咖啡☕
*备忘录:
randomhorizontalflip() 可以水平翻转零个或多个图像,如下所示:
*备忘录:
from torchvision.datasets import oxfordiiitpetfrom torchvision.transforms.v2 import randomhorizontalfliprandomhorizontalflip()# randomhorizontalflip(p=0.5)randomhorizontalflip().p# 0.5origin_data = oxfordiiitpet( root="data", transform=none)trans100_data = oxfordiiitpet( root="data", transform=randomhorizontalflip(p=1.0))trans50_data = oxfordiiitpet( root="data", transform=randomhorizontalflip(p=0.5))import matplotlib.pyplot as pltdef 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")
from torchvision.datasets import OxfordIIITPetfrom torchvision.transforms.v2 import RandomHorizontalFlipmy_data = OxfordIIITPet( root="data", transform=None)import matplotlib.pyplot as pltdef 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) rhf = RandomHorizontalFlip(p=prob) plt.imshow(X=rhf(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)