-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex7_2_gan_cnn_mnist_tf.py
206 lines (166 loc) · 7.08 KB
/
ex7_2_gan_cnn_mnist_tf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
################################
# 공통 패키지 불러오기
################################
import numpy as np
from PIL import Image
import math
import os
from keras import models, layers, optimizers
from keras.datasets import mnist
import keras.backend as K
print(K.image_data_format)
import tensorflow as tf
def mse_4d(y_true, y_pred):
return K.mean(K.square(y_pred - y_true), axis=(1,2,3))
def mse_4d_tf(y_true, y_pred):
return tf.reduce_mean(tf.square(y_pred - y_true), axis=(1,2,3))
################################
# GAN 모델링
################################
class GAN(models.Sequential):
def __init__(self, input_dim=32): # input_dim = args.n_train = 32
"""
self, self.generator, self.discriminator are all models
"""
super().__init__()
self.input_dim = input_dim
self.generator = self.GENERATOR()
self.discriminator = self.DISCRIMINATOR()
self.add(self.generator)
self.discriminator.trainable = False
self.add(self.discriminator)
self.compile_all()
def compile_all(self):
# Compiling stage
d_optim = optimizers.SGD(lr=0.0005, momentum=0.9, nesterov=True)
g_optim = optimizers.SGD(lr=0.0005, momentum=0.9, nesterov=True)
self.generator.compile(loss=mse_4d_tf, optimizer="SGD")
self.compile(loss='binary_crossentropy', optimizer=g_optim)
self.discriminator.trainable = True
self.discriminator.compile(loss='binary_crossentropy', optimizer=d_optim)
def GENERATOR(self):
input_dim = self.input_dim
model = models.Sequential()
model.add(layers.Dense(1024, activation='tanh', input_dim=input_dim))
model.add(layers.Dense(7 * 7 * 128, activation='tanh')) # H, W, C = 7, 7, 128
model.add(layers.BatchNormalization())
# The Conv2D op currently only supports the NHWC tensor format on the CPU.
model.add(layers.Reshape((7, 7, 128), input_shape=(7 * 7 * 128,)))
model.add(layers.UpSampling2D(size=(2, 2)))
model.add(layers.Conv2D(64, (5, 5), padding='same', activation='tanh'))
model.add(layers.UpSampling2D(size=(2, 2)))
model.add(layers.Conv2D(1, (5, 5), padding='same', activation='tanh'))
return model
def DISCRIMINATOR(self):
# The Conv2D op currently only supports the NHWC tensor format on the CPU.
model = models.Sequential()
model.add(layers.Conv2D(64, (5, 5), padding='same', activation='tanh',
input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D(pool_size=(2, 2)))
model.add(layers.Conv2D(128, (5, 5), activation='tanh'))
model.add(layers.MaxPooling2D(pool_size=(2, 2)))
model.add(layers.Flatten())
model.add(layers.Dense(1024, activation='tanh'))
model.add(layers.Dense(1, activation='sigmoid'))
return model
def get_z(self, ln):
input_dim = self.input_dim
return np.random.uniform(-1, 1, (ln, input_dim))
def train_both(self, x):
ln = x.shape[0]
# First trial for training discriminator
z = self.get_z(ln)
w = self.generator.predict(z, verbose=0)
xw = np.concatenate((x, w))
y2 = np.array([1] * ln + [0] * ln).reshape(-1,1) # Necessary!
d_loss = self.discriminator.train_on_batch(xw, y2)
# Second trial for training generator
z = self.get_z(ln)
self.discriminator.trainable = False
g_loss = self.train_on_batch(z, np.array([1] * ln).reshape(-1, 1))
self.discriminator.trainable = True
return d_loss, g_loss
################################
# GAN 학습하기
################################
def combine_images(generated_images):
num = generated_images.shape[0]
width = int(math.sqrt(num))
height = int(math.ceil(float(num) / width))
shape = generated_images.shape[1:3] # (1,2) for NHWC
image = np.zeros((height * shape[0], width * shape[1]),
dtype=generated_images.dtype)
for index, img in enumerate(generated_images):
i = int(index / width)
j = index % width
image[i * shape[0]:(i + 1) * shape[0],
j * shape[1]:(j + 1) * shape[1]] = img[ :, :, 0] # NHWC
return image
def get_x(X_train, index, BATCH_SIZE):
return X_train[index * BATCH_SIZE:(index + 1) * BATCH_SIZE]
def save_images(generated_images, output_fold, epoch, index):
image = combine_images(generated_images)
image = image * 127.5 + 127.5
Image.fromarray(image.astype(np.uint8)).save(
output_fold + '/' +
str(epoch) + "_" + str(index) + ".png")
def load_data(n_train):
(X_train, y_train), (_, _) = mnist.load_data()
return X_train[:n_train]
def train(args):
BATCH_SIZE = args.batch_size
epochs = args.epochs
output_fold = args.output_fold
input_dim = args.input_dim
n_train = args.n_train
os.makedirs(output_fold, exist_ok=True)
print('Output_fold is', output_fold)
X_train = load_data(n_train)
X_train = (X_train.astype(np.float32) - 127.5) / 127.5
# The Conv2D op currently only supports the NHWC tensor format on the CPU. The op was given the format: NCHW
# X_train = X_train.reshape((X_train.shape[0], 1) + X_train.shape[1:]) # <-- NCHW format
X_train = X_train.reshape(X_train.shape + (1,)) # <-- NHWC format
gan = GAN(input_dim)
d_loss_ll = []
g_loss_ll = []
for epoch in range(epochs):
if epoch % 10 == 0:
print("Epoch is", epoch)
print("Number of batches", int(X_train.shape[0] / BATCH_SIZE))
d_loss_l = []
g_loss_l = []
for index in range(int(X_train.shape[0] / BATCH_SIZE)):
x = get_x(X_train, index, BATCH_SIZE)
d_loss, g_loss = gan.train_both(x)
d_loss_l.append(d_loss)
g_loss_l.append(g_loss)
if epoch % 10 == 0 or epoch == epochs - 1:
z = gan.get_z(x.shape[0])
w = gan.generator.predict(z, verbose=0)
save_images(w, output_fold, epoch, 0)
d_loss_ll.append(d_loss_l)
g_loss_ll.append(g_loss_l)
# gan.generator.save_weights(output_fold + '/' + 'generator', True)
# gan.discriminator.save_weights(output_fold + '/' + 'discriminator', True)
np.savetxt(output_fold + '/' + 'd_loss', d_loss_ll)
np.savetxt(output_fold + '/' + 'g_loss', g_loss_ll)
################################
# GAN 예제 실행하기
################################
import argparse
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--batch_size', type=int, default=16,
help='Batch size for the networks')
parser.add_argument('--epochs', type=int, default=1000,
help='Epochs for the networks')
parser.add_argument('--output_fold', type=str, default='GAN_OUT',
help='Output fold to save the results')
parser.add_argument('--input_dim', type=int, default=10,
help='Input dimension for the generator.')
parser.add_argument('--n_train', type=int, default=32,
help='The number of training data.')
args = parser.parse_args()
train(args)
if __name__ == '__main__':
main()