ElasticTransform in PyTorch (2)

Buy Me a Coffee☕ *Memos: My post explains ElasticTransform() about alpha and fill argument. My post explains OxfordIIITPet(). ElasticTransform() can do random morphological transformation for an image as shown below: from torchvision.datasets import OxfordIIITPet from torchvision.transforms.v2 import ElasticTransform from torchvision.transforms.functional import InterpolationMode origin_data = OxfordIIITPet( root="data", transform=None # transform=ElasticTransform(alpha=0, sigma=0) ) a5000s01_data = OxfordIIITPet( # `a` is alpha and `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]) ) a5000s5fgray_data = OxfordIIITPet( root="data", transform=ElasticTransform(alpha=5000, sigma=5, fill=150) ) a5000s10fgray_data = OxfordIIITPet( root="data", transform=ElasticTransform(alpha=5000, sigma=10, fill=150) ) a5000s5fpurple_data = OxfordIIITPet( root="data", transform=ElasticTransform(alpha=5000, sigma=5, fill=[160, 32, 240]) ) a5000s10fpurple_data = OxfordIIITPet( root="data", transform=ElasticTransform(alpha=5000, sigma=10, 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") 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

Feb 21, 2025 - 03:47
 0
ElasticTransform in PyTorch (2)

Buy Me a Coffee

*Memos:

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

from torchvision.datasets import OxfordIIITPet
from torchvision.transforms.v2 import ElasticTransform
from torchvision.transforms.functional import InterpolationMode

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

a5000s01_data = OxfordIIITPet( # `a` is alpha and `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])
)

a5000s5fgray_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=5000, sigma=5, fill=150)
)

a5000s10fgray_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=5000, sigma=10, fill=150)
)

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

a5000s10fpurple_data = OxfordIIITPet(
    root="data",
    transform=ElasticTransform(alpha=5000, sigma=10, 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")
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, 
                 ip=InterpolationMode.BILINEAR, 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, # Here
                              interpolation=ip, fill=f)
        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)
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])
print()
show_images2(data=origin_data, main_title="a5000s5fgray_data", a=5000, 
             s=5, f=150)
show_images2(data=origin_data, main_title="a5000s10fgray_data", a=5000, 
             s=10, f=150)
print()
show_images2(data=origin_data, main_title="a5000s5fpurple_data", a=5000, 
             s=5, f=[160, 32, 240])
show_images2(data=origin_data, main_title="a5000s10fpurple_data", a=5000, 
             s=10, f=[160, 32, 240])

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