์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- Machine learning
- ์๊ทน์ฌ
- ์ ๋ฝ
- ๊ตํํ์
- cost function
- li-ion
- set add
- 2022๋
- special method
- ํน๋ณ ๋ฉ์๋
- gradient descent
- Deeplearning
- anaconda ๊ฐ์ํ๊ฒฝ
- ์ ๋ฝ ๊ตํํ์
- ๋ฅ๋ฌ๋
- fatigue fracture
- ์ด์ฐจ์ ์ง
- ์ ํํ๊ท
- fluent python
- set method
- Python
- m1 anaconda ์ค์น
- Linear Regression
- Andrew ng
- ๋ฏธ๋์์ ์ฅํ์
- ๋ฏธ๋์์ ํด์ธ๊ตํ
- electrochemical models
- ์ค์คํธ๋ฆฌ์
- ๋์23์ด
- ์ฒญ์ถ ํ์ดํ
- Today
- Total
Done is Better Than Perfect
[๋ฅ๋ฌ๋] 7. CNN ๋ณธ๋ฌธ
1. ์ด๋ฏธ์ง์ Convolution ์ฐ์ฐ
๊ธฐ์กด์ ๋ฅ๋ฌ๋์์ ์ฌ์ฉํ๋ Fully-connected Layer๋ 1์ฐจ์ ๋ฐ์ดํฐ (์ ํ ๋ฐ์ดํฐ)๋ฅผ input์ผ๋ก ์๊ตฌํจ
- ์ด๋ฏธ์ง๋ฅผ ๋จ์ํ๊ฒ 1์ฐจ์์ผ๋ก ๋ฐ๊พธ๋ฉด 2์ฐจ์ ์์์ ๊ฐ์ง๋ ์ ๋ณด (์ฌ๋ฌผ ๊ฐ์ ๊ฑฐ๋ฆฌ ๊ด๊ณ, ์์ ๋ณํ ๋ฑ)๋ฅผ ํฌ๊ธฐํด์ผ ํจ
- ์ฆ, ๊ณต๊ฐ ์ ๋ณด (spatial information)๊ฐ ๋ฌด๋์ง (-> FC layer๋ก ์ด๋ฏธ์ง ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ ์ ์์)
๋ฐ๋ผ์ ์ด๋ฏธ์ง ์ฒ๋ฆฌ์ ํนํ๋ ๋ฅ๋ฌ๋ ๋ชจ๋ธ ๋ฑ์ฅ -> CNN (Convolutional Neural Network)
- CNN์ ๋ํ ๊ตฌ์ฑ์์
- Convolutional Layer
- Pooling Layer
- ๋ถ๋ฅ๊ธฐ (classifier) : fully-connected layer๋ก ๊ตฌ์ฑ
[ Convolution ์ฐ์ฐ ]
- CNN์ ๊ตฌํํ๋ ํต์ฌ ์ฐ์ฐ
- ์ปค๋(kernel)๊ณผ Convolution ์ฐ์ฐ
- ์ด๋ฏธ์ง(input)์ ์ปค๋(kernel = filter) ๊ฐ์ convolution ์ฐ์ฐ์ผ๋ก ์ฒ๋ฆฌ
- 2์ฐจ์ ์ด๋ฏธ์ง ๋ฐ์ดํฐ : ํ๋ ฌ๋ก ํํ ( ํ๋ ฌ์ ๊ฐ ์์๋ ํด๋น ์์น์ ์ด๋ฏธ์ง ํฝ์ ๊ฐ )
- Convolution kernel : ํ๋ ฌ๋ก ํํ
Convolution ์ฐ์ฐ์ 2์ฐจ์ ์์์ ์ฐ์ฐ์ด ์ด๋ฃจ์ด์ง๋ฏ๋ก ์ด๋ฏธ์ง ๋ฐ์ดํฐ๋ฅผ ๋ณํ์์ด ๊ทธ๋๋ก ์ฌ์ฉ ๊ฐ๋ฅ
convolution ์ฐ์ฐ ๊ณผ์
- ํ๋ ฌ ๋ด๋ถ์ ์์๋ค์ ์์ ๋ณ๋ก(element-wise) ๊ณฑํด์ ๋ํจ -> ์ ํ ์ฐ์ฐ
- kernel์ ์ด๋ฏธ์ง ์์ญ ๋ด์์ convolution ์ฐ์ฐ ์ํ
- ์ฐ์ฐ ๊ณผ์ : ์ปค๋์ด ์ด๋ฏธ์ง์ ๋ ธ๋์ ์์ญ์ ๊ฒน์ณ์ง
[ Feature Map (Activation Map) ]
- Convolution ์ฐ์ฐ ๊ฒฐ๊ณผ : Feature Map ๋๋ Activation Map ์ด๋ผ ๋ถ๋ฆ ( Feature Map : ์ด๋ฏธ์ง์ ํน์ง ์ถ์ถ )
- ์ปค๋๊ณผ ์ด๋ฏธ์ง๊ฐ ๊ฒน์น๋ ์์ญ : ์์ฉ ์์ญ(Receptive Field)
[ ์ปฌ๋ฌ ์ด๋ฏธ์ง์ convolution ์ฐ์ฐ ]
- ์์ ์์๋ ์ด๋ฏธ์ง์ ์ฑ๋์ด 1๊ฐ → ํ๋ฐฑ ์ด๋ฏธ์ง
- ์ปฌ๋ฌ ์ด๋ฏธ์ง๋ ์ฑ๋์ด 3๊ฐ → ์ปค๋๋ ์ฑ๋์ 3๊ฐ๋ก ์ค๋น
- ๊ฐ ์ฑ๋ ๋ณ๋ก Convolution ์ฐ์ฐ์ ์ํํ๊ณ ๊ฐ ๊ฒฐ๊ณผ(Feature Map)๋ฅผ ์์๋ณ๋ก(element-wise) ๋ํด ํ๋์ Feature Map์ ์์ฑ
[ Convolution ์ฐ์ฐ ํ์ฅ ]
- ์ปค๋์ ์ฌ๋ฌ๊ฐ ๋๋ฉด Feature Map๋ ์ฌ๋ฌ๊ฐ ์์ฑ
- ๋ ธ๋์ ํ๋ ฌ : Filter 1(kernel 1)์ ์ฌ์ฉํ feature map (3๊ฐ์ feature map์ ๋ํด ํ๋์ feature map ํ์ฑ)
- ์ฃผํฉ์ ํ๋ ฌ : Filter 2(kernel 2)์ ์ฌ์ฉํ feature map
- output : ์ด 2๊ฐ์ ์ฑ๋์ ๊ฐ์ง๋ feature map ์์ฑ
2. Convolutional Neural Network
- ์ง๊ธ๊น์ง ์ฌ์ฉํ ์ปค๋๋ค์ ํ์ต ๊ฐ๋ฅํ ์ปค๋ -> ์ฆ, ์ปค๋ ํ๋ ฌ์ ๊ฐ ๊ฐ๋ค์ด ๊ฐ์ค์น(Weight)
- ์ด๋ฌํ ์ปค๋๋ค๋ก ์ด๋ฃจ์ด์ง Layer๋ฅผ Convolutional Layer๋ผ๊ณ ๋ถ๋ฆ
- ์ด Layer๋ค์ ์์์ CNN์ ๊ตฌ์ฑ
- ์ฒซ๋ฒ์งธ convolutional layer : ๋นจ๊ฐ์ feature map๊ณผ ํ๋์ feature map ์ฌ์ด layer
( 6๊ฐ์ kernel์ ์ฌ์ฉํ์ฌ ๋๋ฒ์งธ ํ๋์ feature map์ด 6๊ฐ์ channel์ ๊ฐ์ง ) - ๋๋ฒ์งธ convolutional layer : 10๊ฐ์ kernel์ ์ฌ์ฉํ์ฌ ์ด๋ก์ feature map์ด 10๊ฐ์ channel์ ๊ฐ์ง
- ํ๋์ convolutional layer๋ ์ฌ๋ฌ ๊ฐ์ channel๋ก ๊ตฌ์ฑ๋ ์ ์์ -> ์ด layer๋ฅผ ์์ CNN ๊ตฌ์ฑ
Convolutional Layer
- kernel์ ์ด์ฉํ์ฌ ์ด๋ฏธ์ง์์ feature๋ฅผ ์ถ์ถํ๋ layer
- convolutional layer์์ ์กฐ์ ํ ์ ์๋ hyperparameter : ์ปค๋์ ๊ฐ์, ์ปค๋์ ํฌ๊ธฐ, stride ๋ฑ
[ Layer์ ์ญํ ]
- ์ด๋ฏธ์ง๊ฐ ๊ฐ์ง๋ ํน์ Feature๋ฅผ ๋ฝ์๋ด๋๋ก ์ปค๋์ ํ์ต
- ์ปค๋์ ๋ฐ๋ผ ์ถ์ถํ๋ Feature๋ฅผ ๋ค๋ฅด๊ฒ ํ์ต (Feture ์์ : ์ด๋ฏธ์ง๋ด์ ๋๊ฐ์ , ์ํ, ์์กฐ ๋ฑ๋ฑ)
- ๊ทธ๋ฆผ์์ ํ๋ ฌ์ ํ์นธ = Feature Map ํ๋์ ๊ฒฐ๊ณผ, ์ฆ ์ด 64๊ฐ์ chennel์ด ์์
Convolution ์ฐ์ฐ๊ณผ์ ์ ์กฐ์ ํ๊ธฐ ์ํ Hyper parameter => stride, padding
[ Stride ]
- ์ปค๋์ด ์ด๋ฏธ์ง ๋ด์์ ์ด๋ํ๋ ์นธ ์๋ฅผ ์กฐ์
- ์์ Convolution ์ฐ์ฐ์์ ๋ณด์ฌ์ค ์์๋ ๋ชจ๋ 1์นธ ( ์๋ ๊ทธ๋ฆผ์ Stride๊ฐ 2์นธ์ผ ๊ฒฝ์ฐ ์์ )
[ Padding ]
- ์์ ์์๋ Convolution ์ฐ์ฐ ๊ฒฐ๊ณผ Feature Map ์ฌ์ด์ฆ๊ฐ ๊ณ์ ์ค์ด๋ฆ
- Padding์ ์ถ๊ฐํ์ฌ Feature Map ์ฌ์ด์ฆ๊ฐ ์ค์ด๋๋ ํ์ ๋ฐฉ์ง
- ์ด๋ฏธ์ง์ ํ ๋๋ฆฌ ์ ๋ณด๋ ๊ท ์ผํ๊ฒ ํ์ฉ ๊ฐ๋ฅ
- ์ฃผ๋ก padding ์ ๋ค์ด๊ฐ๋ ๊ฐ์ 0์ด๋ฉฐ, ์ด๋ฅผ zero padding์ด๋ผ ๋ถ๋ฆ
[ Convolutional Layer ์์ ]
- ์ ์ด๋ฏธ์ง ํน์ง์ ์ ๋ฝ์๋ด๋๊ฐ?
- Convolution ์ฐ์ฐ์ ํ๋์ ์ปค๋์ด ํฝ์ ๊ฐ์ ์ ๋ณด๋ฅผ ๋ณด๊ฒ ๋ง๋ฆ
- ์ฆ, ํ๋์ ์ปค๋์ด ์ด๋ฏธ์ง ์ ์ฒด ์์ญ์ ํ์ต
- Parameter Sharing
- ์ปค๋์ด ๊ฐ์ง Parameter๋ฅผ ์ด๋ฏธ์ง์ ๋ชจ๋ ์์ญ์์ ๊ณต์
- Parameter ๊ฐ์๋ฅผ FC Layer์ ๋นํด ๊ทน์ ์ผ๋ก ์ค์ → ๊ณผ์ ํฉ๋ฐฉ์ง์์ ๋ฆฌ
- FC layer์ ๊ฒฝ์ฐ) ์ด๋ฏธ์ง 1*64, ์ปค๋ 1*9 ๋ผ๋ฉด, ์ด 64*9๊ฐ์ ๊ฐ์ค์น ํ์
- Convolution Layer์ ๊ฒฝ์ฐ) ์ด๋ฏธ์ง 8*8, ์ปค๋ 3*3 ๋ผ๋ฉด, ์ด 9๊ฐ(kernel ํ๋ ฌ์ ๋ด๋ถ ๊ฐ)์ ๊ฐ์ค์น ํ์
[Convolutional Layer ํ์ฑํ ํจ์ ]
- Convolution ์ฐ์ฐ์ ์ ํ์ฐ์ฐ ( ๋ชจ๋ ๊ณฑ์ ๊ณผ ๋ง์ ์ผ๋ก๋ง ์ด๋ฃจ์ด์ง )
- ๋ฐ๋ผ์, FC Layer์ฒ๋ผ ๋น์ ํ์ฑ์ ์ถ๊ฐํ๊ธฐ ์ํด ํ์ฑํ ํจ์ ์ฌ์ฉ (CNN์ ์ฃผ๋ก ReLUํจ์ ์ฌ์ฉ)
[ Pooling Layer ]
- CNN์์ ๊ฑฐ์ ํญ์ ๊ฐ์ด ์ฐ์ด๋ Layer
- ์ฑ๋ ๋ณ๋ก ์ฐ์ฐ
- Convolution ์ฐ์ฐ๊ณผ๋ ๋ค๋ฅด๊ฒ, ๊ฐ ์ฑ๋ ๋ณ๋ก ์ํ๋ ์ฐ์ฐ์ ๋ํ์ง ์์
- ์ฃผ์ญํ : Feature Map์ ์ฌ์ด์ฆ๋ฅผ ์ค์ฌ Parameter๊ฐ์๋ฅผ ์ค์ด๋ ๊ฒ → ๊ณผ์ ํฉ ์กฐ์
- Max Pooling
- ์ฃผ์ด์ง ์ด๋ฏธ์ง๋ Feature Map์ ๊ฒน์น์ง ์๋ ๋ ์์ ์์ญ์ผ๋ก๋ถํ
- ์ ๊ทธ๋ฆผ์ ๊ฐ ์์ญ์ ํฌ๊ธฐ๊ฐ 2x2๊ฐ ๋๋๋ก ๋ถํ
- ๊ฐ ์์ญ์์ ์ต๋๊ฐ์ ๋ฝ์๋ด์ด ์๋ก์ด Feature Map์ ๊ตฌ์ฑ
- Average Pooling
- Max Pooling๊ณผ ๊ฑฐ์ ๋์ผํ๋, ๊ฐ ์์ญ์ ํ๊ท ๊ฐ์ ๊ณ์ฐํ์ฌ ์๋ก์ด Feature Map์ ๊ตฌ์ฑ
[ Pooling Layer ์ ๋ฆฌ ]
- ์ผ๋ฐ์ ์ผ๋ก Max Pooling์ ๋ง์ด ์ฌ์ฉ ( Feature Map์ ์กด์ฌํ๋ Feature ์ค ๊ฐ์ฅ ์ํฅ๋ ฅ์ด ํฐ Feature๋ง ์ฌ์ฉ )
- Feature Map์ ์ฑ๋์ด ์ฌ๋ฌ ๊ฐ๋ฉด ๊ฐ ์ฑ๋๋ณ๋ก Pooling ์ฐ์ฐ ์ํ (pooling์ ๊ฒฐ๊ณผ ์ฑ๋๋ณ๋ก ํฉ์น์ง ์์)
- ์ถ๊ฐ Pooling Layer
- Global Average Pooling: ์ ์ฒด Feature Map์์ ํ๋์ ํ๊ท ๊ฐ ๊ณ์ฐ
- Global Max Pooling: ์ ์ฒด Feature Map์์ ํ๋์ ์ต๋๊ฐ์ ๊ณ์ฐ
- ์ฌ๊ธฐ์ Global Average Pooling์ ๋ง์ด ์ฌ์ฉ (๊ทธ๋ฆผ์ pooling์ global average pooling)
[ Classifier ]
- CNN์ ์ผ๋ฐ์ ์ผ๋ก ์ด๋ฏธ์ง ๋ถ๋ฅ ๋ชฉ์ ์ผ๋ก ์ฌ์ฉ
- Feature Map์ Fully-connected Layer์ ํต๊ณผ์์ผ ๋ถ๋ฅ ์ํ
- ์ด๋ฅผ ์ํด Feature Map์ 1์ฐจ์์ผ๋ก ๋ณํ
- 1์ฐจ์์ผ๋ก ๋ณํํ๋ ๋ฐฉ๋ฒ :
- feature map์ ๋จ์ํ flatten ํจ
- global average pooling ๋ฑ์ ๋ฐฉ๋ฒ ์ด์ฉํจ (channel์ ๊ฐ์ = ๋ฒกํฐ์ ๊ธธ์ด)
- 1์ฐจ์์ผ๋ก ๋ณํํ๋ ๋ฐฉ๋ฒ :
[ Tensorflow ๋ก Convolution Layer ๊ตฌํ ]
import tensorflow as tf
from tensorflow import keras
''' ๋ฐฉ๋ฒ 1 : Tensorflow๋ก conv2d ์ฌ์ฉ '''
# input : 1๋ก ๊ตฌ์ฑ๋ 3x3 ํฌ๊ธฐ์ ๊ฐ๋จํ ํ๋ ฌ (3x3 x1 ์ด๋ฏธ์ง๊ฐ 1๊ฐ)
inp = tf.ones((1, 3, 3, 1))
# Filter : 1๋ก ๊ฐ๋์ฐฌ 2x2์ ํฌ๊ธฐ๋ฅผ ๊ฐ์ง ํ๋ ฌ
filter = tf.ones((2, 2, 1, 1))
# stride : [๋์ด, ๋๋น]์ ํ์์ผ๋ก ์
๋ ฅ - 1์นธ์ฉ ์ด๋ํ๋๋ก 1, 1์ ์
๋ ฅ
stride = [1, 1] # [๋์ด, ๋๋น]
# ์ค๋น๋ ์
๋ ฅ๊ฐ, filter, stride๋ก Convolution ์ฐ์ฐ ์ํ (padding์ 'VALID'์ผ๋ก ์ค์ = ํจ๋ฉ์ ํ์ง ์์)
output = tf.nn.conv2d(inp, filter, stride, padding = 'VALID')
print(output)
# [[ [[4.] [4.]]
# [[4.] [4.]] ]], shape=(1, 2, 2, 1), dtype=float32)
## ๊ฒฐ๊ณผ : Padding์ด ์๋ ์ํ์์ Convolution์ ์ํํ๋ ์
๋ ฅ์ ํฌ๊ธฐ(3x3)๋ณด๋ค ์ถ๋ ฅ์ ํฌ๊ธฐ(2x2)๊ฐ ์์์ง
# padding์ต์
์ 'VALID'๊ฐ ์๋ 'SAME'์ผ๋ก ์ค์ (์
๋ ฅ๊ณผ ์ถ๋ ฅ์ ํํ๊ฐ ๊ฐ๋๋ก ํจ๋ฉ์ ์ ์ฉ)
output = tf.nn.conv2d(inp, filter, stride, padding = 'SAME')
print(output)
# [[ [[4.] [4.] [2.]]
# [[4.] [4.] [2.]]
# [[2.] [2.] [1.]] ]], shape=(1, 3, 3, 1), dtype=float32)
## ๊ฒฐ๊ณผ : Convolution Layer์์ padding์ 'SAME'์ผ๋ก ์ค์ ํ๋ฉด ์ฌ๋ฌ๋ฒ ์ฐ์ฐํด๋ ํฌ๊ธฐ๋ ์ค์ด๋ค์ง ์์
''' padding์ ์ง์ ์ค์ ํด์ ์ ๋ฌ '''
# ์,์๋,์ค๋ฅธ์ชฝ,์ผ์ชฝ์ padding์ ๊ฐ๊ฐ ํ ์นธ์ฉ ์ถ๊ฐ
padding = [[0, 0], [1, 1], [1, 1], [0, 0]] # [[0, 0], [pad_top, pad_bottom], [pad_left, pad_right], [0, 0]]
output1 = tf.nn.conv2d(inp, filter, stride, padding = padding)
print(output1)
# [[ [[1.] [2.] [2.] [1.]]
# [[2.] [4.] [4.] [2.]]
# [[2.] [4.] [4.] [2.]]
# [[1.] [2.] [2.] [1.]] ]]
''' ๋ฐฉ๋ฒ 1 : Tensorflow.Keras๋ก Conv2D ์ฌ์ฉ '''
input_shape=(1, 3, 3, 1)
x = tf.ones(input_shape) # 3x3 x1 ์ด๋ฏธ์ง๊ฐ 1๊ฐ (1, ๋์ด, ๋๋น, 1)
print(x)
y = tf.keras.layers.Conv2D( filters = 1, # ํํฐ์ ๊ฐฏ์
kernel_size = [2, 2], # "kernel_size = 2" ์ ๊ฐ์ ์๋ฏธ (๋์ด, ๋๋น)
strides = (1, 1),
padding = 'same', # keras.layers.Conv2D ์ padding์ ์๋ฌธ์ 'same', 'valid'
activation = 'relu',
input_shape = input_shape[1:]) (x) # ์
๋ ฅ : x
print(y)
# [[ [[0.36910588] [0.36910588] [0.54728895]]
# [[0.36910588] [0.36910588] [0.54728895]]
# [[0.8551657 ] [0.8551657 ] [0.6025906 ]] ]], shape=(1, 3, 3, 1), dtype=float32)
[ Fully-connected Layer๋ฅผ ์์ ๋ง๋ Multilayer Perceptron(MLP) ๋ชจ๋ธ vs CNN ๋ชจ๋ธ ๋น๊ต ]
- ์ด๋ฏธ์ง ๋ถ๋ฅ ๋ชจ๋ธ ๊ตฌํ
- ๋ฐ์ดํฐ์
: CIFAR-10 ๋ฐ์ดํฐ์
- ๊ฐ ๋ฐ์ดํฐ๊ฐ 32×32์ ํฌ๊ธฐ๋ฅผ ๊ฐ์ง๋ ์ปฌ๋ฌ ์ด๋ฏธ์ง๋ก ๊ตฌ์ฑ
- ๋นํ๊ธฐ, ์๋์ฐจ, ์ ๋ฑ์ 10๊ฐ์ ํด๋์ค์ ์ํจ
- ํ์ต(Train) ๋ฐ์ดํฐ์ ์ 50000๊ฐ, ํ ์คํธ(Test) ๋ฐ์ดํฐ์ ์ 10000๊ฐ์ ์ด๋ฏธ์ง๊ฐ ํฌํจ - ์๋ ์ฝ๋์์๋
import tensorflow as tf
from tensorflow.keras import layers, Sequential, Input
from tensorflow.keras.optimizers import Adam
import numpy as np
import matplotlib.pyplot as plt
SEED = 42
def load_cifar10_dataset():
train_X = np.load("./dataset/cifar10_train_X.npy")
train_y = np.load("./dataset/cifar10_train_y.npy")
test_X = np.load("./dataset/cifar10_test_X.npy")
test_y = np.load("./dataset/cifar10_test_y.npy")
train_X, test_X = train_X / 255.0, test_X / 255.0
return train_X, train_y, test_X, test_y
''' MLP ๋ชจ๋ธ '''
def build_mlp_model(img_shape, num_classes=10):
model = Sequential()
model.add(Input(shape=img_shape))
model.add(layers.Flatten()) # 2์ฐจ์ ์ด๋ฏธ์ง -> 1์ฐจ์
model.add(layers.Dense(units=4096, activation='relu'))
model.add(layers.Dense(units=1024, activation='relu'))
model.add(layers.Dense(units=256, activation='relu'))
model.add(layers.Dense(units=64, activation='relu'))
model.add(layers.Dense(units=num_classes, activation='softmax'))
return model
''' CNN ๋ชจ๋ธ '''
def build_cnn_model(img_shape, num_classes=10):
model = Sequential()
model.add(layers.Conv2D(filters=16, kernel_size=(3,3), padding='same', activation='relu', input_shape = img_shape)) # convolution layer๋ ์ฒ์์ input_shape ์ง์ ํด์ผ ํจ
model.add(layers.Conv2D(filters=32, kernel_size=(3,3), padding='same',activation='relu'))
model.add(layers.MaxPool2D(pool_size=(2,2), strides=(2,2))) # pooling : ์ด๋ฏธ์ง ์ฌ์ด์ฆ๊ฐ 2๋ฐฐ๋ก ์ค๋๋ก ์ค์
model.add(layers.Conv2D(filters=64, kernel_size=(3,3), padding='same', strides=(2,2),activation='relu')) # strides=(2,2)์ด๋ฏ๋ก feature map๊ฐ ๊ฐ๋ก,์ธ๋ก๋ก 2๋ฐฐ์ฉ ์ค์ด๋ค์ - maxpooling๊ณผ ๊ฐ์ ํจ๊ณผ
model.add(layers.Conv2D(filters=64, kernel_size=(3,3), padding='same', strides=(2,2),activation='relu'))
model.add(layers.MaxPool2D(pool_size=(2,2), strides=(2,2)))
model.add(layers.Flatten())
model.add(layers.Dense(units=128, activation='relu')) # fully connected layer ์ฌ์ฉ
model.add(layers.Dense(units=num_classes, activation='softmax'))
return model
def plot_history(hist):
train_loss = hist.history["loss"]
train_acc = hist.history["accuracy"]
valid_loss = hist.history["val_loss"]
valid_acc = hist.history["val_accuracy"]
fig = plt.figure(figsize=(8, 6))
plt.plot(train_loss)
plt.plot(valid_loss)
plt.title('Loss')
plt.xlabel('epoch')
plt.ylabel('loss')
plt.legend(['Train', 'Valid'], loc='upper right')
plt.savefig("loss.png")
fig = plt.figure(figsize=(8, 6))
plt.plot(train_acc)
plt.plot(valid_acc)
plt.title('Accuracy')
plt.xlabel('epoch')
plt.ylabel('accuracy')
plt.legend(['Train', 'Valid'], loc='upper left')
plt.savefig("accuracy.png")
def run_model(model, train_X, train_y, test_X, test_y, epochs=10):
optimizer = Adam(learning_rate=0.001)
model.summary()
model.compile(loss='sparse_categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
hist = model.fit(train_X, train_y, epochs=epochs, batch_size=64, validation_split=0.2, shuffle=True, verbose=2)
plot_history(hist)
test_loss, test_acc = model.evaluate(test_X, test_y)
print("Test Loss: {:.5f}, Test Accuracy: {:.3f}%".format(test_loss, test_acc * 100))
return optimizer, hist
def main():
tf.random.set_seed(SEED)
np.random.seed(SEED)
train_X, train_y, test_X, test_y = load_cifar10_dataset()
img_shape = train_X[0].shape
mlp_model = build_mlp_model(img_shape)
cnn_model = build_cnn_model(img_shape)
print("=" * 30, "MLP ๋ชจ๋ธ", "=" * 30)
run_model(mlp_model, train_X, train_y, test_X, test_y)
print()
print("=" * 30, "CNN ๋ชจ๋ธ", "=" * 30)
run_model(cnn_model, train_X, train_y, test_X, test_y)
if __name__ == "__main__":
main()
[ ์ฝ๋ ๊ฒฐ๊ณผ ํด์ ]
## MLP ๋ชจ๋ธ
Test Loss: 1.87062, Test Accuracy: 34.200%
## CNN ๋ชจ๋ธ
Test Loss: 1.35545, Test Accuracy: 50.800%
- loss์ accuracy๋ฅผ ๋น๊ตํ์ ๋, CNN ๋ชจ๋ธ์ด MLP ๋ชจ๋ธ๋ณด๋ค ์ฑ๋ฅ์ด ์ข์
- Trainable params = ์ค์ ๋ชจ๋ธ ํ์ต์ ์ฌ์ฉ๋๋ ํ๋ผ๋ฏธํฐ์ ๊ฐ์
- MLP ๋ชจ๋ธ์์ ํ์ํ parameter์ ๊ฐ์(17,061,834)๋ณด๋ค CNN ๋ชจ๋ธ์์ ํ์ํ parameter์ ๊ฐ์(94,698)ํ์ ํ ์์
3. ๋ํ์ ์ธ CNN ๋ชจ๋ธ
- LeNet (1990)
- ์ฐํธ๋ฒํธ ์ธ์์ ์ํ ๋ชจ๋ธ
- subsampling์ pooling๊ณผ ๋์ผํ ์ญํ ์ํ
- ๋ง์ง๋ง์ full connection (FC layer)๋ฅผ ํตํด ๋ถ๋ฅ๊ธฐ ํ์ฑ
- AlexNet (2012)
- 2012๋ ImageNet Challenge ์ฐ์น → ๊ธฐ์กด ๋ชจ๋ธ์ ์ฑ๋ฅ์ ํฐํญ์ผ๋ก ์ํ
- ReLU ํ์ฑํ ํจ์ ์๊ฐ
- ๋ฅ๋ฌ๋ ๋ชจ๋ธ ํ์ต์ GPU๋ฅผ ํ์ฉ → ์ดํ๋ก ๋๋ถ๋ถ์ ๋ฅ๋ฌ๋ ๋ชจ๋ธ์ GPU๋ก ํ์ต
- ๋น์ GPU์ ํ๊ณ๋ก, 2๊ฐ์ GPU๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ ๋ชจ๋ธ๋ก 2๊ฐ๋ก ๋๋ ์ ํ์ตํจ
- VGGNet (2014)
- ์ปค๋ ์ฌ์ด์ฆ๋ฅผ ๋ชจ๋ 3x3์ผ๋ก ํต์ผ
- Parameter์ ์ฆ๊ฐ๋ฅผ ์ต์ ํ๋ฉด์ ๋ชจ๋ธ ์ธต์ ๋ ๋ง์ด ์์ ์ ์๊ฒ ๋จ
- ์ธต์ด ๋ง์์๋ก(์ฆ,๋ชจ๋ธ์ด ๊น์์๋ก)์ผ๋ฐ์ ์ผ๋ก ์ฑ๋ฅ์ด ํฅ์๋จ
[ 16๊ฐ์ layer๋ก ์ด๋ฃจ์ด์ง VGGNet, VGG-16 ๊ตฌํ ]
- VGGNet๋ถํฐ๋ Layer ๊ฐ์๊ฐ ๋ง์ด ๋์ด๋จ์ ๋ฐ๋ผ Block ๋จ์๋ก ๋ชจ๋ธ์ ๊ตฌ์ฑ.
- ๊ฐ Block์ 2๊ฐ ํน์ 3๊ฐ์ Convolutional Layer์ Max Pooling Layer๋ก ๊ตฌ์ฑ.
- parameter๊ฐ ์กด์ฌํ๋ layer๋ง layer ๊ฐ์๋ฅผ ์ - pooling, flatten layer๋ layer ๊ฐ์์ ํฌํจ X
- trainable params๊ฐ 138,357,544 ๊ฐ -> ๋ฅ๋ฌ๋ ์ธต์ด ๊น์ด์ง์๋ก ํ์ํ parameter ๊ฐ์ ๊ธฐํ๊ธ์์ ์ผ๋ก ์ฆ๊ฐ
import tensorflow as tf
from tensorflow.keras import Sequential, layers
def build_vgg16():
model = Sequential()
# ์ฒซ๋ฒ์งธ Block
model.add(layers.Conv2D(filters=64, kernel_size=(3,3), padding='same', activation='relu', input_shape=(224, 224, 3)))
model.add(layers.Conv2D(filters=64, kernel_size=(3,3), padding='same', activation='relu'))
model.add(layers.MaxPooling2D(2)) # pooling : ์ด๋ฏธ์ง ์ฌ์ด์ฆ๊ฐ 2๋ฐฐ๋ก ์ค๋๋ก ์ค์
# ๋๋ฒ์งธ Block
model.add(layers.Conv2D(filters=128, kernel_size=(3,3), padding='same', activation='relu'))
model.add(layers.Conv2D(filters=128, kernel_size=(3,3), padding='same', activation='relu'))
model.add(layers.MaxPooling2D(2))
# ์ธ๋ฒ์งธ Block
model.add(layers.Conv2D(filters=256, kernel_size=(3,3), padding='same', activation='relu'))
model.add(layers.Conv2D(filters=256, kernel_size=(3,3), padding='same', activation='relu'))
model.add(layers.Conv2D(filters=256, kernel_size=(3,3), padding='same', activation='relu'))
model.add(layers.MaxPooling2D(2))
# ๋ค๋ฒ์งธ Block
model.add(layers.Conv2D(filters=512, kernel_size=(3,3), padding='same', activation='relu'))
model.add(layers.Conv2D(filters=512, kernel_size=(3,3), padding='same', activation='relu'))
model.add(layers.Conv2D(filters=512, kernel_size=(3,3), padding='same', activation='relu'))
model.add(layers.MaxPooling2D(2))
# ๋ค์ฏ๋ฒ์งธ Block
model.add(layers.Conv2D(filters=512, kernel_size=(3,3), padding='same', activation='relu'))
model.add(layers.Conv2D(filters=512, kernel_size=(3,3), padding='same', activation='relu'))
model.add(layers.Conv2D(filters=512, kernel_size=(3,3), padding='same', activation='relu'))
model.add(layers.MaxPooling2D(2))
# Fully Connected Layer
model.add(layers.Flatten())
model.add(layers.Dense(4096, activation="relu"))
model.add(layers.Dense(4096, activation="relu"))
model.add(layers.Dense(1000, activation="softmax"))
return model
def main():
model = build_vgg16()
model.summary()
if __name__ == "__main__":
main()
- ResNet (2015)
- Layer ๊ฐ์๋ฅผ ์ต๋ 152๊ฐ๊น์ง ๋๋ฆผ
- ๊น์ ๋ชจ๋ธ์์ ํ์ฐ์ ์ผ๋ก ๋ํ๋๋ ํ์ : Vanishing Gradient
- Vanishing Gradient (๊ธฐ์ธ๊ธฐ์์ค)
- ์ญ์ ํ ๊ณผ์ ์์ ๊ธฐ์ธ๊ธฐ ๊ฐ์ด ์ ์ ์์์ง๋ค 0์ ์๋ ดํ๋ฉด์ ๋ฐ์
- ๋ชจ๋ธ ํ์ต์ ์ค๋ ์๊ฐ์ด ๊ฑธ๋ฆฌ๊ฑฐ๋ ์์ ํ์ต์ด ๋ฉ์ถ๊ฒ ๋จ
- ์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด residual connection ๊ตฌ์กฐ๊ฐ ์ถ๊ฐ๋จ
- Residual connection : vanishing gradient ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํ ๊ตฌ์กฐ
- residual connection์ ์ฌ์ฉํ๋ ค layer ๊ฐ์๋ฅผ ๊ทน์ ์ผ๋ก ๋๋ฆผ
- ๊ธฐ์กด convolutional layer๋ค์ ์ฐํํ๋ ์ฐ๊ฒฐ
- ์ ๋ ฅ Feature Map(x)์ด ์ฐํ๋ก๋ฅผ ํต๊ณผํ์ฌ Convolutioinal Layer์ Feature Map( $ F(x) $ )๊ณผ ๋ํด์ง
- ๊ธฐ์ธ๊ธฐ ๊ฐ์ด ํญ์ 1 ์ด์์ด ๋์ด ๊ธฐ์ธ๊ธฐ ์์ค ๋ฌธ์ ๋ฐฉ์ง
- ๊ทธ๋ฆผ์ weight layer๊ฐ convolution layer์ ๋์ผํ ์ญํ
- pooling layer๋ ๊ฐ์ค์น ๊ฐ์์ ์ํฅ์ ์ฃผ์ง ์์ผ๋ฏ๋ก, ๊ทธ๋ฆผ์์๋ ์๋ตํ์
[ ResNet ๊ตฌํ ]
- Residual Connection์ ๋ณดํต ResNet์ ๊ฐ Block ๋จ์๋ก ๊ตฌํ. ๋ฐ๋ผ์ ์ผ๋ฐ์ ์ผ๋ก Residual Connection์ ๊ฐ์ง๋ ๋ถ๋ถ์ Residual Block์ด๋ผ ํ์ฌ Block ๋จ์๋ก ๊ตฌํํ ํ์ ์ด๋ค์ ์ฐ๊ฒฐํ๋ ์์ผ๋ก ๋ชจ๋ํ ํ์ฌ ์ ์ฒด ๋ชจ๋ธ ๊ตฌํ
import tensorflow as tf
from tensorflow.keras import layers, Model, Sequential
''' Residual Block ๋ชจ๋ '''
class ResidualBlock(Model):
def __init__(self, num_kernels, kernel_size):
super(ResidualBlock, self).__init__()
# 2๊ฐ์ Conv2D Layer
self.conv1 = layers.Conv2D(filters=num_kernels, kernel_size=kernel_size, padding='same',activation='relu')
self.conv2 = layers.Conv2D(filters=num_kernels, kernel_size=kernel_size, padding='same',activation='relu')
# Relu Layer : ํ์ฑํ ํจ์๋ layerํ์์ผ๋ก ์ทจ๊ธ ๊ฐ๋ฅ
self.relu = layers.Activation("relu")
# Add Layer : ๋๊ฐ์ ํ
์๋ฅผ ๋ํ๋ layer
self.add = layers.Add()
def call(self, input_tensor):
x = self.conv1(input_tensor) # 1๋ฒ์ convolution layer ๊ฒฐ๊ณผ
x = self.conv2(x) # 2๋ฒ์ convolution layer ๊ฒฐ๊ณผ
x = self.add([x, input_tensor]) # ๋ ๊ฐ์ ๋ํ๋ ๊ณผ์ (original + 2๊ฐ์ convolution ๊ฒฐ๊ณผ)
x = self.relu(x) # relu layer ๊ตฌํ
return x
def build_resnet(input_shape, num_classes):
model = Sequential()
model.add(layers.Conv2D(64, kernel_size=(3, 3), padding="same", activation="relu", input_shape=input_shape))
model.add(layers.MaxPool2D(2))
model.add(ResidualBlock(64, (3, 3)))
model.add(ResidualBlock(64, (3, 3)))
model.add(ResidualBlock(64, (3, 3)))
model.add(layers.GlobalAveragePooling2D())
model.add(layers.Dense(num_classes, activation="softmax"))
return model
def main():
input_shape = (32, 32, 3)
num_classes = 10
model = build_resnet(input_shape, num_classes)
model.summary()
if __name__=="__main__":
main()
์ง๊ธ๊น์ง ๋์จ ๋ชจ๋ธ์ ๋ชจ๋ ๋ถ๋ฅ ๋ชจ๋ธ์. ๋ถ๋ฅ ์์ ์ด ์๋ ๊ฒฝ์ฐ์ ์ฌ์ฉํ๋ ๋ชจ๋ธ์?
- ์ผ๋ฐ์ ์ผ๋ก ๋ถ๋ฅ ๋ชจ๋ธ๊ณผ ์ ์ฌํ๊ฒ CNN ๊ตฌ์ฑ
- but, ๋ชจ๋ธ์ ์ถ๋ ฅ๊ฐ, ์์คํจ์, ๋ฐ์ดํฐ์ ๊ตฌ์ฑ ๋ฑ์ด ์์ ํ ๋ค๋ฅด๊ฒ ์ด๋ฃจ์ด์ง
- ์) YOLO(๊ฐ์ฒด ์ธ์), R-CNN(๊ฐ์ฒด ์ธ์), U-Net(์ด๋ฏธ์ง segmentation) ๋ฑ
'๐ค AI > Deep Learning' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฅ๋ฌ๋] 9. LSTM, GRU (1) | 2024.07.10 |
---|---|
[๋ฅ๋ฌ๋] 8. RNN (1) | 2024.07.01 |
[๋ฅ๋ฌ๋] 6. ๋ฅ๋ฌ๋ ๋ชจ๋ธ ํ์ต์ ๋ฌธ์ ์ pt.3 : ๊ณผ์ ํฉ (0) | 2024.06.22 |
[๋ฅ๋ฌ๋] 5. ๋ฅ๋ฌ๋ ๋ชจ๋ธ ํ์ต์ ๋ฌธ์ ์ pt.2 : ๊ธฐ์ธ๊ธฐ ์์ค, ๊ฐ์ค์น ์ด๊ธฐํ ๋ฐฉ๋ฒ (2) | 2024.06.12 |
[๋ฅ๋ฌ๋] 4. ๋ฅ๋ฌ๋ ๋ชจ๋ธ ํ์ต์ ๋ฌธ์ ์ pt.1 : ์ต์ ํ ์๊ณ ๋ฆฌ์ฆ (0) | 2024.06.10 |