Computer Vision 분야에서 최적의 모델을 개발하기 위해서는, 모델에 입력되는 데이터 즉 이미지의 전처리가 중요하다. 이미지 전처리는 이미지에서 의미있는 feature와 representation을 추출할 수 있기 때문에 모델 성능과 일반화에 큰 영향을 미치게 된다.
색을 디지털적으로 표현하고 해석하기 위해 정의된 수학적 모델
ex) RGB, HSV, Lab, YCbCr, Grayscale
import cv2
# original RGB image
img = cv2.imread('image.jpg')
# RGB -> HSV
img1 = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # cv2에서는 RGB를 BGR 순으로 표기
# RGB -> LAB
img2 = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
# RGB -> YCrCb
img3 = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
# RGB -> greyscale
img4 = cv2.cvtcolor(img, cv2.COLOR_BGR2GRAY)
new_img = cv2.equalizeHist(img)
이미지의 형태, 크기, 위치 등을 변환하는 기법
translation, rotation, scaling, shearing, perspective 등이 있다.
입력 이미지 크기는 출력 feature map의 크기에 영향을 준다.
CNN은 사전 학습 중에 feature map 크기의 패턴을 찾는 방법을 학습한다. 그러나 입력 이미지 크기가 달라지면, 학습된 패턴의 크기도 변경된다. 따라서 이미지 크기가 변경되면 object를 찾지 못하게 될 수 있다.
=
import numpy as np
row, col = img.shape[:2]
# x 방향으로 100, y 방향으로 50 shift
matrix = np.float([1, 0, 100], [0, 1, 50], [0, 0, 1]])
new_img = cv2.warpPerspective(img, matrix, (rcol, row))
=
# (반시계방향으로) 90도 회전
matrix1 = cv2.getRotationMatrix2D((col/2, row/2), 90, 1)
matrix2 = np.vstack([matrix1, [0, 0, 1]])
new_img = cv2.warpPerspective(img, matrix2, (col, row))
=
# 가로 세로를 2배로 늘림
matrix = np.float32([[2, 0, 0], [0, 2, 0], [0, 0, 1]])
new_img = cv2.warpPerspective(img, matrix, (2*col, 2*row))
크기가 늘어난 후 새로운 위치는 interpolation을 통해 채워진다.
=
pts1 = np.float32([[56, 65], [368, 52], [28, 387], [389, 390]])
pts2 = np.float32([[0, 0], [300, 0], [0, 300], [300, 300]])
matrix = cv2.getPerspectiveTransform(pts1, pts2)
new_img = cv2.warpPerspective(img, matrix, (300, 300))
위 코드는 이미지의 특정 지점을 확대하여 보여주는 것을 의미한다. 기존 이미지에서의 네 꼭지점 ((56, 65), (368, 52), (28, 387), (389, 390)) 좌표를 끄트머리로 하여 잘라내고 이 꼭지점을 새로 확대한 이미지의 네 꼭지점 ((0, 0), (300, 0), (0, 300), (300, 300)) 으로 변환한다는 것이다. 따라서 new_img는 img의 특정 부분을 확대한 300 300 이미지가 된다.
학습 데이터의 다양성을 증가시켜 모델의 견고성 향상과 과적합을 감소시키는 기법
ex) flip, rotation, crop, color jittering
AutoAugment (데이터셋에 맞춘 최적의 augmentation 정책을 자동으로 탐색), RandAugment (랜덤한 크기로 augmentation의 하위 집합을 무작위로 적용) 등의 advanced augmentation 기법도 있다.
Albumentations를 활용한 data augmentation
import albumentations as A
transform = A.Compose([
A.HorizontalFlip(p=0.5),
A.RandomBrightnessContrast(p=0.2),
A.RandomCrop(height=224, width=224)])
이미지의 픽셀 값을 특정 범위로 스케일링하는 기법이다. 딥러닝 모델의 수렴 속도와 안정성을 개선할 수 있고, 큰 비중을 가진 특징이 학습 과정에서 bias를 만드는 것을 방지할 수 있다.
# with PyTorch
from torchvision import transforms
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(
mean = [0.485, 0.456, 0.406],
std = [0.229, 0.224, 0.225])
])
# with albumentations
from albumentations.pytorch import ToTensorV2
import albumentations as A
transform = A.Compose([
A.Normalize(
mean = (0.485, 0.456, 0.406),
std = (0.229, 0.224, 0.225)),
ToTensorV2()
])