DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on

RandomPerspective in PyTorch

Buy Me a Coffee

*Memos:

RandomPerspective() can do perspective transformation for zero or more images as shown below:

*Memos:

  • The 1st argument for initialization is distortion_scale(Optional-Default:0.5-Type:int or float): *Memos:
    • It can do perspective transformation.
    • It must be 0 <= x <= 1.
  • The 2nd argument for initialization is p(Optional-Default:0.5-Type:int or float): *Memos:
    • It's the probability of whether each image is done with perspective transformation or not.
    • It must be 0 <= x <= 1.
  • The 3rd argument for initialization is interpolation(Optional-Default:InterpolationMode.BILINEAR-Type:InterpolationMode).
  • The 4th argument for initialization is fill(Optional-Default:0-Type:int, float or tuple/list(int or float)): *Memos:
    • It can change the background of images. *The background can be seen when doing perspective transformation for images.
    • A tuple/list must be the 1D with 3 elements.
  • There is the 1st argument(Required-Type:PIL Image or tensor(int)). *It must be a 3D tensor.
  • 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 RandomPerspective
from torchvision.transforms.functional import InterpolationMode

randompers = 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.5

randompers.p
# 0.5

randompers.interpolation
# <InterpolationMode.BILINEAR: 'bilinear'>

randompers.fill
# 0

origin_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 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=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])
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Top comments (0)