본문 바로가기
ML/CNN

Convolutional : Autoencoder / Neural Network

by 나른한 사람 2021. 7. 29.

오토인코더에 대한 대략적인 설명은 지난 게시물에 있으므로 생략
Convolutional Model이 일반적으로 더 나은 성능을 보임

# 이미지 표시해줄 함수 선언
def show_imgs(up,down, size):
    plt.style.use('fivethirtyeight')
    plt.figure(figsize=(30,6))
    plt.gray()
    n_img=10
    for i in range(n_img):
        plt.subplot(2,n_img,i+1)
        plt.imshow(np.reshape(up[i], (size,size)))
        plt.axis('off')

        plt.subplot(2,n_img, n_img+i+1)
        plt.imshow(np.reshape(down[i], (size,size)))
        plt.axis('off')

Input Data

# Conv2D 인풋으로 사용하기 위해 (784,) -> (28,28,)
# 쉬운 output shape 계산을 위해 resize (28,28,1) -> (32, 32, 1)
X_train_conv_n = tf.image.resize(X_train_noise.reshape((-1,28,28,1)), (32,32))
X_test_conv_n = tf.image.resize(X_test_noise.reshape((-1,28,28,1)), (32,32))
X_train_conv = tf.image.resize(X_train.reshape((-1,28,28,1)), (32,32))
X_test_conv = tf.image.resize(X_test.reshape((-1,28,28,1)), (32,32))

Convolutional Autoencoder 구현 및 train

class ConvAutoencoder(Model):
    def __init__(self):
        super(ConvAutoencoder, self).__init__()
        self.encoder = Sequential([  # 32*32
            Conv2D(64, (3,3), activation='relu', padding='same'),
            MaxPooling2D((2,2), strides=2, padding='same'),  # 16*16
            Conv2D(32, (3,3), activation='relu', padding='same'),
            MaxPooling2D((2,2), strides=2, padding='same'),  # 8*8
            Conv2D(32, (3,3), activation='relu', padding='same'),
            MaxPooling2D((2,2), strides=2, padding='same')  # 4*4
        ])
        self.decoder = Sequential([ # 4*4
            Conv2D(32, (3,3), activation='relu', padding='same'), 
            UpSampling2D((2,2)),  # 8*8
            Conv2D(32, (3,3), activation='relu', padding='same'),
            UpSampling2D((2,2)),  # 16*16
            Conv2D(64, (3,3), activation='relu', padding='same'),
            UpSampling2D((2,2)),  # 32*32
            Conv2D(1, (3,3), activation='sigmoid', padding='same')
        ])
        self.compile(optimizer='adam', loss='binary_crossentropy')

    def call(self, inputs):
        encoded = self.encoder(inputs)
        decoded = self.decoder(encoded)
        return decoded
with tf.device('GPU:0'):
    conv_autoencoder = ConvAutoencoder()
    conv_autoencoder.fit(X_train_conv_n, X_train_conv,
                  epochs=epochs, batch_size=batch_size,
                  validation_data=(X_test_conv_n, X_test_conv),
                  callbacks=[ae_early_stopping])

Autoencoder 결과 확인

show_imgs(X_train_conv_n, conv_autoencoder.predict(X_train_conv_n), 32)

(위) noise data, (아래) predicted data

Convolutional Neural Network 구현 및 train

class ConvNeural(Model):
    def __init__(self):
        super(ConvNeural, self).__init__()
        self.sequential = Sequential([
            Conv2D(32, (3,3), strides=(2,2), activation='relu', padding='same'),
            BatchNormalization(),
            MaxPooling2D((2,2)),
            Conv2D(64, (3,3), strides=(2,2), activation='relu', padding='same'),
            BatchNormalization(),
            MaxPooling2D((2,2)),
            Dropout(.5),
            Flatten(),
            Dense(512, activation='relu'),
            Dropout(.5),
            Dense(10, activation='softmax')
        ])
        self.compile(optimizer=Nadam(learning_rate=0.002), loss='sparse_categorical_crossentropy', metrics=['acc'])
    def call(self, inputs):
        x = self.sequential(inputs)
        return x
with tf.device('GPU:0'):
    conv_model = ConvNeural()
    conv_model.fit(X_train_conv, y_train,
                  epochs=epochs, batch_size=batch_size,
                  validation_data=(X_test_conv,y_test),
                  callbacks=[clf_early_stopping])

성능확인

# Convolutional Neural Network result
print(classification_report(conv_model.predict(X_test_conv).argmax(axis=1),y_test, digits=4))
print(accuracy_score(conv_model.predict(X_test_conv).argmax(axis=1),y_test))
'''
              precision    recall  f1-score   support

           0     0.9983    0.9960    0.9971      1746
           1     0.9940    0.9925    0.9932      1993
           2     0.9929    0.9935    0.9932      1680
           3     0.9925    0.9954    0.9939      1727
           4     0.9872    0.9907    0.9889      1712
           5     0.9908    0.9914    0.9911      1632
           6     0.9947    0.9923    0.9935      1694
           7     0.9915    0.9874    0.9895      1901
           8     0.9849    0.9915    0.9882      1647
           9     0.9841    0.9813    0.9827      1768

    accuracy                         0.9911     17500
   macro avg     0.9911    0.9912    0.9911     17500
weighted avg     0.9911    0.9911    0.9911     17500

0.9911428571428571
'''
# CNN predict noise data
print(classification_report(conv_model.predict(X_test_conv_n).argmax(axis=1),y_test, digits=4))
print(accuracy_score(conv_model.predict(X_test_conv_n).argmax(axis=1),y_test))
'''
              precision    recall  f1-score   support

           0     0.7744    0.9897    0.8689      1363
           1     0.0588    1.0000    0.1111       117
           2     0.6651    0.8958    0.7634      1248
           3     0.4024    0.9789    0.5704       712
           4     0.8009    0.5637    0.6617      2441
           5     0.4648    0.9656    0.6275       786
           6     0.7438    0.9707    0.8422      1295
           7     0.6334    0.8784    0.7360      1365
           8     0.9952    0.3015    0.4628      5472
           9     0.8763    0.5720    0.6922      2701

    accuracy                         0.6324     17500
   macro avg     0.6415    0.8116    0.6336     17500
weighted avg     0.8080    0.6324    0.6378     17500

0.6324
'''
# CNN with Autoencoder
denoise_conv = conv_autoencoder.predict(X_test_conv_n)
print(classification_report(conv_model.predict(denoise_conv).argmax(axis=1),y_test, digits=4))
print(accuracy_score(conv_model.predict(denoise_conv).argmax(axis=1),y_test))
'''
              precision    recall  f1-score   support

           0     0.9960    0.9920    0.9940      1749
           1     0.9955    0.9783    0.9868      2025
           2     0.9851    0.9887    0.9869      1675
           3     0.9740    0.9906    0.9822      1703
           4     0.9651    0.9916    0.9782      1672
           5     0.9773    0.9809    0.9791      1627
           6     0.9929    0.9853    0.9891      1703
           7     0.9831    0.9821    0.9826      1895
           8     0.9668    0.9798    0.9733      1636
           9     0.9784    0.9504    0.9642      1815

    accuracy                         0.9817     17500
   macro avg     0.9814    0.9820    0.9816     17500
weighted avg     0.9818    0.9817    0.9817     17500

0.9817142857142858
'''

결과

  predict normal data predict noise data predict noise data with AE
NN 0.9823 0.7714 0.9729
CNN 0.9911 0.6324 0.9817

> CNN이 조금 더 나은 성능을 보인다는 것을 확인할 수 있다.

'ML > CNN' 카테고리의 다른 글

[CNN] ResNet50 Implementation(Keras)  (0) 2021.08.09
[CNN] GoogLeNet Implementation (Keras)  (0) 2021.08.06
[CNN] DenseNet 요약  (0) 2021.08.04
[CNN] ResNet 요약  (0) 2021.08.03
[CNN] GoogLeNet 요약  (0) 2021.08.02

댓글