[CNN basic] MNIST ๋ฐ์ดํ„ฐ์…‹ ํ•™์Šต, ์˜ˆ์ธก

2022. 4. 19. 15:49ใ†๐Ÿงช Data Science/ML, DL

 

 

 

 

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„  ๊ฐ„๋‹จํ•˜๊ฒŒ Keras๋ฅผ ์ด์šฉํ•˜์—ฌ CNN๋ชจ๋ธ์„ ๋งŒ๋“ค๊ณ  ํ•™์Šต, ์˜ˆ์ธกํ•œ๋‹ค.

CNN์˜ ํ•™์Šต๊ณผ์ •์„ ์ „์ฒด์ ์œผ๋กœ ๋”ฐ๋ผ๊ฐ€ ๋ณด์ž.

CNN์˜ ๊ฐœ๋…์ด ์ต์ˆ™์ง€ ์•Š๋‹ค๋ฉด ์ด์ „ ํฌ์ŠคํŒ…์„ ๋ณด๊ณ  ์˜ค์ž.

 

 

 

[์ด์ „ ํฌ์ŠคํŒ…: https://mengu.tistory.com/23]

 

[CNN basic] ํ•ฉ์„ฑ๊ณฑ ์ธต, ํ’€๋ง ์ธต

CNN basic ํ•ฉ์„ฑ๊ณฑ ์ธต, ํ’€๋ง ์ธต CNN(Convolutional neural network)์ด๋ž€? ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง์€ ์‹œ๊ฐ ํ”ผ์งˆ ์•ˆ์˜ ๋‰ด๋Ÿฐ ์ž‘๋™๋ฐฉ์‹์„ ๋ณธ ๋”ฐ ๋งŒ๋“ค์–ด์ง„ ๋„คํŠธ์›Œํฌ์ด๋‹ค. ์œ„ ๊ทธ๋ฆผ์€ ์‹ค์ œ ์‹œ๊ฐํ”ผ์งˆ์˜ ๋‰ด๋Ÿฐ์ด ์ž‘๋™ํ•˜๋Š” ๋ฐฉ์‹์ด

mengu.tistory.com

 

 

 

 

MNIST ๋ฐ์ดํ„ฐ์…‹

 

 

MNIST ๋ฐ์ดํ„ฐ์…‹์€ ์†๊ธ€์”จ ๋ฐ์ดํ„ฐ ์…‹์ด๋‹ค.

ํ•ด๋‹น ํฌ์ŠคํŒ…์—์„ , ์ด๋ฏธ์ง€๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์†๊ธ€์”จ๋ฅผ 0~10๊นŒ์ง€ ๋ถ„๋ฅ˜ํ•˜๋Š” ๋ชจ๋ธ์„ ๋งŒ๋“ค ๊ฒƒ์ด๋‹ค.

์ฐจ๊ทผ์ฐจ๊ทผ ๊ฐ€๋ณด์ž.

 

 

 

 

 

1. ๋ฐ์ดํ„ฐ์…‹ ๋กœ๋“œ


(x_train_all, y_train_all), (x_test, y_test) = tf.keras.datasets.fashion_mnist.load_data()

keras๋ฅผ ์ด์šฉํ•ด ๋ฐ์ดํ„ฐ์…‹์„ ๋ถˆ๋Ÿฌ์˜จ๋‹ค. ๋ฐ์ดํ„ฐ์˜ ์ƒ๊น€์ƒˆ๋ฅผ ํ™•์ธํ•ด๋ณด์ž.

 

 

 x_train_all[0]

 

28x28 ํฌ๊ธฐ์ด๋ฉฐ, 0~255๊นŒ์ง€์˜ ๊ฐ’์ด ๋ถ„ํฌํ•ด์žˆ๋‹ค. 

 

 

y_train_all[0]

ํƒ€๊นƒ ๋ฐ์ดํ„ฐ๋Š” 0~9๊นŒ์ง€ ์กด์žฌํ•˜๋ฉฐ, 1์ฐจ์› ์ •์ˆ˜๋‹ค.

 

 

 

 

 

2. ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ ํ›ˆ๋ จ ์„ธํŠธ์™€ ๊ฒ€์ฆ ์„ธํŠธ๋กœ ๋‚˜๋ˆ„๊ธฐ


from sklearn.model_selection import train_test_split
x_train, x_val, y_train, y_val = train_test_split(x_train_all, y_train_all,
                                                  stratify=y_train_all, test_size=0.2, random_state=42)

ํ•™์Šต ๋ฐ์ดํ„ฐ์™€ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋ฅผ 80%/20% ๋น„์œจ๋กœ ๋‚˜๋ˆ ์ค€๋‹ค.

 

 

 

 

 

3. ํƒ€๊นƒ์„ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์œผ๋กœ ๋ณ€ํ™˜


y_train_encoded = tf.keras.utils.to_categorical(y_train)
y_val_encoded = tf.keras.utils.to_categorical(y_val)

์ถœ๋ ฅ ๊ฐ’์€ softmax์™€ ๊ฐ™์€ ์ถœ๋ ฅ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•œ 10์ฐจ์› ๋ฐฐ์—ด์ด ๋  ๊ฒƒ์ด๋‹ค. ์ด์— ๋งž๊ฒŒ ํƒ€๊นƒ๋„ 10์ฐจ์›์˜ ๋ฐฐ์—ด๋กœ ์ธ์ฝ”๋”ฉํ•ด์ค€๋‹ค.

 

 

y_train_encoded[0]

 

 

 

 

4. ์ž…๋ ฅ ๋ฐ์ดํ„ฐ ์ค€๋น„


x_train = x_train.reshape(-1, 28, 28, 1)
x_val = x_val.reshape(-1, 28, 28, 1)

x_train.shape

train์˜ shape ํ•ด์„:  48000๊ฐœ์˜ ์ƒ˜ํ”Œ์ด ์กด์žฌํ•˜๊ณ , 28x28 ํฌ๊ธฐ์˜, ์ฑ„๋„์ด gray ํ•˜๋‚˜๋ฟ์ธ ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ

 

 

 

 

 

5. ์ž…๋ ฅ ๋ฐ์ดํ„ฐ ํ‘œ์ค€ํ™” ์ „์ฒ˜๋ฆฌ


x_train = x_train / 255
x_val = x_val / 255

์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ๋Š” 0~255 ์‚ฌ์ด์˜ ์ •์ˆ˜๋กœ ํ”ฝ์…€ ๊ฐ•๋„๋ฅผ ํ‘œํ•œํ•œ๋‹ค. ์ž…๋ ฅ ๋ฐ์ดํ„ฐ๋ฅผ 255๋กœ ๋‚˜๋ˆ„์–ด 0~1 ์‚ฌ์ด์˜ ๊ฐ’์œผ๋กœ ์กฐ์ •ํ•œ๋‹ค.

 

 

x_train[0]

 

 

 

 

6. ๋ชจ๋ธ๋ง / ๋ชจ๋ธ ๊ตฌ์กฐ ํ™•์ธ


from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense

conv1 = tf.keras.Sequential()
conv1.add(Conv2D(10, (3,3), activation='relu', padding='same', input_shape=(28,28,1)))
conv1.add(MaxPooling2D((2,2)))
conv1.add(Flatten())
conv1.add(Dense(100, activation='relu'))
conv1.add(Dense(10, activation='softmax'))

conv1.summary()

 

 

 

7. ๋ชจ๋ธ ํ›ˆ๋ จ


conv1.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
history = conv1.fit(x_train, y_train_encoded, epochs=20, validation_data=(x_val, y_val_encoded))

 

 

 

 

 

8. ์†์‹ค ๊ทธ๋ž˜ํ”„์™€ ์ •ํ™•๋„ ๊ทธ๋ž˜ํ”„ ํ™•์ธํ•˜๊ธฐ


plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train_loss', 'val_loss'])
plt.show()

plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train_accuracy', 'val_accuracy'])
plt.show()

 

์ ์  loss๊ฐ€ ์ค„์–ด๋“ค๊ณ  accuracy๊ฐ€ ์ฆ๊ฐ€ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

CNN ๋ชจ๋ธ๋ง์ด ์ž˜ ๋˜์—ˆ์œผ๋ฉฐ, ํ•™์Šต/์˜ˆ์ธก ๋˜ํ•œ ์ž˜ ๋˜์—ˆ์Œ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

 

 

์ด๋ฒˆ ํฌ์ŠคํŒ…์€ ์—ฌ๊ธฐ๊นŒ์ง€.