Data Science

MNIST 분류 CNN 모델 - 모델 구현

오기오기 2021. 11. 24. 02:39
728x90
반응형

합성곱을 통해 특징을 추출, 풀링을 통해 사이즈 조절과 노이즈 처리, 활성함수(FL)를 통해 분류가능

Keras에서 CNN 모델을 만들기 위해 필요한 함수/메서드

1. CNN 레이어 : 입력 이미지의 특징, 즉 처리할 특징 맵(map)을 추출하는 레이어

tf.keras.layers.Conv2D(filters, kernel_size, activation, padding)
  • filters : 필터(커널) 개수
  • kernel_size : 필터(커널)의 크기
  • activation : 활성화 함수
  • padding : 이미지가 필터를 거칠 때 그 크기가 줄어드는 것을 방지하기 위해서 가장자리에 0의 값을 가지는 픽셀을 넣을 것인지 말 것인지를 결정하는 변수. ‘SAME’ 또는 ‘VALID'

2. Maxpool 레이어 : 처리할 특징 맵(map)의 크기를 줄여주는 레이어

tf.keras.layers.MaxPool2D(padding)
  • padding : ‘SAME’ 또는 ‘VALID’

3. Flatten 레이어 :  N차원의 텐서 형태를 1차원으로 평평하게 만들어주는 레이어

tf.keras.layers.Flatten()

 

4. Dense 레이어

tf.keras.layers.Dense(node, activation)
  • node : 노드(뉴런) 개수
  • activation : 활성화 함수
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
# MNIST 데이터 세트를 불러옵니다.
mnist = tf.keras.datasets.mnist

# MNIST 데이터 세트를 Train set과 Test set으로 나누어 줍니다.
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()    

# Train 데이터 5000개와 Test 데이터 1000개를 사용합니다.
train_images, train_labels = train_images[:5000], train_labels[:5000]
test_images, test_labels = test_images[:1000], test_labels[:1000]

# CNN 모델의 입력으로 사용할 수 있도록 (샘플개수, 가로픽셀, 세로픽셀, 1) 형태로 변환합니다.
train_images = tf.expand_dims(train_images, -1)
test_images = tf.expand_dims(test_images, -1)


"""
1. CNN 모델을 설정합니다.
   분류 모델에 맞게 마지막 레이어의 노드 수는 10개, activation 함수는 'softmax'로 설정합니다.
"""
model = tf.keras.Sequential([
    tf.keras.layers.Conv2D(filters = 32, kernel_size = (3,3), activation = 'relu', padding = 'SAME', input_shape = (28,28,1)),
    tf.keras.layers.MaxPool2D(padding = 'SAME'),
    tf.keras.layers.Conv2D(filters = 32, kernel_size = (3,3), activation = 'relu', padding = 'SAME'),
    tf.keras.layers.MaxPool2D(padding = 'SAME'),
    tf.keras.layers.Conv2D(filters = 32, kernel_size = (3,3), activation = 'relu', padding = 'SAME'),
    tf.keras.layers.MaxPool2D(padding = 'SAME'),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(64, activation = 'relu'),
    tf.keras.layers.Dense(10, activation = 'softmax')
])

# CNN 모델 구조를 출력합니다.
print(model.summary())

# CNN 모델의 학습 방법을 설정합니다.
model.compile(loss = 'sparse_categorical_crossentropy',
              optimizer = 'adam',
              metrics = ['accuracy'])
              
# 학습을 수행합니다. 
history = model.fit(train_images, train_labels, epochs = 20, batch_size = 512)

# 학습 결과를 출력합니다.
Visulaize([('CNN', history)], 'loss')
def Visulaize(histories, key='loss'):
    for name, history in histories:
        plt.plot(history.epoch, history.history[key], 
             label=name.title()+' Train')

    plt.xlabel('Epochs')
    plt.ylabel(key.replace('_',' ').title())
    plt.legend()
    plt.xlim([0,max(history.epoch)])    
    plt.savefig("plot.png")
    elice_utils.send_image("plot.png")

 

728x90
반응형