-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlayers.py
41 lines (31 loc) · 1.84 KB
/
layers.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
import tensorflow as tf
def conv2d(x, out_channels, kernel_size=4, strides=2, padding='same', use_bias=False, name='Conv'):
with tf.variable_scope(name):
initializer = tf.random_normal_initializer(0, 0.02)
return tf.layers.conv2d(x, out_channels, kernel_size=kernel_size, strides=strides, padding=padding,
kernel_initializer=initializer, use_bias=use_bias)
def deconv2d(x, out_channels, kernel_size=4, stride=2, padding='same', name="Deconv"):
with tf.variable_scope(name):
initializer = tf.random_normal_initializer(0, 0.02)
return tf.layers.conv2d_transpose(x, out_channels, kernel_size=kernel_size, strides=stride, padding=padding,
kernel_initializer=initializer, use_bias=False)
def res_block(x_in, out_channels=256, name='ResBlock'):
with tf.variable_scope(name):
x = conv2d(x_in, out_channels=out_channels, kernel_size=3, strides=1, name='Conv1')
x = instance_norm(x, name='InstNorm1')
x = tf.nn.relu(x, name='ReLU1')
x = conv2d(x, out_channels=out_channels, kernel_size=3, strides=1, name='Conv2')
x = instance_norm(x, name='InstNorm2')
return tf.add(x_in, x)
def instance_norm(x, name='InstNorm'):
with tf.variable_scope(name):
depth = x.get_shape()[3]
scale = tf.get_variable('scale', [depth], initializer=tf.random_normal_initializer(1.0, 0.02))
shift = tf.get_variable('shift', [depth], initializer=tf.constant_initializer(0.0))
mean, variance = tf.nn.moments(x, axes=[1, 2], keep_dims=True)
# Normalization
epsilon = 1e-5
inputs_normed = (x - mean) * tf.rsqrt(variance + epsilon)
# Perform trainable shift.
output = scale * inputs_normed + shift
return output