PHP前端开发

PyTorch 中的随机透视

百变鹏仔 2小时前 #Python
文章标签 透视

请我喝杯咖啡☕

*备忘录:

randomperspective() 可以对零个或多个图像进行透视变换,如下所示:

*备忘录:

  • 初始化的第二个参数是 p(可选-默认:0.5-类型:int 或 float):*备注:
  • 初始化的第三个参数是插值(optional-default:interpolationmode.bilinear-type:interpolationmode)。
  • 初始化的第四个参数是 fill(optional-default:0-type:int, float or tuple/list(int or float)):*备注:
  • 有第一个参数(必需类型:pil 图像或张量(int))。 *它必须是 3d 张量。
  • v2建议按照v1还是v2使用?我应该使用哪一个?
  • from torchvision.datasets import OxfordIIITPetfrom torchvision.transforms.v2 import RandomPerspectivefrom torchvision.transforms.functional import InterpolationModerandompers = RandomPerspective()randompers = RandomPerspective(distortion_scale=0.5,                               p=0.5,                               interpolation=InterpolationMode.BILINEAR,                               fill=0)randompers# RandomPerspective(p=0.5,#                   distortion_scale=0.5,#                   interpolation=InterpolationMode.BILINEAR,#                   fill=0)randompers.distortion_scale# 0.5randompers.p# 0.5randompers.interpolation# <InterpolationMode.BILINEAR: 'bilinear'>randompers.fill# 0origin_data = OxfordIIITPet(    root="data",    transform=None    # transform=RandomPerspective(distortion_scale=0)    # transform=RandomPerspective(p=0))dis02p1_data = OxfordIIITPet(    root="data",    transform=RandomPerspective(distortion_scale=0.2, p=1))dis06p1_data = OxfordIIITPet(    root="data",    transform=RandomPerspective(distortion_scale=0.6, p=1))dis1p1_data = OxfordIIITPet(    root="data",    transform=RandomPerspective(distortion_scale=1, p=1))p1_data = OxfordIIITPet(    root="data",    transform=RandomPerspective(p=1))p05_data = OxfordIIITPet(    root="data",    transform=RandomPerspective(p=0.5))p1fillgray_data = OxfordIIITPet(    root="data",    transform=RandomPerspective(p=1, fill=150))p1fillpurple_data = OxfordIIITPet(    root="data",    transform=RandomPerspective(p=1, fill=[160, 32, 240]))import matplotlib.pyplot as pltdef 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=dis02p1_data, main_title="dis02p1_data")show_images1(data=dis06p1_data, main_title="dis06p1_data")show_images1(data=dis1p1_data, main_title="dis1p1_data")show_images1(data=p1_data, main_title="p1_data")show_images1(data=p05_data, main_title="p05_data")show_images1(data=p1fillgray_data, main_title="p1fillgray_data")show_images1(data=p1fillpurple_data, main_title="p1fillpurple_data")# ↓ ↓ ↓ ↓ ↓ ↓ The code below is identical to the code above. ↓ ↓ ↓ ↓ ↓ ↓def show_images2(data, main_title=None, d=0.5, prob=0.5, 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)        rp = RandomPerspective(distortion_scale=d, p=prob, fill=f) # Here        plt.imshow(X=rp(im)) # Here        plt.xticks(ticks=[])        plt.yticks(ticks=[])    plt.tight_layout()    plt.show()show_images2(data=origin_data, main_title="origin_data", d=0)show_images2(data=origin_data, main_title="dis02p1_data", d=0.2, prob=1)show_images2(data=origin_data, main_title="dis06p1_data", d=0.6, prob=1)show_images2(data=origin_data, main_title="dis1p1_data", d=1, prob=1)show_images2(data=origin_data, main_title="p1_data", prob=1)show_images2(data=origin_data, main_title="p05_data", prob=0.5)show_images2(data=origin_data, main_title="p1fillgray_data", prob=1, f=150)show_images2(data=origin_data, main_title="p1fillpurple_data", prob=1,             f=[160, 32, 240])