basic_ML_models

basic_CNN

명징직조지훈 2022. 10. 23. 11:18

커널은 CNN 연산의 특성이다. 

import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout
from keras.layers import Conv2D, MaxPooling2D, Flatten
from keras.utils import to_categorical, plot_model
from keras.datasets import mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()

# 레이블 개수 계산

num_labels = len(np.unique(y_train))
num_labels

# 원-핫 벡터로 변환

y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

# 이미지 차원

image_size = x_train.shape[1]
image_size

# 크기 조정, 정규화

x_train = np.reshape(x_train, [-1, image_size, image_size, 1])
x_test = np.reshape(x_test, [-1, image_size, image_size, 1])
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0

# 신경망 매개변수

input_shape = (image_size, image_size,1)
batch_size = 128
kernel_size = 3
pool_size = 2
filters = 64
dropout = 0.2

model = Sequential()
model.add(Conv2D(filters = filters, kernel_size=kernel_size, activation='relu', input_shape = input_shape))
model.add(MaxPooling2D(pool_size))
model.add(Conv2D(filters = filters, kernel_size=kernel_size, activation='relu', input_shape = input_shape))
model.add(MaxPooling2D(pool_size))
model.add(Conv2D(filters = filters, kernel_size=kernel_size, activation='relu', input_shape = input_shape))
model.add(Flatten())
model.add(Dropout(dropout))
model.add(Dense(num_labels))
model.add(Activation('softmax'))

model.summary()
>>>
Model: "sequential_2"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv2d_6 (Conv2D)           (None, 26, 26, 64)        640       
                                                                 
 max_pooling2d_4 (MaxPooling  (None, 13, 13, 64)       0         
 2D)                                                             
                                                                 
 conv2d_7 (Conv2D)           (None, 11, 11, 64)        36928     
                                                                 
 max_pooling2d_5 (MaxPooling  (None, 5, 5, 64)         0         
 2D)                                                             
                                                                 
 conv2d_8 (Conv2D)           (None, 3, 3, 64)          36928     
                                                                 
 flatten_2 (Flatten)         (None, 576)               0         
                                                                 
 dropout_1 (Dropout)         (None, 576)               0         
                                                                 
 dense_1 (Dense)             (None, 10)                5770      
                                                                 
 activation_1 (Activation)   (None, 10)                0         
                                                                 
=================================================================
Total params: 80,266
Trainable params: 80,266
Non-trainable params: 0
_________________________________________________________________

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=10, batch_size = batch_size)
>>>
Epoch 1/10
469/469 [==============================] - 86s 182ms/step - loss: 0.2697 - accuracy: 0.9178
Epoch 2/10
469/469 [==============================] - 83s 177ms/step - loss: 0.0675 - accuracy: 0.9786
Epoch 3/10
469/469 [==============================] - 84s 179ms/step - loss: 0.0475 - accuracy: 0.9851
Epoch 4/10
469/469 [==============================] - 84s 179ms/step - loss: 0.0395 - accuracy: 0.9872
Epoch 5/10
469/469 [==============================] - 83s 176ms/step - loss: 0.0328 - accuracy: 0.9896
Epoch 6/10
469/469 [==============================] - 85s 180ms/step - loss: 0.0289 - accuracy: 0.9912
Epoch 7/10
469/469 [==============================] - 83s 178ms/step - loss: 0.0247 - accuracy: 0.9920
Epoch 8/10
469/469 [==============================] - 85s 181ms/step - loss: 0.0221 - accuracy: 0.9930
Epoch 9/10
469/469 [==============================] - 85s 180ms/step - loss: 0.0208 - accuracy: 0.9934
Epoch 10/10
469/469 [==============================] - 83s 177ms/step - loss: 0.0180 - accuracy: 0.9944
<keras.callbacks.History at 0x7f0463f64690>

loss, acc = model.evaluate(x_test, y_test, batch_size=batch_size)
print(loss, acc)
>>>
79/79 [==============================] - 4s 44ms/step - loss: 0.0224 - accuracy: 0.9937
0.02240227907896042 0.9937000274658203

합성곱

합성곱 Convolution 연산은 입력 이미지를 커널이 입력 이미지로부터 학습했던 내용을 표현하는 특징 맵 feature map으로 변환한다. 그런 다음 특징 맵은 잇따라 나오는 계층의 또 다른 특징 맵으로 변환된다. 

결과로 얻은 특징 맵은 원본 입력 이미지보다 작어진다. 이는 합성곱이 유효한 요소에 대해서만 수행되기 때문

풀링 연산

풀링 연산은 각 특징 맵을 압축한다.pool_size * pool_size 크기의 패치가 픽셀 하나로 축소된다. 이는 특징 맵 크기를 줄인다는 점에서 중요하다.

마지막 풀링층의 출력을 특징 맵의 스택이다.