-
Notifications
You must be signed in to change notification settings - Fork 32
/
began.py
181 lines (125 loc) · 5.96 KB
/
began.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
#!/usr/bin/env python
# Tensorflow impl. of BEGAN
from common import *
from datasets import data_celeba, data_mnist
from models.celeba_models import *
from models.mnist_models import *
from eval_funcs import *
def train_began(data, g_net, d_enc, d_dec, name='BEGAN',
dim_z=128, n_iters=1e5, lr=1e-4, batch_size=128,
sampler=sample_z, eval_funcs=[],
l_k=0.001, g_k=0.5):
### 0. Common preparation
hyperparams = {'LR': lr}
base_dir, out_dir, log_dir = create_dirs(name, g_net.name, d_enc.name, hyperparams)
tf.reset_default_graph()
global_step = tf.Variable(0, trainable=False)
increment_step = tf.assign_add(global_step, 1)
lr = tf.constant(lr)
### 1. Define network structure
x_shape = data.train.images[0].shape
z0 = tf.placeholder(tf.float32, shape=[None, dim_z]) # Latent var.
x0 = tf.placeholder(tf.float32, shape=(None,) + x_shape) # Generated images
# BEGAN-specific vars.
k = tf.Variable(0.0, trainable=False)
def began_disc(x, name, **kwargs):
h0 = d_enc(x, name + '_ENC', **kwargs)
x_ = d_dec(h0, name + '_DEC', **kwargs)
out = tf.reduce_mean(tf.reduce_sum((x - x_) ** 2, 1))
return out
G = g_net(z0, 'BEGAN_G')
D_real = began_disc(x0, 'BEGAN_D')
D_fake = began_disc(G, 'BEGAN_D', reuse=True)
# Loss functions
D_loss = tf.reduce_mean(D_real - k * D_fake)
G_loss = tf.reduce_mean(D_fake)
D_solver = (tf.train.AdamOptimizer(learning_rate=lr, beta1=0.5)) \
.minimize(D_loss, var_list=get_trainable_params('BEGAN_D'))
G_solver = (tf.train.AdamOptimizer(learning_rate=lr, beta1=0.5)) \
.minimize(G_loss, var_list=get_trainable_params('BEGAN_G'))
# Convergence metric
M = D_real + tf.abs(g_k * D_real - D_fake)
# update k
update_k = k.assign(k + l_k * (g_k * D_real - D_fake))
#### 2. Operations for log/state back-up
tf.summary.scalar('BEGAN_D(x)', tf.reduce_mean(D_real))
tf.summary.scalar('BEGAN_D(G)', tf.reduce_mean(D_fake))
tf.summary.scalar('BEGAN_D_loss', tf.reduce_mean(D_loss))
tf.summary.scalar('BEGAN_G_loss', tf.reduce_mean(G_loss))
tf.summary.scalar('k', k)
tf.summary.scalar('M', M)
if check_dataset_type(x_shape) != 'synthetic':
tf.summary.image('BEGAN', G, max_outputs=4)
summaries = tf.summary.merge_all()
saver = tf.train.Saver(get_trainable_params('BEGAN_D') + get_trainable_params('BEGAN_G'))
# Initial setup for visualization
outputs = [G]
figs = [None] * len(outputs)
fig_names = ['fig_BEGAN_gen_{:04d}.png']
plt.ion()
### 3. Run a session
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.95)
sess = tf.Session(config=tf.ConfigProto(log_device_placement=False, allow_soft_placement=False, gpu_options=gpu_options))
sess.run(tf.global_variables_initializer())
writer = tf.summary.FileWriter(log_dir, sess.graph)
print('{:>10}, {:>7}, {:>7}, {:>7}') \
.format('Iters', 'cur_LR', 'BEGAN_D', 'BEGAN_G')
for it in range(int(n_iters)):
batch_xs, batch_ys = data.train.next_batch(batch_size)
_, loss_D = sess.run(
[D_solver, D_loss],
feed_dict={x0: batch_xs, z0: sampler(batch_size, dim_z)}
)
_, loss_G = sess.run(
[G_solver, G_loss],
feed_dict={z0: sampler(batch_size, dim_z)}
)
cur_k = sess.run(update_k, feed_dict={x0: batch_xs, z0: sampler(batch_size, dim_z)})
_, cur_lr = sess.run([increment_step, lr])
if it % PRNT_INTERVAL == 0:
print('{:10d}, {:1.4f}, {: 1.4f}, {: 1.4f}, {: 1.4f}') \
.format(it, cur_lr, loss_D, loss_G, cur_k)
# Tensorboard
cur_summary = sess.run(summaries, feed_dict={x0: batch_xs, z0: sampler(batch_size, dim_z)})
writer.add_summary(cur_summary, it)
if it % EVAL_INTERVAL == 0:
img_generator = lambda n: sess.run(output, feed_dict={z0: sampler(n, dim_z)})
for i, output in enumerate(outputs):
figs[i] = data.plot(img_generator, fig_id=i)
figs[i].canvas.draw()
plt.savefig(out_dir + fig_names[i].format(it / 1000), bbox_inches='tight')
# Run evaluation functions
for func in eval_funcs:
func(it, img_generator)
if it % SAVE_INTERVAL == 0:
saver.save(sess, out_dir + 'began', it)
sess.close()
if __name__ == '__main__':
args = parse_args(additional_args=[])
print args
if args.gpu:
set_gpu(args.gpu)
if args.datasets == 'mnist':
out_name = 'BEGAN_mnist'
out_name = out_name if len(args.tag) == 0 else '{}_{}'.format(out_name, args.tag)
dim_z = 64
dim_h = 16
data = data_mnist.MnistWrapper('datasets/mnist/')
# BEGAN doesn't seem to work well with BN
g_net = SimpleGEN(dim_z, last_act=tf.sigmoid, bn=False)
d_enc = SimpleCNN(n_out=dim_h, last_act=tf.identity, bn=False)
d_dec = SimpleGEN(n_in=dim_h, last_act=tf.sigmoid, bn=False)
train_began(data, g_net, d_enc, d_dec, name=out_name, dim_z=dim_z, batch_size=args.batchsize, lr=args.lr,
eval_funcs=[lambda it, gen: eval_images_naive(it, gen, data)])
elif args.datasets == 'celeba':
out_name = 'BEGAN_celeba'
out_name = out_name if len(args.tag) == 0 else '{}_{}'.format(out_name, args.tag)
dim_z = 128
dim_h = 64
data = data_celeba.CelebA('datasets/img_align_celeba')
# BEGAN doesn't seem to work well with BN
g_net = DCGAN_G(dim_z, last_act=tf.sigmoid, bn=False)
d_enc = DCGAN_D(n_out=dim_h, last_act=tf.identity, bn=False)
d_dec = DCGAN_G(n_in=dim_h, last_act=tf.sigmoid, bn=False)
train_began(data, g_net, d_enc, d_dec, name=out_name, dim_z=dim_z, batch_size=args.batchsize, lr=args.lr,
eval_funcs=[lambda it, gen: eval_images_naive(it, gen, data)])