다운로드
작성자: admin 작성일시: 2019-01-27 15:41:56 조회수: 589 다운로드: 45
카테고리: 머신 러닝 태그목록:

오토인코더

오토인코더(autoencoder1)는 입력 데이터 그 자체를 예측 목표(target)로 하는 학습 방법이다. 히든 레이터의 변수를 입력 차원보다 작게 놓으면 압축(compression) 또는 차원 감소(dimension reduction) 효과를 가진다.

In [1]:
from keras.datasets import mnist
(x_train_2d, _), (_, _) = mnist.load_data()
x_train_2d = x_train_2d.astype(np.float32) / 255.0
x_train = x_train_2d.reshape(-1, np.prod(x_train_2d.shape[1:])) 
Using TensorFlow backend.
In [2]:
import tensorflow as tf
tf.logging.set_verbosity(tf.logging.ERROR)  # warning 출력 방지
from keras.layers import Input, Dense
from keras.models import Sequential

input_dim = x_train.shape[1]
encoding_dim = 32

autoencoder1 = Sequential()
autoencoder1.add(Dense(encoding_dim, input_dim=input_dim, activation='relu'))
autoencoder1.add(Dense(input_dim, activation='sigmoid'))
autoencoder1.compile(optimizer="adadelta", loss="binary_crossentropy")
In [3]:
%%time
history = autoencoder1.fit(x_train, x_train, epochs=50, batch_size=256, shuffle=True, verbose=0)
CPU times: user 2min 25s, sys: 48.3 s, total: 3min 13s
Wall time: 1min 28s
In [4]:
n = 4
x_train_recoverd = autoencoder1.predict(x_train[:n, :])
plt.figure(figsize=(10, 4))
for i in range(n):
    plt.subplot(2, n, i + 1)
    plt.imshow(x_train[i, :].reshape(28, 28))
    plt.title("원본 이미지 {}".format(i + 1))
    plt.gray(); plt.axis("off")
    
    plt.subplot(2, n, i + 1 + n)
    plt.imshow(x_train_recoverd[i, :].reshape(28, 28))
    plt.title("복원 이미지 {}".format(i + 1))
    plt.gray(); plt.axis("off")

plt.tight_layout()
plt.show()
In [5]:
from keras.models import Model

encoder = Model(autoencoder1.input, autoencoder1.layers[0].output)
In [6]:
encoder.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1_input (InputLayer)   (None, 784)               0         
_________________________________________________________________
dense_1 (Dense)              (None, 32)                25120     
=================================================================
Total params: 25,120
Trainable params: 25,120
Non-trainable params: 0
_________________________________________________________________
In [7]:
from keras import layers

input_decoder = layers.Input(shape=(encoding_dim,))
decoder = Model(input_decoder, autoencoder1.layers[1](input_decoder))
In [8]:
decoder.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 32)                0         
_________________________________________________________________
dense_2 (Dense)              (None, 784)               25872     
=================================================================
Total params: 25,872
Trainable params: 25,872
Non-trainable params: 0
_________________________________________________________________
In [9]:
n = 4
x_train_encode = encoder.predict(x_train[:n, :])
x_train_recoverd = decoder.predict(x_train_encode)

plt.figure(figsize=(10, 5))
for i in range(n):
    plt.subplot(3, n, i + 1)
    plt.imshow(x_train[i, :].reshape(28, 28))
    plt.title("원본 이미지 {}".format(i + 1))
    plt.gray(); plt.axis("off")
    
    plt.subplot(3, n, i + 1 + n)
    plt.imshow(x_train_encode[i].reshape(1, encoding_dim), aspect=3)
    plt.title("인코딩 벡터 {}".format(i + 1))
    plt.axis("off")
    
    plt.subplot(3, n, i + 1 + 2 * n)
    plt.imshow(x_train_recoverd[i, :].reshape(28, 28))
    plt.title("복원 이미지 {}".format(i + 1))
    plt.gray(); plt.axis("off")

plt.tight_layout()
plt.show()

다층 오토인코더

In [10]:
encoding_dim1 = 128
encoding_dim2 = 64
encoding_dim3 = 32

autoencoder2 = Sequential()
autoencoder2.add(Dense(encoding_dim1, input_dim=input_dim, activation='relu'))
autoencoder2.add(Dense(encoding_dim2, activation='relu'))
autoencoder2.add(Dense(encoding_dim3, activation='relu'))
autoencoder2.add(Dense(encoding_dim2, activation='relu'))
autoencoder2.add(Dense(encoding_dim1, activation='relu'))
autoencoder2.add(Dense(input_dim, activation='sigmoid'))
autoencoder2.compile(optimizer="adadelta", loss="binary_crossentropy")
In [11]:
%%time
history = autoencoder2.fit(x_train, x_train, epochs=100, batch_size=256, shuffle=True, verbose=0)
CPU times: user 7min 12s, sys: 2min 9s, total: 9min 22s
Wall time: 4min 1s
In [12]:
n = 4
x_train_recoverd = autoencoder2.predict(x_train[:n, :])
plt.figure(figsize=(10, 4))
for i in range(n):
    plt.subplot(2, n, i + 1)
    plt.imshow(x_train[i, :].reshape(28, 28))
    plt.title("원본 이미지 {}".format(i + 1))
    plt.gray(); plt.axis("off")
    
    plt.subplot(2, n, i + 1 + n)
    plt.imshow(x_train_recoverd[i, :].reshape(28, 28))
    plt.title("복원 이미지 {}".format(i + 1))
    plt.gray(); plt.axis("off")

plt.tight_layout()
plt.show()

CNN 오토인코더

In [13]:
x_train_2d_c = np.expand_dims(x_train_2d, -1)
In [14]:
from keras.layers import Conv2D, MaxPooling2D, UpSampling2D

autoencoder3 = Sequential()
# 인코딩
autoencoder3.add(Conv2D(16, 3, input_shape=(28, 28, 1), activation='relu', padding='same'))
autoencoder3.add(MaxPooling2D(2, padding='same'))
autoencoder3.add(Conv2D(32, 3, activation='relu', padding='same'))
autoencoder3.add(MaxPooling2D(2, padding='same'))
# 디코딩
autoencoder3.add(UpSampling2D(2))
autoencoder3.add(Conv2D(16, 3, activation='relu', padding='same'))
autoencoder3.add(UpSampling2D(2))
autoencoder3.add(Conv2D(1, 3, activation='relu', padding='same'))

autoencoder3.compile(optimizer="adadelta", loss="binary_crossentropy")

autoencoder3.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 28, 28, 16)        160       
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 14, 14, 16)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 14, 14, 32)        4640      
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 7, 7, 32)          0         
_________________________________________________________________
up_sampling2d_1 (UpSampling2 (None, 14, 14, 32)        0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 14, 14, 16)        4624      
_________________________________________________________________
up_sampling2d_2 (UpSampling2 (None, 28, 28, 16)        0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 28, 28, 1)         145       
=================================================================
Total params: 9,569
Trainable params: 9,569
Non-trainable params: 0
_________________________________________________________________
In [15]:
%%time
history = autoencoder3.fit(x_train_2d_c, x_train_2d_c, 
                           epochs=10, batch_size=256, shuffle=True, verbose=0)
CPU times: user 13min 55s, sys: 1min 31s, total: 15min 27s
Wall time: 5min 16s
In [16]:
n = 4
x_train_recoverd = autoencoder3.predict(x_train_2d_c[:n])
plt.figure(figsize=(10, 4))
for i in range(n):
    plt.subplot(2, n, i + 1)
    plt.imshow(x_train_2d[i])
    plt.title("원본 이미지 {}".format(i + 1))
    plt.gray(); plt.axis("off")
    
    plt.subplot(2, n, i + 1 + n)
    plt.imshow(x_train_recoverd[i, :].reshape(28, 28))
    plt.title("복원 이미지 {}".format(i + 1))
    plt.gray(); plt.axis("off")

plt.tight_layout()
plt.show()

2D Deconvolution

In [17]:
from keras.layers import Conv2DTranspose

autoencoder4 = Sequential()
# 인코딩
autoencoder4.add(Conv2D(16, 3, input_shape=(28, 28, 1), activation='relu', padding='same'))
autoencoder4.add(MaxPooling2D(2, padding='same'))
autoencoder4.add(Conv2D(32, 3, activation='relu', padding='same'))
autoencoder4.add(MaxPooling2D(2, padding='same'))
# 디코딩
autoencoder4.add(Conv2DTranspose(32, 3, strides=2, padding='same'))
autoencoder4.add(Conv2D(16, 3, activation='relu', padding='same'))
autoencoder4.add(Conv2DTranspose(32, 3, strides=2, padding='same'))
autoencoder4.add(Conv2D(1, 3, activation='relu', padding='same'))

autoencoder4.compile(optimizer="adadelta", loss="binary_crossentropy")

autoencoder4.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_5 (Conv2D)            (None, 28, 28, 16)        160       
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 14, 14, 16)        0         
_________________________________________________________________
conv2d_6 (Conv2D)            (None, 14, 14, 32)        4640      
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 7, 7, 32)          0         
_________________________________________________________________
conv2d_transpose_1 (Conv2DTr (None, 14, 14, 32)        9248      
_________________________________________________________________
conv2d_7 (Conv2D)            (None, 14, 14, 16)        4624      
_________________________________________________________________
conv2d_transpose_2 (Conv2DTr (None, 28, 28, 32)        4640      
_________________________________________________________________
conv2d_8 (Conv2D)            (None, 28, 28, 1)         289       
=================================================================
Total params: 23,601
Trainable params: 23,601
Non-trainable params: 0
_________________________________________________________________
In [18]:
%%time
history = autoencoder4.fit(x_train_2d_c, x_train_2d_c, epochs=20, batch_size=256, shuffle=True, verbose=0)
CPU times: user 46min 17s, sys: 7min 3s, total: 53min 21s
Wall time: 15min 7s
In [19]:
n = 4
x_train_recoverd = autoencoder4.predict(x_train_2d_c[:n])
plt.figure(figsize=(10, 4))
for i in range(n):
    plt.subplot(2, n, i + 1)
    plt.imshow(x_train_2d[i])
    plt.title("원본 이미지 {}".format(i + 1))
    plt.gray(); plt.axis("off")
    
    plt.subplot(2, n, i + 1 + n)
    plt.imshow(x_train_recoverd[i, :].reshape(28, 28))
    plt.title("복원 이미지 {}".format(i + 1))
    plt.gray(); plt.axis("off")

plt.tight_layout()
plt.show()

질문/덧글

아직 질문이나 덧글이 없습니다. 첫번째 글을 남겨주세요!