-
Notifications
You must be signed in to change notification settings - Fork 69
/
model.py
243 lines (151 loc) · 10.2 KB
/
model.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
231
232
233
234
235
236
237
238
239
240
241
242
243
# Copyright 2019-2020 by Andrey Ignatov. All Rights Reserved.
import tensorflow as tf
import numpy as np
def PyNET(input, instance_norm=True, instance_norm_level_1=False):
with tf.variable_scope("generator"):
# -----------------------------------------
# Downsampling layers
conv_l1_d1 = _conv_multi_block(input, 3, num_maps=32, instance_norm=False) # 224 -> 224
pool1 = max_pool(conv_l1_d1, 2) # 224 -> 112
conv_l2_d1 = _conv_multi_block(pool1, 3, num_maps=64, instance_norm=instance_norm) # 112 -> 112
pool2 = max_pool(conv_l2_d1, 2) # 112 -> 56
conv_l3_d1 = _conv_multi_block(pool2, 3, num_maps=128, instance_norm=instance_norm) # 56 -> 56
pool3 = max_pool(conv_l3_d1, 2) # 56 -> 28
conv_l4_d1 = _conv_multi_block(pool3, 3, num_maps=256, instance_norm=instance_norm) # 28 -> 28
pool4 = max_pool(conv_l4_d1, 2) # 28 -> 14
# -----------------------------------------
# Processing: Level 5, Input size: 14 x 14
conv_l5_d1 = _conv_multi_block(pool4, 3, num_maps=512, instance_norm=instance_norm)
conv_l5_d2 = _conv_multi_block(conv_l5_d1, 3, num_maps=512, instance_norm=instance_norm) + conv_l5_d1
conv_l5_d3 = _conv_multi_block(conv_l5_d2, 3, num_maps=512, instance_norm=instance_norm) + conv_l5_d2
conv_l5_d4 = _conv_multi_block(conv_l5_d3, 3, num_maps=512, instance_norm=instance_norm)
conv_t4a = _conv_tranpose_layer(conv_l5_d4, 256, 3, 2) # 14 -> 28
conv_t4b = _conv_tranpose_layer(conv_l5_d4, 256, 3, 2) # 14 -> 28
# -> Output: Level 5
conv_l5_out = _conv_layer(conv_l5_d4, 3, 3, 1, relu=False, instance_norm=False)
output_l5 = tf.nn.tanh(conv_l5_out) * 0.58 + 0.5
# -----------------------------------------
# Processing: Level 4, Input size: 28 x 28
conv_l4_d2 = stack(conv_l4_d1, conv_t4a)
conv_l4_d3 = _conv_multi_block(conv_l4_d2, 3, num_maps=256, instance_norm=instance_norm)
conv_l4_d4 = _conv_multi_block(conv_l4_d3, 3, num_maps=256, instance_norm=instance_norm) + conv_l4_d3
conv_l4_d5 = _conv_multi_block(conv_l4_d4, 3, num_maps=256, instance_norm=instance_norm) + conv_l4_d4
conv_l4_d6 = stack(_conv_multi_block(conv_l4_d5, 3, num_maps=256, instance_norm=instance_norm), conv_t4b)
conv_l4_d7 = _conv_multi_block(conv_l4_d6, 3, num_maps=256, instance_norm=instance_norm)
conv_t3a = _conv_tranpose_layer(conv_l4_d7, 128, 3, 2) # 28 -> 56
conv_t3b = _conv_tranpose_layer(conv_l4_d7, 128, 3, 2) # 28 -> 56
# -> Output: Level 4
conv_l4_out = _conv_layer(conv_l4_d7, 3, 3, 1, relu=False, instance_norm=False)
output_l4 = tf.nn.tanh(conv_l4_out) * 0.58 + 0.5
# -----------------------------------------
# Processing: Level 3, Input size: 56 x 56
conv_l3_d2 = stack(conv_l3_d1, conv_t3a)
conv_l3_d3 = _conv_multi_block(conv_l3_d2, 5, num_maps=128, instance_norm=instance_norm) + conv_l3_d2
conv_l3_d4 = _conv_multi_block(conv_l3_d3, 5, num_maps=128, instance_norm=instance_norm) + conv_l3_d3
conv_l3_d5 = _conv_multi_block(conv_l3_d4, 5, num_maps=128, instance_norm=instance_norm) + conv_l3_d4
conv_l3_d6 = stack(_conv_multi_block(conv_l3_d5, 5, num_maps=128, instance_norm=instance_norm), conv_l3_d1)
conv_l3_d7 = stack(conv_l3_d6, conv_t3b)
conv_l3_d8 = _conv_multi_block(conv_l3_d7, 3, num_maps=128, instance_norm=instance_norm)
conv_t2a = _conv_tranpose_layer(conv_l3_d8, 64, 3, 2) # 56 -> 112
conv_t2b = _conv_tranpose_layer(conv_l3_d8, 64, 3, 2) # 56 -> 112
# -> Output: Level 3
conv_l3_out = _conv_layer(conv_l3_d8, 3, 3, 1, relu=False, instance_norm=False)
output_l3 = tf.nn.tanh(conv_l3_out) * 0.58 + 0.5
# -------------------------------------------
# Processing: Level 2, Input size: 112 x 112
conv_l2_d2 = stack(conv_l2_d1, conv_t2a)
conv_l2_d3 = stack(_conv_multi_block(conv_l2_d2, 5, num_maps=64, instance_norm=instance_norm), conv_l2_d1)
conv_l2_d4 = _conv_multi_block(conv_l2_d3, 7, num_maps=64, instance_norm=instance_norm) + conv_l2_d3
conv_l2_d5 = _conv_multi_block(conv_l2_d4, 7, num_maps=64, instance_norm=instance_norm) + conv_l2_d4
conv_l2_d6 = _conv_multi_block(conv_l2_d5, 7, num_maps=64, instance_norm=instance_norm) + conv_l2_d5
conv_l2_d7 = stack(_conv_multi_block(conv_l2_d6, 7, num_maps=64, instance_norm=instance_norm), conv_l2_d1)
conv_l2_d8 = stack(_conv_multi_block(conv_l2_d7, 5, num_maps=64, instance_norm=instance_norm), conv_t2b)
conv_l2_d9 = _conv_multi_block(conv_l2_d8, 3, num_maps=64, instance_norm=instance_norm)
conv_t1a = _conv_tranpose_layer(conv_l2_d9, 32, 3, 2) # 112 -> 224
conv_t1b = _conv_tranpose_layer(conv_l2_d9, 32, 3, 2) # 112 -> 224
# -> Output: Level 2
conv_l2_out = _conv_layer(conv_l2_d9, 3, 3, 1, relu=False, instance_norm=False)
output_l2 = tf.nn.tanh(conv_l2_out) * 0.58 + 0.5
# -------------------------------------------
# Processing: Level 1, Input size: 224 x 224
conv_l1_d2 = stack(conv_l1_d1, conv_t1a)
conv_l1_d3 = stack(_conv_multi_block(conv_l1_d2, 5, num_maps=32, instance_norm=False), conv_l1_d1)
conv_l1_d4 = _conv_multi_block(conv_l1_d3, 7, num_maps=32, instance_norm=False)
conv_l1_d5 = _conv_multi_block(conv_l1_d4, 9, num_maps=32, instance_norm=instance_norm_level_1)
conv_l1_d6 = _conv_multi_block(conv_l1_d5, 9, num_maps=32, instance_norm=instance_norm_level_1) + conv_l1_d5
conv_l1_d7 = _conv_multi_block(conv_l1_d6, 9, num_maps=32, instance_norm=instance_norm_level_1) + conv_l1_d6
conv_l1_d8 = _conv_multi_block(conv_l1_d7, 9, num_maps=32, instance_norm=instance_norm_level_1) + conv_l1_d7
conv_l1_d9 = stack(_conv_multi_block(conv_l1_d8, 7, num_maps=32, instance_norm=False), conv_l1_d1)
conv_l1_d10 = stack(_conv_multi_block(conv_l1_d9, 5, num_maps=32, instance_norm=False), conv_t1b)
conv_l1_d11 = stack(conv_l1_d10, conv_l1_d1)
conv_l1_d12 = _conv_multi_block(conv_l1_d11, 3, num_maps=32, instance_norm=False)
# -> Output: Level 1
conv_l1_out = _conv_layer(conv_l1_d12, 3, 3, 1, relu=False, instance_norm=False)
output_l1 = tf.nn.tanh(conv_l1_out) * 0.58 + 0.5
# ----------------------------------------------------------
# Processing: Level 0 (x2 upscaling), Input size: 224 x 224
conv_l0 = _conv_tranpose_layer(conv_l1_d12, 8, 3, 2) # 224 -> 448
conv_l0_out = _conv_layer(conv_l0, 3, 3, 1, relu=False, instance_norm=False)
output_l0 = tf.nn.tanh(conv_l0_out) * 0.58 + 0.5
return output_l0, output_l1, output_l2, output_l3, output_l4, output_l5
def _conv_multi_block(input, max_size, num_maps, instance_norm):
conv_3a = _conv_layer(input, num_maps, 3, 1, relu=True, instance_norm=instance_norm)
conv_3b = _conv_layer(conv_3a, num_maps, 3, 1, relu=True, instance_norm=instance_norm)
output_tensor = conv_3b
if max_size >= 5:
conv_5a = _conv_layer(input, num_maps, 5, 1, relu=True, instance_norm=instance_norm)
conv_5b = _conv_layer(conv_5a, num_maps, 5, 1, relu=True, instance_norm=instance_norm)
output_tensor = stack(output_tensor, conv_5b)
if max_size >= 7:
conv_7a = _conv_layer(input, num_maps, 7, 1, relu=True, instance_norm=instance_norm)
conv_7b = _conv_layer(conv_7a, num_maps, 7, 1, relu=True, instance_norm=instance_norm)
output_tensor = stack(output_tensor, conv_7b)
if max_size >= 9:
conv_9a = _conv_layer(input, num_maps, 9, 1, relu=True, instance_norm=instance_norm)
conv_9b = _conv_layer(conv_9a, num_maps, 9, 1, relu=True, instance_norm=instance_norm)
output_tensor = stack(output_tensor, conv_9b)
return output_tensor
def stack(x, y):
return tf.concat([x, y], 3)
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def leaky_relu(x, alpha=0.2):
return tf.maximum(alpha * x, x)
def _conv_layer(net, num_filters, filter_size, strides, relu=True, instance_norm=False, padding='SAME'):
weights_init = _conv_init_vars(net, num_filters, filter_size)
strides_shape = [1, strides, strides, 1]
bias = tf.Variable(tf.constant(0.01, shape=[num_filters]))
net = tf.nn.conv2d(net, weights_init, strides_shape, padding=padding) + bias
if instance_norm:
net = _instance_norm(net)
if relu:
net = leaky_relu(net)
return net
def _instance_norm(net):
batch, rows, cols, channels = [i.value for i in net.get_shape()]
var_shape = [channels]
mu, sigma_sq = tf.nn.moments(net, [1,2], keep_dims=True)
shift = tf.Variable(tf.zeros(var_shape))
scale = tf.Variable(tf.ones(var_shape))
epsilon = 1e-3
normalized = (net-mu)/(sigma_sq + epsilon)**(.5)
return scale * normalized + shift
def _conv_init_vars(net, out_channels, filter_size, transpose=False):
_, rows, cols, in_channels = [i.value for i in net.get_shape()]
if not transpose:
weights_shape = [filter_size, filter_size, in_channels, out_channels]
else:
weights_shape = [filter_size, filter_size, out_channels, in_channels]
weights_init = tf.Variable(tf.truncated_normal(weights_shape, stddev=0.01, seed=1), dtype=tf.float32)
return weights_init
def _conv_tranpose_layer(net, num_filters, filter_size, strides):
weights_init = _conv_init_vars(net, num_filters, filter_size, transpose=True)
batch_size, rows, cols, in_channels = [i.value for i in net.get_shape()]
new_rows, new_cols = int(rows * strides), int(cols * strides)
new_shape = [batch_size, new_rows, new_cols, num_filters]
tf_shape = tf.stack(new_shape)
strides_shape = [1, strides, strides, 1]
net = tf.nn.conv2d_transpose(net, weights_init, tf_shape, strides_shape, padding='SAME')
return leaky_relu(net)
def max_pool(x, n):
return tf.nn.max_pool(x, ksize=[1, n, n, 1], strides=[1, n, n, 1], padding='VALID')