ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Deep Learning - 2
    machine learning 2020. 12. 10. 11:32

    google colab에서 튜토리얼을 진행했다. MNIST에서 제공하는 손글씨 dataset을 가지고 0~9까지의 숫자를 분류해 내는 학습이다. "케라스 창시자에게 배우는 딥러닝" 5장 1절 예제이다.

    import keras
    keras.__version__

    학습에 필요한 keras를 import해준다.

    from keras import layers
    from keras import models
    model = models.Sequential()
    model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
    model.add(layers.MaxPooling2D((2, 2)))
    model.add(layers.Conv2D(64, (3, 3), activation='relu'))
    model.add(layers.MaxPooling2D((2, 2)))
    model.add(layers.Conv2D(64, (3, 3), activation='relu'))

    Conv2D층과 MaxPooling2D층을 3개 쌓아 간단한 컨브넷을 만든다.
    사진의 크기가 28*28에 흑백사진이기 때문에 input_shape = (28, 28, 1) 을 입력해준다.

    model.summary()
    
    ------
    Model: "sequential"
    _________________________________________________________________
    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     
    _________________________________________________________________
    max_pooling2d_1 (MaxPooling2 (None, 5, 5, 64)          0         
    _________________________________________________________________
    conv2d_2 (Conv2D)            (None, 3, 3, 64)          36928     
    =================================================================
    Total params: 55,744
    Trainable params: 55,744
    Non-trainable params: 0

    컨브넷 구조를 출력해보면 위와 같다.
    그 다음 컨브넷의 출력인 (3, 3, 64) 3D출력을 1D텐서로 펼치기 위해 완전연결 네트워크와 연결시켜 준다.

    model.add(layers.Flatten())
    model.add(layers.Dense(64, activation='relu'))
    model.add(layers.Dense(10, activation='softmax'))

    이후 다시 구조를 출력해보면 아래와 같다.

    model.summary()
    
    ----------
    Model: "sequential"
    _________________________________________________________________
    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     
    _________________________________________________________________
    max_pooling2d_1 (MaxPooling2 (None, 5, 5, 64)          0         
    _________________________________________________________________
    conv2d_2 (Conv2D)            (None, 3, 3, 64)          36928     
    _________________________________________________________________
    flatten (Flatten)            (None, 576)               0         
    _________________________________________________________________
    dense (Dense)                (None, 64)                36928     
    _________________________________________________________________
    dense_1 (Dense)              (None, 10)                650       
    =================================================================
    Total params: 93,322
    Trainable params: 93,322
    Non-trainable params: 0

    (3, 3, 64) 3D 출력이 (576,)크기로 펼쳐진 후 Dense층으로 주입되는것을 볼 수 있다.
    이제 훈련을 시켜보고 테스트 해보자.

    from keras.datasets import mnist
    from keras.utils import to_categorical
    
    (train_images, train_labels), (test_images, test_labels) = mnist.load_data()
    
    train_images = train_images.reshape((60000, 28, 28, 1))
    train_images = train_images.astype('float32') / 255
    
    test_images = test_images.reshape((10000, 28, 28, 1))
    test_images = test_images.astype('float32') / 255
    
    train_labels = to_categorical(train_labels)
    test_labels = to_categorical(test_labels)
    model.compile(optimizer='rmsprop',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    model.fit(train_images, train_labels, epochs=5, batch_size=64)
    ------------
    Epoch 1/5
    938/938 [==============================] - 3s 4ms/step - loss: 0.1693 - accuracy: 0.9464
    Epoch 2/5
    938/938 [==============================] - 3s 3ms/step - loss: 0.0456 - accuracy: 0.9863
    Epoch 3/5
    938/938 [==============================] - 3s 3ms/step - loss: 0.0316 - accuracy: 0.9904
    Epoch 4/5
    938/938 [==============================] - 3s 3ms/step - loss: 0.0233 - accuracy: 0.9930
    Epoch 5/5
    938/938 [==============================] - 3s 3ms/step - loss: 0.0188 - accuracy: 0.9943

    99.43%의 정확도를 훈련set에서 보이고 있다.

    test_loss, test_acc = model.evaluate(test_images, test_labels)
    test_acc
    ----------
    0.9911999702453613

    테스트set에서도 99.12%정도의 높은 정확도를 보여주고 있다.
    1분도 걸리지 않는 학습과정을 통해 0부터 9까지의 손글씨를 99%확률로 판단할 수 있게 된것이다.

    'machine learning' 카테고리의 다른 글

    Deep Learning - 6  (0) 2020.12.11
    Deep Learning - 5  (0) 2020.12.10
    Deep Learning - 4  (0) 2020.12.10
    Deep Learning - 3  (0) 2020.12.10
    Deep Learning - 1  (0) 2020.12.10
Designed by Tistory.