-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patharchitecture.py
260 lines (210 loc) · 9.25 KB
/
architecture.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
256
257
258
259
260
import tensorflow as tf
from tensorflow.contrib.framework.python.ops import add_arg_scope
from tensorflow.contrib.layers import conv2d,conv2d_transpose
def l1_loss(x, y):
return tf.abs(x - y)
def l2_loss(x, y):
return tf.square(x - y)
def styleloss(f1, f2, f3):
gen_f, style_f = tf.split(f1, 2, 0)
style_loss = tf.reduce_mean(l1_loss(gram(gen_f), gram(style_f)))
gen_f, style_f = tf.split(f2, 2, 0)
style_loss += tf.reduce_mean(l1_loss(gram(gen_f), gram(style_f)))
gen_f, style_f = tf.split(f3, 2, 0)
style_loss += tf.reduce_mean(l1_loss(gram(gen_f), gram(style_f)))
return style_loss
def gram(layer):
shape = tf.shape(layer)
num_images = shape[0]
width = shape[1]
height = shape[2]
num_filters = shape[3]
filters = tf.reshape(layer, tf.stack([num_images, -1, num_filters]))
grams = tf.matmul(filters, filters, transpose_a=True) / tf.to_float(width * height * num_filters)
return grams
def contentloss(f1, f2, f3, f4, f5):
content_1, content_2 = tf.split(f1, 2, 0)
content_loss = tf.reduce_mean(l1_loss(content_1, content_2))
content_1, content_2 = tf.split(f2, 2, 0)
content_loss += tf.reduce_mean(l1_loss(content_1, content_2))
content_1, content_2 = tf.split(f3, 2, 0)
content_loss += tf.reduce_mean(l1_loss(content_1, content_2))
content_1, content_2 = tf.split(f4, 2, 0)
content_loss += tf.reduce_mean(l1_loss(content_1, content_2))
content_1, content_2 = tf.split(f5, 2, 0)
content_loss += tf.reduce_mean(l1_loss(content_1, content_2))
return content_loss
def lrelu(x, leak=0.2, name="lrelu"):
return tf.maximum(x, leak*x)
@add_arg_scope
def conv(x, num_outputs, kernel_size, stride=1, rate=1, scope='conv', padding='SAME', activation_fn=tf.nn.elu, normalizer_fn=None):
"""Define conv for generator.
Args:
x: Input.
cnum: Channel number.
ksize: Kernel size.
Stride: Convolution stride.
Rate: Rate for or dilated conv.
scope: Name of layers.
padding: Default to SYMMETRIC.
activation: Activation function after convolution.
Returns:
tf.Tensor: output
"""
x = conv2d(
x, num_outputs, kernel_size, stride, rate=rate,
activation_fn=activation_fn, padding=padding, scope=scope, normalizer_fn=normalizer_fn)
return x
@add_arg_scope
def deconv(x, num_outputs, scope='deconv', padding='SAME', normalizer_fn=None):
"""Define deconv for generator.
The deconv is defined to be a x2 resize_nearest_neighbor operation with
additional gen_conv operation.
Args:
x: Input.
cnum: Channel number.
scope: Name of layers.
training: If current graph is for training or inference, used for bn.
Returns:
tf.Tensor: output
"""
with tf.variable_scope(scope):
# x = resize(x, func=tf.image.resize_bilinear)
x = resize(x, func=tf.image.resize_nearest_neighbor)
x = conv(x, num_outputs, 3, 1, scope=scope, padding=padding, normalizer_fn=normalizer_fn)
return x
@add_arg_scope
def batch_norm(x, is_training=True, scope='bn'):
return tf.contrib.layers.batch_norm(x,
decay=0.9,
updates_collections=None,
epsilon=1e-5,
scale=True,
is_training=is_training,
scope=scope)
def linear(input, output_size, reuse=False, name="linear"):
shape = input.get_shape().as_list()
with tf.variable_scope(name, reuse=reuse):
matrix = tf.get_variable("W", [shape[1], output_size], tf.float32, tf.random_normal_initializer(stddev=0.02))
bias = tf.get_variable("bias", [output_size], initializer=tf.constant_initializer(0.0))
return tf.matmul(input, matrix) + bias
def contextual_attention(f, b, mask=None, ksize=3, stride=1, rate=1,
fuse_k=3, softmax_scale=10., training=True, fuse=True):
""" Contextual attention layer implementation.
Contextual attention is first introduced in publication:
Generative Image Inpainting with Contextual Attention, Yu et al.
Args:
x: Input feature to match (foreground).
t: Input feature for match (background).
mask: Input mask for t, indicating patches not available.
ksize: Kernel size for contextual attention.
stride: Stride for extracting patches from t.
rate: Dilation for matching.
softmax_scale: Scaled softmax for attention.
training: Indicating if current graph is training or inference.
Returns:
tf.Tensor: output
"""
# get shapes
raw_fs = tf.shape(f)
raw_int_fs = f.get_shape().as_list()
raw_int_bs = b.get_shape().as_list()
# extract patches from background with stride and rate
kernel = 2*rate
raw_w = tf.extract_image_patches(
b, [1,kernel,kernel,1], [1,rate*stride,rate*stride,1], [1,1,1,1], padding='SAME')
raw_w = tf.reshape(raw_w, [raw_int_bs[0], -1, kernel, kernel, raw_int_bs[3]])
raw_w = tf.transpose(raw_w, [0, 2, 3, 4, 1]) # transpose to b*k*k*c*hw
# downscaling foreground option: downscaling both foreground and
# background for matching and use original background for reconstruction.
f = resize(f, scale=1./rate, func=tf.image.resize_nearest_neighbor)
b = resize(b, to_shape=[int(raw_int_bs[1]/rate), int(raw_int_bs[2]/rate)], func=tf.image.resize_nearest_neighbor) # https://github.com/tensorflow/tensorflow/issues/11651
if mask is not None:
mask = resize(mask, scale=1./rate, func=tf.image.resize_nearest_neighbor)
fs = tf.shape(f)
int_fs = f.get_shape().as_list()
f_groups = tf.split(f, int_fs[0], axis=0)
# from t(H*W*C) to w(b*k*k*c*h*w)
bs = tf.shape(b)
int_bs = b.get_shape().as_list()
w = tf.extract_image_patches(
b, [1,ksize,ksize,1], [1,stride,stride,1], [1,1,1,1], padding='SAME')
w = tf.reshape(w, [int_fs[0], -1, ksize, ksize, int_fs[3]])
w = tf.transpose(w, [0, 2, 3, 4, 1]) # transpose to b*k*k*c*hw
# process mask
if mask is None:
mask = tf.zeros([1, bs[1], bs[2], 1])
m = tf.extract_image_patches(
mask, [1,ksize,ksize,1], [1,stride,stride,1], [1,1,1,1], padding='SAME')
m = tf.reshape(m, [1, -1, ksize, ksize, 1])
m = tf.transpose(m, [0, 2, 3, 4, 1]) # transpose to b*k*k*c*hw
m = m[0]
mm = tf.cast(tf.equal(tf.reduce_mean(m, axis=[0,1,2], keep_dims=True), 0.), tf.float32)
w_groups = tf.split(w, int_bs[0], axis=0)
raw_w_groups = tf.split(raw_w, int_bs[0], axis=0)
y = []
offsets = []
k = fuse_k
scale = softmax_scale
fuse_weight = tf.reshape(tf.eye(k), [k, k, 1, 1])
for xi, wi, raw_wi in zip(f_groups, w_groups, raw_w_groups):
# conv for compare
wi = wi[0]
wi_normed = wi / tf.maximum(tf.sqrt(tf.reduce_sum(tf.square(wi), axis=[0,1,2])), 1e-4)
yi = tf.nn.conv2d(xi, wi_normed, strides=[1,1,1,1], padding="SAME")
# conv implementation for fuse scores to encourage large patches
if fuse:
yi = tf.reshape(yi, [1, fs[1]*fs[2], bs[1]*bs[2], 1])
yi = tf.nn.conv2d(yi, fuse_weight, strides=[1,1,1,1], padding='SAME')
yi = tf.reshape(yi, [1, fs[1], fs[2], bs[1], bs[2]])
yi = tf.transpose(yi, [0, 2, 1, 4, 3])
yi = tf.reshape(yi, [1, fs[1]*fs[2], bs[1]*bs[2], 1])
yi = tf.nn.conv2d(yi, fuse_weight, strides=[1,1,1,1], padding='SAME')
yi = tf.reshape(yi, [1, fs[2], fs[1], bs[2], bs[1]])
yi = tf.transpose(yi, [0, 2, 1, 4, 3])
yi = tf.reshape(yi, [1, fs[1], fs[2], bs[1]*bs[2]])
# softmax to match
yi *= mm # mask
yi = tf.nn.softmax(yi*scale, 3)
yi *= mm # mask
offset = tf.argmax(yi, axis=3, output_type=tf.int32)
offset = tf.stack([offset // fs[2], offset % fs[2]], axis=-1)
# deconv for patch pasting
# 3.1 paste center
wi_center = raw_wi[0]
yi = tf.nn.conv2d_transpose(yi, wi_center, tf.concat([[1], raw_fs[1:]], axis=0), strides=[1,rate,rate,1]) / 4.
y.append(yi)
offsets.append(offset)
y = tf.concat(y, axis=0)
y.set_shape(raw_int_fs)
offsets = tf.concat(offsets, axis=0)
offsets.set_shape(int_bs[:3] + [2])
return y
def resize_mask_like(mask, x):
"""Resize mask like shape of x.
Args:
mask: Original mask.
x: To shape of x.
Returns:
tf.Tensor: resized mask
"""
mask_resize = resize(
mask, to_shape=x.get_shape().as_list()[1:3],
func=tf.image.resize_nearest_neighbor)
return mask_resize
def resize(x, scale=2, to_shape=None, align_corners=True, dynamic=False,
func=tf.image.resize_bilinear, name='resize'):
if dynamic:
xs = tf.cast(tf.shape(x), tf.float32)
new_xs = [tf.cast(xs[1]*scale, tf.int32),
tf.cast(xs[2]*scale, tf.int32)]
else:
xs = x.get_shape().as_list()
new_xs = [int(xs[1]*scale), int(xs[2]*scale)]
with tf.variable_scope(name):
if to_shape is None:
x = func(x, new_xs, align_corners=align_corners)
else:
x = func(x, [to_shape[0], to_shape[1]],
align_corners=align_corners)
return x