DEV Community

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

Posted on

ElasticTransform in PyTorch

Buy Me a Coffee

*Memos:

ElasticTransform() can do random morphological transformation for an image as shown below:

*Memos:

  • The 1st argument for initialization is alpha(Optional-Default:50.0-Type:int, float or tuple/list(int or float)): *Memos:
    • It can do morphological transformation.
    • It's the magnitude of displacements [number, number].
    • It must be 0 <= number.
    • A tuple/list must be the 1D with 1 or 2 elements.
    • A single value(int, float or tuple/list(int or float)) means [alpha, alpha].
  • The 2nd argument for initialization is sigma(Optional-Default:0.5-Type:int, float or tuple/list(int or float)): *Memos:
    • It's the smoothness of displacements [number, number].
    • It must be 0 < number.
    • A tuple/list must be the 1D with 1 or 2 elements.
    • A single value(int, float or tuple/list(int or float)) means [sigma, sigma].
  • 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 an image. *The background can be seen when doing morphological transformation for an image.
    • A tuple/list must be the 1D with 1 or 3 elements.
  • The 1st argument is img(Required-Type:PIL Image or tensor(int)): *Memos:
    • A tensor must be 3D.
    • 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 ElasticTransform
from torchvision.transforms.functional import InterpolationMode

elastictf = ElasticTransform()
elastictf = ElasticTransform(alpha=50,
                             sigma=5,
                             interpolation=InterpolationMode.BILINEAR,
                             fill=0)
elastictf
# ElasticTransform(alpha=[50.0, 50.0],
#                  sigma=[5.0, 5.0],
#                  interpolation=InterpolationMode.BILINEAR,
#                  fill=0)

elastictf.alpha
# [50.0, 50.0]

elastictf.sigma
# [5.0, 5.0]

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

elastictf.fill
# 0

origin_data = OxfordIIITPet(
    root="data",
    transform=None
    # transform=ElasticTransform(alpha=0, sigma=0)
)

a50_data = OxfordIIITPet( # `a` is alpha.
    root="data",
    transform=ElasticTransform(alpha=50)
    # transform=ElasticTransform(alpha=[50, 50])
)

a100_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=100)
)

a200_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=200)
)

a500_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=500)
)

a1000_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=1000)
)

a5000_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=5000)
)

a10000_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=10000)
)

a50000_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=50000)
)

a100000_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=100000)
)

a1000000_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=1000000)
)

a10000000_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=1000000)
)

a50_0_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=[50, 0])
)

a100_0_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=[100, 0])
)

a200_0_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=[200, 0])
)

a500_0_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=[500, 0])
)

a1000_0_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=[1000, 0])
)

a5000_0_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=[5000, 0])
)

a10000_0_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=[10000, 0])
)

a50000_0_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=[50000, 0])
)

a100000_0_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=[100000, 0])
)

a1000000_0_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=[1000000, 0])
)

a10000000_0_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=[10000000, 0])
)

a0_50_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=[0, 50])
)

a0_100_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=[0, 100])
)

a0_200_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=[0, 200])
)

a0_500_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=[0, 500])
)

a0_1000_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=[0, 1000])
)

a0_5000_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=[0, 5000])
)

a0_10000_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=[0, 10000])
)

a0_50000_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=[0, 50000])
)

a0_100000_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=[0, 100000])
)

a0_1000000_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=[0, 1000000])
)

a0_10000000_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=[0, 10000000])
)

a5000s01_data = OxfordIIITPet( # `s` is sigma.
    root="data",
    transform=ElasticTransform(alpha=5000, sigma=0.1)
    # transform=ElasticTransform(alpha=5000, sigma=[0.1, 0.1])
)

a5000s1_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=5000, sigma=1)
)

a5000s5_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=5000, sigma=5)
)

a5000s10_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=5000, sigma=10)
)

a5000s20_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=5000, sigma=20)
)

a5000s40_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=5000, sigma=40)
)

a5000s40_01_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=5000, sigma=[40, 0.1])
)

a5000s40_1_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=5000, sigma=[40, 1])
)

a5000s40_5_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=5000, sigma=[40, 5])
)

a5000s40_10_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=5000, sigma=[40, 10])
)

a5000s40_20_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=5000, sigma=[40, 20])
)

a5000s40_40_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=5000, sigma=[40, 40])
)

a5000s01_40_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=5000, sigma=[0.1, 40])
)

a5000s1_40_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=5000, sigma=[1, 40])
)

a5000s5_40_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=5000, sigma=[5, 40])
)

a5000s10_40_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=5000, sigma=[10, 40])
)

a5000s20_40_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=5000, sigma=[20, 40])
)

a5000s40_40_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=5000, sigma=[40, 40])
)

a5000fgray_data = OxfordIIITPet( # `f` is fill.
    root="data",
    transform=ElasticTransform(alpha=5000, fill=150)
)

a10000fgray_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=10000, fill=150)
)

a5000fpurple_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=5000, fill=[160, 32, 240])
)

a10000fpurple_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=10000, 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=a50_data, main_title="a50_data")
show_images1(data=a100_data, main_title="a100_data")
show_images1(data=a200_data, main_title="a200_data")
show_images1(data=a500_data, main_title="a500_data")
show_images1(data=a1000_data, main_title="a1000_data")
show_images1(data=a5000_data, main_title="a5000_data")
show_images1(data=a10000_data, main_title="a10000_data")
show_images1(data=a50000_data, main_title="a50000_data")
show_images1(data=a100000_data, main_title="a100000_data")
show_images1(data=a1000000_data, main_title="a1000000_data")
show_images1(data=a10000000_data, main_title="a10000000_data")
print()
show_images1(data=origin_data, main_title="origin_data")
show_images1(data=a50_0_data, main_title="a50_0_data")
show_images1(data=a100_0_data, main_title="a100_0_data")
show_images1(data=a200_0_data, main_title="a200_0_data")
show_images1(data=a500_0_data, main_title="a500_0_data")
show_images1(data=a1000_0_data, main_title="a1000_0_data")
show_images1(data=a5000_0_data, main_title="a5000_0_data")
show_images1(data=a10000_0_data, main_title="a10000_0_data")
show_images1(data=a50000_0_data, main_title="a50000_0_data")
show_images1(data=a100000_0_data, main_title="a100000_0_data")
show_images1(data=a1000000_0_data, main_title="a1000000_0_data")
show_images1(data=a10000000_0_data, main_title="a10000000_0_data")
print()
show_images1(data=origin_data, main_title="origin_data")
show_images1(data=a0_50_data, main_title="a0_50_data")
show_images1(data=a0_100_data, main_title="a0_100_data")
show_images1(data=a0_200_data, main_title="a0_200_data")
show_images1(data=a0_500_data, main_title="a0_500_data")
show_images1(data=a0_1000_data, main_title="a0_1000_data")
show_images1(data=a0_5000_data, main_title="a0_5000_data")
show_images1(data=a0_10000_data, main_title="a0_10000_data")
show_images1(data=a0_50000_data, main_title="a0_50000_data")
show_images1(data=a0_100000_data, main_title="a0_100000_data")
show_images1(data=a0_1000000_data, main_title="a0_1000000_data")
show_images1(data=a0_10000000_data, main_title="a0_10000000_data")
print()
show_images1(data=a5000s01_data, main_title="a5000s01_data")
show_images1(data=a5000s1_data, main_title="a5000s1_data")
show_images1(data=a5000s5_data, main_title="a5000s5_data")
show_images1(data=a5000s10_data, main_title="a5000s10_data")
show_images1(data=a5000s20_data, main_title="a5000s20_data")
show_images1(data=a5000s40_data, main_title="a5000s40_data")
print()
show_images1(data=a5000s40_01_data, main_title="a5000s40_01_data")
show_images1(data=a5000s40_1_data, main_title="a5000s40_1_data")
show_images1(data=a5000s40_5_data, main_title="a5000s40_5_data")
show_images1(data=a5000s40_10_data, main_title="a5000s40_10_data")
show_images1(data=a5000s40_20_data, main_title="a5000s40_20_data")
show_images1(data=a5000s40_40_data, main_title="a5000s40_40_data")
print()
show_images1(data=a5000s01_40_data, main_title="a5000s01_40_data")
show_images1(data=a5000s1_40_data, main_title="a5000s1_40_data")
show_images1(data=a5000s5_40_data, main_title="a5000s5_40_data")
show_images1(data=a5000s10_40_data, main_title="a5000s10_40_data")
show_images1(data=a5000s20_40_data, main_title="a5000s20_40_data")
show_images1(data=a5000s40_40_data, main_title="a5000s40_40_data")
print()
show_images1(data=a5000fgray_data, main_title="a5000fgray_data")
show_images1(data=a10000fgray_data, main_title="a10000fgray_data")
show_images1(data=a5000fpurple_data, main_title="a5000fpurple_data")
show_images1(data=a10000fpurple_data, main_title="a10000fpurple_data")

# ↓ ↓ ↓ ↓ ↓ ↓ The code below is identical to the code above. ↓ ↓ ↓ ↓ ↓ ↓
def show_images2(data, main_title=None, a=50, s=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)
        et = ElasticTransform(alpha=a, sigma=s, fill=f) # Here
        plt.imshow(X=et(im)) # Here
        plt.xticks(ticks=[])
        plt.yticks(ticks=[])
    plt.tight_layout()
    plt.show()

show_images2(data=origin_data, main_title="origin_data", a=0, s=0)
show_images2(data=origin_data, main_title="a50_data", a=50)
show_images2(data=origin_data, main_title="a100_data", a=100)
show_images2(data=origin_data, main_title="a200_data", a=200)
show_images2(data=origin_data, main_title="a500_data", a=500)
show_images2(data=origin_data, main_title="a1000_data", a=1000)
show_images2(data=origin_data, main_title="a5000_data", a=5000)
show_images2(data=origin_data, main_title="a10000_data", a=10000)
show_images2(data=origin_data, main_title="a50000_data", a=50000)
show_images2(data=origin_data, main_title="a100000_data", a=100000)
show_images2(data=origin_data, main_title="a1000000_data", a=1000000)
show_images2(data=origin_data, main_title="a10000000_data", a=10000000)
print()
show_images2(data=origin_data, main_title="origin_data", a=0, s=0)
show_images2(data=origin_data, main_title="a50_0_data", a=[50, 0])
show_images2(data=origin_data, main_title="a100_0_data", a=[100, 0])
show_images2(data=origin_data, main_title="a200_0_data", a=[200, 0])
show_images2(data=origin_data, main_title="a500_0_data", a=[500, 0])
show_images2(data=origin_data, main_title="a1000_0_data", a=[1000, 0])
show_images2(data=origin_data, main_title="a5000_0_data", a=[5000, 0])
show_images2(data=origin_data, main_title="a10000_0_data", a=[10000, 0])
show_images2(data=origin_data, main_title="a50000_0_data", a=[50000, 0])
show_images2(data=origin_data, main_title="a100000_0_data", a=[100000, 0])
show_images2(data=origin_data, main_title="a1000000_0_data", a=[1000000, 0])
show_images2(data=origin_data, main_title="a10000000_0_data", a=[10000000, 0])
print()
show_images2(data=origin_data, main_title="origin_data", a=0, s=0)
show_images2(data=origin_data, main_title="a0_50_data", a=[0, 50])
show_images2(data=origin_data, main_title="a0_100_data", a=[0, 100])
show_images2(data=origin_data, main_title="a0_200_data", a=[0, 200])
show_images2(data=origin_data, main_title="a0_500_data", a=[0, 500])
show_images2(data=origin_data, main_title="a0_1000_data", a=[0, 1000])
show_images2(data=origin_data, main_title="a0_5000_data", a=[0, 5000])
show_images2(data=origin_data, main_title="a0_10000_data", a=[0, 10000])
show_images2(data=origin_data, main_title="a0_50000_data", a=[0, 50000])
show_images2(data=origin_data, main_title="a0_100000_data", a=[0, 100000])
show_images2(data=origin_data, main_title="a0_1000000_data", a=[0, 1000000])
show_images2(data=origin_data, main_title="a0_10000000_data", a=[0, 10000000])
print()
show_images2(data=origin_data, main_title="a5000s01_data", a=5000, s=0.1)
show_images2(data=origin_data, main_title="a5000s1_data", a=5000, s=1)
show_images2(data=origin_data, main_title="a5000s5_data", a=5000, s=5)
show_images2(data=origin_data, main_title="a5000s10_data", a=5000, s=10)
show_images2(data=origin_data, main_title="a5000s20_data", a=5000, s=20)
show_images2(data=origin_data, main_title="a5000s40_data", a=5000, s=40)
print()
show_images2(data=origin_data, main_title="a5000s40_01_data", a=5000,
             s=[40, 0.1])
show_images2(data=origin_data, main_title="a5000s40_1_data", a=5000,
             s=[40, 1])
show_images2(data=origin_data, main_title="a5000s40_5_data", a=5000,
             s=[40, 5])
show_images2(data=origin_data, main_title="a5000s40_10_data", a=5000,
             s=[40, 10])
show_images2(data=origin_data, main_title="a5000s40_20_data", a=5000,
             s=[40, 20])
show_images2(data=origin_data, main_title="a5000s40_40_data", a=5000,
             s=[40, 40])
print()
show_images2(data=origin_data, main_title="a5000s01_40_data", a=5000,
             s=[0.1, 40])
show_images2(data=origin_data, main_title="a5000s1_40_data", a=5000,
             s=[1, 40])
show_images2(data=origin_data, main_title="a5000s5_40_data", a=5000,
             s=[5, 40])
show_images2(data=origin_data, main_title="a5000s10_40_data", a=5000,
             s=[10, 40])
show_images2(data=origin_data, main_title="a5000s20_40_data", a=5000,
             s=[20, 40])
show_images2(data=origin_data, main_title="a5000s40_40_data", a=5000,
             s=[40, 40])
print()
show_images2(data=origin_data, main_title="a5000fgray_data", a=5000, f=150)
show_images2(data=origin_data, main_title="a10000fgray_data", a=10000, f=150)
show_images2(data=origin_data, main_title="a5000fpurple_data", a=5000,
             f=[160, 32, 240])
show_images2(data=origin_data, main_title="a10000fpurple_data", a=10000,
             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

Image description

Image description

Image description

Image description


Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description


Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description


Image description

Image description

Image description

Image description

Image description

Image description


Image description

Image description

Image description

Image description

Image description

Image description


Image description

Image description

Image description

Image description

Image description

Image description


Image description

Image description

Image description

Image description

Billboard image

Monitoring as code

With Checkly, you can use Playwright tests and Javascript to monitor end-to-end scenarios in your NextJS, Astro, Remix, or other application.

Get started now!

Top comments (0)

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay