PLOD

[AI] Autoencoder 본문

대외 활동 및 IT 지식

[AI] Autoencoder

훌룽이 2022. 12. 13. 05:07

* Autoencoder(AE)

데이터를 인코딩하는 방법을 배우는 효과적인 딥러닝 모델이다. 기존의 모델과의 가장 큰 차이점은 입력 데이터만을 가지고 학습을 진행하는데 있다. (정답데이터 X, Dataset <input = input>) 

Autoencoder은 저차원 공간에서 새로운 이미지를 추출해야 되기 때문에 latent vector가 필요하다.

* 손실함수 (L) : 입력 값과 동일한 추력을 가지도록 학습함으로써 네트어크를 통해서 나온 출력값인 decoder(encoder(x))가 입력 값인 x와 얼마나 차이가 있는지 측정

 

 

 

ex 1) hidden dimension = 32, hidden activation = 'relu', output activation = 'sigmoid' 인 AE를 modeling 

먼저 test_data의 크기를 출력해보았다.

from tensorflow.keras.datasets import mnist
import numpy as np

(x_train, _), (x_test, _) = mnist.load_data()

x_train = x_train.astype('float32') / 255.
x_test  = x_test.astype('float32') / 255.

x_train = x_train.reshape( (len(x_train), np.prod(x_train.shape[1:])))
x_test = x_test.reshape( (len(x_test), np.prod(x_test.shape[1:])))

print(x_train.shape)			
print(x_test.shape)
from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.models import Model
from tensorflow.keras import Sequential


# 모델을 여기에 구현하세요
autoencoder = Sequential()
autoencoder.add(Dense(32,activation ='relu', input_shape = (784,)))
autoencoder.add(Dense(784,activation ='sigmoid'))

from keras.utils import plot_model
plot_model(autoencoder, show_shapes=True)

학습을 수행 , optimizer은 'adam'을 사용하였고 loss는 'binary_crossentropy' 함수를 사용하였다. 

autoencoder.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

autoencoder.fit(x_train, x_train, epochs=20, batch_size=256, shuffle=True, validation_data=(x_test, x_test))

출력

import matplotlib.pyplot as plt
n = 10                #정답지 10개

#그 중 10장을 선택하여 가시화 (입력 10장, 복원된 출력 10장)
plt.figure(figsize=(20, 4))               #총 20장장

for i in range(n):
    # plt.subplot( nrow, ncol, index)
    ax = plt.subplot(2, n, i+1)
    plt.imshow(x_test[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

    ax = plt.subplot(2, n, i+1+n)
    plt.imshow(decoded_imgs[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

plt.show()

ex 1) Denoising Autoencoder

noise_factor = 1
x_train_noisy = x_train + noise_factor * np.random.normal(loc = 0.0, scale = 1.0,size = x_train.shape )
x_test_noisy = x_test + noise_factor * np.random.normal(loc = 0.0, scale = 1.0,size  = x_test.shape )

x_train_noisy = np.clip(x_train_noisy, 0., 1.)
x_test_noisy = np.clip(x_test_noisy,0.,1.)

from tensorflow.keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
from tensorflow.keras.models import Model

inp = Input(shape=(28,28,1))

encoded = Conv2D(16, (3,3), activation='relu', padding='same') (inp)
encoded = Conv2D(16, (3,3), activation='relu', padding='same') (encoded)
encoded = MaxPooling2D((2,2), padding='same') (encoded)

encoded = Conv2D(8, (3,3), activation='relu', padding='same') (encoded)
encoded = Conv2D(8, (3,3), activation='relu', padding='same') (encoded)
encoded = MaxPooling2D((2,2), padding='same') (encoded)

encoder = Model(inputs = [inp], outputs =[encoded])

decoded = Conv2D(8, (3,3), activation='relu', padding='same') (encoded)
decoded = Conv2D(8, (3,3), activation='relu', padding='same') (decoded)
decoded = UpSampling2D((2,2)) (decoded)

decoded = Conv2D(16, (3,3), activation='relu', padding='same') (decoded)
decoded = Conv2D(16, (3,3), activation='relu', padding='same') (decoded)
decoded = UpSampling2D((2,2)) (decoded)

decoded = Conv2D(1, (3,3), activation='sigmoid', padding='same') (decoded)

decoder = Model(inputs = [encoded],  outputs = [decoded])

autoencoder = Model(inputs=inp, outputs=decoded)

from keras.utils.vis_utils import plot_model
plot_model(autoencoder, show_shapes=True)

 

autoencoder.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

autoencoder.fit(x_train, x_train, epochs=20, batch_size=256, shuffle=True, validation_data=(x_test, x_test))

Denosiy_image 학습

decoded_imgs = autoencoder.predict(x_test_noisy)
import matplotlib.pyplot as plt
n = 10

#입력 10장, 복원된 출력 10장 
plt.figure(figsize=(20, 4))

for i in range(n):
    ax = plt.subplot(2,n,i+1)
    plt.imshow(x_test_noisy[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

    ax = plt.subplot(2, n, i+1+n)
    plt.imshow(decoded_imgs[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

plt.show()

 

'대외 활동 및 IT 지식' 카테고리의 다른 글

[IT] smart factory  (0) 2022.12.21
[AI] Generatvice Adversarial Network(GAN)  (0) 2022.12.13
[AI] Sequence to Sequence Learning  (0) 2022.12.13
[AI] dropout | Batch Normalization  (0) 2022.12.12
[Git] git bash 명령어  (1) 2022.09.25
Comments