소프트웨어 개발/Computer Vision

Keras for Computer Vision

Leo's notes 2020. 1. 28. 19:01

Keras는 쉬운 AI 개발을 위한 상위 레벨 인터페이스를 제공하는 파이썬 라이브러리이다.

  • Tensorflow, Theano, CNTK, MXNET, PlaidML 등의 다양한 엔진을 사용할 수 있다.
  • 공통 백엔드 함수를 제공하여 특정 엔진에 사용이 국한되지 않는다.

Install

Anaconda를 이용하여 설치하면 편리하다.

  1. Anaconda Distribution에서 Installer 다운 및 설치
  2. Anaconda Prompt를 실행
  3. conda install keras 입력 (or conda install keras-gpu)

Basics

모델 개발 과정을 크게 4개의 단계로 나눌 수 있다.

  1. Data pre-processing
  2. Modeling
  3. Training
  4. Evaluation

f(x) = 2x를 찾기 위한 간단한 예제이다. x, y는 5쌍의 데이터가 있는데 3쌍은 트레이닝으로, 2쌍은 테스트용으로 사용한다.

import numpy as np
from keras import models, layers
from sklearn.model_selection import train_test_split

# 1. Data pre-processing
x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
y = np.array([0, 2, 4, 6, 8, 10, 12, 14, 16, 18])
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2)

# 2. Modeling
model = models.Sequential()
model.add(layers.Dense(1, input_shape=(1,)))
model.compile(optimizer='SGD', loss='mse', metrics=['accuracy'])

# 3. Training
history = model.fit(x_train, y_train, epochs=30, verbose=1, validation_split=0.2)

# 4. Evaluation
[loss, accuracy] = model.evaluate(x_test, y_test, verbose=1)
print("Accuracy: " + str(accuracy*100) + "%")
2/2 [==============================] - 0s 494us/step
Accuracy: 100.0%

트레이닝한 모델에 x=50을 넣어보면 100의 근사치가 나온다.

model.predict([50])
array([[99.96467]], dtype=float32)

pyplot을 통해 Loss, Accuracy 그래프를 그리는 함수를 만들 수 있다.

import matplotlib.pyplot as plt

def plot_loss(history):
    plt.plot(history.history['loss'])
    plt.plot(history.history['val_loss'])
    plt.title('Model Loss')
    plt.ylabel('Loss')
    plt.xlabel('Epoch')
    plt.legend(['Train', 'Test'], loc=0)
    plt.show()

def plot_accuracy(history):
    plt.plot(history.history['acc'])
    plt.plot(history.history['val_acc'])
    plt.title('Model Accuracy')
    plt.ylabel('Accuracy')
    plt.xlabel('Epoch')
    plt.legend(['Train', 'Test'], loc=0)
    plt.show()

Training (model.fit) 과정에서 반환하는 history 변수를 넘겨준다.

plot_loss(history)
plot_accuracy(history)

훈련 데이터 및 테스트 데이터가 적어서 이 예제에서는 무의미하지만, 테스트 데이터가 충분하고 일반적임을 가정한다면 Epoch 5까지만 트레이닝을 해도 충분히 훈련된 모델이 생성된다는 점을 그래프를 통해 알아낼 수 있다.

CNN

여러 작은 Convolution filter가 이미지를 순회하며 특징점을 찾아 그 합성곱 결과를 다음 레이어로 보내는 것을 반복함으로서 적은 수의 가중치로 이미지 처리를 효율적으로 할 수 있다.

아래 예제는 keras에 기본 내장된 mnist 샘플을 가져와서 CNN을 통해 10개의 클래스로 분류한다.

import keras
from keras import models, layers, datasets

num_classes = 10

# 1. Data pre-processing
(x_train, y_train), (x_test, y_test) = datasets.mnist.load_data()

# Reshape for channel
train_data_count, img_rows, img_cols = x_train.shape
x_train = x_train.reshape(train_data_count, img_rows, img_cols, 1).astype('float32')

test_data_count, img_rows, img_cols = x_test.shape
x_test = x_test.reshape(test_data_count, img_rows, img_cols, 1).astype('float32')

# Normalization
x_train /= 255
x_test /= 255

# One-hot encoding
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
  • 데이터에 채널이 없기 때문에 한 차원을 추가해 채널 개수를 표기해준다.
  • 이미지는 0255의 값을 가지므로 x를 255로 나눠서 01 범위로 정규화한다.
  • Softmax activation function 사용을 위해 y를 one-hot encoding 해준다.
# 2. Modeling
model = models.Sequential()
model.add(layers.Conv2D(filters=32, kernel_size=(3, 3), activation='relu', input_shape=(img_rows, img_cols, 1)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D(pool_size=(2, 2)))
model.add(layers.Dropout(0.25))
model.add(layers.Flatten())
model.add(layers.Dense(128, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

# 3. Training
history = model.fit(x_train, y_train, batch_size=128, epochs=10, validation_split=0.2)

# 4. Evaluation
[loss, accuracy] = model.evaluate(x_test, y_test)
print("Accuracy: " + str(accuracy*100) + "%")

Sequential model로 초기화하고 Layer를 추가하여 모델을 생성한다.

  • Convolution Layers

    • 3x3 필터 32개 Convolution Layer with ReLU
    • 3x3 필터 64개 Convolution Layer with ReLU
    • 2x2 Max Pooling Layer
    • 25% Dropout
  • Fully Connected Layers

    • Flatten으로 2차원 이미지를 1차원 벡터로 변환
    • 길이가 128인 Hidden Layer with ReLU
    • 50% Dropout
    • Softmax를 통해 10개 클래스로 분류
  • Loss function : Categorical Cross-entropy

  • Optimizer : RMSprop

배치사이즈 128로 10에폭 학습시키면 약 99%의 정확도가 나온다.

10000/10000 [==============================] - 0s 46us/step
Accuracy: 99.00999999999999%

Loss, Accuracy 그래프를 출력해본다.

plot_loss(history)
plot_accuracy(history)

References

https://github.com/jskDr/keraspp/