DL- MNIST ๋ฐ์ดํ„ฐ

Jungminยท2023๋…„ 1์›” 30์ผ
1

๋”ฅ๋Ÿฌ๋‹

๋ชฉ๋ก ๋ณด๊ธฐ
3/7
post-thumbnail

๐Ÿ“™ MNIST๋ฐ์ดํ„ฐ๋กœ DL๊ณผ์ • ๋ณต์Šตํ•ด๋ณด๊ธฐ.

import tensorflow as tf
mnist = tf.keras.datasets.mnist

(x_train, y_train),(x_test,y_test) = mnist.load_data()
x_train, x_test = x_train/ 255.0, x_test / 255.0   
# ํ”ฝ์…€ max๊ฐ€ 255. 255๋กœ ๋‚˜๋ˆˆ ๊ฒƒ์€ minmax scaler์“ด ๊ฒƒ๊ณผ ๋น„์Šท

One-hot Encoding

  • lossํ•จ์ˆ˜๋ฅผ sparse_categorical_crossentropy๋กœ ์„ค์ •ํ•˜๋ฉด ๊ฐ™์€ํšจ๊ณผ. ์ €์ ˆ๋กœ onehot encoding ํ•ด์ฃผ๋Š” ํšจ๊ณผ๊ฐ€ ๋‚œ๋‹ค.

โœ28x28์„ ํ•˜๋‚˜์˜ ์—ด๋กœ ํŽผ์น˜๋ฉด 784. --> 1000๊ฐœ์˜ ๋…ธ๋“œ๋ฅผ ๊ฑฐ์ณ 0~9, 10๊ฐœ๋กœ ๋‚˜์•„๊ฐ€๋Š” ๊ณผ์ •.
-->๊ทธ ํ›„ sparse_categorical_crossentropy์„ ๊ฑฐ์ณ ๋‹ต์ด ์ถœ๋ ฅ.

model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(28,28)), #flatten: input shape nXn์œผ๋กœ ์ฃผ๋ฉด 1์—ด๋กœ ํŽผ์ณ์คŒ
    tf.keras.layers.Dense(1000,activation='relu'), #์€๋‹‰์ธต lelu
    tf.keras.layers.Dense(10,activation='softmax')
])
    
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy']) #ํ•™์Šต ์ค‘ accuracy ๋ฐ˜ํ™˜
model.summary()
Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 flatten (Flatten)           (None, 784)               0         
                                                                 
 dense (Dense)               (None, 1000)              785000    
                                                                 
 dense_1 (Dense)             (None, 10)                10010     
                                                                 
=================================================================
Total params: 795,010
Trainable params: 795,010
Non-trainable params: 0
_________________________________________________________________

์ฝ”๋“œ์™€ ๋ชจ๋ธ ์ผ์น˜

%%time

hist = model.fit(x_train,y_train,validation_data=(x_test,y_test),epochs=10,batch_size=100,verbose=1) #verbose : ํ•™์Šตํ•˜๋Š” ๋™์•ˆ์˜ ์ƒํ™ฉ์„ ๊ทธ๋ž˜ํ”ฝํ•˜๊ฒŒ ํ‘œํ˜„
#๋ฐ์ดํ„ฐ ํ•œ ๋ฌถ์Œ์— 100๊ฐœ์”ฉ 600 ๋ฌถ์Œ.
#์†Œ์š” ์‹œ๊ฐ„ 3s : ๋ฐฐ์น˜ ์‚ฌ์ด์ฆˆ 100์œผ๋กœ ํ•˜๊ณ , 600๋ฌถ์Œ์˜ ๋ฐ์ดํ„ฐ ํ•™์Šต์‹œํ‚ค๋Š” ํ•˜๋‚˜์˜ epoch์— ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„ 3์ดˆ.
Epoch 1/10
600/600 [==============================] - 2s 3ms/step - loss: 0.0063 - accuracy: 0.9981 - val_loss: 0.0808 - val_accuracy: 0.9793
Epoch 2/10
600/600 [==============================] - 2s 4ms/step - loss: 0.0067 - accuracy: 0.9979 - val_loss: 0.0817 - val_accuracy: 0.9815
Epoch 3/10
600/600 [==============================] - 2s 3ms/step - loss: 0.0071 - accuracy: 0.9978 - val_loss: 0.0781 - val_accuracy: 0.9815
Epoch 4/10
600/600 [==============================] - 2s 4ms/step - loss: 0.0060 - accuracy: 0.9982 - val_loss: 0.0862 - val_accuracy: 0.9817
Epoch 5/10
600/600 [==============================] - 3s 4ms/step - loss: 0.0074 - accuracy: 0.9976 - val_loss: 0.0914 - val_accuracy: 0.9784
Epoch 6/10
600/600 [==============================] - 3s 4ms/step - loss: 0.0034 - accuracy: 0.9991 - val_loss: 0.0767 - val_accuracy: 0.9822
Epoch 7/10
600/600 [==============================] - 2s 4ms/step - loss: 0.0016 - accuracy: 0.9996 - val_loss: 0.0697 - val_accuracy: 0.9842
Epoch 8/10
600/600 [==============================] - 2s 4ms/step - loss: 0.0020 - accuracy: 0.9995 - val_loss: 0.1102 - val_accuracy: 0.9784
Epoch 9/10
600/600 [==============================] - 2s 4ms/step - loss: 0.0132 - accuracy: 0.9956 - val_loss: 0.0831 - val_accuracy: 0.9823
Epoch 10/10
600/600 [==============================] - 2s 4ms/step - loss: 0.0037 - accuracy: 0.9988 - val_loss: 0.0797 - val_accuracy: 0.9840
CPU times: total: 1min 40s
Wall time: 22.5 s
import matplotlib.pyplot as plt
%matplotlib inline
plot_target = ['loss','val_loss','accuracy','val_accuracy']

plt.figure(figsize=(12,8))

for each in plot_target:
    plt.plot(hist.history[each],label=each)
    
plt.legend()
plt.grid()
plt.show()

validation loss, (training)loss ๊ฐ„์— ๊ฐ„๊ฒฉ์ด ์กฐ๊ธˆ ์žˆ์–ด๋ณด์ด๊ธด ํ•˜๋‚˜, (training) accuracy ๊ฑฐ์˜ 100์— ๊ทผ์ ‘.

score = model.evaluate(x_test,y_test)
print('Test loss:',score[0])
print('test accuracy:',score[1])
313/313 [==============================] - 1s 2ms/step - loss: 0.0797 - accuracy: 0.9840
Test loss: 0.07967743277549744
test accuracy: 0.984000027179718

โžกaccuracy 98%

import numpy as np

predicted_result = model.predict(x_test)
predicted_result[0]   
# ๊ฐ€์žฅ ๋†’์€ ๊ฐ’ max = 1.0000000e+00, '7'
array([4.2500915e-13, 4.3836195e-15, 4.6376505e-12, 4.0975870e-08,
       4.1196887e-18, 3.5640494e-14, 4.5751641e-20, 1.0000000e+00,
       1.9685302e-13, 3.2595003e-11], dtype=float32)
predicted_labels = np.argmax(predicted_result,axis=1) 
predicted_labels[:10]
array([7, 2, 1, 0, 4, 1, 4, 9, 5, 9], dtype=int64)

- ์ž˜๋ชป ์˜ˆ์ธกํ•œ ๊ฒฝ์šฐ ํ™•์ธ

์˜ˆ์ธก ํ™•๋ฅ ์ด ๋†’์€ ๊ฒฝ์šฐ ํ•ด๋‹น ๊ฐ’์„ ์„ ํƒํ•œ๋‹ค.

  • np.argmax : ๊ฐ ํ–‰(axis=1), ์—ด(axis=0)์—์„œ์˜ ์ตœ๋Œ€๊ฐ’์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
# ์ •๋‹ต ํ™•์ธ
y_test[:10]
array([7, 2, 1, 0, 4, 1, 4, 9, 5, 9], dtype=uint8)
wrong_result =[]

for n in range(0,len(y_test)):
    if predicted_labels[n] !=y_test[n]:
        wrong_result.append(n)
        
len(wrong_result) 
160

ํ‹€๋ฆฐ ๋ฐ์ดํ„ฐ ์ด 160๊ฐœ ํ™•์ธ.

import random

samples = random.choices(population = wrong_result,k=16)
plt.figure(figsize=(14,12))

for idx, n in enumerate(samples):
    plt.subplot(4,4,idx+1) # 16๊ฐœ 
    plt.imshow(x_test[n].reshape(28,28),cmap='Greys')
    plt.title('Label:' + str(y_test[n]) + ' | predict :'+ str(predicted_labels[n]))
    plt.axis


๐Ÿ“™MNIST FASHION ๋ฐ์ดํ„ฐ

  • ์ˆซ์ž๋กœ ๋œ MNIST๋ฐ์ดํ„ฐ ์ฒ˜๋Ÿผ 28*28ํฌ๊ธฐ์˜ ํŒจ์…˜ ๊ด€๋ จ 10๊ฐœ ์ข…๋ฅ˜์˜ ๋ฐ์ดํ„ฐ
label์ข…๋ฅ˜
0ํ‹ฐ์…”์ธ 
1๋ฐ”์ง€
2์Šค์›จํ„ฐ
3๋“œ๋ ˆ์Šค
4์ฝ”ํŠธ
5์ƒŒ๋“ค
6์…”์ธ 
7์šด๋™ํ™”
8๊ฐ€๋ฐฉ
9๋ถ€์ธ 
# ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ
fashion_mnist = tf.keras.datasets.fashion_mnist

(x_train, y_train),(x_test,y_test) = fashion_mnist.load_data()
x_train, x_test = x_train/255, x_test/255
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz
32768/29515 [=================================] - 0s 0us/step
40960/29515 [=========================================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz
26427392/26421880 [==============================] - 1s 0us/step
26435584/26421880 [==============================] - 1s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz
16384/5148 [===============================================================================================] - 0s 0s/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz
4423680/4422102 [==============================] - 0s 0us/step
4431872/4422102 [==============================] - 0s 0us/step
samples = random.choices(population=range(0,len(y_train)),k=16)

plt.figure(figsize=(14,12))

for idx, n in enumerate(samples):
    plt.subplot(4,4,idx+1)
    plt.imshow(x_train[n].reshape(28,28),cmap='Greys')
    plt.title('Label:' + str(y_train[n]))
    plt.axis('off')
    
plt.show()

๋ชจ๋ธ์€ ์ˆซ์ž๋•Œ์™€ ๋™์ผํ•œ ๊ตฌ์กฐ๋กœ ๋‘์ž

model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(28,28)), 
    tf.keras.layers.Dense(1000,activation='relu'), 
    tf.keras.layers.Dense(10,activation='softmax')
])
    
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy']) 
%%time

hist = model.fit(x_train,y_train,validation_data=(x_test,y_test),epochs=10,batch_size=100,verbose=1)
Epoch 1/10
600/600 [==============================] - 2s 4ms/step - loss: 0.4840 - accuracy: 0.8289 - val_loss: 0.4564 - val_accuracy: 0.8366
Epoch 2/10
600/600 [==============================] - 2s 3ms/step - loss: 0.3586 - accuracy: 0.8712 - val_loss: 0.3795 - val_accuracy: 0.8632
Epoch 3/10
600/600 [==============================] - 2s 4ms/step - loss: 0.3227 - accuracy: 0.8815 - val_loss: 0.3816 - val_accuracy: 0.8551
Epoch 4/10
600/600 [==============================] - 2s 4ms/step - loss: 0.2959 - accuracy: 0.8909 - val_loss: 0.3531 - val_accuracy: 0.8700
Epoch 5/10
600/600 [==============================] - 2s 4ms/step - loss: 0.2779 - accuracy: 0.8975 - val_loss: 0.3434 - val_accuracy: 0.8761
Epoch 6/10
600/600 [==============================] - 2s 4ms/step - loss: 0.2617 - accuracy: 0.9031 - val_loss: 0.3193 - val_accuracy: 0.8850
Epoch 7/10
600/600 [==============================] - 2s 4ms/step - loss: 0.2490 - accuracy: 0.9074 - val_loss: 0.3164 - val_accuracy: 0.8887
Epoch 8/10
600/600 [==============================] - 2s 4ms/step - loss: 0.2398 - accuracy: 0.9105 - val_loss: 0.3400 - val_accuracy: 0.8803
Epoch 9/10
600/600 [==============================] - 2s 4ms/step - loss: 0.2260 - accuracy: 0.9166 - val_loss: 0.3273 - val_accuracy: 0.8833
Epoch 10/10
600/600 [==============================] - 2s 4ms/step - loss: 0.2199 - accuracy: 0.9172 - val_loss: 0.3211 - val_accuracy: 0.8886
CPU times: total: 1min 46s
Wall time: 22.5 s
plot_target = ['loss','val_loss','accuracy','val_accuracy']

plt.figure(figsize=(12,8))

for each in plot_target:
    plt.plot(hist.history[each],label=each)
    
plt.legend()
plt.grid()
plt.show()

โžกtraining acc๋Š” ์ข‹์•„์ง€์ง€๋งŒ val_acc๋Š” ๋‹ค์†Œ ์ •์ฒดํ•˜๋Š” ๊ฑธ ํ™•์ธ.

score = model.evaluate(x_test,y_test)
print('Test loss :', score[0])
print('Test acc :', score[1])
313/313 [==============================] - 1s 2ms/step - loss: 0.3211 - accuracy: 0.8886
Test loss : 0.3210602104663849
Test acc : 0.8885999917984009
predicted_result = model.predict(x_test)
predicted_labels = np.argmax(predicted_result,axis=1) 
predicted_labels[:10]
array([9, 2, 1, 1, 6, 1, 4, 6, 5, 7], dtype=int64)
y_test[:10]
array([9, 2, 1, 1, 6, 1, 4, 6, 5, 7], dtype=uint8)
# ํ‹€๋ฆฐ ๋ฐ์ดํ„ฐ ํ™•์ธ
wrong_result =[]

for n in range(0,len(y_test)):
    if predicted_labels[n] !=y_test[n]:
        wrong_result.append(n)
        
len(wrong_result) 
1114
samples = random.choices(population = wrong_result,k=16)
plt.figure(figsize=(14,12))

for idx, n in enumerate(samples):
    plt.subplot(4,4,idx+1)
    plt.imshow(x_test[n].reshape(28,28),cmap='Greys', interpolation='nearest')
    plt.title('Label:' + str(y_test[n]) + ' | predict :'+ str(predicted_labels[n]))
    plt.axis('off')
    
plt.show()

label์ข…๋ฅ˜
0ํ‹ฐ์…”์ธ 
1๋ฐ”์ง€
2์Šค์›จํ„ฐ
3๋“œ๋ ˆ์Šค
4์ฝ”ํŠธ
5์ƒŒ๋“ค
6์…”์ธ 
7์šด๋™ํ™”
8๊ฐ€๋ฐฉ
9๋ถ€์ธ 
profile
๋ฐ์ดํ„ฐ๋ถ„์„ ์Šคํ„ฐ๋””๋…ธํŠธ๐Ÿงโœ๏ธ

0๊ฐœ์˜ ๋Œ“๊ธ€