forked from ZhilZheng/Lr-LiVAE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ops.py
230 lines (165 loc) · 7.92 KB
/
ops.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
import tensorflow as tf
import tensorflow.contrib as tf_contrib
from tensorflow.contrib.layers import xavier_initializer
from tensorflow.contrib.layers import variance_scaling_initializer
weight_init = tf.random_normal_initializer(mean=0.0, stddev=0.02)
# weight_init = xavier_initializer()
# weight_init = variance_scaling_initializer()
# weight_regularizer = tf_contrib.layers.l2_regularizer(scale=0.0001)
weight_regularizer = None
# pad = (k-1) // 2 = SAME !
# output = ( input - k + 1 + 2p ) // s
def conv(x, channels, kernel=4, stride=2, pad=0, pad_type='zero', use_bias=True, sn=False, scope='conv_0'):
with tf.variable_scope(scope):
if pad_type == 'zero' :
x = tf.pad(x, [[0, 0], [pad, pad], [pad, pad], [0, 0]])
if pad_type == 'reflect' :
x = tf.pad(x, [[0, 0], [pad, pad], [pad, pad], [0, 0]], mode='REFLECT')
if sn :
w = tf.get_variable("kernel", shape=[kernel, kernel, x.get_shape()[-1], channels], initializer=weight_init,
regularizer=weight_regularizer)
bias = tf.get_variable("bias", [channels], initializer=tf.constant_initializer(0.0))
x = tf.nn.conv2d(input=x, filter=spectral_norm(w),
strides=[1, stride, stride, 1], padding='VALID')
if use_bias :
x = tf.nn.bias_add(x, bias)
else :
x = tf.layers.conv2d(inputs=x, filters=channels,
kernel_size=kernel, kernel_initializer=weight_init,
kernel_regularizer=weight_regularizer,
strides=stride, use_bias=use_bias)
return x
def deconv(x, channels, kernel=4, stride=2, use_bias=True, sn=False, scope='deconv_0'):
with tf.variable_scope(scope):
x_shape = x.get_shape().as_list()
output_shape = [x_shape[0], x_shape[1] * stride, x_shape[2] * stride, channels]
if sn :
w = tf.get_variable("kernel", shape=[kernel, kernel, channels, x.get_shape()[-1]], initializer=weight_init, regularizer=weight_regularizer)
x = tf.nn.conv2d_transpose(x, filter=spectral_normed_weight(w), output_shape=output_shape, strides=[1, stride, stride, 1], padding='SAME')
if use_bias :
bias = tf.get_variable("bias", [channels], initializer=tf.constant_initializer(0.0))
x = tf.nn.bias_add(x, bias)
else :
x = tf.layers.conv2d_transpose(inputs=x, filters=channels,
kernel_size=kernel, kernel_initializer=weight_init, kernel_regularizer=weight_regularizer,
strides=stride, padding='SAME', use_bias=use_bias)
return x
def max_pooling(x, kernel=2, stride=2) :
return tf.layers.max_pooling2d(x, pool_size=kernel, strides=stride)
def avg_pooling(x, kernel=2, stride=2) :
return tf.layers.average_pooling2d(x, pool_size=kernel, strides=stride)
def global_avg_pooling(x):
"""
Incoming Tensor shape must be 4-D
"""
gap = tf.reduce_mean(x, axis=[1, 2])
return gap
def fully_connected(x, units, use_bias=True, sn=False, scope='fully_0'):
with tf.variable_scope(scope):
x = flatten(x)
shape = x.get_shape().as_list()
channels = shape[-1]
if sn :
w = tf.get_variable("kernel", [channels, units], tf.float32,
initializer=weight_init, regularizer=weight_regularizer)
if use_bias :
bias = tf.get_variable("bias", [units],
initializer=tf.constant_initializer(0.0))
x = tf.matmul(x, spectral_norm(w)) + bias
else :
x = tf.matmul(x, spectral_norm(w))
else :
x = tf.layers.dense(x, units=units, kernel_initializer=weight_init, kernel_regularizer=weight_regularizer, use_bias=use_bias)
return x
def flatten(x) :
return tf.contrib.layers.flatten(x)
#def lrelu(x, alpha=0.2):
# # pytorch alpha is 0.01
# return tf.nn.leaky_relu(x, alpha)
def lrelu(x, leak=0.1, name="lrelu"):
with tf.variable_scope(name):
f1 = 0.5 * (1 + leak)
f2 = 0.5 * (1 - leak)
return f1 * x + f2 * abs(x)
def relu(x):
return tf.nn.relu(x)
def sigmoid(x):
return tf.sigmoid(x)
def tanh(x):
return tf.tanh(x)
def swish(x):
return x * sigmoid(x)
def discriminator_loss(real, fake):
real_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=tf.ones_like(real), logits=real))
fake_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=tf.zeros_like(fake), logits=fake))
loss = real_loss + fake_loss
return loss
def generator_loss(fake):
loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=tf.ones_like(fake), logits=fake))
return loss
def batch_norm(x, is_training=True, scope='batch_norm'):
# return tf_contrib.layers.batch_norm(x,
# decay=0.9, epsilon=1e-05,
# center=True, scale=True, updates_collections=None,
# is_training=is_training, scope=scope)
return tf_contrib.layers.batch_norm(x,
decay=0.9, epsilon=1e-05,
center=True, scale=True,
is_training=is_training, scope=scope)
# return tf.layers.batch_normalization(x, momentum=0.99, epsilon=1e-05, center=True, scale=True, training=is_training, name=scope)
def instance_norm(x, scope='instance_norm'):
return tf_contrib.layers.instance_norm(x,
epsilon=1e-05,
center=True, scale=True,
scope=scope)
def layer_norm(x, scope='layer_norm') :
return tf_contrib.layers.layer_norm(x,
center=True, scale=True,
scope=scope)
def group_norm(x, G=32, eps=1e-5, scope='group_norm') :
with tf.variable_scope(scope) :
N, H, W, C = x.get_shape().as_list()
G = min(G, C)
x = tf.reshape(x, [N, H, W, G, C // G])
mean, var = tf.nn.moments(x, [1, 2, 4], keep_dims=True)
x = (x - mean) / tf.sqrt(var + eps)
gamma = tf.get_variable('gamma', [1, 1, 1, C],
initializer=tf.constant_initializer(1.0))
beta = tf.get_variable('beta', [1, 1, 1, C],
initializer=tf.constant_initializer(0.0))
# gamma = tf.reshape(gamma, [1, 1, 1, C])
# beta = tf.reshape(beta, [1, 1, 1, C])
x = tf.reshape(x, [N, H, W, C]) * gamma + beta
return x
def l2_norm(v, eps=1e-12):
return v / (tf.reduce_sum(v ** 2) ** 0.5 + eps)
def spectral_norm(w, iteration=1):
w_shape = w.shape.as_list()
w = tf.reshape(w, [-1, w_shape[-1]])
u = tf.get_variable("u", [1, w_shape[-1]], initializer=tf.truncated_normal_initializer(), trainable=False)
u_hat = u
v_hat = None
for i in range(iteration):
"""
power iteration
Usually iteration = 1 will be enough
"""
v_ = tf.matmul(u_hat, tf.transpose(w))
v_hat = l2_norm(v_)
u_ = tf.matmul(v_hat, w)
u_hat = l2_norm(u_)
sigma = tf.matmul(tf.matmul(v_hat, w), tf.transpose(u_hat))
w_norm = w / sigma
with tf.control_dependencies([u.assign(u_hat)]):
w_norm = tf.reshape(w_norm, w_shape)
return w_norm
def L1_loss(x, y):
loss = tf.reduce_mean(tf.abs(x - y))
return loss
def L2_loss(x, y):
loss = tf.reduce_mean(tf.square(x - y))
# if len(x.get_shape().as_list()) == 4:
# loss = 0.5 * tf.reduce_mean(tf.reduce_sum(tf.square(x - y), axis = [1,2,3]))
# elif len(x.get_shape().as_list()) == 2:
# loss = 0.5 * tf.reduce_mean(tf.reduce_sum(tf.square(x - y), axis= [1]))
return loss