티스토리 뷰

반응형

이미지 자료를 대상을 컨볼루션 뉴럴 네트워크를 실행하기 위해서 기본적으로 필요한 패키지는 다음과 같다.

Convolution Neural Network, CNN 구조

1.  CNN 기본 패키지 로드하기

from tensorflow import keras
# from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, BatchNormalization, Dropout, Flatten, Dense

 

위의 그림과 같은 CNN 모델을 케라스 패키지를 이용해서 정의해보자

크게 보면 입력데이터 레이어, 컨볼루션 레이어 2개 , Flatten 레이어 1개, Dense 레이어 2개, 출력데이터 레이어 등 

5개 부분으로 구성되어 있는 것을 알 수 있다.

 

2. 입력데이터의 크기을 정의한다.

img_height = 128
img_width = 128
img_channel = 3

3. CNN 기본 모델을 정의한다.

input_shape = (img_height, img_width, img_channel)

i = keras.Input(shape=input_shape) 

conv1 = keras.layers.Conv2D(32, kernel_size = (3,3),  activation='relu', padding="same")(i)
pool1 = keras.layers.MaxPooling2D((2,2))(conv1)
norm1 = keras.layers.BatchNormalization(axis = -1)(pool1)
dropout1 = keras.layers.Dropout(rate=0.2)(norm1)

conv2 = keras.layers.Conv2D(32, kernel_size = (3,3),  activation='relu', padding="same")(dropout1 )
pool2 = keras.layers.MaxPooling2D((2,2))(conv2)
norm2 = keras.layers.BatchNormalization(axis = -1)(pool2)
dropout2 = keras.layers.Dropout(rate=0.2)(norm2)

flat = keras.layers.Flatten()(dropout2)

hidden1 = keras.layers.Dense(512, activation='relu')(flat)
norm3 = keras.layers.BatchNormalization(axis = -1)(hidden1)
drop3 = keras.layers.Dropout(rate=0.2)(norm3)

hidden2 = keras.layers.Dense(256, activation='relu')(drop3)
norm4 = keras.layers.BatchNormalization(axis = -1)(hidden2)
drop4 = keras.layers.Dropout(rate=0.2)(norm4)

out = keras.layers.Dense(2, activation='sigmoid')(drop4) 

model = keras.Model(inputs=i, outputs=out)
model.compile(optimizer='adam',
                loss='categorical_crossentropy',   #Check between binary_crossentropy and categorical_crossentropy
                metrics=['accuracy'])
print(model.summary())

모델 구조 결과

Model: "model_5"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 input_18 (InputLayer)       [(None, 64, 64, 3)]       0         
                                                                 
 conv2d_25 (Conv2D)          (None, 64, 64, 32)        896       
                                                                 
 max_pooling2d_24 (MaxPoolin  (None, 32, 32, 32)       0         
 g2D)                                                            
                                                                 
 batch_normalization_29 (Bat  (None, 32, 32, 32)       128       
 chNormalization)                                                
                                                                 
 dropout_29 (Dropout)        (None, 32, 32, 32)        0         
                                                                 
 conv2d_26 (Conv2D)          (None, 32, 32, 32)        9248      
                                                                 
 max_pooling2d_25 (MaxPoolin  (None, 16, 16, 32)       0         
 g2D)                                                            
                                                                 
 batch_normalization_30 (Bat  (None, 16, 16, 32)       128       
 chNormalization)                                                
                                                                 
 dropout_30 (Dropout)        (None, 16, 16, 32)        0         
                                                                 
 flatten_7 (Flatten)         (None, 8192)              0         
                                                                 
 dense_15 (Dense)            (None, 512)               4194816   
                                                                 
 batch_normalization_31 (Bat  (None, 512)              2048      
 chNormalization)                                                
                                                                 
 dropout_31 (Dropout)        (None, 512)               0         
                                                                 
 dense_16 (Dense)            (None, 256)               131328    
                                                                 
 batch_normalization_32 (Bat  (None, 256)              1024      
 chNormalization)                                                
                                                                 
 dropout_32 (Dropout)        (None, 256)               0         
                                                                 
 dense_17 (Dense)            (None, 2)                 514       
                                                                 
=================================================================
Total params: 4,340,130
Trainable params: 4,338,466
Non-trainable params: 1,664
_________________________________________________________________
None

 

반응형
댓글