-
Notifications
You must be signed in to change notification settings - Fork 31
/
models_64x64.py
57 lines (45 loc) · 2.27 KB
/
models_64x64.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
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
import ops
import tensorflow as tf
import tensorflow.contrib.slim as slim
from functools import partial
conv = partial(slim.conv2d, activation_fn=None, weights_initializer=tf.truncated_normal_initializer(stddev=0.02))
dconv = partial(slim.conv2d_transpose, activation_fn=None, weights_initializer=tf.random_normal_initializer(stddev=0.02))
fc = partial(ops.flatten_fully_connected, activation_fn=None, weights_initializer=tf.random_normal_initializer(stddev=0.02))
relu = tf.nn.relu
lrelu = partial(ops.leak_relu, leak=0.2)
batch_norm = partial(slim.batch_norm, decay=0.9, scale=True, epsilon=1e-5, updates_collections=None)
ln = slim.layer_norm
def generator(z, dim=64, reuse=True, training=True):
bn = partial(batch_norm, is_training=training)
dconv_bn_relu = partial(dconv, normalizer_fn=bn, activation_fn=relu, biases_initializer=None)
fc_bn_relu = partial(fc, normalizer_fn=bn, activation_fn=relu, biases_initializer=None)
with tf.variable_scope('generator', reuse=reuse):
y = fc_bn_relu(z, 4 * 4 * dim * 8)
y = tf.reshape(y, [-1, 4, 4, dim * 8])
y = dconv_bn_relu(y, dim * 4, 5, 2)
y = dconv_bn_relu(y, dim * 2, 5, 2)
y = dconv_bn_relu(y, dim * 1, 5, 2)
img = tf.tanh(dconv(y, 3, 5, 2))
return img
def discriminator(img, dim=64, reuse=True, training=True):
bn = partial(batch_norm, is_training=training)
conv_bn_lrelu = partial(conv, normalizer_fn=bn, activation_fn=lrelu, biases_initializer=None)
with tf.variable_scope('discriminator', reuse=reuse):
y = lrelu(conv(img, dim, 5, 2))
y = conv_bn_lrelu(y, dim * 2, 5, 2)
y = conv_bn_lrelu(y, dim * 4, 5, 2)
y = conv_bn_lrelu(y, dim * 8, 5, 2)
logit = fc(y, 1)
return logit
def discriminator_wgan_gp(img, dim=64, reuse=True, training=True):
conv_ln_lrelu = partial(conv, normalizer_fn=ln, activation_fn=lrelu, biases_initializer=None)
with tf.variable_scope('discriminator', reuse=reuse):
y = lrelu(conv(img, dim, 5, 2))
y = conv_ln_lrelu(y, dim * 2, 5, 2)
y = conv_ln_lrelu(y, dim * 4, 5, 2)
y = conv_ln_lrelu(y, dim * 8, 5, 2)
logit = fc(y, 1)
return logit