RandomSolarize in PyTorch

Buy Me a Coffee☕ *Memos: My post explains RandomInvert(). My post explains OxfordIIITPet(). RandomSolarize() can randomly solarize an image with a given probability as shown below: *Memos: The 1st argument for initialization is threshold(Required-Type:int or float). *All pixels equal or above this value are inverted. The 2nd argument for initialization is p(Optional-Default:0.5-Type:int or float): *Memos: It's the probability of whether an image is solarized or not. It must be 0

Feb 18, 2025 - 03:16
 0
RandomSolarize in PyTorch

Buy Me a Coffee

*Memos:

RandomSolarize() can randomly solarize an image with a given probability as shown below:

*Memos:

  • The 1st argument for initialization is threshold(Required-Type:int or float). *All pixels equal or above this value are inverted.
  • The 2nd argument for initialization is p(Optional-Default:0.5-Type:int or float): *Memos:
    • It's the probability of whether an image is solarized or not.
    • It must be 0 <= x <= 1.
  • The 1st argument is img(Required-Type:PIL Image or tensor(int)): *Memos:
    • A tensor must be 2D or 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 RandomSolarize

randomsolarize = RandomSolarize(threshold=0)
randomsolarize = RandomSolarize(threshold=0, p=0.5)

randomsolarize
# RandomSolarize(p=0.5, threshold=0)

randomsolarize.threshold
# 0

randomsolarize.p
# 0.5

origin_data = OxfordIIITPet(
    root="data",
    transform=None
)

t300p1_data = OxfordIIITPet( # `t` is threshold.
    root="data",
    transform=RandomSolarize(threshold=300, p=1)
)

t256p1_data = OxfordIIITPet(
    root="data",
    transform=RandomSolarize(threshold=256, p=1)
)

t250p1_data = OxfordIIITPet(
    root="data",
    transform=RandomSolarize(threshold=250, p=1)
)

t240p1_data  = OxfordIIITPet(
    root="data",
    transform=RandomSolarize(threshold=240, p=1)
)

t220p1_data = OxfordIIITPet(
    root="data",
    transform=RandomSolarize(threshold=220, p=1)
)

t200p1_data = OxfordIIITPet(
    root="data",
    transform=RandomSolarize(threshold=200, p=1)
)

t150p1_data = OxfordIIITPet(
    root="data",
    transform=RandomSolarize(threshold=150, p=1)
)

t100p1_data = OxfordIIITPet(
    root="data",
    transform=RandomSolarize(threshold=100, p=1)
)

t50p1_data = OxfordIIITPet(
    root="data",
    transform=RandomSolarize(threshold=50, p=1)
)

t10p1_data = OxfordIIITPet(
    root="data",
    transform=RandomSolarize(threshold=10, p=1)
)

t0p1_data = OxfordIIITPet(
    root="data",
    transform=RandomSolarize(threshold=0, p=1)
)

tn10p1_data = OxfordIIITPet( # `n` is negative.
    root="data",
    transform=RandomSolarize(threshold=-10, p=1)
)

tn100p1_data = OxfordIIITPet(
    root="data",
    transform=RandomSolarize(threshold=-100, p=1)
)

t0p0_data = OxfordIIITPet(
    root="data",
    transform=RandomSolarize(threshold=0, p=0)
)

t0p05_data = OxfordIIITPet(
    root="data",
    transform=RandomSolarize(threshold=0, p=0.5)
)

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=t300p1_data, main_title="t300p1_data")
show_images1(data=t256p1_data, main_title="t256p1_data")
show_images1(data=t255p1_data, main_title="t255p1_data")
show_images1(data=t250p1_data, main_title="t250p1_data")
show_images1(data=t240p1_data, main_title="t240p1_data")
show_images1(data=t220p1_data, main_title="t220p1_data")
show_images1(data=t200p1_data, main_title="t200p1_data")
show_images1(data=t150p1_data, main_title="t150p1_data")
show_images1(data=t100p1_data, main_title="t100p1_data")
show_images1(data=t50p1_data, main_title="t50p1_data")
show_images1(data=t10p1_data, main_title="t10p1_data")
show_images1(data=t0p1_data, main_title="t0p1_data")
show_images1(data=tn10p1_data, main_title="tn10p1_data")
show_images1(data=tn100p1_data, main_title="tn100p1_data")
print()
show_images1(data=t0p0_data, main_title="t0p0_data")
show_images1(data=t0p0_data, main_title="t0p0_data")
show_images1(data=t0p0_data, main_title="t0p0_data")
print()
show_images1(data=t0p05_data, main_title="t0p05_data")
show_images1(data=t0p05_data, main_title="t0p05_data")
show_images1(data=t0p05_data, main_title="t0p05_data")
print()
show_images1(data=t0p1_data, main_title="t0p1_data")
show_images1(data=t0p1_data, main_title="t0p1_data")
show_images1(data=t0p1_data, main_title="t0p1_data")

# ↓ ↓ ↓ ↓ ↓ ↓ The code below is identical to the code above. ↓ ↓ ↓ ↓ ↓ ↓
def show_images2(data, main_title=None, t=None, prob=0):
    plt.figure(figsize=[10, 5])
    plt.suptitle(t=main_title, y=0.8, fontsize=14)
    if t != None:
        for i, (im, _) in zip(range(1, 6), data):
            plt.subplot(1, 5, i)
            rs = RandomSolarize(threshold=t, p=prob)
            plt.imshow(X=rs(im))
            plt.xticks(ticks=[])
            plt.yticks(ticks=[])
    else:
        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_images2(data=origin_data, main_title="origin_data")
print()
show_images2(data=origin_data, main_title="t300p1_data", t=300, prob=1)
show_images2(data=origin_data, main_title="t256p1_data", t=256, prob=1)
show_images2(data=origin_data, main_title="t255p1_data", t=255, prob=1)
show_images2(data=origin_data, main_title="t250p1_data", t=250, prob=1)
show_images2(data=origin_data, main_title="t240p1_data", t=240, prob=1)
show_images2(data=origin_data, main_title="t220p1_data", t=220, prob=1)
show_images2(data=origin_data, main_title="t200p1_data", t=200, prob=1)
show_images2(data=origin_data, main_title="t150p1_data", t=150, prob=1)
show_images2(data=origin_data, main_title="t100p1_data", t=100, prob=1)
show_images2(data=origin_data, main_title="t50p1_data", t=50, prob=1)
show_images2(data=origin_data, main_title="t10p1_data", t=10, prob=1)
show_images2(data=origin_data, main_title="t0p1_data", t=0, prob=1)
show_images2(data=origin_data, main_title="tn10p1_data", t=-10, prob=1)
show_images2(data=origin_data, main_title="tn100p1_data", t=-100, prob=1)
print()
show_images2(data=origin_data, main_title="t0p0_data", t=0, prob=0)
show_images2(data=origin_data, main_title="t0p0_data", t=0, prob=0)
show_images2(data=origin_data, main_title="t0p0_data", t=0, prob=0)
print()
show_images2(data=origin_data, main_title="t0p05_data", t=0, prob=0.5)
show_images2(data=origin_data, main_title="t0p05_data", t=0, prob=0.5)
show_images2(data=origin_data, main_title="t0p05_data", t=0, prob=0.5)
print()
show_images2(data=origin_data, main_title="t0p1_data", t=0, prob=1)
show_images2(data=origin_data, main_title="t0p1_data", t=0, prob=1)
show_images2(data=origin_data, main_title="t0p1_data", t=0, prob=1)

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