-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
174 lines (146 loc) · 6.96 KB
/
models.py
File metadata and controls
174 lines (146 loc) · 6.96 KB
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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
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.leaky_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
tanh = tf.tanh
# WGAN-GP 64 x 64
def generator(z, dim=64, reuse=True, training=True):
bn = partial(batch_norm, is_training=training)
dconv_relu = partial(dconv, normalizer_fn=None, activation_fn=relu, biases_initializer=None)
fc_relu = partial(fc, normalizer_fn=None, activation_fn=relu, biases_initializer=None)
with tf.variable_scope('generator', reuse=reuse):
y = bn(fc_relu(z, 4 * 4 * dim * 8))
y = tf.reshape(y, [-1, 4, 4, dim * 8])
y = bn(dconv_relu(y, dim * 4, 5, 2))
y = bn(dconv_relu(y, dim * 2, 5, 2))
y = bn(dconv_relu(y, dim * 1, 5, 2))
img = tanh(dconv(y, 3, 5, 2))
return img
def discriminator(img, dim=64, reuse=True, training=True):
conv_lrelu = partial(conv, normalizer_fn=None, activation_fn=lrelu, biases_initializer=None)
with tf.variable_scope('discriminator', reuse=reuse):
y = lrelu(conv(img, dim, 5, 2))
y = ln(conv_lrelu(y, dim * 2, 5, 2))
y = ln(conv_lrelu(y, dim * 4, 5, 2))
y = ln(conv_lrelu(y, dim * 8, 5, 2))
logit = fc(y, 1)
return logit
def generator_AC_GAN_64_64(z, t, dim=64, reuse=True, training=True):
bn = partial(batch_norm, is_training=training)
dconv_relu = partial(dconv, normalizer_fn=None, activation_fn=relu, biases_initializer=None)
fc_relu = partial(fc, normalizer_fn=None, activation_fn=relu, biases_initializer=None)
with tf.variable_scope('generator', reuse=reuse):
t = fc_relu(t, 256)
y = tf.concat((z, t), axis=1)
y = tf.nn.relu(bn(fc(y, 4*4*8*dim)))
y = tf.reshape(y, [-1, 4, 4, dim * 8])
y = tf.nn.relu(bn(dconv(y, dim * 4, 5, 2)))
y = tf.nn.relu(bn(dconv(y, dim * 2, 5, 2)))
y = tf.nn.relu(bn(dconv(y, dim * 1, 5, 2)))
img = tanh(dconv(y, 3, 5, 2))
return img
def discriminator_AC_GAN_64_64(img, t, dim=64, reuse=True, training=True):
bn = partial(batch_norm, is_training=training)
conv_lrelu = partial(conv, normalizer_fn=None, activation_fn=lrelu, biases_initializer=None)
fc_relu = partial(fc, normalizer_fn=None, activation_fn=relu, biases_initializer=None)
with tf.variable_scope('discriminator', reuse=reuse):
t = fc_relu(t, dim * 4)
t = tf.reshape(t, [-1, 1, 1, dim * 4])
t = tf.tile(t, multiples=[1, 4, 4, 1])
t = conv_lrelu(t, dim, 5)
y = lrelu(conv(img, dim, 5, 2))
y = lrelu(bn(conv(y, dim * 2, 5, 2)))
y = lrelu(bn(conv_lrelu(y, dim * 4, 5, 2)))
y = lrelu(bn(conv_lrelu(y, dim * 8, 5, 2)))
y = tf.concat((y, t), axis=3)
y = conv(y, dim * 8, 1, 1)
logit = fc(y, 1)
return logit
# WGAN-GP 28 x 28
def generator_231(z, dim=64, reuse=True, training=True):
bn = partial(batch_norm, is_training=training)
fc_lrelu = partial(fc, activation_fn=lrelu)
dconv_relu = partial(dconv, activation_fn=relu)
with tf.variable_scope('generator', reuse=reuse):
y = bn(fc_lrelu(z, 1024))
y = bn(fc_lrelu(y, 7 * 7 * dim * 2))
y = tf.reshape(y, (-1, 7, 7, dim * 2))
y = bn(dconv_relu(y, dim, 4, 2))
img = tanh(dconv(y, 1, 4, 2))
return img
def discriminator_WGAN_231(img, dim=64, reuse=True, training=True):
bn = partial(batch_norm, is_training=training)
conv_lrelu = partial(conv, activation_fn=lrelu)
fc_lrelu = partial(fc, activation_fn=lrelu)
with tf.variable_scope('discriminator', reuse=reuse):
y = tf.reshape(img, [-1, 28, 28, 1])
y = conv_lrelu(y, dim, 4, 2)
y = bn(conv_lrelu(y, dim*2, 4, 2))
y = fc_lrelu(y, 1024)
logits = fc(y, 1)
return logits
# ACGAN 28 x 28
def generator_AC_GAN(z, t, dim=64, reuse=True, training=True):
bn = partial(batch_norm, is_training=training)
fc_relu = partial(fc, normalizer_fn=None, activation_fn=relu, biases_initializer=None)
dconv_bn_relu = partial(dconv, normalizer_fn=bn, activation_fn=relu, biases_initializer=None)
dconv_tanh = partial(dconv, activation_fn=tanh, biases_initializer=None)
with tf.variable_scope('generator', reuse=reuse):
t = bn(fc_relu(t, 256))
z = bn(fc_relu(z, 256))
y = tf.concat((z, t), axis=1)
y = bn(fc_relu(y, 1024))
y = bn(fc_relu(y, 7 * 7 * dim * 2))
y = tf.reshape(y, [-1, 7, 7, dim * 2])
y = dconv_bn_relu(y, dim, 4, 2)
img = tf.nn.tanh(dconv(y, 1, 4, 2))
return img
def discriminator_AC_GAN(img, dim=64, reuse=True, training=True):
bn = partial(batch_norm, is_training=training)
conv_lrelu = partial(conv, normalizer_fn=None, activation_fn=lrelu, biases_initializer=None)
fc_lrelu = partial(fc, normalizer_fn=None, activation_fn=lrelu)
with tf.variable_scope('discriminator', reuse=reuse):
y = tf.reshape(img, [-1, 28, 28, 1])
y = lrelu(conv(y, dim, 4, 2))
y = bn(conv_lrelu(y, dim*2, 4, 2))
y = fc_lrelu(y, 1024)
logits = fc(y, 1)
return logits
def discriminator_AC_GAN_2(img, dim=64, reuse=True, training=True):
bn = partial(batch_norm, is_training=training)
conv_lrelu = partial(conv, normalizer_fn=None, activation_fn=lrelu, biases_initializer=None)
fc_lrelu = partial(fc, normalizer_fn=None, activation_fn=lrelu)
fc_bn_lrelu = partial(fc, normalizer_fn=bn, activation_fn=lrelu)
with tf.variable_scope('discriminator', reuse=reuse):
y = tf.reshape(img, [-1, 28, 28, 1])
y = lrelu(conv(y, dim, 4, 2))
feature = bn(conv_lrelu(y, dim*2, 4, 2))
y = fc_lrelu(feature, 1024)
logits = fc(y, 1)
return logits, feature
def classifier_AC_GAN_2(feature, dim=64, reuse=True, training=True):
bn = partial(batch_norm, is_training=training)
fc_bn_lrelu = partial(fc, normalizer_fn=bn, activation_fn=lrelu)
with tf.variable_scope('classifier', reuse=reuse):
y = fc_bn_lrelu(feature, 1024)
y = fc_bn_lrelu(y, 1024)
logits = fc(y, 10)
return logits
def classifier_AC_GAN(img, dim=64, reuse=True, training=True):
bn = partial(batch_norm, is_training=training)
fc_bn_lrelu = partial(fc, normalizer_fn=bn, activation_fn=lrelu)
with tf.variable_scope('classifier', reuse=reuse):
y = fc_bn_lrelu(img, 1024)
y = fc_bn_lrelu(y, 512)
logits = fc(y, 10)
return logits