[TensorFlow 2.0] 각 Layer별 역할 및 파라미터IT/AI2022. 9. 7. 17:16
Table of Contents
반응형
Load Packages
import tensorflow as tf
import os
import matplotlib.pyplot as plt
%matplotlib inline
Input Image
from tensorflow.keras import datasets
(train_x, train_y), (test_x, test_y) = datasets.mnist.load_data()
image = train_x[0]
# 차원 수 높이기
image = image[tf.newaxis, ..., tf.newaxis]
image.shape
# Out
(1, 28, 28, 1)
Feature Extraction
Convolution
- filters: layer에서 나갈 때 몇 개의 filter를 만들 것인지
- kernel_size: filter(Weight)의 크기
- strides: 몇 개의 pixel을 skip 하면서 훑어지나갈 것인지 (크기에도 영향을 줍니다)
- padding: zero padding을 만들 것인지. VALID는 padding이 없고, SAME은 padding이 있습니다. (크기에도 영향을 줍니다)
- activation: Activation Function을 만들 것인지. 당장 설정 안해도 Layer 층을 따로 만들 수 있습니다. (Default: None)
tf.keras.layers.Conv2D(filters=3, kernel_size=(3, 3), strides=(1, 1), padding='VALID', activation='relu')
# (3, 3) 대신에 3으로도 대체 가능
tf.keras.layers.Conv2D(3, 3, 1, 'VALID')
Visualization
image = tf.cast(image, dtype=tf.float32)
layer = tf.keras.layers.Conv2D(3, 3, strides=(1, 1), padding='SAME')
output = layer(image)
plt.subplot(121)
plt.imshow(image[0, :, :, 0], 'gray')
plt.subplot(122)
plt.imshow(output[0, :, :, 0], 'gray')
plt.show()
weight 불러오기
weight = layer.get_weights()[0]
plt.figure(figsize=(15, 5))
plt.subplot(131)
plt.hist(output.numpy().ravel(), range=[-2, 2])
plt.ylim(0, 100)
plt.subplot(132)
plt.title(weight.shape)
plt.imshow(weight[:, :, 0, 0], 'gray')
plt.subplot(133)
plt.title(output.shape)
plt.imshow(output[0, :, :, 0], 'gray')
plt.colorbar()
plt.show()
Activation Function
0 미만의 값들을 0으로 바꿔줍니다.
import numpy as np
act_layer = tf.keras.layers.ReLU()
act_output = act_layer(output)
np.min(act_output), np.max(act_output)
# Out
(0.0, 244.73064) # 0미만 값들이 0으로 바뀜
plt.figure(figsize=(15, 5))
plt.subplot(121)
plt.hist(act_output.numpy().ravel(), range=[-2, 2])
plt.ylim(0, 100)
plt.subplot(122)
plt.title(act_output.shape)
plt.imshow(act_output[0, :, :, 0], 'gray')
plt.colorbar()
plt.show()
Pooling
강조되는 것들만 압축합니다.
pool_layer = tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding='SAME')
pool_output = pool_layer(act_output)
pool_output.shape
# Out
TensorShape([1, 14, 14, 3])
plt.figure(figsize=(15, 5))
plt.subplot(121)
plt.hist(pool_output.numpy().ravel(), range=[-2, 2])
plt.ylim(0, 100)
plt.subplot(122)
plt.title(pool_output.shape)
plt.imshow(pool_output[0, :, :, 0], 'gray')
plt.colorbar()
plt.show()
Fully Connected
Flatten
layer = tf.keras.layers.Flatten()
flatten = layer(output)
flatten.shape
# Out
TensorShape([1, 2352]) # 28 * 28 * 3
output.shape
# Out
TensorShape([1, 28, 28, 3])
plt.figure(figsize=(10, 5))
plt.subplot(211)
plt.hist(flatten.numpy().ravel())
plt.subplot(212)
plt.imshow(flatten[:, :100])
plt.show()
Dense
하나씩 연결하겠다는 의미입니다.
layer = tf.keras.layers.Dense(32, activation='relu')
output = layer(flatten)
output.shape
# Out
TensorShape([1, 32]) # 32로 줄어들었습니다.
DropOut
학습할 때만 랜덤하게 끊어줍니다. 학습이 끝나면 다시 복구시킵니다.
# 0.7은 비율입니다. 얼마나 끊을 것인지 살릴 것인지
layer = tf.keras.layers.Dropout(0.7)
output = layer(output)
output.shape
# Out
TensorShape([1, 32])
Build Model
from tensorflow.keras import layers
input_shape = (28, 28, 1)
num_classes = 10
inputs = layers.Input(shape=input_shape)
# Feature Extraction
net = layers.Conv2D(32, 3, padding='SAME')(inputs)
net = layers.Activation('relu')(net)
net = layers.Conv2D(32, 3, padding='SAME')(net)
net = layers.Activation('relu')(net)
net = layers.MaxPool2D(2, 2)(net)
net = layers.Dropout(0.25)(net)
net = layers.Conv2D(64, 3, padding='SAME')(net)
net = layers.Activation('relu')(net)
net = layers.Conv2D(64, 3, padding='SAME')(net)
net = layers.Activation('relu')(net)
net = layers.MaxPool2D(2, 2)(net)
net = layers.Dropout(0.25)(net)
# Fully Connected
net = layers.Flatten()(net)
net = layers.Dense(512)(net)
net = layers.Activation('relu')(net)
net = layers.Dropout(0.25)(net)
net = layers.Dense(num_classes)(net)
net = layers.Activation('softmax')(net)
model = tf.keras.Model(inputs=inputs, outputs=net, name='Basic-CNN')
model.summary()
# Out
Model: "Basic-CNN"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 28, 28, 1)] 0
_________________________________________________________________
conv2d_25 (Conv2D) (None, 28, 28, 32) 320
_________________________________________________________________
activation_8 (Activation) (None, 28, 28, 32) 0
_________________________________________________________________
conv2d_26 (Conv2D) (None, 28, 28, 32) 9248
_________________________________________________________________
activation_9 (Activation) (None, 28, 28, 32) 0
_________________________________________________________________
max_pooling2d_8 (MaxPooling2 (None, 14, 14, 32) 0
_________________________________________________________________
dropout_5 (Dropout) (None, 14, 14, 32) 0
_________________________________________________________________
conv2d_27 (Conv2D) (None, 14, 14, 64) 18496
_________________________________________________________________
activation_10 (Activation) (None, 14, 14, 64) 0
_________________________________________________________________
conv2d_28 (Conv2D) (None, 14, 14, 64) 36928
_________________________________________________________________
activation_11 (Activation) (None, 14, 14, 64) 0
_________________________________________________________________
max_pooling2d_9 (MaxPooling2 (None, 7, 7, 64) 0
_________________________________________________________________
dropout_6 (Dropout) (None, 7, 7, 64) 0
_________________________________________________________________
flatten_4 (Flatten) (None, 3136) 0
_________________________________________________________________
dense_3 (Dense) (None, 512) 1606144
_________________________________________________________________
activation_12 (Activation) (None, 512) 0
_________________________________________________________________
dropout_7 (Dropout) (None, 512) 0
_________________________________________________________________
dense_4 (Dense) (None, 10) 5130
_________________________________________________________________
activation_13 (Activation) (None, 10) 0
=================================================================
Total params: 1,676,266
Trainable params: 1,676,266
Non-trainable params: 0
_________________________________________________________________
반응형
'IT > AI' 카테고리의 다른 글
[TensorFlow 2.0] Optimizer 및 Training (Expert) (0) | 2022.09.08 |
---|---|
[TensorFlow 2.0] Optimizer 및 Training (Keras) (0) | 2022.09.08 |
[TensorFlow 2.0] 예제 데이터셋 (MNIST) 사용 (0) | 2022.09.07 |
[Tensorflow 2.0] 기초 사용법 (0) | 2022.09.06 |
Tensorflow 2.0과 PyTorch 소개 (0) | 2022.09.06 |
@고지니어스 :: 규니의 개발 블로그
IT 기술과 개발 내용을 포스팅하는 블로그
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!