2020_01_21 CNN

ro_ot ㅣ 2020. 1. 21. 20:58

※ 이전 DNN 코드

     *  CNN 들어가기 전 복습

import numpy as np
import matplotlib.pyplot as plt

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import optimizers
from tensorflow.keras.layers import Dense, Flatten, Input

import time
(raw_train_x, raw_train_y), (raw_test_x, raw_test_y) = tf.keras.datasets.mnist.load_data()

train_x = raw_train_x/255
test_x = raw_test_x/255

train_y = raw_train_y
test_y = raw_test_y

* train_x = raw_train_x/255 : 255로 나눠서 정규화 시킨다.

model = keras.Sequential()
model.add(Input((28,28)))
model.add(Flatten())
model.add(Dense(10, activation='relu'))
model.add(Dense(10, activation='relu'))
model.add(Dense(10, activation='softmax'))

model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
model.summary()


model.fit(train_x, train_y, epochs=5, verbose=1, batch_size=128)


loss, acc = model.evaluate(test_x, test_y)
print("loss=",loss)
print("acc=",acc)

y_ = model.predict(test_x)
predicted = np.argmax(y_, axis=1)

print(predicted)

* model.add(Flatten()) : 일차원으로 바꿔주는 레이어

※ CNN

import numpy as np
import matplotlib.pyplot as plt

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import optimizers
from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, Input

import time
(raw_train_x, raw_train_y), (raw_test_x, raw_test_y) = tf.keras.datasets.mnist.load_data()

train_x = raw_train_x/255
test_x = raw_test_x/255

# Conv2D는 width*height*channel을 입력으로 받음
train_x = train_x.reshape((60000, 28, 28, 1)) # ADDED
test_x = test_x.reshape((10000, 28, 28, 1)) # ADDED

train_y = raw_train_y
test_y = raw_test_y

* conv2D는 width*height*channel으로 입력받음

model = keras.Sequential()
# model.add(Input((28,28)))
model.add(Input((28,28,1)))
model.add(Conv2D(32, (3, 3))) # ADDED
model.add(MaxPooling2D((2, 2))) # ADDED
model.add(Conv2D(64, (3, 3))) # ADDED
model.add(MaxPooling2D((2, 2))) # ADDED
model.add(Flatten())
model.add(Dense(10, activation='relu'))
model.add(Dense(10, activation='relu'))
model.add(Dense(10, activation='softmax'))

model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
model.summary()


model.fit(train_x, train_y, epochs=5, verbose=1, batch_size=128)


loss, acc = model.evaluate(test_x, test_y)
print("loss=",loss)
print("acc=",acc)

y_ = model.predict(test_x)
predicted = np.argmax(y_, axis=1)

print(predicted)

* CNN은 Conv2D, Maxpooling2D를 추가하면 가능하다.

 

1. conv2d 레이어의 파라매터 수는 다음에 의해서 결정된다.

   * 파라매터 수 = (conv필터 가로 * conv 필터 세로 * 데이터 깊이 + bias 1개) * 필터 수

2. 위 모델의 경우 파라매터 수 320, 18496은 다음과 같이 결정된다.

   * 320 = (3*3*1+1) * 32
   * 18496 = (3*3*32+1)*64

 

Layer (type)    Output Shape   Param #   
conv2d (Conv2D)   (None, 26, 26, 32)    320
max_pooling2d (MaxPooling2D) (None, 13, 13, 32)  0
conv2d_1 (Conv2D)   (None, 11, 11, 64)  18496



'Deep learning > Code' 카테고리의 다른 글

object_detection_yolo_darknet  (0) 2020.01.22
2020_01_22 VGG16  (0) 2020.01.22
2020_01_22 CIFAR10  (0) 2020.01.22
2020_01_21 IRIS  (0) 2020.01.21
2020_01_20 DL(Keras)  (0) 2020.01.20