-
Notifications
You must be signed in to change notification settings - Fork 5
/
generator.py
180 lines (150 loc) · 7.38 KB
/
generator.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
import tensorflow as tf
from tensorflow.python.framework import ops
from tensorflow.python.framework import dtypes
from network_common import *
def get_inference(x_image, FLAGS, reuse, drop_prob = 0.5, is_train = True):
with tf.variable_scope('generator', reuse) as scope:
if reuse:
scope.reuse_variables()
channel_size = [64, 128, 256, 512, 512, 512, 512, 512, 512, 512]
# convolution
# 256x256
with tf.variable_scope('conv1', reuse) as scope:
W_conv = weight_variable([ 4, 4, 3, channel_size[0] ])
b_conv = bias_variable([ channel_size[0] ])
conv = conv2d(x_image, W_conv)
conv1 = tf.nn.bias_add(conv, b_conv)
# 128x128
with tf.variable_scope('conv2', reuse) as scope:
re = lrelu(conv1)
W_conv = weight_variable([ 4, 4, channel_size[0], channel_size[1] ])
b_conv = bias_variable([ channel_size[1] ])
conv = conv2d(re, W_conv)
bias = tf.nn.bias_add(conv, b_conv)
conv2 = batch_norm(bias, is_train)
# 64x64
with tf.variable_scope('conv3' , reuse) as scope:
re = lrelu(conv2)
W_conv = weight_variable([4, 4, channel_size[1], channel_size[2] ])
b_conv = bias_variable([ channel_size[2] ])
conv = conv2d(re, W_conv)
bias = tf.nn.bias_add(conv, b_conv)
conv3 = batch_norm(bias, is_train)
# 32x32
with tf.variable_scope('conv4', reuse) as scope:
re = lrelu(conv3)
W_conv = weight_variable([ 4, 4, channel_size[2], channel_size[3] ])
b_conv = bias_variable([ channel_size[3] ])
conv = conv2d(re, W_conv)
bias = tf.nn.bias_add(conv, b_conv)
conv4 = batch_norm(bias, is_train)
# 16x16
with tf.variable_scope('conv5', reuse) as scope:
re = lrelu(conv4)
W_conv = weight_variable([ 4, 4, channel_size[3], channel_size[4] ])
b_conv = bias_variable([ channel_size[4] ])
conv = conv2d(re, W_conv)
bias = tf.nn.bias_add(conv, b_conv)
conv5 = batch_norm(bias, is_train)
# 8x8
with tf.variable_scope('conv6', reuse) as scope:
re = lrelu(conv5)
W_conv = weight_variable([ 4, 4, channel_size[4], channel_size[5] ])
b_conv = bias_variable([ channel_size[5] ])
conv = conv2d(re, W_conv)
bias = tf.nn.bias_add(conv, b_conv)
conv6 = batch_norm(bias, is_train)
# 4x4
with tf.variable_scope('conv7', reuse) as scope:
re = lrelu(conv6)
W_conv = weight_variable([ 4, 4, channel_size[5], channel_size[6] ])
b_conv = bias_variable([ channel_size[6] ])
conv = conv2d(re, W_conv)
bias = tf.nn.bias_add(conv, b_conv)
conv7 = batch_norm(bias, is_train)
# 2x2 -> 1x1
with tf.variable_scope('conv8', reuse) as scope:
re = lrelu(conv7)
W_conv = weight_variable([ 4, 4, channel_size[6], channel_size[7] ])
b_conv = bias_variable([ channel_size[7] ])
conv = conv2d(re, W_conv)
conv8 = tf.nn.bias_add(conv, b_conv)
# deconvolution
# 1x1 -> 2x2
with tf.variable_scope('deconv8', reuse) as scope:
re = tf.nn.relu(conv8)
W_conv = weight_variable([4, 4, channel_size[6], channel_size[7] ])
b_conv = bias_variable([ channel_size[6] ])
deconv = deconv2d(re, W_conv, conv7.get_shape())
bias = tf.nn.bias_add(deconv, b_conv)
bias = batch_norm(bias, is_train)
drop = tf.nn.dropout(bias, drop_prob)
deconv8 = tf.concat([drop, conv7], 3)
# 2x2 -> 4x4
with tf.variable_scope('deconv7', reuse) as scope:
deconv8 = tf.nn.relu(deconv8, name=scope.name)
W_conv = weight_variable([4, 4, channel_size[5], channel_size[6] * 2])
b_conv = bias_variable([ channel_size[5] ])
deconv = deconv2d(deconv8, W_conv, conv6.get_shape())
bias = tf.nn.bias_add(deconv, b_conv)
bias = batch_norm(bias, is_train)
drop = tf.nn.dropout(bias, drop_prob)
deconv7 = tf.concat([drop, conv6], 3)
# 4x4 -> 8x8
with tf.variable_scope('deconv6', reuse) as scope:
deconv7 = tf.nn.relu(deconv7, name=scope.name)
W_conv = weight_variable([4, 4, channel_size[4], channel_size[5] * 2 ])
b_conv = bias_variable([ channel_size[4] ])
deconv = deconv2d(deconv7, W_conv, conv5.get_shape())
bias = tf.nn.bias_add(deconv, b_conv)
bias = batch_norm(bias, is_train)
drop = tf.nn.dropout(bias, drop_prob)
deconv6 = tf.concat([drop, conv5], 3)
# 8x8 -> 16x16
with tf.variable_scope('deconv5', reuse) as scope:
deconv6 = tf.nn.relu(deconv6, name=scope.name)
W_conv = weight_variable([4, 4, channel_size[3], channel_size[4] * 2 ])
b_conv = bias_variable([ channel_size[3] ])
deconv = deconv2d(deconv6, W_conv, conv4.get_shape())
bias = tf.nn.bias_add(deconv, b_conv)
bias = batch_norm(bias, is_train)
deconv5 = tf.concat([bias, conv4], 3)
# 16x16 -> 32x32
with tf.variable_scope('deconv4', reuse) as scope:
deconv5 = tf.nn.relu(deconv5, name=scope.name)
W_conv = weight_variable([4, 4, channel_size[2], channel_size[3] * 2])
b_conv = bias_variable([ channel_size[2] ])
deconv = deconv2d(deconv5, W_conv, conv3.get_shape())
bias = tf.nn.bias_add(deconv, b_conv)
bias = batch_norm(bias, is_train)
deconv4 = tf.concat([bias, conv3], 3)
# 32x32 -> 64x64
with tf.variable_scope('deconv3', reuse) as scope:
deconv4 = tf.nn.relu(deconv4, name=scope.name)
W_conv = weight_variable([4, 4, channel_size[1], channel_size[2] * 2])
b_conv = bias_variable([channel_size[1]])
deconv = deconv2d(deconv4, W_conv, conv2.get_shape())
bias = tf.nn.bias_add(deconv, b_conv)
bias = batch_norm(bias, is_train)
deconv3 = tf.concat([bias, conv2], 3)
# 64x64 -> 128x128
with tf.variable_scope('deconv2', reuse) as scope:
deconv3 = tf.nn.relu(deconv3, name=scope.name)
W_conv = weight_variable([4, 4, channel_size[0], channel_size[1] * 2])
b_conv = bias_variable([channel_size[0]])
deconv = deconv2d(deconv3, W_conv, conv1.get_shape())
bias = tf.nn.bias_add(deconv, b_conv)
bias = batch_norm(bias, is_train)
deconv2 = tf.concat([bias, conv1], 3)
# 128x128 -> 256x256
with tf.variable_scope('deconv1', reuse) as scope:
deconv2 = tf.nn.relu(deconv2, name=scope.name)
W_conv = weight_variable([4, 4, 3, channel_size[0] * 2])
b_conv = bias_variable([3])
deconv = deconv2d(deconv2, W_conv, x_image.get_shape())
bias = tf.nn.bias_add(deconv, b_conv)
output = tf.nn.tanh(bias, name=scope.name)
return output
def get_l1_loss(preds, labels, FLAGS):
abs_diff = tf.abs(preds - labels)
return tf.reduce_mean(abs_diff)