ML (MachineLearning)

๋”ฅ๋Ÿฌ๋‹ : CNN(ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง, Convolutional Neural Network), ์ปฌ๋Ÿฌ์‚ฌ์ง„ ์‹๋ณ„ํ•˜๊ธฐ

567Rabbit 2024. 4. 18. 18:09

 

CNN์€ ์ปจ๋ณผ๋ฃจ์…˜ ๊ณ„์ธต(convolutional layer)๊ณผ ํ’€๋ง ๊ณ„์ธต(pooling layer)์œผ๋กœ ๊ตฌ์„ฑ๋œ ์‹ ๊ฒฝ๋ง์ด๋‹ค.

 

 

1. ์ปจ๋ณผ๋ฃจ์…˜ ๊ณ„์ธต์€ ์ž…๋ ฅ ์ด๋ฏธ์ง€์— ๋Œ€ํ•ด ํ•„ํ„ฐ(๋˜๋Š” ์ปค๋„)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€ํ™˜ํ•˜๋Š” ๊ณ„์ธต์ด๋‹ค. ์ด ํ•„ํ„ฐ๋Š” ์ž…๋ ฅ ์ด๋ฏธ์ง€์˜ ํŠน์ • ํŒจํ„ด์„ ๊ฐ์ง€ํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๊ฐ€์žฅ์ž๋ฆฌ, ์งˆ๊ฐ, ์ƒ‰์ƒ ๋“ฑ์„ ๊ฐ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

2. ํ’€๋ง ๊ณ„์ธต์€ ์ถœ๋ ฅ์˜ ๊ณต๊ฐ„ ํฌ๊ธฐ๋ฅผ ์ค„์ด๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์ตœ๋Œ€ ํ’€๋ง(max pooling)์ด๋‚˜ ํ‰๊ท  ํ’€๋ง(average pooling)์„ ์‚ฌ์šฉํ•˜์—ฌ ์ถœ๋ ฅ์„ ๊ฐ ์˜์—ญ์—์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’ ๋˜๋Š” ํ‰๊ท  ๊ฐ’์œผ๋กœ ๋Œ€์ฒดํ•œ๋‹ค. ์ด๋Š” ๊ณต๊ฐ„ ํ•ด์ƒ๋„๋ฅผ ๊ฐ์†Œ์‹œํ‚ค๊ณ , ๊ณ„์‚ฐ๋Ÿ‰์„ ์ค„์ด๋ฉฐ, ๋ถˆํ•„์š”ํ•œ ์ •๋ณด๋ฅผ ์ œ๊ฑฐํ•˜์—ฌ ํŠน์ง•์„ ๋ณด๋‹ค ๊ฐ•์กฐํ•œ๋‹ค.

 

 

 

 

CNN์€ ์ด๋Ÿฌํ•œ ์ปจ๋ณผ๋ฃจ์…˜ ๊ณ„์ธต๊ณผ ํ’€๋ง ๊ณ„์ธต์„ ์—ฌ๋Ÿฌ ์ธต์œผ๋กœ ์Œ“์•„์„œ ๊ตฌ์„ฑ๋œ๋‹ค. ์ดํ›„์—๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์™„์ „ ์—ฐ๊ฒฐ๋œ๊ณ„์ธต์œผ๋กœ ์—ฐ๊ฒฐํ•˜์—ฌ ์ตœ์ข… ๋ถ„๋ฅ˜๋‚˜ ์˜ˆ์ธก์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

 

์ด๋Ÿฌํ•œ ์•„ํ‚คํ…์ฒ˜๋Š” ์ด๋ฏธ์ง€๋‚˜ ๋น„๋””์˜ค์™€ ๊ฐ™์€ ๊ณ ์ฐจ์› ๋ฐ์ดํ„ฐ์˜ ํŠน์ง•์„ ์ถ”์ถœํ•˜๋Š” ๋ฐ ๋งค์šฐ ํšจ๊ณผ์ ์ด๋ฉฐ ์Œ์„ฑ ์ธ์‹, ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ, ๊ฒŒ์ž„ ์ธ๊ณต์ง€๋Šฅ ๋“ฑ ๋‹ค์–‘ํ•œ ๋ถ„์•ผ์—์„œ๋„ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋‹ค.

 
 
 

 

 

1. ์ปจ๋ณผ๋ฃจ์…˜ ๊ณ„์ธต

ํ•„ํ„ฐ๋Š” ์ž…๋ ฅ ์ด๋ฏธ์ง€ ์œ„๋ฅผ ์Šฌ๋ผ์ด๋”ฉํ•˜๋ฉด์„œ ๊ฐ ์œ„์น˜์—์„œ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์™€ ํ•„ํ„ฐ์˜ ์š”์†Œ ๊ฐ„์˜ ๊ณฑ์„ ๊ณ„์‚ฐํ•˜๊ณ , ์ด๋ฅผ ๋ชจ๋‘ ํ•ฉ์‚ฐํ•˜์—ฌ ์ถœ๋ ฅ๊ฐ’์„ ์ƒ์„ฑํ•œ๋‹ค
 

 

 

'relu'๋Š” 0 ์ดํ•˜์˜ ๊ฐ’๋“ค์„ ๋ชจ๋‘ 0์œผ๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค

 

 

์•„๋ž˜๋Š” ์ตœ์ข…์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ ํ•„ํ„ฐ(์ปค๋„) ์ด๋‹ค.

 

 

 

 

 

 

2. ํ’€๋ง ๊ณ„์ธต

 

์•„๋ž˜๋Š” ์ตœ๋Œ€ํ’€๋ง(max pooling)์œผ๋กœ, ํ•„ํ„ฐ๋ฅผ ๋ฐ์ดํ„ฐ์— ์ ์šฉํ•œ ํ›„, ๊ฐ„๊ฒฉ์—์„œ ๊ฐ€์žฅ ๋†’์€๊ฐ’์„ ๊ฐ€์ง€๋Š” ๊ฐ’์œผ๋กœ ๋ฐ์ดํ„ฐํ™”ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์‹ค๋ฌด์—์„œ๋Š” ๋Œ€๋ถ€๋ถ„ max pooling์„ ์‚ฌ์šฉํ•œ๋‹ค.

ํ‰๊ท  ํ’€๋ง(average pooling)์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ„๊ฒฉ์—์„œ ํ‰๊ท ๊ฐ’์„ ๊ตฌํ•ด, ๊ทธ ๊ฐ’์œผ๋กœ ๋ฐ์ดํ„ฐํ™”ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

 

 

 

 

 

 

 

๋ฐ์ดํ„ฐ์— ์ ์šฉํ•ด๋ณด๊ธฐ

import tensorflow as tf
from tensorflow.keras.datasets import fashion_mnist

#๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
(X_train, y_train),(X_test, y_test)= fashion_mnist.load_data()

 

๋ฐ์ดํ„ฐ๋Š” fashon_mnist ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋“œํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค.

 

 

<ํ”ผ์ฒ˜์Šค์ผ€์ผ๋ง>

X_train = X_train / 255.0

X_test = X_test / 255.0

 

 

<reshape>

- ์นผ๋ผ์ด๋ฏธ์ง€์ด๋“  ๊ทธ๋ ˆ์ด์Šค์ผ€์ผ ์ด๋ฏธ์ง€๋“  ์ „๋ถ€ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ์ธ๊ณต์ง€๋Šฅ ๊ฐœ๋ฐœ ์œ„ํ•ด 4์ฐจ์›์œผ๋กœ reshape ํ•œ๋‹ค.

 

X_train.shape

(60000, 28, 28)

 

X_test.shape

(10000, 28, 28)

 

 

X_train = X_train.reshape(60000, 28, 28, 1)        #1 : grayscale, 3 : colorscale(rgb์ด๋ฏ€๋กœ 3)

X_test = X_test.reshape(10000, 28, 28, 1)

 

 

 

<๋ชจ๋ธ๋งํ•˜๊ธฐ>

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense  #convolution2D

 

from typing import Sequence

def build_model():
  model = Sequential()
  model.add( Conv2D( filters=64, kernel_size=(3,3), activation='relu' , input_shape=(28,28,1) ) ) #kernel(=filter)์„ 3ํ–‰ 3์—ด๋กœ
  model.add( MaxPooling2D(pool_size=(2,2), strides=2) )
  model.add( Conv2D( filters=64, kernel_size=(2,2), activation='relu' ) )
  model.add( MaxPooling2D(pool_size=(2,2), strides=2) )
  model.add( Flatten() )                 #์ž…๋ ฅ๋ ˆ์ด์–ด
  model.add( Dense(128, 'relu') )        #์€๋‹‰๋ ˆ์ด์–ด
  model.add( Dense(10, 'softmax') )      #์ถœ๋ ฅ๋ ˆ์ด์–ด
  model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])  #์ปดํŒŒ์ผ
  return model

 

model = build_model()
from tensorflow.keras.callbacks import EarlyStopping

early_stopping = EarlyStopping(monitor='val_accuracy', patience=10)

epoch_history = model.fit(X_train, y_train, epochs=1000, validation_split=0.2, callbacks=[early_stopping])

 

 

 

<ํ‰๊ฐ€ํ•˜๊ธฐ>

model.evaluate(X_test, y_test)

 [==============================] - 1s 3ms/step - loss: 0.4803 - accuracy: 0.9096
[0.48025116324424744, 0.909600019454956]
 
 

 

 

<confusion_matrix>

from sklearn.metrics import confusion_matrix

import numpy as np

 

confusion_matrix(y_test, y_pred)

array([[843,   1,  13,  13,   7,   1, 117,   0,   5,   0],
       [  1, 980,   0,  10,   4,   0,   3,   0,   2,   0],
       [ 17,   0, 802,   8,  81,   0,  92,   0,   0,   0],
       [ 19,   3,   7, 909,  17,   0,  42,   0,   2,   1],
       [  3,   1,  20,  23, 878,   0,  75,   0,   0,   0],
       [  0,   0,   0,   0,   0, 977,   0,  12,   1,  10],
       [ 83,   1,  38,  22,  55,   0, 796,   0,   5,   0],
       [  0,   0,   0,   0,   0,   5,   0, 970,   0,  25],
       [  1,   0,   1,   6,   3,   4,   8,   4, 973,   0],
       [  0,   0,   0,   0,   0,   4,   2,  26,   0, 968]])