-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmodels.py
278 lines (238 loc) · 12.2 KB
/
models.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
from functools import partial
import os
import numpy as np
import tensorflow as tf
from ops import lrelu, linear, conv2d, deconv2d
from utils import make_batches, Prior, conv_out_size_same, create_image_grid
batch_norm = partial(tf.contrib.layers.batch_norm,
decay=0.9,
updates_collections=None,
epsilon=1e-5,
scale=True)
class MGAN(object):
"""Mixture Generative Adversarial Nets
"""
def __init__(self,
model_name='MGAN',
beta=1.0,
num_z=128,
num_gens=4,
d_batch_size=64,
g_batch_size=32,
z_prior="uniform",
same_input=True,
learning_rate=0.0002,
img_size=(32, 32, 3), # (height, width, channels)
num_conv_layers=3,
num_gen_feature_maps=128, # number of feature maps of generator
num_dis_feature_maps=128, # number of feature maps of discriminator
sample_fp=None,
sample_by_gen_fp=None,
num_epochs=25000,
random_seed=6789):
self.beta = beta
self.num_z = num_z
self.num_gens = num_gens
self.d_batch_size = d_batch_size
self.g_batch_size = g_batch_size
self.z_prior = Prior(z_prior)
self.same_input = same_input
self.learning_rate = learning_rate
self.num_epochs = num_epochs
self.img_size = img_size
self.num_conv_layers = num_conv_layers
self.num_gen_feature_maps = num_gen_feature_maps
self.num_dis_feature_maps = num_dis_feature_maps
self.sample_fp = sample_fp
self.sample_by_gen_fp = sample_by_gen_fp
self.random_seed = random_seed
def _init(self):
self.epoch = 0
# TensorFlow's initialization
self.tf_graph = tf.Graph()
self.tf_config = tf.ConfigProto()
self.tf_config.gpu_options.allow_growth = True
self.tf_config.log_device_placement = False
self.tf_config.allow_soft_placement = True
self.tf_session = tf.Session(config=self.tf_config, graph=self.tf_graph)
np.random.seed(self.random_seed)
with self.tf_graph.as_default():
tf.set_random_seed(self.random_seed)
def _build_model(self):
arr = np.array([i // self.g_batch_size for i in range(self.g_batch_size * self.num_gens)])
d_mul_labels = tf.constant(arr, dtype=tf.int32)
self.x = tf.placeholder(tf.float32, [None,
self.img_size[0], self.img_size[1], self.img_size[2]],
name="real_data")
self.z = tf.placeholder(tf.float32, [self.g_batch_size * self.num_gens, self.num_z], name='noise')
# create generator G
self.g = self._create_generator(self.z)
# create sampler to generate samples
self.sampler = self._create_generator(self.z, train=False, reuse=True)
# create discriminator D
d_bin_x_logits, d_mul_x_logits = self._create_discriminator(self.x)
d_bin_g_logits, d_mul_g_logits = self._create_discriminator(self.g, reuse=True)
# define loss functions
self.d_bin_x_loss = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(
logits=d_bin_x_logits, labels=tf.ones_like(d_bin_x_logits)),
name='d_bin_x_loss')
self.d_bin_g_loss = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(
logits=d_bin_g_logits, labels=tf.zeros_like(d_bin_g_logits)),
name='d_bin_g_loss')
self.d_bin_loss = tf.add(self.d_bin_x_loss, self.d_bin_g_loss, name='d_bin_loss')
self.d_mul_loss = tf.reduce_mean(
tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=d_mul_g_logits, labels=d_mul_labels),
name="d_mul_loss")
self.d_loss = tf.add(self.d_bin_loss, self.d_mul_loss, name="d_loss")
self.g_bin_loss = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(
logits=d_bin_g_logits, labels=tf.ones_like(d_bin_g_logits)),
name="g_bin_loss")
self.g_mul_loss = tf.multiply(self.beta, self.d_mul_loss, name='g_mul_loss')
self.g_loss = tf.add(self.g_bin_loss, self.g_mul_loss, name="g_loss")
# create optimizers
self.d_opt = self._create_optimizer(self.d_loss, scope='discriminator',
lr=self.learning_rate)
self.g_opt = self._create_optimizer(self.g_loss, scope='generator',
lr=self.learning_rate)
def _create_generator(self, z, train=True, reuse=False, name="generator"):
out_size = [(conv_out_size_same(self.img_size[0], 2),
conv_out_size_same(self.img_size[1], 2),
self.num_gen_feature_maps)]
for i in range(self.num_conv_layers - 1):
out_size = [(conv_out_size_same(out_size[0][0], 2),
conv_out_size_same(out_size[0][1], 2),
out_size[0][2] * 2)] + out_size
with tf.variable_scope(name) as scope:
if reuse:
scope.reuse_variables()
z_split = tf.split(z, self.num_gens, axis=0)
h0 = []
for i, var in enumerate(z_split):
h0.append(tf.nn.relu(batch_norm(linear(var, out_size[0][0] * out_size[0][1] * out_size[0][2],
scope='g_h0_linear{}'.format(i), stddev=0.02),
is_training=train,
scope="g_h0_bn{}".format(i)),
name="g_h0_relu{}".format(i)))
h = []
for var in h0:
h.append(tf.reshape(var, [self.g_batch_size, out_size[0][0], out_size[0][1], out_size[0][2]]))
h = tf.concat(h, axis=0, name="g_h0_relu")
for i in range(1, self.num_conv_layers):
h = tf.nn.relu(
batch_norm(
deconv2d(h,
[self.g_batch_size * self.num_gens, out_size[i][0], out_size[i][1], out_size[i][2]],
stddev=0.02, name="g_h{}_deconv".format(i)),
is_training=train,
center=False,
scope="g_h{}_bn".format(i)),
name="g_h{}_relu".format(i))
g_out = tf.nn.tanh(
deconv2d(h,
[self.g_batch_size * self.num_gens, self.img_size[0], self.img_size[1], self.img_size[2]],
stddev=0.02, name="g_out_deconv"),
name="g_out_tanh")
return g_out
def _create_discriminator(self, x, train=True, reuse=False, name="discriminator"):
with tf.variable_scope(name) as scope:
if reuse:
scope.reuse_variables()
h = x
for i in range(self.num_conv_layers):
h = lrelu(batch_norm(conv2d(h, self.num_dis_feature_maps * (2 ** i),
stddev=0.02, name="d_h{}_conv".format(i)),
is_training=train,
scope="d_bn{}".format(i)))
dim = h.get_shape()[1:].num_elements()
h = tf.reshape(h, [-1, dim])
d_bin_logits = linear(h, 1, scope='d_bin_logits')
d_mul_logits = linear(h, self.num_gens, scope='d_mul_logits')
return d_bin_logits, d_mul_logits
def _create_optimizer(self, loss, scope, lr):
params = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=scope)
opt = tf.train.AdamOptimizer(lr, beta1=0.5)
grads = opt.compute_gradients(loss, var_list=params)
train_op = opt.apply_gradients(grads)
return train_op
def fit(self, x):
if (not hasattr(self, 'epoch')) or self.epoch == 0:
self._init()
with self.tf_graph.as_default():
self._build_model()
self.tf_session.run(tf.global_variables_initializer())
num_data = x.shape[0] - x.shape[0] % self.d_batch_size
batches = make_batches(num_data, self.d_batch_size)
best_is = 0.0
while (self.epoch < self.num_epochs):
for batch_idx, (batch_start, batch_end) in enumerate(batches):
batch_size = batch_end - batch_start
x_batch = x[batch_start:batch_end]
if self.same_input:
z_batch = self.z_prior.sample([self.g_batch_size, self.num_z]).astype(np.float32)
z_batch = np.vstack([z_batch] * self.num_gens)
else:
z_batch = self.z_prior.sample([self.g_batch_size * self.num_gens, self.num_z]).astype(np.float32)
# update discriminator D
d_bin_loss, d_mul_loss, d_loss, _ = self.tf_session.run(
[self.d_bin_loss, self.d_mul_loss, self.d_loss, self.d_opt],
feed_dict={self.x: x_batch, self.z: z_batch})
# update generator G
g_bin_loss, g_mul_loss, g_loss, _ = self.tf_session.run(
[self.g_bin_loss, self.g_mul_loss, self.g_loss, self.g_opt],
feed_dict={self.z: z_batch})
self.epoch += 1
print("Epoch: [%4d/%4d] d_bin_loss: %.5f, d_mul_loss: %.5f, d_loss: %.5f,"
" g_bin_loss: %.5f, g_mul_loss: %.5f, g_loss: %.5f" % (self.epoch, self.num_epochs,
d_bin_loss, d_mul_loss, d_loss, g_bin_loss, g_mul_loss, g_loss))
self._samples(self.sample_fp.format(epoch=self.epoch+1))
self._samples_by_gen(self.sample_by_gen_fp.format(epoch=self.epoch+1))
def _generate(self, num_samples=100):
sess = self.tf_session
batch_size = self.g_batch_size * self.num_gens
num = ((num_samples - 1) // batch_size + 1) * batch_size
z = self.z_prior.sample([num, self.num_z]).astype(np.float32)
x = np.zeros([num, self.img_size[0], self.img_size[1], self.img_size[2]],
dtype=np.float32)
batches = make_batches(num, batch_size)
for batch_idx, (batch_start, batch_end) in enumerate(batches):
z_batch = z[batch_start:batch_end]
x[batch_start:batch_end] = sess.run(self.sampler,
feed_dict={self.z: z_batch})
idx = np.random.permutation(num)[:num_samples]
x = (x[idx] + 1.0) / 2.0
return x
def _samples(self, filepath, tile_shape=(10, 10)):
if not os.path.exists(os.path.dirname(filepath)):
os.makedirs(os.path.dirname(filepath))
num_samples = tile_shape[0] * tile_shape[1]
x = self._generate(num_samples)
imgs = create_image_grid(x, img_size=self.img_size, tile_shape=tile_shape)
import scipy.misc
scipy.misc.imsave(filepath, imgs)
def _samples_by_gen(self, filepath):
if not os.path.exists(os.path.dirname(filepath)):
os.makedirs(os.path.dirname(filepath))
num_samples = self.num_gens * 10
tile_shape = (self.num_gens, 10)
sess = self.tf_session
img_per_gen = num_samples // self.num_gens
x = np.zeros([num_samples, self.img_size[0], self.img_size[1], self.img_size[2]],
dtype=np.float32)
for i in range(0, img_per_gen, self.g_batch_size):
z_batch = self.z_prior.sample([self.g_batch_size * self.num_gens, self.num_z]).astype(np.float32)
samples = sess.run(self.sampler, feed_dict={self.z: z_batch})
for gen in range(self.num_gens):
x[gen * img_per_gen + i:gen * img_per_gen + min(i + self.g_batch_size, img_per_gen)] = \
samples[
gen * self.g_batch_size:gen * self.g_batch_size + min(self.g_batch_size, img_per_gen)]
x = (x + 1.0) / 2.0
imgs = create_image_grid(x, img_size=self.img_size, tile_shape=tile_shape)
import scipy.misc
scipy.misc.imsave(filepath, imgs)