다운로드
작성자: admin 작성일시: 2018-04-17 01:37:36 조회수: 1465 다운로드: 189
카테고리: 기타 태그목록:

Keras를 이용한 CNN 구현 - MNIST

In [1]:
import tensorflow as tf
tf.logging.set_verbosity(tf.logging.ERROR)  # warning 출력 방지

from keras.datasets import mnist
from keras.utils import np_utils

(X_train0, y_train0), (X_test0, y_test0) = mnist.load_data()
X_train = X_train0[:, :, :, np.newaxis].astype('float32') / 255.0
X_test = X_test0[:, :, :, np.newaxis].astype('float32') / 255.0
Y_train = np_utils.to_categorical(y_train0, 10)
Y_test = np_utils.to_categorical(y_test0, 10)
Using TensorFlow backend.
In [2]:
X_train.shape
Out:
(60000, 28, 28, 1)
In [3]:
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
from keras.optimizers import Adadelta
from keras.regularizers import l2

model1 = Sequential()
model1.add(Conv2D(4, (5, 5), input_shape=(28, 28, 1), activation='relu', 
                  kernel_initializer="glorot_uniform"))
model1.add(MaxPooling2D())
model1.add(Flatten())
model1.add(Dense(10, activation='softmax',
                 kernel_initializer="glorot_uniform"))

np.random.seed(0)
model1.compile(loss='categorical_crossentropy', metrics=[
               'accuracy'], optimizer=Adadelta())
In [4]:
model1.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 24, 24, 4)         104       
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 12, 12, 4)         0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 576)               0         
_________________________________________________________________
dense_1 (Dense)              (None, 10)                5770      
=================================================================
Total params: 5,874
Trainable params: 5,874
Non-trainable params: 0
_________________________________________________________________
In [5]:
%%time
hist1 = model1.fit(X_train, Y_train, epochs=10, batch_size=600,
                   validation_data=(X_test, Y_test), verbose=2)
Train on 60000 samples, validate on 10000 samples
Epoch 1/10
 - 4s - loss: 0.8461 - acc: 0.7501 - val_loss: 0.3720 - val_acc: 0.8946
Epoch 2/10
 - 4s - loss: 0.3462 - acc: 0.8985 - val_loss: 0.3228 - val_acc: 0.9031
Epoch 3/10
 - 5s - loss: 0.3049 - acc: 0.9116 - val_loss: 0.2743 - val_acc: 0.9226
Epoch 4/10
 - 4s - loss: 0.2770 - acc: 0.9197 - val_loss: 0.2461 - val_acc: 0.9292
Epoch 5/10
 - 4s - loss: 0.2523 - acc: 0.9278 - val_loss: 0.2294 - val_acc: 0.9330
Epoch 6/10
 - 5s - loss: 0.2303 - acc: 0.9340 - val_loss: 0.2116 - val_acc: 0.9396
Epoch 7/10
 - 4s - loss: 0.2083 - acc: 0.9408 - val_loss: 0.1911 - val_acc: 0.9442
Epoch 8/10
 - 4s - loss: 0.1882 - acc: 0.9463 - val_loss: 0.1704 - val_acc: 0.9516
Epoch 9/10
 - 4s - loss: 0.1702 - acc: 0.9521 - val_loss: 0.1596 - val_acc: 0.9540
Epoch 10/10
 - 5s - loss: 0.1537 - acc: 0.9566 - val_loss: 0.1400 - val_acc: 0.9594
CPU times: user 2min 4s, sys: 12.4 s, total: 2min 16s
Wall time: 43.2 s
In [6]:
plt.plot(hist1.history['acc'], 'b-', label="training")
plt.plot(hist1.history['val_acc'], 'r:', label="test")
plt.legend()
plt.show()
In [7]:
l1 = model1.layers[0]
w1, b1 = l1.get_weights()

M = 4
for i in range(M):
    plt.subplot(1, M, i + 1)
    plt.imshow(w1[:, :, 0, i], cmap=mpl.cm.bone)
    plt.title("필터 {}".format(i + 1))
    plt.grid(False)
In [8]:
import keras.backend as K

f10 = K.function([model1.input], [model1.layers[0].output])
f11 = K.function([model1.input], [model1.layers[1].output])
f12 = K.function([model1.input], [model1.layers[2].output])
f13 = K.function([model1.input], [model1.layers[3].output])

def visualize1(n):
    x = X_train[n:n + 1]
    plt.figure(figsize=(8, 3))
    for i in range(3):
        plt.subplot(1, 3, i + 1)
        if i == 1:
            plt.imshow(x[0, :, :, 0], cmap=mpl.cm.bone_r)
            plt.xticks([]); plt.yticks([]); plt.grid(False)
        else:
            plt.imshow(np.zeros_like(x[0, :, :, 0]), cmap=mpl.cm.bone_r)
            plt.axis("off")
    plt.tight_layout()
    plt.suptitle("입력 이미지", y=1)
    
    z0 = f10([x])[0]
    plt.figure(figsize=(8, 2))
    for i in range(4):
        plt.subplot(1, 4, i + 1)
        plt.imshow(z0[0, :, :, i], cmap=mpl.cm.bone_r)
        plt.xticks([]); plt.yticks([]); plt.grid(False)
        plt.title("{}번 필터 통과 후".format(i))
    plt.tight_layout()
    plt.suptitle("컨벌루션 필터 통과 후", y=1.1)
    
    z1 = f11([x])[0]
    plt.figure(figsize=(8, 2))
    for i in range(4):
        plt.subplot(1, 4, i + 1)
        plt.imshow(z1[0, :, :, i], cmap=mpl.cm.bone_r)
        plt.xticks([]); plt.yticks([]); plt.grid(False)
        plt.title("{}번 맥스풀링 통과 후".format(i))
    plt.tight_layout()
    plt.suptitle("맥스풀링 통과 후", y=1.1)
        
    z2 = f12([x])[0]
    plt.figure(figsize=(9, 1))
    plt.imshow(z2, aspect=10, cmap=mpl.cm.bone_r)
    plt.xticks([]); plt.yticks([]); plt.grid(False)
    plt.title("벡터화")
    
    z3 = f13([x])[0]
    plt.figure(figsize=(9, 1))
    plt.imshow(z3, aspect=0.2, cmap=mpl.cm.bone_r)
    plt.xticks(range(10)); plt.yticks([]); plt.grid(False)
    plt.title("10-클래스 분류 결과")

visualize1(0)
In [9]:
visualize1(1)

2 Convolution Layers

In [10]:
np.random.seed(0)

model2 = Sequential()

model2.add(Conv2D(4, (5, 5), activation='relu', input_shape=(
    28, 28, 1), kernel_initializer="glorot_uniform"))
model2.add(MaxPooling2D())
model2.add(Conv2D(4, (5, 5), activation='relu',
                  kernel_initializer="glorot_uniform"))
model2.add(MaxPooling2D())
model2.add(Flatten())
model2.add(Dense(10, activation='softmax',
                 kernel_initializer="glorot_uniform"))
In [11]:
model2.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_2 (Conv2D)            (None, 24, 24, 4)         104       
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 12, 12, 4)         0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 8, 8, 4)           404       
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 4, 4, 4)           0         
_________________________________________________________________
flatten_2 (Flatten)          (None, 64)                0         
_________________________________________________________________
dense_2 (Dense)              (None, 10)                650       
=================================================================
Total params: 1,158
Trainable params: 1,158
Non-trainable params: 0
_________________________________________________________________
In [12]:
model2.compile(loss='categorical_crossentropy', metrics=[
               'accuracy'], optimizer=Adadelta())
In [13]:
%%time
hist2 = model2.fit(X_train, Y_train, epochs=20, batch_size=600,
                   validation_data=(X_test, Y_test), verbose=2)
Train on 60000 samples, validate on 10000 samples
Epoch 1/20
 - 5s - loss: 1.0249 - acc: 0.6604 - val_loss: 0.4348 - val_acc: 0.8598
Epoch 2/20
 - 5s - loss: 0.3465 - acc: 0.8943 - val_loss: 0.2701 - val_acc: 0.9196
Epoch 3/20
 - 5s - loss: 0.2666 - acc: 0.9191 - val_loss: 0.2226 - val_acc: 0.9323
Epoch 4/20
 - 5s - loss: 0.2281 - acc: 0.9313 - val_loss: 0.1973 - val_acc: 0.9401
Epoch 5/20
 - 5s - loss: 0.2018 - acc: 0.9394 - val_loss: 0.1785 - val_acc: 0.9470
Epoch 6/20
 - 5s - loss: 0.1841 - acc: 0.9450 - val_loss: 0.1575 - val_acc: 0.9536
Epoch 7/20
 - 5s - loss: 0.1694 - acc: 0.9490 - val_loss: 0.1481 - val_acc: 0.9565
Epoch 8/20
 - 5s - loss: 0.1583 - acc: 0.9528 - val_loss: 0.1384 - val_acc: 0.9593
Epoch 9/20
 - 5s - loss: 0.1480 - acc: 0.9560 - val_loss: 0.1287 - val_acc: 0.9624
Epoch 10/20
 - 5s - loss: 0.1399 - acc: 0.9583 - val_loss: 0.1253 - val_acc: 0.9626
Epoch 11/20
 - 5s - loss: 0.1338 - acc: 0.9602 - val_loss: 0.1158 - val_acc: 0.9652
Epoch 12/20
 - 5s - loss: 0.1285 - acc: 0.9616 - val_loss: 0.1120 - val_acc: 0.9666
Epoch 13/20
 - 5s - loss: 0.1237 - acc: 0.9630 - val_loss: 0.1066 - val_acc: 0.9675
Epoch 14/20
 - 5s - loss: 0.1191 - acc: 0.9646 - val_loss: 0.1038 - val_acc: 0.9684
Epoch 15/20
 - 5s - loss: 0.1148 - acc: 0.9653 - val_loss: 0.1011 - val_acc: 0.9696
Epoch 16/20
 - 5s - loss: 0.1114 - acc: 0.9670 - val_loss: 0.1007 - val_acc: 0.9693
Epoch 17/20
 - 5s - loss: 0.1078 - acc: 0.9675 - val_loss: 0.0950 - val_acc: 0.9713
Epoch 18/20
 - 5s - loss: 0.1046 - acc: 0.9686 - val_loss: 0.0927 - val_acc: 0.9725
Epoch 19/20
 - 5s - loss: 0.1024 - acc: 0.9691 - val_loss: 0.0928 - val_acc: 0.9720
Epoch 20/20
 - 5s - loss: 0.1004 - acc: 0.9698 - val_loss: 0.0904 - val_acc: 0.9715
CPU times: user 4min 36s, sys: 26.5 s, total: 5min 3s
Wall time: 1min 35s
In [14]:
plt.plot(hist2.history['acc'], 'b-', label="training")
plt.plot(hist2.history['val_acc'], 'r:', label="test")
plt.legend()
plt.show()
In [15]:
model2.save("mnist_cnn21.hdf5")
In [16]:
l21 = model2.layers[0]
w21, b21 = l21.get_weights()
In [17]:
M = 4
for i in range(M):
    plt.subplot(1, M, i + 1)
    plt.imshow(w21[:, :, 0, i], cmap=mpl.cm.bone_r)
    plt.grid(False)
In [18]:
l22 = model2.layers[2]
w22, b22 = l22.get_weights()
In [19]:
M = 4
for i in range(M):
    plt.subplot(1, M, i + 1)
    plt.imshow(w22[:, :, 0, i], cmap=mpl.cm.bone_r)
    plt.grid(False)
In [20]:
import keras.backend as K

f20 = K.function([model2.input], [model2.layers[0].output])
f21 = K.function([model2.input], [model2.layers[1].output])
f22 = K.function([model2.input], [model2.layers[2].output])
f23 = K.function([model2.input], [model2.layers[3].output])
f24 = K.function([model2.input], [model2.layers[4].output])
f25 = K.function([model2.input], [model2.layers[5].output])

def visualize2(n):
    x = X_train[n:n + 1]
    plt.figure(figsize=(8, 3))
    for i in range(3):
        plt.subplot(1, 3, i + 1)
        if i == 1:
            plt.imshow(x[0, :, :, 0], cmap=mpl.cm.bone_r)
            plt.xticks([]); plt.yticks([]); plt.grid(False)
        else:
            plt.imshow(np.zeros_like(x[0, :, :, 0]), cmap=mpl.cm.bone_r)
            plt.axis("off")
    plt.tight_layout()
    plt.suptitle("입력 이미지", y=1)
    
    z0 = f20([x])[0]
    plt.figure(figsize=(8, 2))
    for i in range(4):
        plt.subplot(1, 4, i + 1)
        plt.imshow(z0[0, :, :, i], cmap=mpl.cm.bone_r)
        plt.xticks([]); plt.yticks([]); plt.grid(False)
        plt.title("{}번 필터 통과 후".format(i))
    plt.tight_layout()
    plt.suptitle("1차 컨벌루션 필터 통과 후", y=1.1)
    
    z1 = f21([x])[0]
    plt.figure(figsize=(8, 2))
    for i in range(4):
        plt.subplot(1, 4, i + 1)
        plt.imshow(z1[0, :, :, i], cmap=mpl.cm.bone_r)
        plt.xticks([]); plt.yticks([]); plt.grid(False)
        plt.title("{}번 맥스풀링 통과 후".format(i))
    plt.tight_layout()
    plt.suptitle("1차 맥스풀링 통과 후", y=1.1)
        
    z2 = f22([x])[0]
    plt.figure(figsize=(8, 2))
    for i in range(4):
        plt.subplot(1, 4, i + 1)
        plt.imshow(z2[0, :, :, i], cmap=mpl.cm.bone_r)
        plt.xticks([]); plt.yticks([]); plt.grid(False)
        plt.title("{}번 필터 통과 후".format(i))
    plt.tight_layout()
    plt.suptitle("2차 컨벌루션 필터 통과 후", y=1.1)
    
    z3 = f23([x])[0]
    plt.figure(figsize=(8, 2))
    for i in range(4):
        plt.subplot(1, 4, i + 1)
        plt.imshow(z3[0, :, :, i], cmap=mpl.cm.bone_r)
        plt.xticks([]); plt.yticks([]); plt.grid(False)
        plt.title("{}번 맥스풀링 통과 후".format(i))
    plt.tight_layout()
    plt.suptitle("2차 맥스풀링 통과 후", y=1.1)
        
    z4 = f24([x])[0]
    plt.figure(figsize=(9, 1))
    plt.imshow(z4, aspect=1, cmap=mpl.cm.bone_r)
    plt.xticks([]); plt.yticks([]); plt.grid(False)
    plt.title("벡터화")
    
    z5 = f25([x])[0]
    plt.figure(figsize=(9, 1))
    plt.imshow(z5, aspect=0.2, cmap=mpl.cm.bone_r)
    plt.xticks(range(10)); plt.yticks([]); plt.grid(False)
    plt.title("10-클래스 분류 결과")

visualize2(0)

질문/덧글

학습후 모델에 테스트 emar*** 2018년 11월 6일 6:49 오전

학습후에 모델을 저장하고 해당 모델에 제가 임의의 데이터를 넣어서 결과가 제대로 나오는지 테스트하고 싶은데 어떻게 해야하는지 궁금합니다.

답변: 학습후 모델에 테스트 관리자 2018년 11월 8일 9:15 오전

`save` 메서드와 `predict` 메서드를 사용하세요.