RandomPosterize in PyTorch

Buy Me a Coffee☕ *Memos: My post explains OxfordIIITPet(). RandomPosterize() can randomly posterize an image with a given probability as shown below: *Memos: The 1st argument for initialization is bits(Required-Type:int): *Memos: It's the number of bits to keep for each channel. It must be x

Feb 16, 2025 - 23:54
 0
RandomPosterize in PyTorch

Buy Me a Coffee

*Memos:

RandomPosterize() can randomly posterize an image with a given probability as shown below:

*Memos:

  • The 1st argument for initialization is bits(Required-Type:int): *Memos:
    • It's the number of bits to keep for each channel.
    • It must be x <= 8.
  • The 1st argument for initialization is p(Optional-Default:0.5-Type:int or float): *Memos:
    • It's the probability of whether an image is posterized 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 RandomPosterize

randomposterize = RandomPosterize(bits=1)
randomposterize = RandomPosterize(bits=1, p=0.5)

randomposterize 
# RandomPosterize(p=0.5, bits=1)

randomposterize.bits
# 1

randomposterize.p
# 0.5

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

b8p1origin_data = OxfordIIITPet(
    root="data",
    transform=RandomPosterize(bits=8, p=1)
)

b7p1_data = OxfordIIITPet(
    root="data",
    transform=RandomPosterize(bits=7, p=1)
)

b6p1_data = OxfordIIITPet(
    root="data",
    transform=RandomPosterize(bits=6, p=1)
)

b5p1_data = OxfordIIITPet(
    root="data",
    transform=RandomPosterize(bits=5, p=1)
)

b4p1_data = OxfordIIITPet(
    root="data",
    transform=RandomPosterize(bits=4, p=1)
)

b3p1_data = OxfordIIITPet(
    root="data",
    transform=RandomPosterize(bits=3, p=1)
)

b2p1_data = OxfordIIITPet(
    root="data",
    transform=RandomPosterize(bits=2, p=1)
)

b1p1_data = OxfordIIITPet(
    root="data",
    transform=RandomPosterize(bits=1, p=1)
)

b0p1_data = OxfordIIITPet(
    root="data",
    transform=RandomPosterize(bits=0, p=1)
)

bn1p1_data = OxfordIIITPet(
    root="data",
    transform=RandomPosterize(bits=-1, p=1)
)

bn10p1_data = OxfordIIITPet(
    root="data",
    transform=RandomPosterize(bits=-10, p=1)
)

bn100p1_data = OxfordIIITPet(
    root="data",
    transform=RandomPosterize(bits=-100, p=1)
)

b1p0_data = OxfordIIITPet(
    root="data",
    transform=RandomPosterize(bits=1, p=0)
)

b1p05_data = OxfordIIITPet(
    root="data",
    transform=RandomPosterize(bits=1, p=0.5)
    # transform=RandomPosterize(bits=1)
)

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=b8p1origin_data, main_title="b8p1origin_data")
show_images1(data=b7p1_data, main_title="b7p1_data")
show_images1(data=b6p1_data, main_title="b6p1_data")
show_images1(data=b5p1_data, main_title="b5p1_data")
show_images1(data=b4p1_data, main_title="b4p1_data")
show_images1(data=b3p1_data, main_title="b3p1_data")
show_images1(data=b2p1_data, main_title="b2p1_data")
show_images1(data=b1p1_data, main_title="b1p1_data")
show_images1(data=b0p1_data, main_title="b0p1_data")
show_images1(data=bn1p1_data, main_title="bn1p1_data")
show_images1(data=bn10p1_data, main_title="bn10p1_data")
show_images1(data=bn100p1_data, main_title="bn100p1_data")
print()
show_images1(data=b1p0_data, main_title="b1p0_data")
show_images1(data=b1p0_data, main_title="b1p0_data")
show_images1(data=b1p0_data, main_title="b1p0_data")
print()
show_images1(data=b1p05_data, main_title="b1p05_data")
show_images1(data=b1p05_data, main_title="b1p05_data")
show_images1(data=b1p05_data, main_title="b1p05_data")
print()
show_images1(data=b1p1_data, main_title="b1p1_data")
show_images1(data=b1p1_data, main_title="b1p1_data")
show_images1(data=b1p1_data, main_title="b1p1_data")

# ↓ ↓ ↓ ↓ ↓ ↓ The code below is identical to the code above. ↓ ↓ ↓ ↓ ↓ ↓
def show_images2(data, main_title=None, b=None, prob=0):
    plt.figure(figsize=[10, 5])
    plt.suptitle(t=main_title, y=0.8, fontsize=14)
    if b != None:
        for i, (im, _) in zip(range(1, 6), data):
            plt.subplot(1, 5, i)
            rp = RandomPosterize(bits=b, p=prob)
            plt.imshow(X=rp(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="b8p1origin_data", b=8, prob=1)
show_images2(data=origin_data, main_title="b7p1_data", b=7, prob=1)
show_images2(data=origin_data, main_title="b6p1_data", b=6, prob=1)
show_images2(data=origin_data, main_title="b5p1_data", b=5, prob=1)
show_images2(data=origin_data, main_title="b4p1_data", b=4, prob=1)
show_images2(data=origin_data, main_title="b3p1_data", b=3, prob=1)
show_images2(data=origin_data, main_title="b2p1_data", b=2, prob=1)
show_images2(data=origin_data, main_title="b1p1_data", b=1, prob=1)
show_images2(data=origin_data, main_title="b0p1_data", b=0, prob=1)
show_images2(data=origin_data, main_title="bn1p1_data", b=-1, prob=1)
show_images2(data=origin_data, main_title="bn10p1_data", b=-10, prob=1)
show_images2(data=origin_data, main_title="bn100p1_data", b=-100, prob=1)
print()
show_images2(data=origin_data, main_title="b1p0_data", b=1, prob=0)
show_images2(data=origin_data, main_title="b1p0_data", b=1, prob=0)
show_images2(data=origin_data, main_title="b1p0_data", b=1, prob=0)
print()
show_images2(data=origin_data, main_title="b1p05_data", b=1, prob=0.5)
show_images2(data=origin_data, main_title="b1p05_data", b=1, prob=0.5)
show_images2(data=origin_data, main_title="b1p05_data", b=1, prob=0.5)
print()
show_images2(data=origin_data, main_title="b1p1_data", b=1, prob=1)
show_images2(data=origin_data, main_title="b1p1_data", b=1, prob=1)
show_images2(data=origin_data, main_title="b1p1_data", b=1, 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