DATA/CV

[Tensorflow] mnist 코드

728x90

사용 라이브러리

  • tensorflow: 프레임워크 
  • keras: 프레임워크
  • numpy: array 연산 
  • matplotlib: 시각화 
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt

 

모델 구현 코드

  • keras.models.Sequential
  • keras.layers.Conv2D
  • keras.layers.MaxPool2D
  • keras.layers.Flatten
  • keras.layers.Dense
mnist = keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()   
x_train_norm, x_test_norm = x_train / 255.0, x_test / 255.0
x_train_reshaped=x_train_norm.reshape( -1, 28, 28, 1)  
x_test_reshaped=x_test_norm.reshape( -1, 28, 28, 1)

model=keras.models.Sequential()
model.add(keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(28,28,1)))
model.add(keras.layers.MaxPool2D(2,2))
model.add(keras.layers.Conv2D(32, (3,3), activation='relu'))
model.add(keras.layers.MaxPooling2D((2,2)))
model.add(keras.layers.Flatten())
model.add(keras.layers.Dense(32, activation='relu'))
model.add(keras.layers.Dense(10, activation='softmax'))

model.compile(optimizer='adam',
             loss='sparse_categorical_crossentropy',
             metrics=['accuracy'])

 

모델 구조 

model.summary()

Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_2 (Conv2D)            (None, 26, 26, 16)        160       
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 13, 13, 16)        0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 11, 11, 32)        4640      
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 5, 5, 32)          0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 800)               0         
_________________________________________________________________
dense_2 (Dense)              (None, 32)                25632     
_________________________________________________________________
dense_3 (Dense)              (None, 10)                330       
=================================================================
Total params: 30,762
Trainable params: 30,762
Non-trainable params: 0
_________________________________________________________________

 

모델 학습 

# 모델 훈련
model.fit(x_train_reshaped, y_train, epochs=10)

Epoch 1/10
1875/1875 [==============================] - 9s 5ms/step - loss: 0.1916 - accuracy: 0.9408
Epoch 2/10
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0647 - accuracy: 0.9803
Epoch 3/10
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0472 - accuracy: 0.9850
Epoch 4/10
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0376 - accuracy: 0.9885
Epoch 5/10
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0295 - accuracy: 0.9908
Epoch 6/10
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0256 - accuracy: 0.9916
Epoch 7/10
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0203 - accuracy: 0.9935
Epoch 8/10
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0174 - accuracy: 0.9945
Epoch 9/10
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0145 - accuracy: 0.9954
Epoch 10/10
1875/1875 [==============================] - 9s 5ms/step - loss: 0.0124 - accuracy: 0.9961

<tensorflow.python.keras.callbacks.History at 0x7f66a544e310>
728x90