2024.03.13 15:17
mnist 알파벳 데이터를 식별하는 CNN 모델 예제이다.
import tensorflow as tfimport 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")
[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,146Trainable params: 515,146Non-trainable params: 0_________________________________________________________________Epoch 1/26000/6000 [==============================] - 94s 16ms/step - loss: 0.1885 - accuracy: 0.9462Epoch 2/26000/6000 [==============================] - 95s 16ms/step - loss: 0.1358 - accuracy: 0.9648
최적화 완료 ================================================ 313/313 [==============================] - 3s 9ms/step313/313 - 3s - loss: 0.1350 - accuracy: 0.9671 - 3s/epoch - 8ms/stepaccuracy: 0.9671
mnist 알파벳 데이터를 식별하는 RNN 모델 예제이다.
Keras의 SimpleRNN을 사용하여 간단하게 구현하였다.
import tensorflow as tfimport numpy as npfrom tensorflow.keras.layers import SimpleRNN, Denseimport 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,602Trainable params: 6,602Non-trainable params: 0_________________________________________________________________Epoch 1/51875/1875 [==============================] - 14s 7ms/step - loss: 0.4983 - accuracy: 0.8468Epoch 2/51875/1875 [==============================] - 13s 7ms/step - loss: 0.2367 - accuracy: 0.9312Epoch 3/51875/1875 [==============================] - 13s 7ms/step - loss: 0.1963 - accuracy: 0.9415Epoch 4/51875/1875 [==============================] - 13s 7ms/step - loss: 0.1778 - accuracy: 0.9492Epoch 5/51875/1875 [==============================] - 12s 7ms/step - loss: 0.1622 - accuracy: 0.9534
최적화 완료 ================================================ 313/313 [==============================] - 1s 3ms/step313/313 - 1s - loss: 0.1581 - accuracy: 0.9550 - 1s/epoch - 3ms/stepaccuracy: 0.9550