-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgan_mnist_cnn_sgd.py
179 lines (134 loc) · 5.93 KB
/
gan_mnist_cnn_sgd.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
import math
import os
import argparse
import numpy as np
from PIL import Image
from keras import models, layers, optimizers
from keras.datasets import mnist
import keras.backend as K
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))
class GAN(models.Sequential):
def __init__(self, input_dim=64):
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(128 * 7 * 7, activation='tanh'))
model.add(layers.BatchNormalization())
model.add(layers.Reshape((128, 7, 7), input_shape=(128 * 7 * 7,)))
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):
model = models.Sequential()
model.add(layers.Conv2D(64, (5, 5), padding='same', activation='tanh', input_shape=(1, 28, 28)))
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]
z = self.get_z(ln)
w = self.generator.predict(z, verbose=0)
xw = np.concatenate((x, w))
y2 = [1] * ln + [0] * ln
d_loss = self.discriminator.train_on_batch(xw, y2)
z = self.get_z(ln)
self.discriminator.trainable = False
g_loss = self.train_on_batch(z, [1] * ln)
self.discriminator.trainable = True
return d_loss, g_loss
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[2:]
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, :, :]
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
X_train = X_train.reshape((X_train.shape[0], 1) + X_train.shape[1:])
gan = GAN(input_dim)
d_loss_ll = []
g_loss_ll = []
for epoch in range(epochs):
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)
if __name__ == '__main__':
K.set_image_data_format('channels_first')
print(K.image_data_format)
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=2000, 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=100, 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)