Data augmentation

 

인위적으로 학습 데이터를 변형시켜 추가의 학습 데이터로 사용하는 기법을 data augmentation (데이터 증식), training set expansion이라고 합니다.


이미지의 경우, 8방향으로 평행이동시키거나 rotation, scaling, crop 등의 방법을 주로 사용하는데 scipy 패키지에 대부분의 방법들이 구현되어 있으니 잘 가져다 쓰면 됩니다. 여기선 이미지를 평행이동시키고 새로운 학습 데이터를 만드는 코드를 보겠습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from scipy.ndimage.interpolation import shift
import matplotlib
import matplotlib.pyplot as plt

def matshow(one_img):
    plt.matshow(one_img, cmap=matplotlib.cm.binary)
    plt.show()

X_train_aug, y_train_aug = X_train, y_train
X_train_img = X_train.reshape([-1, 28, 28])
for pos in ([0, 0, 1], [0, 1, 0], [0, 0, -1], [0, -1, 0]):  # 4 방향 평행이동
    X_train_shift = shift(X_train_img, pos).reshape(-1, 784)
    X_train_aug = np.concatenate([X_train_aug, X_train_shift], axis=0)
    y_train_aug = np.concatenate([y_train_aug, y_train])

X_train_aug.shape, y_train_aug.shape