-
Notifications
You must be signed in to change notification settings - Fork 0
/
gan.py
187 lines (156 loc) · 5.63 KB
/
gan.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
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 18 12:22:17 2019
@author: tanma
"""
from keras.layers import Activation, Dense, Input
from keras.layers import Conv2D, Flatten
from keras.layers import Reshape, Conv2DTranspose
from keras.layers import LeakyReLU
from keras.layers import BatchNormalization
from keras.models import Model
from keras.layers.merge import concatenate
import numpy as np
import math
import matplotlib.pyplot as plt
import os
def generator(inputs,
image_size,
activation='sigmoid',
labels=None,
codes=None):
image_resize = image_size // 4
kernel_size = 5
layer_filters = [128, 64, 32, 1]
if labels is not None:
if codes is None:
inputs = [inputs, labels]
else:
inputs = [inputs, labels] + codes
x = concatenate(inputs, axis=1)
elif codes is not None:
inputs = [inputs, codes]
x = concatenate(inputs, axis=1)
else:
x = inputs
x = Dense(image_resize * image_resize * layer_filters[0])(x)
x = Reshape((image_resize, image_resize, layer_filters[0]))(x)
for filters in layer_filters:
if filters > layer_filters[-2]:
strides = 2
else:
strides = 1
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2DTranspose(filters=filters,
kernel_size=kernel_size,
strides=strides,
padding='same')(x)
if activation is not None:
x = Activation(activation)(x)
return Model(inputs, x, name='generator')
def discriminator(inputs,
activation='sigmoid',
num_labels=None,
num_codes=None):
kernel_size = 5
layer_filters = [32, 64, 128, 256]
x = inputs
for filters in layer_filters:
if filters == layer_filters[-1]:
strides = 1
else:
strides = 2
x = LeakyReLU(alpha=0.2)(x)
x = Conv2D(filters=filters,
kernel_size=kernel_size,
strides=strides,
padding='same')(x)
x = Flatten()(x)
outputs = Dense(1)(x)
if activation is not None:
print(activation)
outputs = Activation(activation)(outputs)
if num_labels:
layer = Dense(layer_filters[-2])(x)
labels = Dense(num_labels)(layer)
labels = Activation('softmax', name='label')(labels)
if num_codes is None:
outputs = [outputs, labels]
else:
code1 = Dense(1)(layer)
code1 = Activation('sigmoid', name='code1')(code1)
code2 = Dense(1)(layer)
code2 = Activation('sigmoid', name='code2')(code2)
outputs = [outputs, labels, code1, code2]
elif num_codes is not None:
z0_recon = Dense(num_codes)(x)
z0_recon = Activation('tanh', name='z0')(z0_recon)
outputs = [outputs, z0_recon]
return Model(inputs, outputs, name='discriminator')
def train(models, x_train, params):
generator, discriminator, adversarial = models
batch_size, latent_size, train_steps, model_name = params
save_interval = 100
noise_input = np.random.uniform(-1.0, 1.0, size=[16, latent_size])
train_size = x_train.shape[0]
for i in range(train_steps):
rand_indexes = np.random.randint(0, train_size, size=batch_size)
real_images = x_train[rand_indexes]
noise = np.random.uniform(-1.0, 1.0, size=[batch_size, latent_size])
fake_images = generator.predict(noise)
x = np.concatenate((real_images, fake_images))
y = np.ones([2 * batch_size, 1])
y[batch_size:, :] = 0.0
loss, acc = discriminator.train_on_batch(x, y)
log = "%d: [discriminator loss: %f, acc: %f]" % (i, loss, acc)
noise = np.random.uniform(-1.0, 1.0, size=[batch_size, latent_size])
y = np.ones([batch_size, 1])
loss, acc = adversarial.train_on_batch(noise, y)
log = "%s [adversarial loss: %f, acc: %f]" % (log, loss, acc)
print(log)
if (i + 1) % save_interval == 0:
if (i + 1) == train_steps:
show = True
else:
show = False
plot_images(generator,
noise_input=noise_input,
show=show,
step=(i + 1),
model_name=model_name)
generator.save(model_name + ".h5")
def plot_images(generator,
noise_input,
noise_label=None,
noise_codes=None,
show=False,
step=0,
model_name="gan"):
os.makedirs(model_name, exist_ok=True)
filename = os.path.join(model_name, "%05d.png" % step)
rows = int(math.sqrt(noise_input.shape[0]))
if noise_label is not None:
noise_input = [noise_input, noise_label]
if noise_codes is not None:
noise_input += noise_codes
images = generator.predict(noise_input)
plt.figure(figsize=(2.2, 2.2))
num_images = images.shape[0]
image_size = images.shape[1]
for i in range(num_images):
plt.subplot(rows, rows, i + 1)
image = np.reshape(images[i], [image_size, image_size])
plt.imshow(image, cmap='gray')
plt.axis('off')
plt.savefig(filename)
if show:
plt.show()
else:
plt.close('all')
def test_generator(generator):
noise_input = np.random.uniform(-1.0, 1.0, size=[16, 100])
plot_images(generator,
noise_input=noise_input,
show=True,
model_name="test_outputs")