ColorJitter

torchvision.transforms.ColorJitter(brightnes, contrast, saturation, hue)

이미지를 대상으로 밝기(brightness), 대비(contrast), 채도(saturation), 색상(hue)를 무작위로 변경합니다.

forward(img)

img (PIL Image or Tensor) – Input image

from PIL import Image
import torch
from torchvision.transforms import v2

orig_img = Image.open(Path('../assets') / 'astronaut.jpg')

print(orig_img)

<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=512x512 at 0x7D802A830F10>

jitter = v2.ColorJitter(brightness=.5, hue=.3)
jittered_imgs = [jitter(orig_img) for _ in range(4)]
plot([orig_img] + jittered_imgs)

image.png

[pytorch tutorials] geometric transforms

mnist = keras.datasets.cifar10 #cifar10 데이터셋 불러오기
(x_train, y_train), (x_test, y_test) = mnist.load_data()
test_image = x_train[0] 

print(test_image)

test_image가 PIL Image가 아닌 경우 TypeError: img should be PIL Image. Got <class 'numpy.ndarray'> 발생

[[[ 59 62 63] [ 43 46 45] [ 50 48 43] ... [158 132 108] [152 125 102] [148 124 103]] ...


test_image_np = np.array(test_image)
test_image = Image.fromarray(test_image)
print(test_image)

<PIL.Image.Image image mode=RGB size=32x32 at 0x7AF9DB6ADBA0>

albumentations

Step 1. Import the required libraries

import albumentations as A
import cv2

Step 2. Define an augmentation pipeline

transform = A.Compose([
    A.RandomCrop(width=256, height=256),
    A.HorizontalFlip(p=0.5),
    A.RandomBrightnessContrast(p=0.2),
])

image.png

Step 3. Read images from the disk.

from PIL import Image
import numpy as np

pillow_image = Image.open("image.jpg")
image = np.array(pillow_image)

OpenCV는 이미지를 BGR 형식으로 읽기 때문에, Albumentations와 같이 RGB 형식을 사용하는 라이브러리와 함께 사용할 때는 이미지 색상 채널 순서를 맞추기 위해 BGR을 RGB로 변환해야 한다