BACK END/Deep Learning
[딥러닝] GAN
circle kim
2021. 4. 12. 16:15
Autoencoder
- Auto Encoder
08. 오토인코더 (AutoEncoder)
이번 포스팅은 핸즈온 머신러닝 교재를 가지고 공부한 것을 정리한 포스팅입니다. 08. 오토인코더 - Autoencoder 저번 포스팅 07. 순환 신경망, RNN에서는 자연어, 음성신호, 주식과 같은 연속적인 데
excelsior-cjh.tistory.com
- Autoencoder
: 실제 이미지를 이용하여 가상의 이미지를 생성
: 데이터 수가 충분하지않은 이미지를 얻고자 할 경우 사용
* tf_16_autoencoder.ipynb
from tensorflow.keras.datasets import mnist, fashion_mnist
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Input, Dense, MaxPool2D, Conv2D, UpSampling2D, Flatten, Reshape
import matplotlib.pyplot as plt
import numpy as np
(x_train, _), (x_test, _) = fashion_mnist.load_data() # 비지도 학습이므로 feature만 사용
#(x_train, _), (x_test, _) = mnist.load_data() # 비지도 학습이므로 feature만 사용
print(x_train[:1])
print(x_train.shape[0])
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1).astype('float32') / 255
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1).astype('float32') / 255
#print(x_train[:1])
[[[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 13 73
0 0 1 4 0 0 0 0 1 1 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 3 0 36 136 127
62 54 0 0 0 1 3 4 0 0 3]
[ 0 0 0 0 0 0 0 0 0 0 0 0 6 0 102 204 176
134 144 123 23 0 0 0 0 12 10 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 155 236 207
178 107 156 161 109 64 23 77 130 72 15]
[ 0 0 0 0 0 0 0 0 0 0 0 1 0 69 207 223 218
216 216 163 127 121 122 146 141 88 172 66]
[ 0 0 0 0 0 0 0 0 0 1 1 1 0 200 232 232 233
229 223 223 215 213 164 127 123 196 229 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 183 225 216 223
228 235 227 224 222 224 221 223 245 173 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 193 228 218 213
198 180 212 210 211 213 223 220 243 202 0]
[ 0 0 0 0 0 0 0 0 0 1 3 0 12 219 220 212 218
192 169 227 208 218 224 212 226 197 209 52]
[ 0 0 0 0 0 0 0 0 0 0 6 0 99 244 222 220 218
203 198 221 215 213 222 220 245 119 167 56]
[ 0 0 0 0 0 0 0 0 0 4 0 0 55 236 228 230 228
240 232 213 218 223 234 217 217 209 92 0]
[ 0 0 1 4 6 7 2 0 0 0 0 0 237 226 217 223 222
219 222 221 216 223 229 215 218 255 77 0]
[ 0 3 0 0 0 0 0 0 0 62 145 204 228 207 213 221 218
208 211 218 224 223 219 215 224 244 159 0]
[ 0 0 0 0 18 44 82 107 189 228 220 222 217 226 200 205 211
230 224 234 176 188 250 248 233 238 215 0]
[ 0 57 187 208 224 221 224 208 204 214 208 209 200 159 245 193 206
223 255 255 221 234 221 211 220 232 246 0]
[ 3 202 228 224 221 211 211 214 205 205 205 220 240 80 150 255 229
221 188 154 191 210 204 209 222 228 225 0]
[ 98 233 198 210 222 229 229 234 249 220 194 215 217 241 65 73 106
117 168 219 221 215 217 223 223 224 229 29]
[ 75 204 212 204 193 205 211 225 216 185 197 206 198 213 240 195 227
245 239 223 218 212 209 222 220 221 230 67]
[ 48 203 183 194 213 197 185 190 194 192 202 214 219 221 220 236 225
216 199 206 186 181 177 172 181 205 206 115]
[ 0 122 219 193 179 171 183 196 204 210 213 207 211 210 200 196 194
191 195 191 198 192 176 156 167 177 210 92]
[ 0 0 74 189 212 191 175 172 175 181 185 188 189 188 193 198 204
209 210 210 211 188 188 194 192 216 170 0]
[ 2 0 0 0 66 200 222 237 239 242 246 243 244 221 220 193 191
179 182 182 181 176 166 168 99 58 0 0]
[ 0 0 0 0 0 0 0 40 61 44 72 41 35 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0]]]
60000
autoencoder = Sequential()
# 인코더 : 차원 축소
autoencoder.add(Conv2D(32, kernel_size=3, padding='same', input_shape=(28, 28, 1), activation='relu'))
autoencoder.add(MaxPool2D(pool_size=2, padding='same'))
autoencoder.add(Conv2D(16, kernel_size=3, padding='same', activation='relu'))
autoencoder.add(MaxPool2D(pool_size=2, padding='same'))
autoencoder.add(Conv2D(8, kernel_size=3, padding='same', activation='relu'))
# 디코더 : 차원 확장
autoencoder.add(Conv2D(8, kernel_size=3, padding='same', activation='relu'))
autoencoder.add(UpSampling2D())
autoencoder.add(Conv2D(16, kernel_size=3, padding='same', activation='relu'))
autoencoder.add(UpSampling2D())
autoencoder.add(Conv2D(32, kernel_size=3, padding='same', activation='relu'))
autoencoder.add(Conv2D(1, kernel_size=3, padding='same', activation='sigmoid'))
print(autoencoder.summary())
# Layer (type) Output Shape Param #
# =================================================================
# conv2d_11 (Conv2D) (None, 28, 28, 32) 320
# _________________________________________________________________
# max_pooling2d_4 (MaxPooling2 (None, 14, 14, 32) 0
# _________________________________________________________________
# conv2d_12 (Conv2D) (None, 14, 14, 16) 4624
# _________________________________________________________________
# max_pooling2d_5 (MaxPooling2 (None, 7, 7, 16) 0
# _________________________________________________________________
# conv2d_13 (Conv2D) (None, 7, 7, 8) 1160
# _________________________________________________________________
# conv2d_14 (Conv2D) (None, 7, 7, 8) 584
# _________________________________________________________________
# up_sampling2d_2 (UpSampling2 (None, 14, 14, 8) 0
# _________________________________________________________________
# conv2d_15 (Conv2D) (None, 14, 14, 16) 1168
# _________________________________________________________________
# up_sampling2d_3 (UpSampling2 (None, 28, 28, 16) 0
# _________________________________________________________________
# conv2d_16 (Conv2D) (None, 28, 28, 32) 4640
# _________________________________________________________________
# conv2d_17 (Conv2D) (None, 28, 28, 1) 289
# =================================================================
# Total params: 12,785
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
autoencoder.fit(x_train, x_train, epochs=30, batch_size=128, validation_data=(x_test, x_test), verbose=2)
random_test = np.random.randint(x_test.shape[0], size=5)
ae_imgs = autoencoder.predict(x_test)
plt.figure(figsize=(7, 2))
for i, image_idx in enumerate(random_test):
ax = plt.subplot(2, 7, i+1)
plt.imshow(x_test[image_idx].reshape(28, 28))
ax = plt.subplot(2, 7, 7 + i + 1)
plt.imshow(ae_imgs[image_idx].reshape(28, 28))
ax.axis('off')
plt.show()
GAN
- GAN에 대해 알고 싶다면..
cafe.daum.net/flowlife/S2Ul/13
- GAN
dreamgonfly.github.io/blog/gan-explained/
- GAN : DcGAN(CNN을 GAN에 적용한 알고리즘)
: MNIST dataset으로 새로운 숫자를 생성
* tf_17_GAN.ipynb
from tensorflow.keras.datasets import mnist
from tensorflow.keras.layers import Input, Dense, Reshape, Flatten, Dropout, BatchNormalization, Activation, LeakyReLU, UpSampling2D, Conv2D
from tensorflow.keras.models import Sequential, Model
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import os
if not os.path.exists("./gen_imgs"):
os.makedirs("./gen_imgs")
np.random.seed(3)
tf.random.set_seed(3)
generator = Sequential()
generator.add(Dense(128 * 7 * 7, input_dim = 100, activation=LeakyReLU(alpha=0.2)))
generator.add(BatchNormalization())
generator.add(Reshape((7, 7, 128)))
generator.add(UpSampling2D())
generator.add(Conv2D(64, kernel_size=5, padding='same'))
generator.add(BatchNormalization())
generator.add(Activation(LeakyReLU(alpha=0.2)))
generator.add(UpSampling2D())
generator.add(Conv2D(1, kernel_size=5, padding='same', activation='tanh'))
print(generator.summary())
# Layer (type) Output Shape Param #
# =================================================================
# dense_3 (Dense) (None, 6272) 633472
# _________________________________________________________________
# batch_normalization_4 (Batch (None, 6272) 25088
# _________________________________________________________________
# reshape_3 (Reshape) (None, 7, 7, 128) 0
# _________________________________________________________________
# up_sampling2d_2 (UpSampling2 (None, 14, 14, 128) 0
# _________________________________________________________________
# conv2d_2 (Conv2D) (None, 14, 14, 64) 204864
# _________________________________________________________________
# batch_normalization_5 (Batch (None, 14, 14, 64) 256
# _________________________________________________________________
# activation_1 (Activation) (None, 14, 14, 64) 0
# _________________________________________________________________
# up_sampling2d_3 (UpSampling2 (None, 28, 28, 64) 0
# _________________________________________________________________
# conv2d_3 (Conv2D) (None, 28, 28, 1) 1601
# =================================================================
# Total params: 865,281
discriminator = Sequential()
discriminator.add(Conv2D(64, kernel_size=5, strides=2, input_shape=(28, 28, 1), padding='same'))
discriminator.add(Activation(LeakyReLU(alpha=0.2)))
discriminator.add(Conv2D(128, kernel_size=5, strides=2, padding='same'))
discriminator.add(Activation(LeakyReLU(alpha=0.2)))
discriminator.add(Flatten())
discriminator.add(Dense(1, activation='sigmoid'))
print(discriminator.summary())
# Layer (type) Output Shape Param #
# =================================================================
# conv2d_10 (Conv2D) (None, 14, 14, 64) 1664
# _________________________________________________________________
# activation_7 (Activation) (None, 14, 14, 64) 0
# _________________________________________________________________
# conv2d_11 (Conv2D) (None, 7, 7, 128) 204928
# _________________________________________________________________
# activation_8 (Activation) (None, 7, 7, 128) 0
# _________________________________________________________________
# flatten (Flatten) (None, 6272) 0
# _________________________________________________________________
# dense_5 (Dense) (None, 1) 6273
# =================================================================
# Total params: 212,865
discriminator.compile(loss='binary_crossentropy', optimizer='adam')
discriminator.trainable = False # 학습기능 해제
# GAN 모델
ginput = Input(shape=(100, ))
dis_output = discriminator(generator(ginput))
gan = Model(ginput, dis_output)
gan.compile(loss='binary_crossentropy', optimizer='adam')
print(gan.summary())
# Layer (type) Output Shape Param #
# =================================================================
# input_2 (InputLayer) [(None, 100)] 0
# _________________________________________________________________
# sequential_4 (Sequential) (None, 28, 28, 1) 865281
# _________________________________________________________________
# sequential_9 (Sequential) (None, 1) 212865
# =================================================================
# Total params: 1,078,146
# 신경망 실행 함수
def gan_train(epoch, batch_size, save_interval):
(x_train, _), (_, _) = mnist.load_data()
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1).astype('float32')
x_train = (x_train - 127.5) / 127.5
#print(x_train[0]) # -1 ~ 1 사이의 값으로 변경. generator에서 활성화 함수로 tanh를 사용했으므로
true = np.ones((batch_size, 1))
fake = np.zeros((batch_size, 1))
for i in range(epoch):
# 실제 데이터를 판별자에 입력
idx = np.random.randint(0, x_train.shape[0], batch_size)
imgs = x_train[idx]
d_loss_real = discriminator.train_on_batch(imgs, true)
# 가상 데이터를 판병자에 입력
noise = np.random.normal(0, 1, (batch_size, 100))
gen_images = generator.predict(noise)
d_loss_fake = discriminator.train_on_batch(gen_images, fake)
# 판별자와 생성자의 오차 계산
d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)
g_loss = gan.train_on_batch(noise, true)
print('epoch :%d'%i, ', d_loss : %.3f'%d_loss, ', g_loss : %.3f'%g_loss)
if i % save_interval == 0:
noise = np.random.normal(0, 1, (25, 100))
gen_images = generator.predict(noise)
gen_images = 0.5 * gen_images + 0.5
fig, axs = plt.subplots(5, 5)
count = 0
for j in range(5):
for k in range(5):
axs[j, k].imshow(gen_images[count, :, :, 0], cmap='gray')
axs[j, k].axis('off')
count += 1
fig.savefig('./gen_imgs/gan_mnist_%d.png'%i)