-
Notifications
You must be signed in to change notification settings - Fork 30
/
model.py
255 lines (159 loc) · 10.9 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
244
245
246
247
248
249
250
251
252
253
254
255
# 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):
# Note: the paper uses a different layer naming scheme.
# In this code, layer N corresponds to layer N+2 from the article.
with tf.compat.v1.variable_scope("generator"):
# -----------------------------------------
# Space-to-depth layer
space2depth_l0 = tf.nn.space_to_depth(input, 2) # 512 -> 256
# -----------------------------------------
# Downsampling layers
conv_l1_d1 = _conv_multi_block(space2depth_l0, 3, num_maps=32, instance_norm=False) # 256 -> 256
pool1 = max_pool(conv_l1_d1, 2) # 256 -> 128
conv_l2_d1 = _conv_multi_block(pool1, 3, num_maps=64, instance_norm=instance_norm) # 128 -> 128
pool2 = max_pool(conv_l2_d1, 2) # 128 -> 64
conv_l3_d1 = _conv_multi_block(pool2, 3, num_maps=128, instance_norm=instance_norm) # 64 -> 64
pool3 = max_pool(conv_l3_d1, 2) # 64 -> 32
conv_l4_d1 = _conv_multi_block(pool3, 3, num_maps=256, instance_norm=instance_norm) # 32 -> 32
pool4 = max_pool(conv_l4_d1, 2) # 32 -> 16
# -----------------------------------------
# Processing: Level 5, Input size: 16 x 16
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) # 16 -> 32
conv_t4b = _conv_tranpose_layer(conv_l5_d4, 256, 3, 2) # 16 -> 32
# -> 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: 32 x 32
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) # 32 -> 64
conv_t3b = _conv_tranpose_layer(conv_l4_d7, 128, 3, 2) # 32 -> 64
# -> 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: 64 x 64
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) # 64 -> 128
conv_t2b = _conv_tranpose_layer(conv_l3_d8, 64, 3, 2) # 64 -> 128
# -> 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: 128 x 128
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) # 128 -> 256
conv_t1b = _conv_tranpose_layer(conv_l2_d9, 32, 3, 2) # 128 -> 256
# -> 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: 256 x 256
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: 256 x 256
conv_l0 = _conv_tranpose_layer(conv_l1_d12, 8, 3, 2) # 256 -> 512
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
# ----------------------------------------------------------
# Processing: Level Up (x4 upscaling), Input size: 512 x 512
conv_l_up = _conv_tranpose_layer(conv_l0_out, 3, 3, 2) # 512 -> 1024
conv_l_up_out = _conv_layer(conv_l_up, 3, 3, 1, relu=False, instance_norm=False)
output_l_up = tf.nn.tanh(conv_l_up_out) * 0.58 + 0.5
return output_l_up, 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.compat.v1.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.compat.v1.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)
net_shape = tf.shape(net)
tf_shape = tf.stack([net_shape[0], net_shape[1] * strides, net_shape[2] * strides, num_filters])
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')