-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils2_local_transform.py
More file actions
762 lines (666 loc) · 48 KB
/
Copy pathutils2_local_transform.py
File metadata and controls
762 lines (666 loc) · 48 KB
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
import os
import random
import numpy as np
from PIL import Image
import jsonlines
from glob import glob
import copy
import torch
# import pydiffvg
from hparam import HParams
from dataset_utils.common import load_txt_ids
def copy_hparams(hparams):
"""Return a copy of an HParams instance."""
return HParams(**hparams.values())
class LineDataLoader(object):
def __init__(self,
dataset_base,
batch_size,
window_size_scaling_comp,
window_size_min_comp,
transform_global_model_name,
use_optical_flow,
do_dataset_filtering,
is_train,
target_layer_method=None):
self.dataset_base = dataset_base
self.batch_size = batch_size
self.window_size_scaling_comp = window_size_scaling_comp
self.window_size_min_comp = window_size_min_comp
self.transform_global_model_name = transform_global_model_name
self.use_optical_flow = use_optical_flow
self.target_layer_method = target_layer_method
self.do_dataset_filtering = do_dataset_filtering
self.is_train = is_train
self.dataset_names = ['creature', 'bird']
self.ref_tar_split_names = ['ref', 'tar']
self.dataset_split = 'train' if is_train else 'val'
self.img_ids = self.get_img_ids()
self.example_num = len(self.img_ids)
print('Loaded', self.dataset_split, ':', self.example_num)
if self.do_dataset_filtering:
## Load invalid component ids
outsider_img_comp_ids_list_path = os.path.join(self.dataset_base, 'transform_invalid_comp_ids', 'out-of-bound',
self.dataset_split + '-win=' + str(self.window_size_scaling_comp) + '-min=' + str(self.window_size_min_comp) + '.txt')
outsider_img_comp_ids_list = load_txt_ids(outsider_img_comp_ids_list_path)
invalid_occ_img_comp_ids_list_path = os.path.join(self.dataset_base, 'transform_invalid_comp_ids', 'occlusion',
self.dataset_split + '_invalid.txt')
invalid_occ_img_comp_ids_list = load_txt_ids(invalid_occ_img_comp_ids_list_path)
single_stroke_comp_ids_list_path = os.path.join(self.dataset_base, 'transform_invalid_comp_ids', 'single-stroke-component',
self.dataset_split + '_invalid.txt')
single_stroke_comp_ids_list = load_txt_ids(single_stroke_comp_ids_list_path)
invalid_img_comp_ids_list = outsider_img_comp_ids_list + invalid_occ_img_comp_ids_list + single_stroke_comp_ids_list
self.invalid_img_comp_ids_list = list(set(invalid_img_comp_ids_list))
else:
self.invalid_img_comp_ids_list = []
self.valid_endpoint_index_buffer = []
def get_img_ids(self):
img_ids = []
for dataset_name in self.dataset_names:
vector_data_dir = os.path.join(self.dataset_base, dataset_name + '_512', self.dataset_split, 'vector-params')
all_files = os.listdir(vector_data_dir)
all_files = [item for item in all_files if '_ref.jsonl' in item]
for filename in all_files:
img_index = filename[:filename.find('_')]
img_ids.append(dataset_name + '-' + img_index)
img_ids.sort()
return img_ids
def get_valid_img_endpoints(self, dataset_name, image_index, reference_stroke_data, occluded_only=False):
valid_occlusion_txt_path = os.path.join(self.dataset_base, dataset_name + '_512', self.dataset_split, 'endpoint_type_ids',
'win=-1', str(image_index), 'valid_occlusion.txt')
valid_occlusion_endpoint_ids = load_txt_ids(valid_occlusion_txt_path)
img_id = dataset_name + '-' + str(image_index) + '-'
invalid_img_comp_ids = [item for item in self.invalid_img_comp_ids_list if img_id in item]
invalid_comp_indices = [int(item[item.find(img_id) + len(img_id):]) for item in invalid_img_comp_ids]
valid_img_endpoint_ids = []
for c_i in range(len(reference_stroke_data)):
if c_i in invalid_comp_indices:
continue
curve_b_list = reference_stroke_data[c_i] # list of (N', 4, 2)
for curve_i in range(len(curve_b_list)):
curve_b_points = curve_b_list[curve_i] # list (N') of (4, 2)
stroke_num = len(curve_b_points)
for point_index in range(stroke_num + 1):
endpoint_id = "%s_%s_%s" % (c_i, curve_i, point_index)
if occluded_only and endpoint_id not in valid_occlusion_endpoint_ids:
continue
valid_img_endpoint_ids.append(endpoint_id)
valid_img_endpoint_ids.sort()
return valid_img_endpoint_ids
def load_image(self, img_path):
image = Image.open(img_path).convert("RGB")
image = np.array(image, dtype=np.float32) # (H, W, 3), [0.0-strokes, 255.0-BG]
image = image[:, :, 0] / 255.0 # (H, W), [0.0-strokes, 1.0-BG]
return image
def load_stroke_parameter(self, vector_data_path):
stroke_data_b_list = []
# parts_data_list = []
with open(vector_data_path, "r+") as f:
for item in jsonlines.Reader(f):
stroke_data_b = item['stroke_params']
# parts_data = item['component_part']
stroke_data_b_list.append(stroke_data_b)
# parts_data_list.append(parts_data)
assert len(stroke_data_b_list) == 1
# assert len(parts_data_list) == 1
return stroke_data_b_list[0]
def load_transform_parameter(self, transform_params_path):
transform_params_data = {}
with open(transform_params_path, "r+") as f:
for item in jsonlines.Reader(f):
c_idx = item['component_index']
transform_params_data[c_idx] = {}
transform_params_data[c_idx]['component_center'] = item['component_center'] # (2), [0.0, 1.0], relative to image size
transform_params_data[c_idx]['component_win_size'] = item['component_win_size'] # (2), in image size
transform_params_data[c_idx]['pred_cursor'] = item['pred_cursor'] # (2), [0.0, 1.0], relative to image size
transform_params_data[c_idx]['pred_window_size'] = item['pred_window_size'] # (2), in image size
transform_params_data[c_idx]['pred_rotate_angle'] = item['pred_rotate_angle'] # (), [-180.0, 180.0]
transform_params_data[c_idx]['pred_shear_x_angle'] = item['pred_shear_x_angle'] # (), [-90.0, 90.0]
transform_params_data[c_idx]['pred_shear_y_angle'] = item['pred_shear_y_angle'] # (), [-90.0, 90.0]
return transform_params_data
def process_stroke_parameter(self, parameters_ref, comp_index, curve_index, stroke_index, image_size):
'''
parameters_ref / parameters_tar: component list => curve list => stroke list (N', 4, 2)
'''
curve_points_ref = parameters_ref[comp_index][curve_index] # (N', 4, 2)
if stroke_index == 0:
p_prev = curve_points_ref[stroke_index][0] # (2)
p_curr = curve_points_ref[stroke_index][0]
p_next = curve_points_ref[stroke_index][-1]
elif stroke_index >= len(curve_points_ref):
p_prev = curve_points_ref[stroke_index - 1][0] # (2)
p_curr = curve_points_ref[stroke_index - 1][-1]
p_next = curve_points_ref[stroke_index - 1][-1]
else:
p_prev = curve_points_ref[stroke_index - 1][0] # (2)
p_curr = curve_points_ref[stroke_index - 1][-1]
p_next = curve_points_ref[stroke_index][-1]
window_size_dist1 = np.abs(np.array(p_prev) - np.array(p_curr)) # (2), full size
window_size_dist2 = np.abs(np.array(p_curr) - np.array(p_next)) # (2), full size
window_size_dist = np.concatenate([window_size_dist1, window_size_dist2], axis=-1) # (4), full size
window_size = np.max(window_size_dist, axis=-1) * 2.0 # (), full size
window_size_norm = window_size / float(image_size) # (), [0.0, 1.0]
centerpoint_ref = np.array(p_curr, dtype=np.float32) # (2), full size
centerpoint_ref_norm = centerpoint_ref / float(image_size) # (2), [0.0, 1.0]
return centerpoint_ref_norm, window_size_norm
def get_batch(self, use_cuda, batch_idx=None, all_example=False, batch_idx_offset=0, occluded_only=False):
reference_image_batch = []
reference_component_batch = []
reference_stroke_batch = []
target_image_batch = []
target_component_batch = []
target_layer_mask_batch = []
reference_centerpoints_batch = []
reference_centerpoints_offset_batch = []
base_window_size_batch = []
image_id_batch = []
endpoint_id_batch = []
component_centerpoints_batch = []
component_win_size_batch = []
target_transform_cursor_batch = []
target_transform_win_size_batch = []
target_transform_angle_batch = []
target_transform_shear_x_batch = []
target_transform_shear_y_batch = []
if self.is_train:
selected_indices = np.random.choice(np.arange(self.example_num), size=self.batch_size, replace=False)
else:
selected_indices = [self.batch_size * batch_idx + i + batch_idx_offset for i in range(self.batch_size)]
for batch_i in range(len(selected_indices)):
selected_id = self.img_ids[selected_indices[batch_i]] # 'bird-1' or 'creature-230'
selected_dataset_name = selected_id[:selected_id.find('-')]
selected_index = selected_id[selected_id.find('-') + 1:]
image_id_batch.append(selected_id)
reference_image_path = os.path.join(self.dataset_base, selected_dataset_name + '_512', self.dataset_split, 'raster_black', 'sketch_' + str(selected_index) + '_bezier-' + self.ref_tar_split_names[0] + '.png')
reference_stroke_path = os.path.join(self.dataset_base, selected_dataset_name + '_512', self.dataset_split, 'vector-params', str(selected_index) + '_' + self.ref_tar_split_names[0] + '.jsonl')
target_image_path = os.path.join(self.dataset_base, selected_dataset_name + '_512', self.dataset_split, 'raster_black', 'sketch_' + str(selected_index) + '_bezier-' + self.ref_tar_split_names[1] + '.png')
reference_image = self.load_image(reference_image_path) # (H, W), [0.0-strokes, 1.0-BG]
target_image = self.load_image(target_image_path) # (H, W), [0.0-strokes, 1.0-BG]
reference_stroke_data = self.load_stroke_parameter(reference_stroke_path)
# reference_stroke_data: component list => curve list => stroke list (N', 4, 2)
image_size = reference_image.shape[0]
valid_endpoint_ids = self.get_valid_img_endpoints(selected_dataset_name, selected_index, reference_stroke_data,
occluded_only=occluded_only)
if not occluded_only:
assert len(valid_endpoint_ids) > 0
else:
if len(valid_endpoint_ids) == 0:
return None
transform_global_model_name_plus = self.transform_global_model_name + '-[c_min=' + str(self.window_size_min_comp) + ']'
if self.use_optical_flow:
transform_global_model_name_plus += '-[optical]'
transform_params_path = os.path.join(self.dataset_base, selected_dataset_name + '_512', self.dataset_split,
'component_transform_params', transform_global_model_name_plus, selected_index + '.jsonl')
transform_params_data = self.load_transform_parameter(transform_params_path)
if self.is_train:
random.shuffle(valid_endpoint_ids)
random_endpoint_ids = [valid_endpoint_ids[0]]
else:
if not all_example:
if len(self.valid_endpoint_index_buffer) <= batch_idx:
random.shuffle(valid_endpoint_ids)
random_endpoint_ids = [valid_endpoint_ids[0]]
self.valid_endpoint_index_buffer.append(random_endpoint_ids[0])
else:
random_endpoint_ids = [self.valid_endpoint_index_buffer[batch_idx]]
else:
random_endpoint_ids = [item for item in valid_endpoint_ids]
for random_endpoint_id in random_endpoint_ids:
comp_curve_point = random_endpoint_id.split('_')
c_i, curve_i, point_index = comp_curve_point
endpoint_id_batch.append(random_endpoint_id)
reference_component_path = os.path.join(self.dataset_base, selected_dataset_name + '_512', self.dataset_split, 'raster_black_component',
str(selected_index), 'component_' + str(c_i) + '-' + self.ref_tar_split_names[0] + '.png')
target_component_path = os.path.join(self.dataset_base, selected_dataset_name + '_512', self.dataset_split, 'raster_black_component',
str(selected_index), 'component_' + str(c_i) + '-' + self.ref_tar_split_names[1] + '.png')
reference_component_image = self.load_image(reference_component_path) # (H, W), [0.0-strokes, 1.0-BG]
target_component_image = self.load_image(target_component_path) # (H, W), [0.0-strokes, 1.0-BG]
## Target layer masks
if self.target_layer_method is not None:
if self.target_layer_method == 'box_depth_ol':
target_layer_dir = '[box]-[depth_overlap]'
elif self.target_layer_method == 'box_depth':
target_layer_dir = '[box]-[depth]'
elif self.target_layer_method == 'mask_line':
target_layer_dir = '[mask]-[linearts]'
elif self.target_layer_method == 'box_depth+mask_line' or self.target_layer_method == 'box_depth_ol+mask_line':
target_layer_dir = '[both]'
else:
raise Exception('Unknown target_layer_method:', self.target_layer_method)
target_layer_mask_path = os.path.join(self.dataset_base, selected_dataset_name + '_512', self.dataset_split, 'layers', target_layer_dir, 'mask_proc',
str(selected_index), str(c_i) + '_tar.png')
target_layer_mask = 1.0 - self.load_image(target_layer_mask_path) # (H, W), [0.0-FG, 1.0-BG]
target_layer_mask_batch.append(target_layer_mask)
else:
target_layer_mask_batch.append(np.ones_like(target_image))
reference_stroke_path = os.path.join(self.dataset_base, selected_dataset_name + '_512', self.dataset_split, 'raster_black_endpoint_stroke',
str(selected_index), 'endpoint_' + random_endpoint_id + '.png')
reference_stroke_image = self.load_image(reference_stroke_path) # (H, W), [0.0-strokes, 1.0-BG]
centerpoint, window_size = self.process_stroke_parameter(reference_stroke_data, int(c_i), int(curve_i), int(point_index), image_size)
# centerpoints: (2), [0.0, 1.0]
# endpoint_offset_gt: (2), [-1.0, 1.0]
# window_sizes: (), [0.0, 1.0]
centerpoint_offset = np.maximum(np.minimum(centerpoint, 1.0), 0.0)
reference_image_batch.append(reference_image)
reference_component_batch.append(reference_component_image)
reference_stroke_batch.append(reference_stroke_image)
target_image_batch.append(target_image)
target_component_batch.append(target_component_image)
reference_centerpoints_batch.append(centerpoint)
reference_centerpoints_offset_batch.append(centerpoint_offset)
base_window_size_batch.append(window_size)
component_centerpoints_batch.append(transform_params_data[int(c_i)]['component_center'])
component_win_size_batch.append(transform_params_data[int(c_i)]['component_win_size'])
target_transform_cursor_batch.append(transform_params_data[int(c_i)]['pred_cursor'])
target_transform_win_size_batch.append(transform_params_data[int(c_i)]['pred_window_size'])
target_transform_angle_batch.append(transform_params_data[int(c_i)]['pred_rotate_angle'])
target_transform_shear_x_batch.append(transform_params_data[int(c_i)]['pred_shear_x_angle'])
target_transform_shear_y_batch.append(transform_params_data[int(c_i)]['pred_shear_y_angle'])
reference_image_batch = np.expand_dims(np.stack(reference_image_batch, axis=0), axis=-1) # (N, H, W, 1), [0.0-strokes, 1.0-BG]
reference_component_batch = np.expand_dims(np.stack(reference_component_batch, axis=0), axis=-1) # (N, H, W, 1), [0.0-strokes, 1.0-BG]
reference_stroke_batch = np.expand_dims(np.stack(reference_stroke_batch, axis=0), axis=-1) # (N, H, W, 1), [0.0-strokes, 1.0-BG]
target_image_batch = np.expand_dims(np.stack(target_image_batch, axis=0), axis=-1) # (N, H, W, 1), [0.0-strokes, 1.0-BG]
target_component_batch = np.expand_dims(np.stack(target_component_batch, axis=0), axis=-1) # (N, H, W, 1), [0.0-strokes, 1.0-BG]
target_layer_mask_batch = np.expand_dims(np.stack(target_layer_mask_batch, axis=0), axis=-1) # (N, H, W, 1), [0.0-FG, 1.0-BG]
reference_centerpoints_batch = np.expand_dims(np.stack(reference_centerpoints_batch, axis=0), axis=1) # (N, 1, 2), [0.0, 1.0]
reference_centerpoints_offset_batch = np.expand_dims(np.stack(reference_centerpoints_offset_batch, axis=0), axis=1) # (N, 1, 2), [0.0, 1.0]
base_window_size_batch = np.expand_dims(np.stack(base_window_size_batch, axis=0), axis=-1) # (N, 1), [0.0, 1.0]
component_centerpoints_batch = np.expand_dims(np.stack(component_centerpoints_batch, axis=0), axis=1) # (N, 1, 2), [0.0, 1.0], relative to image size
component_win_size_batch = np.expand_dims(np.stack(component_win_size_batch, axis=0), axis=1) # (N, 1, 2), in image size
target_transform_cursor_batch = np.expand_dims(np.stack(target_transform_cursor_batch, axis=0), axis=1) # (N, 1, 2), [0.0, 1.0], relative to image size
target_transform_win_size_batch = np.expand_dims(np.stack(target_transform_win_size_batch, axis=0), axis=1) # (N, 1, 2), in image size
target_transform_angle_batch = np.expand_dims(np.stack(target_transform_angle_batch, axis=0), axis=1) # (N, 1), [-180.0, 180.0]
target_transform_shear_x_batch = np.expand_dims(np.stack(target_transform_shear_x_batch, axis=0), axis=1) # (N, 1), [-90.0, 90.0]
target_transform_shear_y_batch = np.expand_dims(np.stack(target_transform_shear_y_batch, axis=0), axis=1) # (N, 1), [-90.0, 90.0]
## convert to tensor
reference_image_batch = torch.tensor(reference_image_batch).float()
reference_component_batch = torch.tensor(reference_component_batch).float()
reference_stroke_batch = torch.tensor(reference_stroke_batch).float()
target_image_batch = torch.tensor(target_image_batch).float()
target_component_batch = torch.tensor(target_component_batch).float()
target_layer_mask_batch = torch.tensor(target_layer_mask_batch).float()
reference_centerpoints_batch = torch.tensor(reference_centerpoints_batch).float()
reference_centerpoints_offset_batch = torch.tensor(reference_centerpoints_offset_batch).float()
base_window_size_batch = torch.tensor(base_window_size_batch).float()
component_centerpoints_batch = torch.tensor(component_centerpoints_batch).float()
component_win_size_batch = torch.tensor(component_win_size_batch).float()
target_transform_cursor_batch = torch.tensor(target_transform_cursor_batch).float()
target_transform_win_size_batch = torch.tensor(target_transform_win_size_batch).float()
target_transform_angle_batch = torch.tensor(target_transform_angle_batch).float()
target_transform_shear_x_batch = torch.tensor(target_transform_shear_x_batch).float()
target_transform_shear_y_batch = torch.tensor(target_transform_shear_y_batch).float()
if use_cuda:
reference_image_batch = reference_image_batch.cuda()
reference_component_batch = reference_component_batch.cuda()
reference_stroke_batch = reference_stroke_batch.cuda()
target_image_batch = target_image_batch.cuda()
target_component_batch = target_component_batch.cuda()
target_layer_mask_batch = target_layer_mask_batch.cuda()
reference_centerpoints_batch = reference_centerpoints_batch.cuda()
reference_centerpoints_offset_batch = reference_centerpoints_offset_batch.cuda()
base_window_size_batch = base_window_size_batch.cuda()
component_centerpoints_batch = component_centerpoints_batch.cuda()
component_win_size_batch = component_win_size_batch.cuda()
target_transform_cursor_batch = target_transform_cursor_batch.cuda()
target_transform_win_size_batch = target_transform_win_size_batch.cuda()
target_transform_angle_batch = target_transform_angle_batch.cuda()
target_transform_shear_x_batch = target_transform_shear_x_batch.cuda()
target_transform_shear_y_batch = target_transform_shear_y_batch.cuda()
return reference_image_batch, reference_component_batch, reference_stroke_batch, target_image_batch, target_component_batch, target_layer_mask_batch, \
reference_centerpoints_batch, reference_centerpoints_offset_batch, \
base_window_size_batch, image_id_batch, endpoint_id_batch, \
component_centerpoints_batch, component_win_size_batch, \
target_transform_cursor_batch, target_transform_win_size_batch, target_transform_angle_batch, \
target_transform_shear_x_batch, target_transform_shear_y_batch
class RealLineDataLoader(object):
def __init__(self,
dataset_base,
dataset_base_extra, # None for forward prediction; otherwise, for inverse prediction
batch_size,
window_size_scaling_comp,
window_size_min_comp,
transform_global_model_name,
use_optical_flow,
use_target_layer,
use_target_layer_mask,
target_layer_method,
generation_time):
self.dataset_base = dataset_base
self.dataset_base_extra = dataset_base_extra
self.batch_size = batch_size
self.window_size_scaling_comp = window_size_scaling_comp
self.window_size_min_comp = window_size_min_comp
self.transform_global_model_name = transform_global_model_name
self.use_optical_flow = use_optical_flow
self.use_target_layer = use_target_layer
self.use_target_layer_mask = use_target_layer_mask
self.target_layer_method = target_layer_method
self.generation_time = generation_time
self.ref_tar_split_names = ['ref', 'tar']
def get_valid_img_endpoints(self, reference_stroke_data):
valid_img_endpoint_ids = []
for c_i in range(len(reference_stroke_data)):
curve_b_list = reference_stroke_data[c_i] # list of (N', 4, 2)
for curve_i in range(len(curve_b_list)):
curve_b_points = curve_b_list[curve_i] # list (N') of (4, 2)
stroke_num = len(curve_b_points)
for point_index in range(stroke_num + 1):
endpoint_id = "%s_%s_%s" % (c_i, curve_i, point_index)
valid_img_endpoint_ids.append(endpoint_id)
valid_img_endpoint_ids.sort()
return valid_img_endpoint_ids
def load_image(self, img_path):
image = Image.open(img_path).convert("RGB")
image = np.array(image, dtype=np.float32) # (H, W, 3), [0.0-strokes, 255.0-BG]
image = image[:, :, 0] / 255.0 # (H, W), [0.0-strokes, 1.0-BG]
return image
def load_stroke_parameter(self, vector_data_path):
curves_endpoint_connected_state = None
with open(vector_data_path, "r+") as f:
for item in jsonlines.Reader(f):
stroke_data_b = item['stroke_params']
if 'connect_state' in item.keys():
curves_endpoint_connected_state = item['connect_state'] # component list => curve list => ['0_1_2', None]
return stroke_data_b, curves_endpoint_connected_state
def load_transform_parameter(self, transform_params_path):
transform_params_data = {}
with open(transform_params_path, "r+") as f:
for item in jsonlines.Reader(f):
c_idx = item['component_index']
transform_params_data[c_idx] = {}
transform_params_data[c_idx]['component_center'] = item['component_center'] # (2), [0.0, 1.0], relative to image size
transform_params_data[c_idx]['component_win_size'] = item['component_win_size'] # (2), in image size
transform_params_data[c_idx]['pred_cursor'] = item['pred_cursor'] # (2), [0.0, 1.0], relative to image size
transform_params_data[c_idx]['pred_window_size'] = item['pred_window_size'] # (2), in image size
transform_params_data[c_idx]['pred_rotate_angle'] = item['pred_rotate_angle'] # (), [-180.0, 180.0]
transform_params_data[c_idx]['pred_shear_x_angle'] = item['pred_shear_x_angle'] # (), [-90.0, 90.0]
transform_params_data[c_idx]['pred_shear_y_angle'] = item['pred_shear_y_angle'] # (), [-90.0, 90.0]
return transform_params_data
def process_stroke_parameter(self, parameters_ref, comp_index, curve_index, stroke_index, image_size,
curves_endpoint_connected_state=None, parameters_ref_extra=None):
'''
parameters_ref / parameters_tar: component list => curve list => stroke list (N', 4, 2)
curves_endpoint_connected_state & parameters_ref_extra: for finding the connected stroke during inverse prediction or forward gen-1
'''
curve_points_ref = parameters_ref[comp_index][curve_index] # (N', 4, 2)
if parameters_ref_extra is None:
assert curves_endpoint_connected_state is None
if stroke_index == 0:
p_prev = curve_points_ref[stroke_index][0] # (2)
p_curr = curve_points_ref[stroke_index][0]
p_next = curve_points_ref[stroke_index][-1]
elif stroke_index >= len(curve_points_ref):
p_prev = curve_points_ref[stroke_index - 1][0] # (2)
p_curr = curve_points_ref[stroke_index - 1][-1]
p_next = curve_points_ref[stroke_index - 1][-1]
else:
p_prev = curve_points_ref[stroke_index - 1][0] # (2)
p_curr = curve_points_ref[stroke_index - 1][-1]
p_next = curve_points_ref[stroke_index][-1]
else:
assert curves_endpoint_connected_state is not None
endpoint_connected_states = curves_endpoint_connected_state[comp_index][curve_index] # ['0_1_2', None]
if stroke_index == 0: # starting point
if endpoint_connected_states[0] is None:
p_prev = curve_points_ref[stroke_index][0] # (2)
else:
corr_comp, corr_curve, corr_point = endpoint_connected_states[0].split('_')
assert int(corr_comp) == comp_index
if int(corr_point) == 0: # connected to the starting point of a curve
corr_stroke = parameters_ref_extra[int(corr_comp)][int(corr_curve)][int(corr_point)] # (4, 2)
p_prev = corr_stroke[-1]
else: # connected to the ending point of a curve
corr_stroke = parameters_ref_extra[int(corr_comp)][int(corr_curve)][int(corr_point) - 1] # (4, 2)
p_prev = corr_stroke[0]
p_curr = curve_points_ref[stroke_index][0]
p_next = curve_points_ref[stroke_index][-1]
elif stroke_index >= len(curve_points_ref): # ending point
p_prev = curve_points_ref[stroke_index - 1][0] # (2)
p_curr = curve_points_ref[stroke_index - 1][-1]
if endpoint_connected_states[1] is None:
p_next = curve_points_ref[stroke_index - 1][-1]
else:
corr_comp, corr_curve, corr_point = endpoint_connected_states[1].split('_')
assert int(corr_comp) == comp_index
if int(corr_point) == 0: # connected to the starting point of a curve
corr_stroke = parameters_ref_extra[int(corr_comp)][int(corr_curve)][int(corr_point)] # (4, 2)
p_next = corr_stroke[-1]
else: # connected to the ending point of a curve
corr_stroke = parameters_ref_extra[int(corr_comp)][int(corr_curve)][int(corr_point) - 1] # (4, 2)
p_next = corr_stroke[0]
else:
p_prev = curve_points_ref[stroke_index - 1][0] # (2)
p_curr = curve_points_ref[stroke_index - 1][-1]
p_next = curve_points_ref[stroke_index][-1]
window_size_dist1 = np.abs(np.array(p_prev) - np.array(p_curr)) # (2), full size
window_size_dist2 = np.abs(np.array(p_curr) - np.array(p_next)) # (2), full size
window_size_dist = np.concatenate([window_size_dist1, window_size_dist2], axis=-1) # (4), full size
window_size = np.max(window_size_dist, axis=-1) * 2.0 # (), full size
window_size_norm = window_size / float(image_size) # (), [0.0, 1.0]
centerpoint_ref = np.array(p_curr, dtype=np.float32) # (2), full size
centerpoint_ref_norm = centerpoint_ref / float(image_size) # (2), [0.0, 1.0]
return centerpoint_ref_norm, window_size_norm
def get_batch(self, use_cuda, test_img_id):
reference_image_batch = []
reference_component_batch = []
reference_stroke_batch = []
reference_stroke_prev_batch = []
reference_stroke_next_batch = []
target_image_batch = []
target_layer_mask_batch = []
reference_centerpoints_batch = []
reference_centerpoints_offset_batch = []
base_window_size_batch = []
image_id_batch = []
endpoint_id_batch = []
component_centerpoints_batch = []
component_win_size_batch = []
target_transform_cursor_batch = []
target_transform_win_size_batch = []
target_transform_angle_batch = []
target_transform_shear_x_batch = []
target_transform_shear_y_batch = []
selected_indices = [test_img_id]
for batch_i in range(len(selected_indices)):
selected_index = str(selected_indices[batch_i])
image_id_batch.append(selected_index)
reference_image_path = os.path.join(self.dataset_base, 'raster_black', str(selected_index) + '_' + self.ref_tar_split_names[0] + '.png')
reference_stroke_path = os.path.join(self.dataset_base, 'vector-params', str(selected_index) + '_' + self.ref_tar_split_names[0] + '.jsonl')
target_image_path = os.path.join(self.dataset_base, 'raster_black', str(selected_index) + '_' + self.ref_tar_split_names[1] + '.png')
reference_image = self.load_image(reference_image_path) # (H, W), [0.0-strokes, 1.0-BG]
target_image = self.load_image(target_image_path) # (H, W), [0.0-strokes, 1.0-BG]
reference_stroke_data, curves_endpoint_connected_state = self.load_stroke_parameter(reference_stroke_path)
# reference_stroke_data: component list => curve list => stroke list (N', 4, 2)
# curves_endpoint_connected_state: for inverse prediction
if self.dataset_base_extra is not None:
if self.generation_time > 0:
assert '-Gen%d' % self.generation_time in self.dataset_base_extra
if self.use_target_layer_mask == "none":
assert '[layer_mask_stroke]' not in self.dataset_base_extra
# Loading pseudo ref data during inverse prediction
reference_stroke_path_extra = os.path.join(self.dataset_base_extra, 'params', 'tar_pred-' + str(selected_index) + '.jsonl')
reference_stroke_data_extra, _ = self.load_stroke_parameter(reference_stroke_path_extra)
else: # Forward prediction
reference_stroke_data_extra = None if self.generation_time == 0 else copy.deepcopy(reference_stroke_data)
image_size = reference_image.shape[0]
valid_endpoint_ids = self.get_valid_img_endpoints(reference_stroke_data)
assert len(valid_endpoint_ids) > 0
transform_global_model_name_plus = self.transform_global_model_name + '-[c_min=' + str(self.window_size_min_comp) + ']'
if self.use_optical_flow:
transform_global_model_name_plus += '-[optical]'
transform_params_path = os.path.join(self.dataset_base, 'component_transform_params',
transform_global_model_name_plus, selected_index + '.jsonl')
transform_params_data = self.load_transform_parameter(transform_params_path)
random_endpoint_ids = [item for item in valid_endpoint_ids]
for random_endpoint_id in random_endpoint_ids:
comp_curve_point = random_endpoint_id.split('_')
c_i, curve_i, point_index = comp_curve_point
endpoint_id_batch.append(random_endpoint_id)
reference_component_path = os.path.join(self.dataset_base, 'raster_black_component',
str(selected_index), 'component_' + str(c_i) + '-' + self.ref_tar_split_names[0] + '.png')
reference_component_image = self.load_image(reference_component_path) # (H, W), [0.0-strokes, 1.0-BG]
reference_stroke_path = os.path.join(self.dataset_base, 'raster_black_endpoint_stroke',
str(selected_index), 'endpoint_' + random_endpoint_id + '.png')
reference_stroke_image = self.load_image(reference_stroke_path) # (H, W), [0.0-strokes, 1.0-BG]
if self.dataset_base_extra is None: # Forward prediction
reference_stroke_path_prev = os.path.join(self.dataset_base, 'raster_black_endpoint_stroke_prev',
str(selected_index), 'endpoint_' + random_endpoint_id + '.png')
reference_stroke_image_prev = self.load_image(reference_stroke_path_prev) # (H, W), [0.0-strokes, 1.0-BG]
reference_stroke_path_next = os.path.join(self.dataset_base, 'raster_black_endpoint_stroke_next',
str(selected_index), 'endpoint_' + random_endpoint_id + '.png')
reference_stroke_image_next = self.load_image(reference_stroke_path_next) # (H, W), [0.0-strokes, 1.0-BG]
centerpoint, window_size = self.process_stroke_parameter(reference_stroke_data, int(c_i), int(curve_i), int(point_index), image_size,
curves_endpoint_connected_state, reference_stroke_data_extra)
# centerpoints: (2), [0.0, 1.0]
# endpoint_offset_gt: (2), [-1.0, 1.0]
# window_sizes: (), [0.0, 1.0]
centerpoint_offset = np.maximum(np.minimum(centerpoint, 1.0), 0.0)
if self.target_layer_method == 'box_depth_ol':
target_layer_dir = '[box]-[depth_overlap]'
elif self.target_layer_method == 'box_depth':
target_layer_dir = '[box]-[depth]'
elif self.target_layer_method == 'mask_line':
target_layer_dir = '[mask]-[linearts]'
elif self.target_layer_method == 'box_depth+mask_line' or self.target_layer_method == 'box_depth_ol+mask_line':
target_layer_dir = '[both]'
else:
raise Exception('Unknown target_layer_method:', self.target_layer_method)
if self.use_target_layer:
reference_image_batch.append(reference_image)
target_layer_image_path = os.path.join(self.dataset_base, 'layers', target_layer_dir, 'image',
str(selected_index), str(c_i) + '_tar.png')
target_layer_image = self.load_image(target_layer_image_path) # (H, W), [0.0-strokes, 1.0-BG]
target_image_batch.append(target_layer_image)
else:
reference_image_batch.append(reference_image)
target_image_batch.append(target_image)
if self.use_target_layer_mask == 'stroke':
if self.dataset_base_extra is None: # Forward prediction
target_layer_mask_path = os.path.join(self.dataset_base, 'layers', target_layer_dir, 'mask_proc',
str(selected_index), str(c_i) + '_tar.png')
target_layer_mask = 1.0 - self.load_image(target_layer_mask_path) # (H, W), [0.0-FG, 1.0-BG]
target_layer_mask_batch.append(target_layer_mask)
else: # Inverse prediction
target_layer_mask_batch.append(np.ones_like(target_image))
else:
target_layer_mask_batch.append(np.ones_like(target_image))
reference_component_batch.append(reference_component_image)
reference_stroke_batch.append(reference_stroke_image)
if self.dataset_base_extra is None: # Forward prediction
reference_stroke_prev_batch.append(reference_stroke_image_prev)
reference_stroke_next_batch.append(reference_stroke_image_next)
else: # Inverse prediction
reference_stroke_prev_batch.append(reference_stroke_image)
reference_stroke_next_batch.append(reference_stroke_image)
reference_centerpoints_batch.append(centerpoint)
reference_centerpoints_offset_batch.append(centerpoint_offset)
base_window_size_batch.append(window_size)
component_centerpoints_batch.append(transform_params_data[int(c_i)]['component_center'])
component_win_size_batch.append(transform_params_data[int(c_i)]['component_win_size'])
target_transform_cursor_batch.append(transform_params_data[int(c_i)]['pred_cursor'])
target_transform_win_size_batch.append(transform_params_data[int(c_i)]['pred_window_size'])
target_transform_angle_batch.append(transform_params_data[int(c_i)]['pred_rotate_angle'])
target_transform_shear_x_batch.append(transform_params_data[int(c_i)]['pred_shear_x_angle'])
target_transform_shear_y_batch.append(transform_params_data[int(c_i)]['pred_shear_y_angle'])
reference_image_batch = np.expand_dims(np.stack(reference_image_batch, axis=0), axis=-1) # (N, H, W, 1), [0.0-strokes, 1.0-BG]
reference_component_batch = np.expand_dims(np.stack(reference_component_batch, axis=0), axis=-1) # (N, H, W, 1), [0.0-strokes, 1.0-BG]
reference_stroke_batch = np.expand_dims(np.stack(reference_stroke_batch, axis=0), axis=-1) # (N, H, W, 1), [0.0-strokes, 1.0-BG]
reference_stroke_prev_batch = np.expand_dims(np.stack(reference_stroke_prev_batch, axis=0), axis=-1) # (N, H, W, 1), [0.0-strokes, 1.0-BG]
reference_stroke_next_batch = np.expand_dims(np.stack(reference_stroke_next_batch, axis=0), axis=-1) # (N, H, W, 1), [0.0-strokes, 1.0-BG]
target_image_batch = np.expand_dims(np.stack(target_image_batch, axis=0), axis=-1) # (N, H, W, 1), [0.0-strokes, 1.0-BG]
target_layer_mask_batch = np.expand_dims(np.stack(target_layer_mask_batch, axis=0), axis=-1) # (N, H, W, 1), [0.0-FG, 1.0-BG]
reference_centerpoints_batch = np.expand_dims(np.stack(reference_centerpoints_batch, axis=0), axis=1) # (N, 1, 2), [0.0, 1.0]
reference_centerpoints_offset_batch = np.expand_dims(np.stack(reference_centerpoints_offset_batch, axis=0), axis=1) # (N, 1, 2), [0.0, 1.0]
base_window_size_batch = np.expand_dims(np.stack(base_window_size_batch, axis=0), axis=-1) # (N, 1), [0.0, 1.0]
component_centerpoints_batch = np.expand_dims(np.stack(component_centerpoints_batch, axis=0), axis=1) # (N, 1, 2), [0.0, 1.0], relative to image size
component_win_size_batch = np.expand_dims(np.stack(component_win_size_batch, axis=0), axis=1) # (N, 1, 2), in image size
target_transform_cursor_batch = np.expand_dims(np.stack(target_transform_cursor_batch, axis=0), axis=1) # (N, 1, 2), [0.0, 1.0], relative to image size
target_transform_win_size_batch = np.expand_dims(np.stack(target_transform_win_size_batch, axis=0), axis=1) # (N, 1, 2), in image size
target_transform_angle_batch = np.expand_dims(np.stack(target_transform_angle_batch, axis=0), axis=1) # (N, 1), [-180.0, 180.0]
target_transform_shear_x_batch = np.expand_dims(np.stack(target_transform_shear_x_batch, axis=0), axis=1) # (N, 1), [-90.0, 90.0]
target_transform_shear_y_batch = np.expand_dims(np.stack(target_transform_shear_y_batch, axis=0), axis=1) # (N, 1), [-90.0, 90.0]
## convert to tensor
reference_image_batch = torch.tensor(reference_image_batch).float()
reference_component_batch = torch.tensor(reference_component_batch).float()
reference_stroke_batch = torch.tensor(reference_stroke_batch).float()
reference_stroke_prev_batch = torch.tensor(reference_stroke_prev_batch).float()
reference_stroke_next_batch = torch.tensor(reference_stroke_next_batch).float()
target_image_batch = torch.tensor(target_image_batch).float()
target_layer_mask_batch = torch.tensor(target_layer_mask_batch).float()
reference_centerpoints_batch = torch.tensor(reference_centerpoints_batch).float()
reference_centerpoints_offset_batch = torch.tensor(reference_centerpoints_offset_batch).float()
base_window_size_batch = torch.tensor(base_window_size_batch).float()
component_centerpoints_batch = torch.tensor(component_centerpoints_batch).float()
component_win_size_batch = torch.tensor(component_win_size_batch).float()
target_transform_cursor_batch = torch.tensor(target_transform_cursor_batch).float()
target_transform_win_size_batch = torch.tensor(target_transform_win_size_batch).float()
target_transform_angle_batch = torch.tensor(target_transform_angle_batch).float()
target_transform_shear_x_batch = torch.tensor(target_transform_shear_x_batch).float()
target_transform_shear_y_batch = torch.tensor(target_transform_shear_y_batch).float()
if use_cuda:
reference_image_batch = reference_image_batch.cuda()
reference_component_batch = reference_component_batch.cuda()
reference_stroke_batch = reference_stroke_batch.cuda()
reference_stroke_prev_batch = reference_stroke_prev_batch.cuda()
reference_stroke_next_batch = reference_stroke_next_batch.cuda()
target_image_batch = target_image_batch.cuda()
target_layer_mask_batch = target_layer_mask_batch.cuda()
reference_centerpoints_batch = reference_centerpoints_batch.cuda()
reference_centerpoints_offset_batch = reference_centerpoints_offset_batch.cuda()
base_window_size_batch = base_window_size_batch.cuda()
component_centerpoints_batch = component_centerpoints_batch.cuda()
component_win_size_batch = component_win_size_batch.cuda()
target_transform_cursor_batch = target_transform_cursor_batch.cuda()
target_transform_win_size_batch = target_transform_win_size_batch.cuda()
target_transform_angle_batch = target_transform_angle_batch.cuda()
target_transform_shear_x_batch = target_transform_shear_x_batch.cuda()
target_transform_shear_y_batch = target_transform_shear_y_batch.cuda()
return reference_image_batch, reference_component_batch, \
reference_stroke_batch, reference_stroke_prev_batch, reference_stroke_next_batch, \
target_image_batch, target_layer_mask_batch, \
reference_centerpoints_batch, reference_centerpoints_offset_batch, \
base_window_size_batch, image_id_batch, endpoint_id_batch, \
component_centerpoints_batch, component_win_size_batch, \
target_transform_cursor_batch, target_transform_win_size_batch, target_transform_angle_batch, \
target_transform_shear_x_batch, target_transform_shear_y_batch
def load_dataset(model_params, test_only=False):
data_base = model_params.dataset_base
valid_model_params = copy_hparams(model_params)
valid_model_params.batch_size = 1 # only sample one at a time
if not test_only:
train_set = LineDataLoader(dataset_base=data_base, batch_size=model_params.batch_size,
window_size_scaling_comp=model_params.window_size_scaling_ref_comp,
window_size_min_comp=model_params.window_size_min_comp,
transform_global_model_name=model_params.transform_global_model_name,
use_optical_flow=model_params.use_optical_flow,
do_dataset_filtering=model_params.do_dataset_filtering,
is_train=True)
valid_model_params.target_layer_method = None
else:
train_set = None
val_set = LineDataLoader(dataset_base=data_base, batch_size=valid_model_params.batch_size,
window_size_scaling_comp=valid_model_params.window_size_scaling_ref_comp,
window_size_min_comp=valid_model_params.window_size_min_comp,
transform_global_model_name=valid_model_params.transform_global_model_name,
use_optical_flow=valid_model_params.use_optical_flow,
do_dataset_filtering=valid_model_params.do_dataset_filtering,
is_train=False,
target_layer_method=valid_model_params.target_layer_method)
result = [train_set, val_set, model_params, valid_model_params]
return result
def load_real_dataset(model_params, data_base, data_base_extra=None, generation_time=0):
valid_model_params = copy_hparams(model_params)
valid_model_params.batch_size = 1 # only sample one at a time
val_set = RealLineDataLoader(dataset_base=data_base,
dataset_base_extra=data_base_extra,
batch_size=valid_model_params.batch_size,
window_size_scaling_comp=valid_model_params.window_size_scaling_ref_comp,
window_size_min_comp=valid_model_params.window_size_min_comp,
transform_global_model_name=valid_model_params.transform_global_model_name,
use_optical_flow=valid_model_params.use_optical_flow,
use_target_layer=valid_model_params.use_target_layer,
use_target_layer_mask=valid_model_params.use_target_layer_mask,
target_layer_method=valid_model_params.target_layer_method,
generation_time=generation_time)
result = [val_set, model_params, valid_model_params]
return result