728x90
반응형
AE(AutoEncoder)
- 인코더와 디코더로 이루어진 경망
- 인코더(encoder) 네트워크는 고차원 입력 데이터를 저차원 표현 벡터(representation vector)로 압축한다.
- 디코더(decoder) 네트워크는 주어진 표현 벡터를 원본 차원으로 다시 압축 해제한다.
표현 벡터
- 원본 이미지를 저차원 잠재 공간(latent space)으로 압축한 것
잠재 공간의 차원이 증가하면 AE를 생성 모델로 사용하는 데 금방 문제가 발생한다.
첫 번째 AE
# AE 정의하기
import os
from utils.loaders import load_mnist
from models.AE import Autoencoder
AE = Autoencoder(
input_dim = (28,28,1)
, encoder_conv_filters = [32, 64, 64, 64]
, encoder_conv_kernel_size = [3, 3, 3, 3]
, encoder_conv_strides = [1, 2, 2, 1]
, decoder_conv_t_filters = [64, 64, 32, 1]
, decoder_conv_t_kernel_size = [3, 3, 3, 3]
, decoder_conv_t_strides = [1, 2, 2, 1]
, z_dim = 2
)
인코더 모델 구조
AE.encoder.summary()
케라스 인코더
encoder_input = Input(shape=self.input_dim, name = 'encoder_input')
x = encoder_input
for i in range(slef.n_layers_encoder):
conv_layer = Conv2D(
filters = self.encoder_conv_filters[i]
, kernel_size = self.encoder_conv_kernel_size[i]
, strides = self.encoder_conv_strides[i]
, padding = 'same'
, name = 'encoder_conv' + str(i)
)
x = conv_layer(x)
x = LeakyReLU(x)
shape_before_flattening = K.int_shape(x)[1:]
x = Flatten()(x)
encoder_output = Dense(self.z_dim, name = 'encoder_output')(x)
self.encoder = Model(encoder_inpuit, encoder_output)
디코더
- 전치 합성곱(convolutional transpose)층을 사용한다.
AE.decoder.summary()
전치 합성곱 층
- 표준 합성곱 층은 strides = 2로 설정했을 때 입력 텐서의 높이와 너비를 모두 절반으로 줄인다.
- 전치 합성곱 층은 표준 합성곱 층의 원리와 동일하지만 strides = 2로 설정해을 때 텐서의 높이와 너비를 두 배로 늘린다.
인코더와 디코더 연결
# 오토인코더
# AE
model_input = encoder_input # 인코더 입력
model_ouput = decoder(encoder_output) # AE의 출력은 디코더의 출력 디코더는 인코더의 출력을 입력으로 받는다.
self.model = Model(model_input, model_output) # 완전한 AE를 정의한 케라스 모델
컴파일
# 컴파일
optimizer = Adam(lr = learning_rate)
def r_loss(y_true, y_pred):
return K.mean(K.square(y_true - y_pred), axis = [1,2,3])
self.model.compile(optimizer = optimizer, loss = r_loss)
AE 훈련
# AE 훈련
self.model.fit(
x = x_train
, y = x_train
, batch_size = batch_size
, shuffle = True
, epochs = 10
, callbacks = callbacks_list
)
http://www.yes24.com/Product/Goods/81538614?OzSrank=1
728x90
반응형
최근댓글