DEV Community

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

Posted on • Edited on

ColorJitter in PyTorch

Buy Me a Coffee

ColorJitter() can change the brightness, contrast, saturation and hue of an image as shown below:

*Memos:

  • The 1st argument for initialization is brightness(Optional-Default:0-Type:int, float or tuple/list(int or float)): *Memos:
    • It's the range of the brightness [min, max] so it must be min <= max.
    • It must be 0 <= x.
    • A tuple/list must be the 1D with 2 elements.
    • A single value means [max(0, 1-brightness), 1+brightness].
  • The 2nd argument for initialization is contrast(Optional-Default:0-Type:int, float or tuple/list(int or float)): *Memos:
    • It's the range of the contrast [min, max] so it must be min <= max.
    • It must be 0 <= x.
    • A tuple/list must be the 1D with 2 elements.
    • A single value means [max(0, 1-contrast), 1+contrast].
  • The 3rd argument for initialization is saturation(Optional-Default:0-Type:int, float or tuple/list(int or float)): *Memos:
    • It's the range of the saturation [min, max] so it must be min <= max.
    • It must be 0 <= x.
    • A tuple/list must be the 1D with 2 elements.
    • A single value means [max(0, 1-saturation), 1+saturation].
  • The 4th argument for initialization is hue(Optional-Default:0-Type:float or tuple/list(float)): *Memos:
    • It's the range of the hue [min, max] so it must be min <= max.
    • It must be -0.5 <= x <= 0.5.
    • A tuple or list must be the 1D with 2 elements.
    • A single value means [-hue, +hue].
  • 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 ColorJitter

colorjitter = ColorJitter()
colorjitter = ColorJitter(brightness=0,
                          contrast=0,
                          saturation=0,
                          hue=0)
colorjitter = transform=ColorJitter(brightness=[1, 1]),
                                    contrast=[1, 1],
                                    saturation=[1, 1],
                                    hue=[0, 0])
colorjitter
# ColorJitter()

print(colorjitter.brightness)
# None

print(colorjitter.contrast)
# None

print(colorjitter.saturation)
# None

print(colorjitter.hue)
# None

origin_data = OxfordIIITPet(
    root="data",
    transform=None
    # transform=ColorJitter()
    # colorjitter = ColorJitter(brightness=0,
    #                           contrast=0,
    #                           saturation=0,
    #                           hue=0)
    # transform=ColorJitter(brightness=[1, 1]),
    #                       contrast=[1, 1],
    #                       saturation=[1, 1],
    #                       hue=[0, 0])
)

brightp2_data = OxfordIIITPet( # `bright` is brightness and `p` is plus.
    root="data",
    transform=ColorJitter(brightness=2)
    # transform=ColorJitter(brightness=[0, 3])
)

brightp2p2_data = OxfordIIITPet(
    root="data",
    transform=ColorJitter(brightness=[2, 2])
)

brightp05p05_data = OxfordIIITPet(
    root="data",
    transform=ColorJitter(brightness=[0.5, 0.5])
)

contrap2_data = OxfordIIITPet( # `contra` is contrast.
    root="data",
    transform=ColorJitter(contrast=2)
    # transform=ColorJitter(contrast=[0, 3])
)

contrap2p2_data = OxfordIIITPet(
    root="data",
    transform=ColorJitter(contrast=[2, 2])
)

contrap05p05_data = OxfordIIITPet(
    root="data",
    transform=ColorJitter(contrast=[0.5, 0.5])
)

saturap2_data = OxfordIIITPet( # `satura` is saturation.
    root="data",
    transform=ColorJitter(saturation=2)
    # transform=ColorJitter(saturation=[0, 3])
)

saturap2p2_data = OxfordIIITPet(
    root="data",
    transform=ColorJitter(saturation=[2, 2])
)

saturap05p05_data = OxfordIIITPet(
    root="data",
    transform=ColorJitter(saturation=[0.5, 0.5])
)

huep05_data = OxfordIIITPet(
    root="data",
    transform=ColorJitter(hue=0.5)
    # transform=ColorJitter(hue=[-0.5, 0.5])
)

huep025p025_data = OxfordIIITPet( # `m` is minus.
    root="data",
    transform=ColorJitter(hue=[0.25, 0.25])
)

huem025m025_data = OxfordIIITPet( # `m` is minus.
    root="data",
    transform=ColorJitter(hue=[-0.25, -0.25])
)

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=brightp2_data, main_title="brightp2_data")
show_images1(data=brightp2p2_data, main_title="brightp2p2_data")
show_images1(data=brightp05p05_data, main_title="brightp05p05_data")
print()
show_images1(data=origin_data, main_title="origin_data")
show_images1(data=contrap2_data, main_title="contrap2_data")
show_images1(data=contrap2p2_data, main_title="contrap2p2_data")
show_images1(data=contrap05p05_data, main_title="contrap05p05_data")
print()
show_images1(data=origin_data, main_title="origin_data")
show_images1(data=saturap2_data, main_title="saturap2_data")
show_images1(data=saturap2p2_data, main_title="saturap2p2_data")
show_images1(data=saturap05p05_data, main_title="saturap05p05_data")
print()
show_images1(data=origin_data, main_title="origin_data")
show_images1(data=huep05_data, main_title="huep05_data")
show_images1(data=huep025p025_data, main_title="huep025p025_data")
show_images1(data=huem025m025_data, main_title="huem025m025_data")

# ↓ ↓ ↓ ↓ ↓ ↓ The code below is identical to the code above. ↓ ↓ ↓ ↓ ↓ ↓
def show_images2(data, main_title=None, b=0, c=0, s=0, h=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)
        cj = ColorJitter(brightness=b, contrast=c, # Here
                         saturation=s, hue=h)
        plt.imshow(X=cj(im)) # Here
        plt.xticks(ticks=[])
        plt.yticks(ticks=[])
    plt.tight_layout()
    plt.show()

show_images2(data=my_data, main_title="origin_data")
show_images2(data=my_data, main_title="brightp2_data", b=2)
show_images2(data=my_data, main_title="brightp2p2_data", b=[2, 2])
show_images2(data=my_data, main_title="brightp05p05_data", b=[0.5, 0.5])
print()
show_images2(data=my_data, main_title="origin_data")
show_images2(data=my_data, main_title="contrap2_data", c=2)
show_images2(data=my_data, main_title="contrap2p2_data", c=[2, 2])
show_images2(data=my_data, main_title="contrap05p05_data", c=[0.5, 0.5])
print()
show_images2(data=my_data, main_title="origin_data")
show_images2(data=my_data, main_title="saturap2_data", s=2)
show_images2(data=my_data, main_title="saturap2p2_data", s=[2, 2])
show_images2(data=my_data, main_title="saturap05p05_data", s=[0.5, 0.5])
print()
show_images2(data=my_data, main_title="origin_data")
show_images2(data=my_data, main_title="huep05_data", h=0.5)
show_images2(data=my_data, main_title="huep025p025_data", h=[0.25, 0.25])
show_images2(data=my_data, main_title="huem025m025_data", h=[-0.25, -0.25])
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

Top comments (0)