Skip to content

CNN, RNN

mnist 알파벳 데이터를 식별하는 CNN 모델 예제이다.

import tensorflow as tf
import numpy as np
import matplotlib.pylab as plt
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
plt.figure(figsize=(6,1))
for i in range(36):
plt.subplot(3,12,i+1)
plt.imshow(train_images[i], cmap="gray")
plt.axis("off")
plt.show()
# 28 * 28의 벡터 이미지 60000개, 채널은 1개
train_images = train_images.reshape((60000, 28, 28, 1))
test_images = test_images.reshape((10000, 28, 28, 1))
train_images, test_images = train_images / 255.0, test_images / 255.0
print(train_labels[:10])
one_hot_train_labels = tf.keras.utils.to_categorical(train_labels, 10)
one_hot_test_labels = tf.keras.utils.to_categorical(test_labels, 10)
print(one_hot_train_labels[:10])
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv2D(32, (3,3), activation='relu', strides=(1,1), input_shape=(28, 28, 1)))
model.add(tf.keras.layers.MaxPooling2D((2,2)))
model.add(tf.keras.layers.Conv2D(64, (3,3), activation='relu'))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(64, activation='relu'))
model.add(tf.keras.layers.Dense(10, activation='softmax'))
model.compile(optimizer=tf.optimizers.Adam(learning_rate=0.01),loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()
history = model.fit(train_images, one_hot_train_labels, epochs=2, batch_size=10)
plt.figure(figsize=(12,4))
plt.subplot(1,1,1)
plt.plot(history.history['loss'], 'b--', label='loss')
plt.plot(history.history['accuracy'], 'g-', label='Accuracy')
plt.xlabel('Epoch')
plt.legend()
plt.show()
print("최적화 완료")
print("\n================================================\n")
labels = model.predict(test_images)
print("accuracy: %.4f"% model.evaluate(test_images, one_hot_test_labels, verbose=2)[1])
fig = plt.figure()
for i in range(36):
subplot = fig.add_subplot(3,12,i+1)
subplot.set_xticks([])
subplot.set_yticks([])
subplot.set_title('%d' % np.argmax(labels[i]))
subplot.imshow(test_images[i].reshape((28, 28)), cmap=plt.cm.gray_r)
plt.show()
print("\n================================================\n")

출력

image
[5 0 4 1 9 2 1 3 1 4]
[[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
[0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]
[0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
[0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]]
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_2 (Conv2D) (None, 26, 26, 32) 320
max_pooling2d_1 (MaxPooling (None, 13, 13, 32) 0
2D)
conv2d_3 (Conv2D) (None, 11, 11, 64) 18496
flatten_1 (Flatten) (None, 7744) 0
dense_2 (Dense) (None, 64) 495680
dense_3 (Dense) (None, 10) 650
=================================================================
Total params: 515,146
Trainable params: 515,146
Non-trainable params: 0
_________________________________________________________________
Epoch 1/2
6000/6000 [==============================] - 94s 16ms/step - loss: 0.1885 - accuracy: 0.9462
Epoch 2/2
6000/6000 [==============================] - 95s 16ms/step - loss: 0.1358 - accuracy: 0.9648
image
최적화 완료
================================================
313/313 [==============================] - 3s 9ms/step
313/313 - 3s - loss: 0.1350 - accuracy: 0.9671 - 3s/epoch - 8ms/step
accuracy: 0.9671
image

RNN

mnist 알파벳 데이터를 식별하는 RNN 모델 예제이다.

Keras의 SimpleRNN을 사용하여 간단하게 구현하였다.

import tensorflow as tf
import numpy as np
from tensorflow.keras.layers import SimpleRNN, Dense
import matplotlib.pylab as plt
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
train_images, test_images = train_images / 255.0, test_images / 255.0
print(train_labels[:10])
one_hot_train_labels = tf.keras.utils.to_categorical(train_labels, 10)
one_hot_test_labels = tf.keras.utils.to_categorical(test_labels, 10)
model = tf.keras.models.Sequential()
model.add(SimpleRNN(units=64, input_shape = (28, 28), return_sequences=False))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer=tf.optimizers.Adam(learning_rate=0.01),loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()
history = model.fit(train_images, one_hot_train_labels, epochs=2, batch_size=10)
plt.figure(figsize=(12,4))
plt.subplot(1,1,1)
plt.plot(history.history['loss'], 'b--', label='loss')
plt.plot(history.history['accuracy'], 'g-', label='Accuracy')
plt.xlabel('Epoch')
plt.legend()
plt.show()
print("최적화 완료")
print("\n================================================\n")
labels=model.predict(test_images)
print("accuracy: %.4f"% model.evaluate(test_images, one_hot_test_labels, verbose=2)[1])
fig = plt.figure()
for i in range(36):
subplot = fig.add_subplot(3,12,i+1)
subplot.set_xticks([])
subplot.set_yticks([])
subplot.set_title('%d' % np.argmax(labels[i]))
subplot.imshow(test_images[i].reshape((28, 28)), cmap=plt.cm.gray_r)
plt.show()
print("\n================================================\n")

실행결과

[5 0 4 1 9 2 1 3 1 4]
Model: "sequential_4"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
simple_rnn_2 (SimpleRNN) (None, 64) 5952
dense_6 (Dense) (None, 10) 650
=================================================================
Total params: 6,602
Trainable params: 6,602
Non-trainable params: 0
_________________________________________________________________
Epoch 1/5
1875/1875 [==============================] - 14s 7ms/step - loss: 0.4983 - accuracy: 0.8468
Epoch 2/5
1875/1875 [==============================] - 13s 7ms/step - loss: 0.2367 - accuracy: 0.9312
Epoch 3/5
1875/1875 [==============================] - 13s 7ms/step - loss: 0.1963 - accuracy: 0.9415
Epoch 4/5
1875/1875 [==============================] - 13s 7ms/step - loss: 0.1778 - accuracy: 0.9492
Epoch 5/5
1875/1875 [==============================] - 12s 7ms/step - loss: 0.1622 - accuracy: 0.9534
image
최적화 완료
================================================
313/313 [==============================] - 1s 3ms/step
313/313 - 1s - loss: 0.1581 - accuracy: 0.9550 - 1s/epoch - 3ms/step
accuracy: 0.9550
image