forked from khanrc/tf.gans-comparison
-
Notifications
You must be signed in to change notification settings - Fork 0
/
began.py
160 lines (126 loc) · 6.46 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
# coding: utf-8
import tensorflow as tf
slim = tf.contrib.slim
from utils import expected_shape
import ops
from basemodel import BaseModel
class BEGAN(BaseModel):
def __init__(self, name, training, image_shape=[64, 64, 3], z_dim=64, gamma=0.5):
self.gamma = gamma
super(BEGAN, self).__init__(name=name, training=training, image_shape=image_shape, z_dim=z_dim)
def _build_train_graph(self):
with tf.variable_scope(self.name):
X = tf.placeholder(tf.float32, [None] + self.shape)
z = tf.placeholder(tf.float32, [None, self.z_dim])
global_step = tf.Variable(0, name='global_step', trainable=False)
G = self._generator(z)
# Discriminator is not called an energy function in BEGAN. The naming is from EBGAN.
D_real_energy = self._discriminator(X)
D_fake_energy = self._discriminator(G, reuse=True)
k = tf.Variable(0., name='k', trainable=False)
with tf.variable_scope('D_loss'):
D_loss = D_real_energy - k * D_fake_energy
with tf.variable_scope('G_loss'):
G_loss = D_fake_energy
with tf.variable_scope('balance'):
balance = self.gamma*D_real_energy - D_fake_energy
with tf.variable_scope('M'):
M = D_real_energy + tf.abs(balance)
D_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name+'/D/')
G_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name+'/G/')
D_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS, scope=self.name+'/D/')
G_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS, scope=self.name+'/G/')
# The authors suggest decaying learning rate by 0.5 when the convergence mesure stall
# carpedm20 decays by 0.5 per 100000 steps
# Heumi decays by 0.95 per 2000 steps (https://github.com/Heumi/BEGAN-tensorflow/)
st_lr = 1e-4
lr = tf.train.exponential_decay(st_lr, global_step, 3000, 0.95, staircase=True)
with tf.variable_scope('D_train_op'):
with tf.control_dependencies(D_update_ops):
D_train_op = tf.train.AdamOptimizer(learning_rate=lr, beta1=0.5).minimize(D_loss, var_list=D_vars)
with tf.variable_scope('G_train_op'):
with tf.control_dependencies(G_update_ops):
G_train_op = tf.train.AdamOptimizer(learning_rate=lr, beta1=0.5).minimize(G_loss, var_list=G_vars, global_step=global_step)
# It should be ops `define` under control_dependencies
lambd_k = 0.001
with tf.control_dependencies([D_train_op]): # should be iterable
with tf.variable_scope('update_k'):
update_k = tf.assign(k, tf.clip_by_value(k + lambd_k * balance, 0., 1.)) # define
D_train_op = update_k # run op
# summaries
# per-step summary
self.summary_op = tf.summary.merge([
tf.summary.scalar('G_loss', G_loss),
tf.summary.scalar('D_loss', D_loss),
tf.summary.scalar('D_energy/real', D_real_energy),
tf.summary.scalar('D_energy/fake', D_fake_energy),
tf.summary.scalar('convergence_measure', M),
tf.summary.scalar('balance', balance),
tf.summary.scalar('k', k),
tf.summary.scalar('lr', lr)
])
# sparse-step summary
tf.summary.image('fake_sample', G, max_outputs=6)
# histogram all varibles
# for var in tf.trainable_variables():
# tf.summary.histogram(var.op.name, var)
self.all_summary_op = tf.summary.merge_all()
# accesible points
self.X = X
self.z = z
self.D_train_op = D_train_op
self.G_train_op = G_train_op
self.fake_sample = G
self.global_step = global_step
def _encoder(self, X, reuse=False):
with tf.variable_scope('encoder', reuse=reuse):
nf = 128
nh = self.z_dim
with slim.arg_scope([slim.conv2d], kernel_size=[3,3], padding='SAME', activation_fn=tf.nn.elu):
net = slim.conv2d(X, nf)
net = slim.conv2d(net, nf)
net = slim.conv2d(net, nf)
net = slim.conv2d(net, nf*2, stride=2) # 32x32
net = slim.conv2d(net, nf*2)
net = slim.conv2d(net, nf*2)
net = slim.conv2d(net, nf*3, stride=2) # 16x16
net = slim.conv2d(net, nf*3)
net = slim.conv2d(net, nf*3)
net = slim.conv2d(net, nf*4, stride=2) # 8x8
net = slim.conv2d(net, nf*4)
net = slim.conv2d(net, nf*4)
net = slim.conv2d(net, nf*4)
net = slim.flatten(net)
h = slim.fully_connected(net, nh, activation_fn=None)
return h
def _decoder(self, h, reuse=False):
with tf.variable_scope('decoder', reuse=reuse):
nf = 128
nh = self.z_dim
h0 = slim.fully_connected(h, 8*8*nf, activation_fn=None) # h0
net = tf.reshape(h0, [-1, 8, 8, nf])
with slim.arg_scope([slim.conv2d], kernel_size=[3,3], padding='SAME', activation_fn=tf.nn.elu):
net = slim.conv2d(net, nf)
net = slim.conv2d(net, nf)
net = tf.image.resize_nearest_neighbor(net, [16, 16]) # upsampling
net = slim.conv2d(net, nf)
net = slim.conv2d(net, nf)
net = tf.image.resize_nearest_neighbor(net, [32, 32])
net = slim.conv2d(net, nf)
net = slim.conv2d(net, nf)
net = tf.image.resize_nearest_neighbor(net, [64, 64])
net = slim.conv2d(net, nf)
net = slim.conv2d(net, nf)
net = slim.conv2d(net, 3, activation_fn=None)
return net
def _discriminator(self, X, reuse=False):
with tf.variable_scope('D', reuse=reuse):
h = self._encoder(X, reuse=reuse)
x_recon = self._decoder(h, reuse=reuse)
energy = tf.abs(X-x_recon) # L1 loss
energy = tf.reduce_mean(energy)
return energy
def _generator(self, z, reuse=False):
with tf.variable_scope('G', reuse=reuse):
x_fake = self._decoder(z, reuse=reuse)
return x_fake