forked from kdh4672/hgonet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedge_connect.py
493 lines (389 loc) · 20.7 KB
/
edge_connect.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
import os
import numpy as np
import torch
from torch.utils.data import DataLoader
from .dataset import Dataset
from .models import EdgeModel, InpaintingModel
from .utils import Progbar, create_dir, stitch_images, imsave
from .metrics import PSNR, EdgeAccuracy
import cv2
class EdgeConnect():
def __init__(self, config):
self.config = config
if config.MODEL == 1:
model_name = 'edge'
elif config.MODEL == 2:
model_name = 'inpaint'
elif config.MODEL == 3:
model_name = 'edge_inpaint'
elif config.MODEL == 4:
model_name = 'joint'
self.debug = False
self.model_name = model_name
self.edge_model = EdgeModel(config).to(config.DEVICE)
self.inpaint_model = InpaintingModel(config).to(config.DEVICE)
self.psnr = PSNR(255.0).to(config.DEVICE)
self.edgeacc = EdgeAccuracy(config.EDGE_THRESHOLD).to(config.DEVICE)
# test mode
if self.config.MODE == 2:
self.test_dataset = Dataset(config, config.TEST_FLIST, config.TEST_EDGE_FLIST, config.TEST_MASK_FLIST, augment=False, training=False)
else:
self.train_dataset = Dataset(config, config.TRAIN_FLIST, config.TRAIN_EDGE_FLIST, config.TRAIN_MASK_FLIST, augment=False, training=True)
self.val_dataset = Dataset(config, config.VAL_FLIST, config.VAL_EDGE_FLIST, config.VAL_MASK_FLIST, augment=True, training=True)
self.sample_iterator = self.val_dataset.create_iterator(config.SAMPLE_SIZE)
self.samples_path = os.path.join(config.PATH, 'samples')
self.results_path = os.path.join(config.PATH, 'results')
if config.RESULTS is not None:
self.results_path = os.path.join(config.RESULTS)
if config.DEBUG is not None and config.DEBUG != 0:
self.debug = True
self.log_file = os.path.join(config.PATH, 'log_' + model_name + '.dat')
def load(self):
if self.config.MODEL == 1:
self.edge_model.load()
elif self.config.MODEL == 2:
self.inpaint_model.load()
pass
else:
self.edge_model.load()
self.inpaint_model.load()
def save(self):
if self.config.MODEL == 1:
self.edge_model.save()
elif self.config.MODEL == 2 or self.config.MODEL == 3:
self.inpaint_model.save()
else:
self.edge_model.save()
self.inpaint_model.save()
def partial_load(self):
if self.config.MODEL == 2 or self.config.MODEL == 3:
self.inpaint_model.partial_load()
def partial_save(self):
if self.config.MODEL == 2 or self.config.MODEL == 3:
self.inpaint_model.partial_save()
def train(self):
train_loader = DataLoader(
dataset=self.train_dataset,
batch_size=self.config.BATCH_SIZE,
num_workers=4,
drop_last=True,
shuffle=True
)
epoch = 0
keep_training = True
model = self.config.MODEL
max_iteration = int(float((self.config.MAX_ITERS)))
total = len(self.train_dataset)
if total == 0:
print('No training data was provided! Check \'TRAIN_FLIST\' value in the configuration file.')
return
while(keep_training):
epoch += 1
print('\n\nTraining epoch: %d' % epoch)
progbar = Progbar(total, width=20, stateful_metrics=['epoch', 'iter'])
for items in train_loader:
self.edge_model.train()
self.inpaint_model.train()
images, images_gray, edges, masks = self.cuda(*items)
# edge model
if model == 1:
# train
outputs, gen_loss, dis_loss, logs = self.edge_model.process(images_gray, edges, masks)
vs_images = torch.cat((edges[:,:,:,:self.config.GEN_PIXEL_SIZE],edges[:,:,:,128:]),dim=3)
outputs_merged = torch.cat((outputs[:,:,:,:self.config.GEN_PIXEL_SIZE],edges[:,:,:,self.config.PATCH_SIZE_W*self.config.GEN_PIXEL_SIZE:128],outputs[:,:,:,-128:]),dim=3)
# metrics
precision, recall = self.edgeacc(vs_images, outputs_merged)
# precision, recall = self.edgeacc(edges * masks, outputs * masks)
logs.append(('precision', precision.item()))
logs.append(('recall', recall.item()))
# backward
self.edge_model.backward(gen_loss, dis_loss)
iteration = self.edge_model.iteration
# inpaint model
elif model == 2:
# train
outputs, gen_loss, dis_loss, logs = self.inpaint_model.process(images, edges, masks)
width = images.shape[3]
# print(outputs.shape)
# outputs_merged = torch.cat((outputs[:,:,:,:16],images[:,:,:,16:]),dim=3)
# outputs_merged = (outputs * masks) + (images * (1 - masks))
vs_images = torch.cat((images[:,:,:,:self.config.GEN_PIXEL_SIZE],images[:,:,:, width//4: 3*width//4],images[:,:,:,-self.config.GEN_PIXEL_SIZE:]),dim=3)
outputs_merged = torch.cat((outputs[:,:,:,:self.config.GEN_PIXEL_SIZE],images[:,:,:, width//4 : 3 * width //4],outputs[:,:,:,-self.config.GEN_PIXEL_SIZE:]),dim=3)
# metrics
psnr = self.psnr(self.postprocess(vs_images), self.postprocess(outputs_merged))
mae = (torch.sum(torch.abs(vs_images - outputs_merged)) / torch.sum(vs_images)).float()
logs.append(('psnr', psnr.item()))
logs.append(('mae', mae.item()))
# metrics
# psnr = self.psnr(self.postprocess(images), self.postprocess(outputs_merged))
# mae = (torch.sum(torch.abs(images - outputs_merged)) / torch.sum(images)).float()
# logs.append(('psnr', psnr.item()))
# logs.append(('mae', mae.item()))
# backward
self.inpaint_model.backward(gen_loss,dis_loss)
iteration = self.inpaint_model.iteration
# inpaint with edge model
elif model == 3:
# train
if True or np.random.binomial(1, 0.5) > 0:
outputs = self.edge_model(images_gray, edges, masks)
outputs = outputs * masks + edges * (1 - masks)
else:
outputs = edges
outputs, gen_loss, dis_loss, logs = self.inpaint_model.process(images, outputs.detach(), masks)
outputs_merged = (outputs * masks) + (images * (1 - masks))
# metrics
psnr = self.psnr(self.postprocess(images), self.postprocess(outputs_merged))
mae = (torch.sum(torch.abs(images - outputs_merged)) / torch.sum(images)).float()
logs.append(('psnr', psnr.item()))
logs.append(('mae', mae.item()))
# backward
self.inpaint_model.backward(gen_loss, dis_loss)
iteration = self.inpaint_model.iteration
# joint model
else:
# train
e_outputs, e_gen_loss, e_dis_loss, e_logs = self.edge_model.process(images_gray, edges, masks)
e_outputs = e_outputs * masks + edges * (1 - masks)
i_outputs, i_gen_loss, i_dis_loss, i_logs = self.inpaint_model.process(images, e_outputs, masks)
outputs_merged = (i_outputs * masks) + (images * (1 - masks))
# metrics
psnr = self.psnr(self.postprocess(images), self.postprocess(outputs_merged))
mae = (torch.sum(torch.abs(images - outputs_merged)) / torch.sum(images)).float()
precision, recall = self.edgeacc(edges * masks, e_outputs * masks)
e_logs.append(('pre', precision.item()))
e_logs.append(('rec', recall.item()))
i_logs.append(('psnr', psnr.item()))
i_logs.append(('mae', mae.item()))
logs = e_logs + i_logs
# backward
self.inpaint_model.backward(i_gen_loss, i_dis_loss)
self.edge_model.backward(e_gen_loss, e_dis_loss)
iteration = self.inpaint_model.iteration
if iteration >= max_iteration:
keep_training = False
break
logs = [
("epoch", epoch),
("iter", iteration),
] + logs
progbar.add(len(images), values=logs if self.config.VERBOSE else [x for x in logs if not x[0].startswith('l_')])
# log model at checkpoints
if self.config.LOG_INTERVAL and iteration % self.config.LOG_INTERVAL == 0:
self.log(logs)
# sample model at checkpoints
if self.config.SAMPLE_INTERVAL and iteration % self.config.SAMPLE_INTERVAL == 0:
self.sample()
# evaluate model at checkpoints
if self.config.EVAL_INTERVAL and iteration % self.config.EVAL_INTERVAL == 0:
print('\nstart eval...\n')
self.eval()
# save model at checkpoints
if self.config.SAVE_INTERVAL and iteration % self.config.SAVE_INTERVAL == 0:
self.save()
if iteration %10000 == 0:
self.partial_save()
print('\nEnd training....')
def eval(self):
val_loader = DataLoader(
dataset=self.val_dataset,
# batch_size=self.config.BATCH_SIZE,
batch_size=8,
drop_last=True,
shuffle=False
)
model = self.config.MODEL
total = len(self.val_dataset)
self.edge_model.eval()
self.inpaint_model.eval()
progbar = Progbar(total, width=20, stateful_metrics=['it'])
iteration = 0
for items in val_loader:
iteration += 1
images, images_gray, edges, masks = self.cuda(*items)
# edge model
if model == 1:
# eval
outputs, gen_loss, dis_loss, logs = self.edge_model.process(images_gray, edges, masks)
# metrics
precision, recall = self.edgeacc(edges * masks, outputs * masks)
logs.append(('precision', precision.item()))
logs.append(('recall', recall.item()))
# inpaint model
elif model == 2:
# eval
outputs, gen_loss, dis_loss, logs = self.inpaint_model.process(images, edges, masks)
# outputs_merged = (outputs * masks) + (images * (1 - masks))
width = images.shape[3]
# print(outputs.shape)
# outputs_merged = torch.cat((outputs[:,:,:,:16],images[:,:,:,16:]),dim=3)
# outputs_merged = (outputs * masks) + (images * (1 - masks))
# vs_images = torch.cat((images[:,:,:,:self.config.GEN_PIXEL_SIZE],images[:,:,:, width//4: 3*width//4],images[:,:,:,-self.config.GEN_PIXEL_SIZE:]),dim=3)
vs_images = images
outputs_merged = torch.cat((outputs[:,:,:,:self.config.GEN_PIXEL_SIZE],images[:,:,:, self.config.GEN_PIXEL_SIZE:-self.config.GEN_PIXEL_SIZE],outputs[:,:,:,-self.config.GEN_PIXEL_SIZE:]),dim=3)
# metrics
psnr = self.psnr(self.postprocess(vs_images), self.postprocess(outputs_merged))
mae = (torch.sum(torch.abs(vs_images - outputs_merged)) / torch.sum(vs_images)).float()
logs.append(('psnr', psnr.item()))
logs.append(('mae', mae.item()))
# inpaint with edge model
elif model == 3:
# eval
outputs = self.edge_model(images_gray, edges, masks)
outputs = outputs * masks + edges * (1 - masks)
outputs, gen_loss, dis_loss, logs = self.inpaint_model.process(images, outputs.detach(), masks)
outputs_merged = (outputs * masks) + (images * (1 - masks))
# metrics
psnr = self.psnr(self.postprocess(images), self.postprocess(outputs_merged))
mae = (torch.sum(torch.abs(images - outputs_merged)) / torch.sum(images)).float()
logs.append(('psnr', psnr.item()))
logs.append(('mae', mae.item()))
# joint model
else:
# eval
e_outputs, e_gen_loss, e_dis_loss, e_logs = self.edge_model.process(images_gray, edges, masks)
e_outputs = e_outputs * masks + edges * (1 - masks)
i_outputs, i_gen_loss, i_dis_loss, i_logs = self.inpaint_model.process(images, e_outputs, masks)
outputs_merged = (i_outputs * masks) + (images * (1 - masks))
# metrics
psnr = self.psnr(self.postprocess(images), self.postprocess(outputs_merged))
mae = (torch.sum(torch.abs(images - outputs_merged)) / torch.sum(images)).float()
precision, recall = self.edgeacc(edges * masks, e_outputs * masks)
e_logs.append(('pre', precision.item()))
e_logs.append(('rec', recall.item()))
i_logs.append(('psnr', psnr.item()))
i_logs.append(('mae', mae.item()))
logs = e_logs + i_logs
logs = [("it", iteration), ] + logs
progbar.add(len(images), values=logs)
def test(self):
self.edge_model.eval()
self.inpaint_model.eval()
model = self.config.MODEL
create_dir(self.results_path)
test_loader = DataLoader(
dataset=self.test_dataset,
batch_size=1,
)
index = 0
for items in test_loader:
name = self.test_dataset.load_name(index)
images, images_gray, edges, masks = self.cuda(*items)
index += 1
# edge model
if model == 1:
outputs = self.edge_model(images_gray, edges, masks)
outputs_merged = (outputs * masks) + (edges * (1 - masks))
# inpaint model
elif model == 2:
outputs, gen_loss, dis_loss, logs = self.inpaint_model.process(images, edges, masks)
# vs_images = torch.cat((images[:,:,:,:self.config.GEN_PIXEL_SIZE],images[:,:,:,128:]),dim=3)
width = images.shape[3]
# print(outputs.shape)
# outputs_merged = torch.cat((outputs[:,:,:,:16],images[:,:,:,16:]),dim=3)
# outputs_merged = (outputs * masks) + (images * (1 - masks))
vs_images = torch.cat((images[:,:,:,:self.config.GEN_PIXEL_SIZE],images[:,:,:, width//4: 3*width//4],images[:,:,:,-width//4:-self.config.GEN_PIXEL_SIZE:1]),dim=3)
zeros = torch.zeros_like(images)
outputs_merged = torch.cat((outputs[:,:,:,:self.config.GEN_PIXEL_SIZE],images[:,:,:, self.config.GEN_PIXEL_SIZE : width - self.config.GEN_PIXEL_SIZE], outputs[:,:,:,-self.config.GEN_PIXEL_SIZE:]),dim=3)
# outputs_merged = outputs
if self.config.EXTERNAL:
outputs_merged = torch.cat((outputs[:,:,:,:self.config.GEN_PIXEL_SIZE],images[:,:,:,:], outputs[:,:,:,-self.config.GEN_PIXEL_SIZE:]),dim=3)
# outputs = self.inpaint_model(images, edges, masks)
# # outputs_merged = (outputs * masks) + (images * (1 - masks))
# vs_images = torch.cat((images[:,:,:,:self.config.PATCH_SIZE_W*self.config.GEN_PIXEL_SIZE],images[:,:,:,128:]),dim=3)
# outputs_merged = torch.cat((outputs[:,:,:,:self.config.PATCH_SIZE_W*self.config.GEN_PIXEL_SIZE],images[:,:,:,self.config.PATCH_SIZE_W*self.config.GEN_PIXEL_SIZE:128],outputs[:,:,:,-128:]),dim=3)
# inpaint with edge model / joint model
else:
edges = self.edge_model(images_gray, edges, masks).detach()
outputs = self.inpaint_model(images, edges, masks)
outputs_merged = (outputs * masks) + (images * (1 - masks))
outputs_merged = torch.clamp(outputs_merged, min=0, max=1) ## anonymous clipping
output = self.postprocess(outputs_merged)[0]
# output = self.postprocess(outputs_merged)[0]
path = os.path.join(self.results_path, name)
print(index, name)
imsave(output, path)
if self.debug:
edges = self.postprocess(1 - edges)[0]
masked = self.postprocess(images * (1 - masks) + masks)[0]
fname, fext = name.split('.')
imsave(edges, os.path.join(self.results_path, fname + '_edge.' + fext))
imsave(masked, os.path.join(self.results_path, fname + '_masked.' + fext))
print('\nEnd test....')
def sample(self, it=None):
# do not sample when validation set is empty
if len(self.val_dataset) == 0:
return
self.edge_model.eval()
self.inpaint_model.eval()
model = self.config.MODEL
items = next(self.sample_iterator)
images, images_gray, edges, masks = self.cuda(*items)
# edge model
if model == 1:
iteration = self.edge_model.iteration
inputs = (images_gray * (1 - masks)) + masks
outputs = self.edge_model(images_gray, edges, masks)
outputs_merged = (outputs * masks) + (edges * (1 - masks))
# inpaint model
elif model == 2:
iteration = self.inpaint_model.iteration
width = images.shape[3]
inputs = images
# inputs = (images * (1 - masks)) + masks
# print("input image shape {}".format(images.shape))
outputs = self.inpaint_model(inputs,edges,masks)
# print(outputs.shape)
# outputs_merged = (outputs * masks) + (images * (1 - masks))
outputs_merged = torch.cat((outputs[:,:,:,:self.config.GEN_PIXEL_SIZE],images[:,:,:,width//4:3*width//4],outputs[:,:,:,-self.config.GEN_PIXEL_SIZE:]),dim=3)
visual_image = torch.cat((outputs[:,:,:,:self.config.GEN_PIXEL_SIZE],images[:,:,:,self.config.GEN_PIXEL_SIZE:-self.config.GEN_PIXEL_SIZE],outputs[:,:,:,-self.config.GEN_PIXEL_SIZE:]),dim=3)
# visual_image = outputs
visual_image = torch.clamp(visual_image, min=0, max=1) ## anonymous clipping
black_line = torch.zeros_like(visual_image)[:,:,:,:4]
# inpaint with edge model / joint model
else:
iteration = self.inpaint_model.iteration
inputs = (images * (1 - masks)) + masks
outputs = self.edge_model(images_gray, edges, masks).detach()
edges = (outputs * masks + edges * (1 - masks)).detach()
outputs = self.inpaint_model(images, edges, masks)
outputs_merged = (outputs * masks) + (images * (1 - masks))
if it is not None:
iteration = it
image_per_row = 2
if self.config.SAMPLE_SIZE <= 6:
image_per_row = 1
# images = cv2.hconcat([
# # self.postprocess(images),
# self.postprocess(inputs),
# # self.postprocess(edges),
# self.postprocess(outputs)]
# # self.postprocess(edges),
# # self.postprocess(outputs_merged)]
# )
images = stitch_images(
self.postprocess(images),
# self.postprocess(edges),
# self.postprocess(inputs),
self.postprocess(black_line),
self.postprocess(outputs),
self.postprocess(black_line),
self.postprocess(visual_image),
img_per_row = image_per_row
)
# print("saved : {}".format(images.size))
path = os.path.join(self.samples_path, self.model_name)
name = os.path.join(path, str(iteration).zfill(5) + ".png")
create_dir(path)
print('\nsaving sample ' + name)
images.save(name)
def log(self, logs):
with open(self.log_file, 'a') as f:
f.write('%s\n' % ' '.join([str(item[1]) for item in logs]))
def cuda(self, *args):
return (item.to(self.config.DEVICE) for item in args)
def postprocess(self, img):
# [0, 1] => [0, 255]
img = img * 255.0
img = img.permute(0, 2, 3, 1)
return img.int()