-
Notifications
You must be signed in to change notification settings - Fork 2
/
models.py
306 lines (190 loc) · 11.5 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import tensorflow as tf
import tensorlayer as tl
from tensorlayer.layers import *
def encoder(rgb, num_of_resblock=4, h_dim=128, is_train=True, reuse=False):
w_init = tf.truncated_normal_initializer(mean=0.0, stddev=0.02)
b_init = None
g_init = tf.truncated_normal_initializer(mean=1.0, stddev=0.02)
gf_dim = 64
filter_size = (3, 3)
strides = (1, 1)
down_filter_size = (3, 3)
down_strides = (2, 2)
with tf.variable_scope('encoder', reuse=reuse):
tl.layers.set_name_reuse(reuse)
# (64, 64 3)
net = InputLayer(rgb, name='e/in')
# (32, 32, 64)
net = Conv2d(net, n_filter=gf_dim, filter_size=down_filter_size, strides=down_strides,
act=tf.nn.relu, padding='SAME', W_init=w_init, name='e/n32s2/c0')
# (16, 16, 128)
net = Conv2d(net, n_filter=gf_dim * 2, filter_size=down_filter_size, strides=down_strides,
act=tf.nn.relu, padding='SAME', W_init=w_init, name='e/n64s2/c0')
net = BatchNormLayer(net, act=tf.nn.relu, is_train=is_train, gamma_init=g_init,
name='e/n64s2/b0')
# (8, 8, 256)
net = Conv2d(net, n_filter=gf_dim*4, filter_size=down_filter_size, strides=down_strides,
act=tf.nn.relu, padding='SAME', W_init=w_init, name='e/n128s2/c0')
net = BatchNormLayer(net, act=tf.nn.relu, is_train=is_train, gamma_init=g_init,
name='e/n128s2/b0')
temp = net
################################# Residual block ############################################
for i in range(num_of_resblock):
net_r = Conv2d(net, n_filter=gf_dim*4, filter_size=filter_size, strides=strides, act=tf.identity,
padding='SAME', W_init=w_init, b_init=b_init, name='e/n256s1/c1/%s' % i)
net_r = BatchNormLayer(net_r, act=tf.nn.relu, is_train=is_train, gamma_init=g_init, name='e/n64s1/b1/%s' % i)
net_r = Conv2d(net_r, n_filter=gf_dim*4, filter_size=filter_size, strides=strides, act=tf.identity,
padding='SAME', W_init=w_init, b_init=b_init, name='e/n256s1/c2/%s' % i)
net_r = BatchNormLayer(net_r, act=tf.nn.relu, is_train=is_train, gamma_init=g_init, name='e/n64s1/b2/%s' % i)
net_r = ElementwiseLayer([net, net_r], combine_fn=tf.add, name='e/residual_add/%s' % i)
net = net_r
net = Conv2d(net, n_filter=gf_dim*4, filter_size=filter_size, strides=strides, act=tf.identity,
W_init=w_init, b_init=b_init, name='e/n256s1/c/m')
net = BatchNormLayer(net, is_train=is_train, gamma_init=g_init, name='e/n256s1/b/m')
net = ElementwiseLayer([net, temp], combine_fn=tf.add, name='e/add3')
################################### End of residual block ####################################
net = FlattenLayer(net, name='e/flatten')
net = DenseLayer(net, n_units=h_dim, act=tf.identity, W_init=w_init, name='e/lin/output')
logits = net.outputs
return net, logits
def generator(feat_vec, hidden_dim=128, is_train=True, reuse=False, type='dcgan'):
if type == 'dcgan':
return generator_dcgan(feat_vec, hidden_dim, is_train, reuse)
elif type == 'resblk':
return generator_resblk(feat_vec, num_of_resblk=3,
hidden_dim=hidden_dim, is_train=is_train, reuse=reuse)
else:
raise Exception("Generator type is not supported: {}".format(type))
def generator_dcgan(feat_vec, hidden_dim=128, is_train=True, reuse=False):
w_init = tf.truncated_normal_initializer(stddev=0.02)
g_init = tf.truncated_normal_initializer(mean=1.0, stddev=0.02)
image_size = 64
s2, s4, s8, s16 = int(image_size/2), int(image_size/4), int(image_size/8), int(image_size/16),
gf_dim = 64
c_dim = 3
assert feat_vec.get_shape().as_list()[1:] == [hidden_dim]
# make sure the size matches if the size of current batch is not batch size
batch_size = feat_vec.get_shape().as_list()[0]
filter_size = (3, 3)
strides = (2, 2)
with tf.variable_scope('generator', reuse=reuse):
tl.layers.set_name_reuse(reuse)
# (128,)
net_in = InputLayer(feat_vec, name='g/in')
# (64*16*4*4=4*4*1024, )
net_h0 = DenseLayer(net_in, n_units=gf_dim*16*s16*s16, W_init=w_init,
act = tf.identity, name='g/h0/lin')
# (4,4, 1024)
net_h0 = ReshapeLayer(net_h0, shape=[-1, s16, s16, gf_dim*16], name="g/h0/reshape")
net_h0 = BatchNormLayer(net_h0, act=tf.nn.relu, is_train=is_train,
gamma_init=g_init, name='g/h0/batch_norm')
# (8, 8, 512)
net_h1 = DeConv2d(net_h0, n_filter=gf_dim*8, filter_size=filter_size, out_size=(s8, s8), strides=strides,
padding='SAME', batch_size=batch_size, W_init=w_init, name='g/h1/deconv2d')
net_h1 = BatchNormLayer(net_h1, act=tf.nn.relu, is_train=is_train,
gamma_init=g_init, name='g/h1/batch_norm')
# (16, 16, 256)
net_h2 = DeConv2d(net_h1, n_filter=gf_dim*4, filter_size=filter_size, out_size=(s4, s4), strides=strides,
padding='SAME', batch_size=batch_size, W_init=w_init, name='g/h2/deconv2d')
net_h2 = BatchNormLayer(net_h2, act=tf.nn.relu, is_train=is_train,
gamma_init=g_init, name='g/h2/batch_norm')
# (32, 32, 128)
net_h3 = DeConv2d(net_h2, n_filter=gf_dim*2, filter_size=filter_size, out_size=(s2, s2), strides=strides,
padding='SAME', batch_size=batch_size, W_init=w_init, name='g/h3/deconv2d')
net_h3 = BatchNormLayer(net_h3, act=tf.nn.relu, is_train=is_train,
gamma_init=g_init, name='g/h3/batch_norm')
# (64, 64, 3)
net_h4 = DeConv2d(net_h3, n_filter=c_dim, filter_size=filter_size, out_size=(image_size, image_size), strides=strides,
padding='SAME', batch_size=batch_size, W_init=w_init, name='g/h4/deconv2d')
net_h4.outputs = tf.nn.tanh(net_h4.outputs)
return net_h4, net_h4.outputs
def generator_resblk(feat_vec, num_of_resblk=3, hidden_dim=128, is_train=True, reuse=False):
w_init = tf.truncated_normal_initializer(stddev=0.02)
g_init = tf.truncated_normal_initializer(mean=1.0, stddev=0.02)
b_init = None
image_size = 64
s2, s4, s8, s16 = int(image_size/2), int(image_size/4), int(image_size/8), int(image_size/16),
gf_dim = 512
c_dim = 3
assert feat_vec.get_shape().as_list()[1:] == [hidden_dim]
up_filter_size = (5, 5)
up_strides = (2, 2)
filter_size = (3, 3)
strides = (1, 1)
with tf.variable_scope('generator', reuse=reuse):
tl.layers.set_name_reuse(reuse)
# (128,)
net = InputLayer(feat_vec, name='g/in')
# (4*4*512, )
net = DenseLayer(net, n_units=gf_dim*s16*s16, W_init=w_init,
act = tf.identity, name='g/h0/lin')
# (4, 4, 512)
net = ReshapeLayer(net, shape=[-1, s16, s16, gf_dim], name="g/h0/reshape")
net = BatchNormLayer(net, act=tf.nn.relu, is_train=is_train,
gamma_init=g_init, name='g/h0/batch_norm')
for i in range(num_of_resblk):
scale_factor = 2**(i)
for j in range(2):
net_r = Conv2d(net, n_filter=gf_dim//scale_factor , filter_size=filter_size, strides=strides, act=tf.identity,
padding='SAME', W_init=w_init, b_init=b_init, name='g/n512s1/c1/%s/%s' % (i, j))
net_r = BatchNormLayer(net_r, act=tf.nn.relu, is_train=is_train, gamma_init=g_init,
name='g/n512s1/b1/%s/%s'%(i,j))
net_r = Conv2d(net_r, n_filter=gf_dim//scale_factor, filter_size=filter_size, strides=strides, act=tf.identity,
padding='SAME', W_init=w_init, b_init=b_init, name='g/n512s1/c2/%s/%s'%(i,j))
net_r = BatchNormLayer(net_r, act=tf.nn.relu, is_train=is_train, gamma_init=g_init,
name='g/n512s1/b2/%s/%s'%(i,j))
net_r = ElementwiseLayer([net, net_r], combine_fn=tf.add, name='g/residual_add/%s/%s'%(i,j))
net = net_r
net = DeConv2d(net, n_filter=gf_dim//(scale_factor*2), filter_size=up_filter_size, strides=up_strides,
padding='SAME', W_init=w_init, name='g/h1/deconv2d/%s'%i)
net = BatchNormLayer(net, act=tf.nn.relu, is_train=is_train,
gamma_init=g_init, name='g/h1/batch_norm/%d'%i)
net = DeConv2d(net, n_filter=c_dim, filter_size=up_filter_size, strides=up_strides,
padding='SAME', W_init=w_init, name='g/h4/deconv2d')
net.outputs = tf.nn.tanh(net.outputs)
return net, net.outputs
def discriminator(inputs, is_train=True, reuse=False):
df_dim = 64
w_init = tf.truncated_normal_initializer(stddev=0.02)
gamma_init = tf.truncated_normal_initializer(mean=1.0, stddev=0.02)
filter_size = (5, 5)
strides = (2, 2)
with tf.variable_scope('discriminator', reuse=reuse):
tl.layers.set_name_reuse(reuse)
net_in = InputLayer(inputs=inputs, name='d/in')
# (32, 32, 64)
net_h0 = Conv2d(net_in, n_filter=df_dim, filter_size=filter_size, strides=strides,
act = lambda x: tl.act.lrelu(x, 0.2), padding='SAME', W_init=w_init, name='d/h0/conv2d')
# (16, 16, 128)
net_h1 = Conv2d(net_h0, n_filter=df_dim*2, filter_size=filter_size, strides=strides,
act=None, padding='SAME', W_init=w_init, name='d/h1/conv2d')
net_h1 = BatchNormLayer(net_h1, act=lambda x:tl.act.lrelu(x, 0.2), is_train=is_train,
gamma_init=gamma_init, name='d/h1/batch_norm')
# (8, 8, 256)
net_h2 = Conv2d(net_h1, n_filter=df_dim*4, filter_size=filter_size, strides=strides,
act=None, padding='SAME', W_init=w_init, name='d/h2/conv2')
net_h2 = BatchNormLayer(net_h2, act=lambda x:tl.act.lrelu(x, 0.2), is_train=is_train,
gamma_init=gamma_init, name='d/h2/batch_norm')
# (4, 4, 512)
net_h3 = Conv2d(net_h2, n_filter=df_dim*8, filter_size=filter_size, strides=strides,
act=None, padding='SAME', W_init=w_init, name='d/h3/conv2')
net_h3 = BatchNormLayer(net_h3, act=lambda x:tl.act.lrelu(x, 0.2), is_train=is_train,
gamma_init=gamma_init, name='d/h3/batch_norm')
net_h4 = FlattenLayer(net_h3, name='d/h4/flatten')
net_h4 = DenseLayer(net_h4, n_units=1, act=tf.identity, W_init=w_init, name='d/h4/lin_sigmoid')
logits = net_h4.outputs
return net_h4, logits
def code_discriminator(code, reuse=False):
h = 4096
w_init = tf.truncated_normal_initializer(mean=0.0, stddev=0.02)
with tf.variable_scope('code_evaluator', reuse=reuse):
tl.layers.set_name_reuse(reuse)
net = InputLayer(code, name='cd/in')
net = DenseLayer(net, n_units=h, act=lambda x: tl.act.lrelu(x, 0.2), W_init=w_init,
name="cd/fc1")
net = DenseLayer(net, n_units=h, act=lambda x: tl.act.lrelu(x, 0.2), W_init=w_init,
name="cd/fc2")
net = DenseLayer(net, n_units=1, act=tf.identity, W_init=w_init,
name="cd/sigmoid")
logits = net.outputs
return net, logits