-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathdecoder.py
More file actions
74 lines (52 loc) · 2.73 KB
/
decoder.py
File metadata and controls
74 lines (52 loc) · 2.73 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
# Decoder mostly mirrors the encoder with all pooling layers replaced by nearest
# up-sampling to reduce checker-board effects.
# Decoder has no BN/IN layers.
import tensorflow as tf
class Decoder(object):
def __init__(self):
self.weight_vars = []
with tf.variable_scope('decoder'):
self.weight_vars.append(self._create_variables(512, 256, 3, scope='conv4_1'))
self.weight_vars.append(self._create_variables(256, 256, 3, scope='conv3_4'))
self.weight_vars.append(self._create_variables(256, 256, 3, scope='conv3_3'))
self.weight_vars.append(self._create_variables(256, 256, 3, scope='conv3_2'))
self.weight_vars.append(self._create_variables(256, 128, 3, scope='conv3_1'))
self.weight_vars.append(self._create_variables(128, 128, 3, scope='conv2_2'))
self.weight_vars.append(self._create_variables(128, 64, 3, scope='conv2_1'))
self.weight_vars.append(self._create_variables( 64, 64, 3, scope='conv1_2'))
self.weight_vars.append(self._create_variables( 64, 3, 3, scope='conv1_1'))
def _create_variables(self, input_filters, output_filters, kernel_size, scope):
with tf.variable_scope(scope):
shape = [kernel_size, kernel_size, input_filters, output_filters]
kernel = tf.get_variable(initializer=tf.contrib.layers.xavier_initializer(uniform=False), shape=shape, name='kernel')
bias = tf.get_variable(initializer=tf.contrib.layers.xavier_initializer(uniform=False), shape=[output_filters], name='bias')
return (kernel, bias)
def decode(self, image):
# upsampling after 'conv4_1', 'conv3_1', 'conv2_1'
upsample_indices = (0, 4, 6)
final_layer_idx = len(self.weight_vars) - 1
out = image
for i in range(len(self.weight_vars)):
kernel, bias = self.weight_vars[i]
if i == final_layer_idx:
out = conv2d(out, kernel, bias, use_relu=False)
else:
out = conv2d(out, kernel, bias)
if i in upsample_indices:
out = upsample(out)
return out
def conv2d(x, kernel, bias, use_relu=True):
# padding image with reflection mode
x_padded = tf.pad(x, [[0, 0], [1, 1], [1, 1], [0, 0]], mode='REFLECT')
# conv and add bias
out = tf.nn.conv2d(x_padded, kernel, strides=[1, 1, 1, 1], padding='VALID')
out = tf.nn.bias_add(out, bias)
if use_relu:
out = tf.nn.relu(out)
return out
def upsample(x, scale=2):
height = tf.shape(x)[1] * scale
width = tf.shape(x)[2] * scale
output = tf.image.resize_images(x, [height, width],
method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
return output