가장 쉬운 denoising autoencoder by 바죠

가장 쉬운 Denoising autoencoder

노이즈를 없애버리는 autoencoder, denoising autoencoder

autoencoder의 변형으로 denosing autoencoder를 생각할 수 있다.
아래의 왼쪽의 오염된 문서를 오른쪽의 문서로 변환하는 식으로 응용될 수 있다.

기계학습은 기존의 전통적인 계산체계와는 다른 새로운 형식의 계산을 추구한다.
기존의 계산 방법으로는 푸리에 변환같은 것을 상상할 수 있다.

denoising autoencoder 는 x → x 가 아니라 x x 가 되도록 훈련한 것. 여기서 x'은 노이즈를 가지고 있는 객체이다. 노이즈를 업애는 작업을 훈련시킬 수 있다.
x' → x가 되는 훈련을 시키면 된다.

가장 쉬운 생성적 적대 모델, GAN : http://incredible.egloos.com/7473304
가장 쉬운 오토인코더: http://incredible.egloos.com/7473318

---------------------------------------------------------------------------------------------------------------------
#     get MNIST
import numpy as np
from keras.datasets import mnist
(x_train, _), (x_test, y_test) = mnist.load_data()
print(x_train.shape)
print(x_test.shape)
#     noising
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1))
#     adapt this if using `channels_first` image data format
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1))
#     adapt this if using `channels_first` image data format
noise_factor = 0.5
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.)
#     make a model
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
from keras.models import Model
#     adapt this if using `channels_first` image data format
input_img = Input(shape=(28, 28, 1))
x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)
#     at this point the representation is (7, 7, 32)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
#     this model maps an input to its reconstruction
autoencoder = Model(input_img, decoded)
#     this model maps an input to its encoded representation
encoder = Model(input_img, encoded)
#     compile
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
#     train
from keras.callbacks import TensorBoard
history = autoencoder.fit(x_train_noisy, x_train, epochs=30, batch_size=256, shuffle=True, validation_data=(x_test, x_test), callbacks=[TensorBoard(log_dir='/tmp/autoencoder')])
#     test
#     encode and decode some digits
#     note that we take them from the *test* set
encoded_imgs = encoder.predict(x_test)
decoded_imgs = autoencoder.predict(x_test)
print(encoded_imgs.shape)
print('z: ' + str(encoded_imgs))
#     structure of model
from keras.utils import plot_model
plot_model(autoencoder, show_shapes=True, to_file='autoencoder.png')
#     visualize
import matplotlib.pyplot as plt
n = 10
#     how many digits we will display
plt.figure(figsize=(20, 4))
for i in range(n):
#     display original
    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)
#     display reconstruction
    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()



---------------------------------------------------------------------------------------------------------------------
import matplotlib.pyplot as plt
import numpy as np
from keras.datasets import mnist
from keras.layers import Dense
from keras.models import Sequential

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

x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = np.reshape(x_train, (len(x_train), 784))
x_test = np.reshape(x_test, (len(x_test), 784))

noise_factor = 0.5
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.)


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


model = Sequential()
model.add(Dense(128, activation='relu', input_dim=784))
model.add(Dense(64, activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(784, activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy')
model.fit(x_train_noisy, x_train, epochs=100,batch_size=256, shuffle=True, validation_data=(x_test_noisy, x_test))
decoded_imgs = model.predict(x_test)
n = 10
plt.figure(figsize=(20, 6))
for i in range(1, n+1):
    # display original
    ax = plt.subplot(3, n, i)
    plt.imshow(x_test[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
    # display noisy
    ax = plt.subplot(3, n, i + n)
    plt.imshow(x_test_noisy[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
    # display reconstruction
    ax = plt.subplot(3, n, i + 2*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()
---------------------------------------------------------------------------------------------------------------------

핑백

덧글

댓글 입력 영역

최근 포토로그



MathJax