-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinpaint_model.py
398 lines (348 loc) · 19.2 KB
/
inpaint_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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
""" common model for DCGAN """
import logging
import sys
sys.path.insert(0, '/usr/local/lib/python3.5/dist-packages')
import cv2
import PIL
import numpy as np
import neuralgym as ng
import tensorflow as tf
from tensorflow.contrib.framework.python.ops import arg_scope
from neuralgym.models import Model
from neuralgym.ops.summary_ops import scalar_summary, images_summary
from neuralgym.ops.summary_ops import gradients_summary
from neuralgym.ops.layers import flatten, resize
from neuralgym.ops.gan_ops import gan_wgan_loss, gradients_penalty
from neuralgym.ops.gan_ops import random_interpolates
from inpaint_ops import gen_conv, gen_deconv, dis_conv
from inpaint_ops import random_bbox, bbox2mask, local_patch
from inpaint_ops import spatial_discounting_mask
from inpaint_ops import resize_mask_like, contextual_attention, surface_attention
logger = logging.getLogger()
def surf_conv(depth):
kernel_x = np.zeros((3,3,1,1))
kernel_y = np.zeros((3,3,1,1))
kernel_blur = np.ones((3,3,1,1)) / 9
kernel_x[0,1,:,:] = 0.5
kernel_x[2,1,:,:] = -0.5
kernel_y[1,0,:,:] = 0.5
kernel_y[1,2,:,:] = -0.5
out_x = tf.nn.conv2d(depth, kernel_x, strides=[1,1,1,1], padding='SAME')
out_y = tf.nn.conv2d(depth, kernel_y, strides=[1,1,1,1], padding='SAME')
out = tf.concat([tf.ones_like(depth), out_y, out_x], axis=-1)
norm_out = tf.norm(out, axis=-1, keep_dims=True)
out = ((out/norm_out) + 1.) * 127.5
out_r, out_g, out_b = tf.split(out, axis=-1, num_or_size_splits=3)
out_r = tf.nn.conv2d(out_r, kernel_blur, strides=[1,1,1,1], padding='SAME')
out_g = tf.nn.conv2d(out_g, kernel_blur, strides=[1,1,1,1], padding='SAME')
out_b = tf.nn.conv2d(out_b, kernel_blur, strides=[1,1,1,1], padding='SAME')
out_surf = tf.concat([out_r, out_g, out_b], axis=-1)
out_surf = (out_surf/127.5) - 1.
return out_surf
class InpaintCAModel(Model):
def __init__(self):
super().__init__('InpaintCAModel')
def build_inpaint_net(self, x, mask, config=None, reuse=False,
training=True, padding='SAME', name='inpaint_net'):
"""Inpaint network.
Args:
x: incomplete image, [-1, 1]
mask: mask region {0, 1}
Returns:
[-1, 1] as predicted image
"""
xin = x
offset_flow = None
ones_x = tf.ones_like(x)[:, :, :, 0:1]
xsurf_mask = tf.clip_by_value(((x + 1.) * 127.5), 0., 1.)
x = tf.concat([x, ones_x, ones_x*mask], axis=3)
# two stage network
cnum = 32
with tf.variable_scope(name, reuse=tf.AUTO_REUSE), \
arg_scope([gen_conv, gen_deconv],
training=training, padding=padding):
# stage1
#####################################################
#Tensorflow Map Function (RGB + Disparity) = Input #
#-> Input = map_function(RGB, Disparity) #
#####################################################
x = gen_conv(x, cnum, 5, 1, name='conv1')
x = gen_conv(x, 2*cnum, 3, 2, name='conv2_downsample')
x = gen_conv(x, 2*cnum, 3, 1, name='conv3')
x = gen_conv(x, 4*cnum, 3, 2, name='conv4_downsample')
x = gen_conv(x, 4*cnum, 3, 1, name='conv5')
x = gen_conv(x, 4*cnum, 3, 1, name='conv6')
mask_s = resize_mask_like(mask, x)
x = gen_conv(x, 4*cnum, 3, rate=2, name='conv7_atrous')
x = gen_conv(x, 4*cnum, 3, rate=4, name='conv8_atrous')
x = gen_conv(x, 4*cnum, 3, rate=8, name='conv9_atrous')
x = gen_conv(x, 4*cnum, 3, rate=16, name='conv10_atrous')
x = gen_conv(x, 4*cnum, 3, 1, name='conv11')
x = gen_conv(x, 4*cnum, 3, 1, name='conv12')
x = gen_deconv(x, 2*cnum, name='conv13_upsample')
x = gen_conv(x, 2*cnum, 3, 1, name='conv14')
x = gen_deconv(x, cnum, name='conv15_upsample')
x = gen_conv(x, cnum//2, 3, 1, name='conv16')
x = gen_conv(x, 3, 3, 1, activation=None, name='conv17')
x = tf.clip_by_value(x, -1., 1.)
x_stage1 = x
# return x_stage1, None, None
# stage2, paste result as input
# x = tf.stop_gradient(x)
#cnum = 32
x = x*mask + xin*(1.-mask)
x.set_shape(xin.get_shape().as_list())
# conv branch
xnow = tf.concat([x, ones_x, ones_x*mask], axis=3)
x_surf = surf_conv((tf.image.rgb_to_grayscale(x) + 1.) * 127.5)
xnow_surf = tf.concat([tf.image.rgb_to_grayscale(x), x_surf, ones_x, ones_x*mask], axis=3)
x = gen_conv(xnow, cnum, 5, 1, name='xconv1')
x = gen_conv(x, cnum, 3, 2, name='xconv2_downsample')
x = gen_conv(x, 2*cnum, 3, 1, name='xconv3')
x = gen_conv(x, 2*cnum, 3, 2, name='xconv4_downsample')
x = gen_conv(x, 4*cnum, 3, 1, name='xconv5')
x = gen_conv(x, 4*cnum, 3, 1, name='xconv6')
x = gen_conv(x, 4*cnum, 3, rate=2, name='xconv7_atrous')
x = gen_conv(x, 4*cnum, 3, rate=4, name='xconv8_atrous')
x = gen_conv(x, 4*cnum, 3, rate=8, name='xconv9_atrous')
x = gen_conv(x, 4*cnum, 3, rate=16, name='xconv10_atrous')
x_hallu = x
# attention branch
x = gen_conv(xnow_surf, cnum, 5, 1, name='pmconv1')
x = gen_conv(x, cnum, 3, 2, name='pmconv2_downsample')
x = gen_conv(x, 2*cnum, 3, 1, name='pmconv3')
x = gen_conv(x, 4*cnum, 3, 2, name='pmconv4_downsample')
x = gen_conv(x, 4*cnum, 3, 1, name='pmconv5')
x = gen_conv(x, 4*cnum, 3, 1, name='pmconv6',
activation=tf.nn.relu)
x, offset_flow = surface_attention(x, x, mask_s, 3, 1, rate=2)
x = gen_conv(x, 4*cnum, 3, 1, name='pmconv9')
x = gen_conv(x, 4*cnum, 3, 1, name='pmconv10')
pm = x
x = tf.concat([x_hallu, pm], axis=3)
x = gen_conv(x, 4*cnum, 3, 1, name='allconv11')
x = gen_conv(x, 4*cnum, 3, 1, name='allconv12')
x = gen_deconv(x, 2*cnum, name='allconv13_upsample')
x = gen_conv(x, 2*cnum, 3, 1, name='allconv14')
x = gen_deconv(x, cnum, name='allconv15_upsample')
x = gen_conv(x, cnum//2, 3, 1, name='allconv16')
x = gen_conv(x, 3, 3, 1, activation=None, name='allconv17')
x_stage2 = tf.clip_by_value(x, -1., 1.)
return x_stage1, x_stage2, offset_flow
def build_wgan_local_discriminator(self, x, reuse=False, training=True):
with tf.variable_scope('discriminator_local', reuse=reuse):
cnum = 64
x = dis_conv(x, cnum, name='conv1', training=training)
x = dis_conv(x, cnum*2, name='conv2', training=training)
x = dis_conv(x, cnum*4, name='conv3', training=training)
x = dis_conv(x, cnum*8, name='conv4', training=training)
x = flatten(x, name='flatten')
return x
def build_wgan_global_discriminator(self, x, reuse=False, training=True):
with tf.variable_scope('discriminator_global', reuse=reuse):
cnum = 64
x = dis_conv(x, cnum, name='conv1', training=training)
x = dis_conv(x, cnum*2, name='conv2', training=training)
x = dis_conv(x, cnum*4, name='conv3', training=training)
x = dis_conv(x, cnum*4, name='conv4', training=training)
x = flatten(x, name='flatten')
return x
def build_wgan_discriminator(self, batch_local, batch_global,
reuse=False, training=True):
with tf.variable_scope('discriminator', reuse=reuse):
#add noisy to discirminator input
batch_local = batch_local + tf.random_normal(shape=tf.shape(batch_local), mean=0.0, stddev=0.1, dtype=tf.float32)
batch_global = batch_global + tf.random_normal(shape=tf.shape(batch_global), mean=0.0, stddev=0.1, dtype=tf.float32)
dlocal = self.build_wgan_local_discriminator(
batch_local, reuse=reuse, training=training)
dglobal = self.build_wgan_global_discriminator(
batch_global, reuse=reuse, training=training)
dout_local = tf.layers.dense(dlocal, 1, name='dout_local_fc')
dout_global = tf.layers.dense(dglobal, 1, name='dout_global_fc')
return dout_local, dout_global
def build_graph_with_losses(self, batch_data, config, training=True,
summary=False, reuse=False):
batch_pos = batch_data / 127.5 - 1.
# generate mask, 1 represents masked point
bbox = random_bbox(config)
#Generate mask from box parameters
mask = bbox2mask(bbox, config, name='mask_c')
batch_incomplete = batch_pos*(1.-mask)
x1, x2, offset_flow = self.build_inpaint_net(
batch_incomplete, mask, config, reuse=reuse, training=training,
padding=config.PADDING)
if config.PRETRAIN_COARSE_NETWORK:
batch_predicted = x1
logger.info('Set batch_predicted to x1.')
else:
batch_predicted = x2
logger.info('Set batch_predicted to x2.')
losses = {}
# apply mask and complete image
batch_complete = batch_predicted*mask + batch_incomplete*(1.-mask)
# local patches
local_patch_batch_pos = local_patch(batch_pos, bbox)
local_patch_batch_predicted = local_patch(batch_predicted, bbox)
local_patch_x1 = local_patch(x1, bbox)
local_patch_x2 = local_patch(x2, bbox)
local_patch_batch_complete = local_patch(batch_complete, bbox)
local_patch_mask = local_patch(mask, bbox)
l1_alpha = config.COARSE_L1_ALPHA
losses['l1_loss'] = l1_alpha * tf.reduce_mean(tf.abs(local_patch_batch_pos - local_patch_x1)*spatial_discounting_mask(config))
if not config.PRETRAIN_COARSE_NETWORK:
losses['l1_loss'] += tf.reduce_mean(tf.abs(local_patch_batch_pos - local_patch_x2)*spatial_discounting_mask(config))
losses['ae_loss'] = l1_alpha * tf.reduce_mean(tf.abs(batch_pos - x1) * (1.-mask))
if not config.PRETRAIN_COARSE_NETWORK:
losses['ae_loss'] += tf.reduce_mean(tf.abs(batch_pos - x2) * (1.-mask))
losses['ae_loss'] /= tf.reduce_mean(1.-mask)
if summary:
scalar_summary('losses/l1_loss', losses['l1_loss'])
scalar_summary('losses/ae_loss', losses['ae_loss'])
viz_img = [batch_pos, batch_incomplete, batch_complete]
if offset_flow is not None:
viz_img.append(
resize(offset_flow, scale=4,
func=tf.image.resize_nearest_neighbor))
images_summary(
tf.concat(viz_img, axis=2),
'raw_incomplete_predicted_complete', config.VIZ_MAX_OUT)
##################################GLOBAL SURFACE DISCRIMINATION#################################
surf_pos = surf_conv((tf.image.rgb_to_grayscale(batch_pos) + 1.) * 127.5)
surf_neg = surf_conv((tf.image.rgb_to_grayscale(batch_complete) + 1.) * 127.5)
batch_pos = tf.concat([batch_pos, surf_pos], axis=-1)
batch_complete = tf.concat([batch_complete, surf_neg], axis=-1)
# gan
batch_pos_neg = tf.concat([batch_pos, batch_complete], axis=0)
############################################################################################
##################################LOCAL SURFACE DISCRIMINATION#################################
local_surf_pos = surf_conv((tf.image.rgb_to_grayscale(local_patch_batch_pos) + 1.) * 127.5)
local_surf_neg = surf_conv((tf.image.rgb_to_grayscale(local_patch_batch_complete) + 1.) * 127.5)
local_patch_batch_pos = tf.concat([local_patch_batch_pos, local_surf_pos], axis=-1)
local_patch_batch_complete = tf.concat([local_patch_batch_complete, local_surf_neg], axis=-1)
# local deterministic patch
local_patch_batch_pos_neg = tf.concat([local_patch_batch_pos, local_patch_batch_complete], 0)
############################################################################################
if config.GAN_WITH_MASK:
batch_pos_neg = tf.concat([batch_pos_neg, tf.tile(mask, [config.BATCH_SIZE*2, 1, 1, 1])], axis=3)
# wgan with gradient penalty
if config.GAN == 'wgan_gp':
# seperate gan
pos_neg_local, pos_neg_global = self.build_wgan_discriminator(local_patch_batch_pos_neg, batch_pos_neg, training=training, reuse=reuse)
pos_local, neg_local = tf.split(pos_neg_local, 2)
pos_global, neg_global = tf.split(pos_neg_global, 2)
# wgan loss
g_loss_local, d_loss_local = gan_wgan_loss(pos_local, neg_local, name='gan/local_gan')
g_loss_global, d_loss_global = gan_wgan_loss(pos_global, neg_global, name='gan/global_gan')
losses['g_loss'] = config.GLOBAL_WGAN_LOSS_ALPHA * g_loss_global + g_loss_local
losses['d_loss'] = d_loss_global + d_loss_local
# gp
interpolates_local = random_interpolates(local_patch_batch_pos, local_patch_batch_complete)
interpolates_global = random_interpolates(batch_pos, batch_complete)
dout_local, dout_global = self.build_wgan_discriminator(
interpolates_local, interpolates_global, reuse=True)
# apply penalty
penalty_local = gradients_penalty(interpolates_local, dout_local, mask=local_patch_mask)
penalty_global = gradients_penalty(interpolates_global, dout_global, mask=mask)
losses['gp_loss'] = config.WGAN_GP_LAMBDA * (penalty_local + penalty_global)
losses['d_loss'] = losses['d_loss'] + losses['gp_loss']
####################################VECTORIAL LOSS##########################################
batch_out = tf.image.rgb_to_grayscale((batch_predicted + 1.) * 127.5)
batch_coarse = tf.image.rgb_to_grayscale((x1 + 1.) * 127.5)
batch_in = tf.image.rgb_to_grayscale(batch_data)
losses['v_loss'] = tf.reduce_mean(tf.abs(surf_conv(batch_out) - surf_conv(batch_in))) #FINAL RESULT
losses['v_loss'] += tf.reduce_mean(tf.abs(surf_conv(batch_coarse) - surf_conv(batch_in))) #COARSE RESULT (AE LOSS)
############################################################################################
if summary and not config.PRETRAIN_COARSE_NETWORK:
gradients_summary(g_loss_local, batch_predicted, name='g_loss_local')
gradients_summary(g_loss_global, batch_predicted, name='g_loss_global')
scalar_summary('convergence/d_loss', losses['d_loss'])
scalar_summary('convergence/local_d_loss', d_loss_local)
scalar_summary('convergence/global_d_loss', d_loss_global)
scalar_summary('gan_wgan_loss/gp_loss', losses['gp_loss'])
scalar_summary('gan_wgan_loss/gp_penalty_local', penalty_local)
scalar_summary('gan_wgan_loss/gp_penalty_global', penalty_global)
if summary and not config.PRETRAIN_COARSE_NETWORK:
# summary the magnitude of gradients from different losses w.r.t. predicted image
gradients_summary(losses['g_loss'], batch_predicted, name='g_loss')
gradients_summary(losses['g_loss'], x1, name='g_loss_to_x1')
gradients_summary(losses['g_loss'], x2, name='g_loss_to_x2')
gradients_summary(losses['l1_loss'], x1, name='l1_loss_to_x1')
gradients_summary(losses['l1_loss'], x2, name='l1_loss_to_x2')
gradients_summary(losses['ae_loss'], x1, name='ae_loss_to_x1')
gradients_summary(losses['ae_loss'], x2, name='ae_loss_to_x2')
#################################V_LOSS GRADIENTS###########################################
gradients_summary(losses['v_loss'], batch_coarse, name='v_loss_to_x1')
gradients_summary(losses['v_loss'], batch_out, name='v_loss_to_x2')
############################################################################################
if config.PRETRAIN_COARSE_NETWORK:
losses['g_loss'] = 0
else:
losses['g_loss'] = config.GAN_LOSS_ALPHA * losses['g_loss']
losses['g_loss'] += config.L1_LOSS_ALPHA * losses['l1_loss']
losses['g_loss'] += config.V_LOSS_ALPHA * losses['v_loss']
logger.info('Set L1_LOSS_ALPHA to %f' % config.L1_LOSS_ALPHA)
logger.info('Set GAN_LOSS_ALPHA to %f' % config.GAN_LOSS_ALPHA)
if config.AE_LOSS:
losses['g_loss'] += config.AE_LOSS_ALPHA * losses['ae_loss']
logger.info('Set AE_LOSS_ALPHA to %f' % config.AE_LOSS_ALPHA)
g_vars = tf.get_collection(
tf.GraphKeys.TRAINABLE_VARIABLES, 'inpaint_net')
d_vars = tf.get_collection(
tf.GraphKeys.TRAINABLE_VARIABLES, 'discriminator')
return g_vars, d_vars, losses
def build_infer_graph(self, batch_data, config, bbox=None, name='val'):
"""
"""
config.MAX_DELTA_HEIGHT = 0
config.MAX_DELTA_WIDTH = 0
if bbox is None:
bbox = random_bbox(config)
mask = bbox2mask(bbox, config, name=name+'mask_c')
batch_pos = batch_data / 127.5 - 1.
edges = None
batch_incomplete = batch_pos*(1.-mask)
# inpaint
x1, x2, offset_flow = self.build_inpaint_net(
batch_incomplete, mask, config, reuse=True,
training=False, padding=config.PADDING)
if config.PRETRAIN_COARSE_NETWORK:
batch_predicted = x1
logger.info('Set batch_predicted to x1.')
else:
batch_predicted = x2
logger.info('Set batch_predicted to x2.')
# apply mask and reconstruct
batch_complete = batch_predicted*mask + batch_incomplete*(1.-mask)
# global image visualization
viz_img = [batch_pos, batch_incomplete, batch_complete]
if offset_flow is not None:
viz_img.append(
resize(offset_flow, scale=4,
func=tf.image.resize_nearest_neighbor))
images_summary(
tf.concat(viz_img, axis=2),
name+'_raw_incomplete_complete', config.VIZ_MAX_OUT)
return batch_complete
def build_static_infer_graph(self, batch_data, config, name):
"""
"""
# generate mask, 1 represents masked point
bbox = (tf.constant(config.HEIGHT//2), tf.constant(config.WIDTH//2),
tf.constant(config.HEIGHT), tf.constant(config.WIDTH))
return self.build_infer_graph(batch_data, config, bbox, name)
def build_server_graph(self, batch_data, reuse=False, is_training=False):
"""
"""
# generate mask, 1 represents masked point
batch_raw, masks_raw = tf.split(batch_data, 2, axis=-1)
masks = tf.cast(masks_raw[0:1, :, :, 0:1] > 127.5, tf.float32)
batch_pos = batch_raw / 127.5 - 1.
batch_incomplete = batch_pos * (1. - masks)
# inpaint
x1, x2, flow = self.build_inpaint_net(
batch_incomplete, masks, reuse=reuse, training=is_training,
config=None)
batch_predict = x2
# apply mask and reconstruct
batch_complete = batch_predict*masks + batch_incomplete*(1-masks)
return batch_complete