-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdataloader_mesh.py
More file actions
2160 lines (1723 loc) · 89.5 KB
/
dataloader_mesh.py
File metadata and controls
2160 lines (1723 loc) · 89.5 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
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import glob
import numpy as np
import torch
import torch.utils.data as data
import random
import pickle
import trimesh
from functools import partial
from utils import (
ICT_face_model,
procrustes_LDM,
plot_image_array,
calc_norm_torch
)
from utils.keys import get_data_splits, get_identity_num, ICT_KEYS, DATA_KEYS, KEYS
from utils.remesh_utils import map_vertices, decimate_mesh_vertex
from utils.mesh_utils import get_dfn_info2, get_mesh_operators
from tqdm import tqdm
import time
def random_ict_exp_coeff(N=1000, min_ones=1, max_ones=5):
identity = np.eye(53)
num_ones_per_row = np.random.randint(min_ones, max_ones + 1, size=N)
exp_coeffs = np.array([np.sum(identity[np.random.choice(53, num_ones, replace=False)], axis=0)
for num_ones in num_ones_per_row], dtype=int)
return exp_coeffs
class MeshDataset(data.Dataset):
def __init__(self,
opts,
data_basedir='/data/sihun/',
is_train=False,
is_valid=False,
window_size=8, # batch size
device='cpu',
print_config=False,
ict_face_only=False
):
super().__init__()
# get basenames
self.opts = opts
self.is_train = is_train
self.is_valid = is_valid
self.device = device
self.data_basedir = data_basedir
self.ict_face_only=ict_face_only
self.data_config(self.opts, window_size)
self.mode = 'test'
if is_train:
self.mode = 'train'
elif is_valid:
self.mode = 'val'
## precomputes ------------------------------------------------------------------
p_mode = 'face_only' if self.ict_face_only else 'fullhead'
self.ict_data_split, \
self.voca_data_split, \
self.biwi_data_split, \
self.mf_data_split, \
self.ict_data_synth = get_data_splits()
self.identity_num = get_identity_num()
## loading data -----------------------------------------------------------------
# self.set_multiface_SEN()
# self.set_multiface_ROM()
# self.set_biwi()
self.set_ict_base()
## ------------------------------------------------------------------------------
self.len_ict_synth = 0
self.len_ict_real = 0
self.len_mfROM = 0
self.len_mfSEN = 0
self.total_len = 0
self.total_len = 0
self.id_sent_len = 0
self.len_list = []
if self.use_ict_real:
total_len, id_sent_len = self.set_ict_real()
self.total_len += total_len
self.id_sent_len += id_sent_len
self.len_list.append([total_len, self.get_ICTcapture, torch.tensor(0), id_sent_len])
if self.use_ict_synth:
total_len, id_sent_len = self.set_ict_synth()
self.total_len += total_len
self.id_sent_len += id_sent_len
self.len_list.append([self.total_len, self.get_ICTsynthetic, torch.tensor(0), id_sent_len])
if self.use_ict_synth_single:
total_len, id_sent_len = self.set_ict_synth_single()
self.total_len += total_len
self.id_sent_len += id_sent_len
self.len_list.append([self.total_len, self.get_ICTsynthetic, torch.tensor(0), id_sent_len])
if self.use_voca:
total_len, id_sent_len = self.set_voca()
self.total_len += total_len
self.id_sent_len += id_sent_len
self.len_list.append([self.total_len, self.get_voca, torch.tensor(1), id_sent_len])
if self.use_coma:
total_len, id_sent_len = self.set_coma()
self.total_len += total_len
self.id_sent_len += id_sent_len
self.len_list.append([self.total_len, self.get_coma, torch.tensor(1), id_sent_len])
if self.use_biwi:
total_len, id_sent_len = self.set_biwi()
self.total_len += total_len
self.id_sent_len += id_sent_len
self.len_list.append([self.total_len, self.get_biwi, torch.tensor(2), id_sent_len])
if self.use_mf_SEN:
total_len, id_sent_len = self.set_multiface_SEN()
self.total_len += total_len
self.id_sent_len += id_sent_len
self.len_list.append([total_len, self.get_multiface_SEN, torch.tensor(3), id_sent_len])
if self.use_mf_ROM:
total_len, id_sent_len = self.set_multiface_ROM()
self.total_len += total_len
self.id_sent_len += id_sent_len
self.len_list.append([total_len, self.get_multiface_ROM, torch.tensor(3), id_sent_len])
#self.total_len = self.len_ict_real+self.len_ict_synth+self.len_mfSEN+self.len_mfROM
if print_config:
print(self.get_data_config())
def get_data_config(self):
text = "===========[Dataset]===========\n"
text+= f"[mode]: {self.mode}\n"
# text+= f"[ICT-synth data]: {self.len_ict_synth}\n"
# text+= f"[ICT-capture data]: {self.len_ict_real}\n"
# text+= f"[multiface data]: {self.len_mfSEN+self.len_mfROM}\n"
text+= f"------------------------------\n"
text+= f"[total frame]: {self.total_len}\n"
text+= f"[data chunks (ID x Motion) - matching batch_size]: {self.id_sent_len}\n"
text+= "===============================\n"
return text
def __len__(self):
#return self.total_len
return self.id_sent_len
def data_config(self, opts=None, window_size=8):
flag = True if opts is not None else False
self.use_ict_synth_single = opts.use_ict_synth_single if flag else False
self.use_ict_real = opts.use_ict_real if flag else False
self.use_ict_synth = opts.use_ict_synth if flag else False
self.use_mf_SEN = opts.use_mf_SEN if flag else False
self.use_mf_ROM = opts.use_mf_ROM if flag else False
self.use_voca = opts.use_voca if flag else False
self.use_coma = opts.use_coma if flag else False
self.use_biwi = opts.use_biwi if flag else False
if flag:
self.use_decimate = False
self.WS = window_size
else:
self.use_decimate = self.opts.use_decimate
self.WS = self.opts.window_size
def set_multiface_SEN(self):
self.mf_dir = os.path.join(self.data_basedir, 'multiface_align')
self.mf_precompute_path=f'{self.mf_dir}/precomputes'
self.mf_obj_path=f'{self.mf_dir}/obj'
self.mf_std = np.load('utils/mf/standardization.npy', allow_pickle=True).item()
self.mf_verts = np.load(f"{self.mf_dir}/id_verts_{self.mode}.npy")
self.mf_SEN_dir = f'{self.mf_dir}/SEN/{self.mode}'
self.mf_SEN_v_npy_list = {}
self.mf_SEN_n_npy_list = {}
self.mf_SEN_wav_list = {} # TODO
self.mf_SEN_count = [0] # start
self.mf_SEN_remain = [0] # start
self.mf_id = []
total_len=0
id_sent_len = 0
for id_ in tqdm(self.mf_data_split[self.mode], desc='multiface SEN'):
self.mf_SEN_v_npy_list[id_]={}
self.mf_SEN_n_npy_list[id_]={}
curr_v_dir = f"{self.mf_SEN_dir}/vertices_npy/{id_}"
v_listdirs = sorted([f for f in os.listdir(curr_v_dir) if f!='.ipynb_checkpoints'])
curr_n_dir = f"{self.mf_SEN_dir}/normals_npy/{id_}"
n_listdirs = sorted([f for f in os.listdir(curr_n_dir) if f!='.ipynb_checkpoints'])
assert len(v_listdirs) == len(n_listdirs), 'sentence lenth do not match'
for sen_ in v_listdirs:
self.mf_SEN_v_npy_list[id_][sen_] = sorted(glob.glob(f"{curr_v_dir}/{sen_}/*.npy"))
self.mf_SEN_n_npy_list[id_][sen_] = sorted(glob.glob(f"{curr_n_dir}/{sen_}/*.npy"))
assert len(self.mf_SEN_v_npy_list[id_][sen_]) == len(self.mf_SEN_n_npy_list[id_][sen_]), 'mismatch'
total_len += len(self.mf_SEN_v_npy_list[id_][sen_])
remain = len(v_listdirs) % self.opts.batch_size
if remain > 0:
dummy = self.opts.batch_size - remain
else:
dummy = 0
n_id_sent = len(v_listdirs) + dummy
id_sent_len += n_id_sent # SEN len
self.mf_SEN_count.append(n_id_sent)
self.mf_SEN_remain.append(dummy)
self.mf_SEN_count=np.array(self.mf_SEN_count)
self.mf_SEN_count=np.cumsum(self.mf_SEN_count)
self.mf_SEN_remain=np.array(self.mf_SEN_remain)
return total_len, id_sent_len
def set_multiface_ROM(self):
self.mf_dir = os.path.join(self.data_basedir, 'multiface_align')
self.mf_precompute_path=f'{self.mf_dir}/precomputes'
self.mf_obj_path=f'{self.mf_dir}/obj'
self.mf_std = np.load('utils/mf/standardization.npy', allow_pickle=True).item()
self.mf_verts = np.load(f"{self.mf_dir}/id_verts_{self.mode}.npy")
self.mf_ROM_dir = f'{self.mf_dir}/ROM/{self.mode}'
self.mf_ROM_v_npy_list = {}
self.mf_ROM_n_npy_list = {}
self.mf_ROM_count = [0] # start
self.mf_ROM_remain = [0] # start
self.mf_id = []
total_len=0
id_sent_len = 0
for idx, id_ in tqdm(enumerate(self.mf_data_split[self.mode]), desc='multiface ROM'):
self.mf_ROM_v_npy_list[id_]={}
self.mf_ROM_n_npy_list[id_]={}
curr_v_dir = f"{self.mf_ROM_dir}/vertices_npy/{id_}"
v_listdirs = sorted([f for f in os.listdir(curr_v_dir) if f!='.ipynb_checkpoints'])
curr_n_dir = f"{self.mf_ROM_dir}/normals_npy/{id_}"
n_listdirs = sorted([f for f in os.listdir(curr_n_dir) if f!='.ipynb_checkpoints'])
assert len(v_listdirs) == len(n_listdirs), 'sentence lenth do not match'
self.mf_id.append(idx)
for sen_ in v_listdirs:
v_npy_list = sorted(glob.glob(f"{self.mf_ROM_dir}/vertices_npy/{id_}/{sen_}/*.npy"))
n_npy_list = sorted(glob.glob(f"{self.mf_ROM_dir}/normals_npy/{id_}/{sen_}/*.npy" ))
assert len(v_npy_list) == len(n_npy_list)
self.mf_ROM_v_npy_list[id_][sen_] = v_npy_list
self.mf_ROM_n_npy_list[id_][sen_] = n_npy_list
total_len += len(self.mf_ROM_v_npy_list[id_][sen_]) # SEN frame len
remain = len(v_listdirs) % self.opts.batch_size
if remain > 0:
dummy = self.opts.batch_size - remain
else:
dummy = 0
n_id_sent = len(v_listdirs) + dummy
id_sent_len += n_id_sent # SEN len
self.mf_ROM_count.append(n_id_sent)
self.mf_ROM_remain.append(dummy)
self.mf_ROM_count=np.array(self.mf_ROM_count)
self.mf_ROM_count=np.cumsum(self.mf_ROM_count)
self.mf_ROM_remain=np.array(self.mf_ROM_remain)
return total_len, id_sent_len
def set_biwi(self):
biwi_base_dir = os.path.join(self.data_basedir, 'BIWI_align_deci')
self.biwi_precompute_path = f'{biwi_base_dir}/precomputes'
self.biwi_templates = pickle.load(open(f'{biwi_base_dir}/templates_align_deci.pkl', 'rb'), encoding='latin1')
self.biwi_std = np.load('./utils/biwi/standardization.npy', allow_pickle=True).item() # not used
self.biwi_dir = f'{biwi_base_dir}/{self.mode}'
self.biwi_v_npy_list = {}
self.biwi_n_npy_list = {}
self.biwi_wav_list = {} # TODO
#self.biwi_len_list = {}
self.biwi_count = [0] # start
self.biwi_remain = [0] # start
self.biwi_id = []
total_len = 0
id_sent_len = 0
for id_ in tqdm(self.biwi_data_split[self.mode], desc='BIWI'):
self.biwi_v_npy_list[id_]={}
self.biwi_n_npy_list[id_]={}
v_listdirs = [f.split('/')[-1] for f in glob.glob(f"{self.biwi_dir}/vertices_npy/{id_}*")]
n_listdirs = [f.split('/')[-1] for f in glob.glob(f"{self.biwi_dir}/normals_npy/{id_}*")]
assert len(v_listdirs) == len(n_listdirs), 'sentence lenth do not match'
self.biwi_id.append(id_)
for id_sent in v_listdirs:
v_npy_list = sorted(glob.glob(f"{self.biwi_dir}/vertices_npy/{id_sent}/*.npy"))
n_npy_list = sorted(glob.glob(f"{self.biwi_dir}/normals_npy/{id_sent}/*.npy" ))
assert len(v_npy_list) == len(n_npy_list)
id_, sent = id_sent.split('_')
self.biwi_v_npy_list[id_][sent] = v_npy_list
self.biwi_n_npy_list[id_][sent] = n_npy_list
total_len += len(self.biwi_v_npy_list[id_][sent]) # SEN frame len
remain = len(v_listdirs) % self.opts.batch_size
if remain > 0:
dummy = self.opts.batch_size - remain
else:
dummy = 0
n_id_sent = len(v_listdirs) + dummy
id_sent_len += n_id_sent # SEN len
self.biwi_count.append(n_id_sent)
self.biwi_remain.append(dummy)
self.biwi_count=np.array(self.biwi_count)
self.biwi_count=np.cumsum(self.biwi_count)
self.biwi_remain=np.array(self.biwi_remain)
return total_len, id_sent_len
def set_voca(self):
self.all_dir = os.path.join(self.data_basedir, 'VOCA-COMA')
self.voca_dir = f'{self.all_dir}/VOCASET/{self.mode}'
self.voca_coma_precompute_path=f'{self.all_dir}/precomputes'
self.voca_coma_templates = pickle.load(open(f'{self.all_dir}/voca_templates.pkl', 'rb'), encoding='latin1')
#dfn_info = pickle.load(open(self.mf_dir, 'rb'))
self.voca_coma_std = np.load('./utils/voca/standardization.npy', allow_pickle=True).item()
self.voca_v_npy_list = {}
self.voca_n_npy_list = {}
self.voca_wav_list = {} # TODO
#self.voca_len_list = {}
self.voca_count = [0] # start
self.voca_remain = [0] # start
total_len = 0
id_sent_len = 0
for id_ in tqdm(self.voca_data_split[self.mode], desc='VOCA'):
self.voca_v_npy_list[id_]={}
self.voca_n_npy_list[id_]={}
curr_v_dir = f"{self.voca_dir}/{id_}/vertices_npy"
v_listdirs = sorted([f for f in os.listdir(curr_v_dir) if f!='.ipynb_checkpoints' and not '.npy' in f])
curr_n_dir = f"{self.voca_dir}/{id_}/normals_npy"
n_listdirs = sorted([f for f in os.listdir(curr_n_dir) if f!='.ipynb_checkpoints' and not '.npy' in f])
assert v_listdirs == n_listdirs, 'sentence lenth do not match'
for sen_ in v_listdirs:
self.voca_v_npy_list[id_][sen_]=sorted(glob.glob(f"{curr_v_dir}/{sen_}/*30fps*.npy"))
self.voca_n_npy_list[id_][sen_]=sorted(glob.glob(f"{curr_n_dir}/{sen_}/*30fps*.npy"))
assert len(self.voca_v_npy_list[id_][sen_]) == len(self.voca_n_npy_list[id_][sen_])
total_len+=len(self.voca_v_npy_list[id_][sen_])
remain = len(v_listdirs) % self.opts.batch_size
if remain > 0:
dummy = self.opts.batch_size - remain
else:
dummy = 0
n_id_sent = len(v_listdirs) + dummy
id_sent_len += n_id_sent # SEN len
self.voca_count.append(n_id_sent)
self.voca_remain.append(dummy)
self.voca_count=np.array(self.voca_count)
self.voca_count=np.cumsum(self.voca_count)
self.voca_remain=np.array(self.voca_remain)
return total_len, id_sent_len
def set_coma(self):
self.all_dir = os.path.join(self.data_basedir, 'VOCA-COMA')
self.coma_dir = f'{self.all_dir}/COMA/{self.mode}'
self.voca_coma_precompute_path=f'{self.all_dir}/precomputes'
self.voca_coma_templates = pickle.load(open(f'{self.all_dir}/voca_templates.pkl', 'rb'), encoding='latin1')
#dfn_info = pickle.load(open(self.mf_dir, 'rb'))
self.voca_coma_std = np.load('./utils/voca/standardization.npy', allow_pickle=True).item()
self.coma_dir = f'{self.all_dir}/COMA/{self.mode}'
self.coma_v_npy_list = {}
self.coma_n_npy_list = {}
# self.coma_len_list = {}
self.coma_count = [0] # start
self.coma_remain = [0] # start
total_len=0
id_sent_len=0
# coma has same identities with voca
#import pdb;pdb.set_trace()
for id_ in tqdm(self.voca_data_split[self.mode], desc='COMA'):
self.coma_v_npy_list[id_]={}
self.coma_n_npy_list[id_]={}
curr_v_dir = f"{self.coma_dir}/{id_}/vertices_npy"
v_listdirs = sorted([f for f in os.listdir(curr_v_dir) if f!='.ipynb_checkpoints' and not '.npy' in f])
curr_n_dir = f"{self.coma_dir}/{id_}/normals_npy"
n_listdirs = sorted([f for f in os.listdir(curr_n_dir) if f!='.ipynb_checkpoints' and not '.npy' in f])
assert v_listdirs == n_listdirs, 'sentence lenth do not match'
for sen_ in v_listdirs:
self.coma_v_npy_list[id_][sen_]=sorted(glob.glob(f"{curr_v_dir}/{sen_}/*.npy"))
self.coma_n_npy_list[id_][sen_]=sorted(glob.glob(f"{curr_n_dir}/{sen_}/*.npy"))
assert len(self.coma_v_npy_list[id_][sen_]) == len(self.coma_n_npy_list[id_][sen_])
total_len+=len(self.coma_v_npy_list[id_][sen_])
remain = len(v_listdirs) % self.opts.batch_size
if remain > 0:
dummy = self.opts.batch_size - remain
else:
dummy = 0
n_id_sent = len(v_listdirs) + dummy
id_sent_len += n_id_sent # SEN len
self.coma_count.append(n_id_sent)
self.coma_remain.append(dummy)
self.coma_count=np.array(self.coma_count)
self.coma_count=np.cumsum(self.coma_count)
self.coma_remain=np.array(self.coma_remain)
return total_len, id_sent_len
def set_ict_base(self):
self.ict_basedir = os.path.join(self.data_basedir, 'ICT-audio2face')
self.ict_templates_path = './ICT/templates'
with open("/data/sihun/ICT-audio2face/split_set/ict_real_templates.pkl", 'rb') as f:
self.ict_real_templates_dict = pickle.load(f)
with open("/data/sihun/ICT-audio2face/synth_set/ict_synth_templates.pkl", 'rb') as f:
self.ict_synth_templates_dict = pickle.load(f)
self.ict_face_model = ICT_face_model()
if self.opts.seg_dim == 20:
self.ict_vert_segment = torch.from_numpy(np.load('./utils/ict/ICT_segment_onehot.npy'))
elif self.opts.seg_dim == 24:
self.ict_vert_segment = torch.from_numpy(np.load('./utils/ict/ICT_segment_onehot_24.npy'))
elif self.opts.seg_dim == 14:
self.ict_vert_segment = torch.from_numpy(np.load('./utils/ict/ICT_segment_onehot_14.npy'))
elif self.opts.seg_dim == 6:
self.ict_vert_segment = torch.from_numpy(np.load('./utils/ict/ICT_segment_onehot_06.npy'))
else:
raise NotImplementedError(f"no segment map for seg_dim: {self.opts.seg_dim}")
def set_ict_real(self):
#self.iden_vecs = np.load('./ict_face_pt/random_identity_vecs.npy')
self.ict_real_id_vecs = np.load(f'{self.ict_basedir}/split_set/id_vecs.npy')
self.ict_real_precompute = f'{self.ict_basedir}/precompute-real-fullhead'
self.ict_real_precompute_fo = f'{self.ict_basedir}/precompute-real-face_only'
self.ict_dir = os.path.join(self.ict_basedir, 'split_set', self.mode)
self.ict_real_count = [0] # start
self.ict_real_remain = [0] # start
self.ict_real_v_npy_list = {}
self.ict_real_n_npy_list = {}
self.ict_real_exp_coeff_list = {}
self.ict_real_wav_list = {} # TODO
#self.ict_real_len_list = {}
total_len=0
id_sent_len=0
for id_ in tqdm(self.ict_data_split[self.mode], desc='ict real'):
self.ict_real_v_npy_list[id_]={}
self.ict_real_n_npy_list[id_]={}
curr_v_dir = f"{self.ict_dir}/{id_}/vertices_npy"
v_listdirs = sorted([f for f in os.listdir(curr_v_dir) if f!='.ipynb_checkpoints' and not '.npy' in f])
curr_n_dir = f"{self.ict_dir}/{id_}/normals_npy"
n_listdirs = sorted([f for f in os.listdir(curr_n_dir) if f!='.ipynb_checkpoints' and not '.npy' in f])
assert v_listdirs == n_listdirs, 'sentence lenth do not match'
curr_r_dir = f"{self.ict_dir}/{id_}/rig_param"
self.ict_real_exp_coeff_list[id_]=sorted(glob.glob(f"{curr_r_dir}/*.npy"))
for sen_ in v_listdirs:
v_npy_list = sorted(glob.glob(f"{curr_v_dir}/{sen_}/*.npy"))
n_npy_list = sorted(glob.glob(f"{curr_n_dir}/{sen_}/*.npy"))
assert len(v_npy_list) == len(n_npy_list), f"{id_}-{sen_}"
self.ict_real_v_npy_list[id_][sen_]=v_npy_list
self.ict_real_n_npy_list[id_][sen_]=n_npy_list
# self.ict_real_wav_list[id_]=f"{self.ict_dir}/{id_}/wav/{sen_}.wav" ## TODO
total_len += len(self.ict_real_v_npy_list[id_][sen_])
#id_sent_len += len(self.ict_real_v_npy_list[id_])
remain = len(v_listdirs) % self.opts.batch_size
if remain > 0:
dummy = self.opts.batch_size - remain
else:
dummy = 0
n_id_sent = len(v_listdirs) + dummy
id_sent_len += n_id_sent # SEN len
self.ict_real_count.append(n_id_sent)
self.ict_real_remain.append(dummy)
assert len(self.ict_real_exp_coeff_list[id_]) == len(self.ict_real_v_npy_list[id_]), \
f"exp_coeff and v mismatch in {id_}"
#id_key = self.ict_data_split[self.mode][id_idx]
self.ict_sent_key_list = list(self.ict_real_v_npy_list['m00'].keys())
self.ict_sent_len = len(self.ict_real_v_npy_list['m00'].keys())
self.ict_real_count=np.array(self.ict_real_count)
self.ict_real_count=np.cumsum(self.ict_real_count)
self.ict_real_remain=np.array(self.ict_real_remain)
return total_len, id_sent_len
def set_ict_synth(self):
self.iden_vecs = np.load('./ict_face_pt/random_identity_vecs.npy')
self.expression_vecs = np.load('./ict_face_pt/random_expression_vecs.npy')
if self.mode != 'train':
self.iden_vecs = np.load('./data/ICT_live_100/iden_vecs.npy')
self.expression_vecs = np.load(f'./data/ICT_live_100/expression_vecs_{self.mode}.npy')
self.ict_synth_precompute = f'{self.ict_basedir}/precompute-synth-fullhead'
self.ict_synth_precompute_fo = f'{self.ict_basedir}/precompute-synth-face_only'
self.ict_synth_precompute_nf = f'{self.ict_basedir}/precompute-synth-narrow_face'
self.ict_dir = os.path.join(self.ict_basedir, 'synth_set', self.mode)
self.ict_synth_v_npy_list = {}
self.ict_synth_n_npy_list = {}
#self.ict_synth_len_list = {}
self.ict_synth_count = [0]
self.ict_synth_remain = [0]
total_len=0
#id_sent_len=self.iden_vecs.shape[0] #*self.expression_vecs.shape[0]
id_sent_len=0
for id_ in tqdm(self.ict_data_synth[self.mode], desc='ict synth'):
curr_v_dir = f"{self.ict_dir}/{id_}/vertices_npy"
v_listdirs = sorted(glob.glob(f"{curr_v_dir}/*.npy"))
curr_n_dir = f"{self.ict_dir}/{id_}/normals_npy"
n_listdirs = sorted(glob.glob(f"{curr_n_dir}/*.npy"))
assert len(v_listdirs) == len(n_listdirs), f"{id_}-{sen_}"
self.ict_synth_v_npy_list[id_]=v_listdirs
self.ict_synth_n_npy_list[id_]=n_listdirs
total_len += len(self.ict_synth_v_npy_list[id_])
# remain = len(v_listdirs) % self.opts.batch_size
remain = 1
if remain > 0:
dummy = self.opts.batch_size - remain
else:
dummy = 0
n_id_sent = self.opts.batch_size # len(v_listdirs) + dummy
id_sent_len += n_id_sent # SEN len
self.ict_synth_count.append(n_id_sent)
self.ict_synth_remain.append(dummy)
self.ict_synth_count=np.array(self.ict_synth_count)
self.ict_synth_count=np.cumsum(self.ict_synth_count)
self.ict_synth_remain=np.array(self.ict_synth_remain)
return total_len, id_sent_len
def set_ict_synth_single(self):
self.iden_vecs = np.load('./ict_face_pt/random_identity_vecs.npy')
self.expression_vecs = np.load('./ict_face_pt/random_expression_vecs.npy')
self.ict_synth_precompute = f'{self.ict_basedir}/precompute-synth-fullhead'
self.ict_synth_precompute_fo = f'{self.ict_basedir}/precompute-synth-face_only'
self.ict_dir = os.path.join(self.ict_basedir, 'synth_set', 'train')
self.ict_synth_v_npy_list = {}
self.ict_synth_n_npy_list = {}
#self.ict_synth_len_list = {}
total_len=0
id_ = '100'
id_sent_len=self.expression_vecs.shape[0]
curr_v_dir=f"{self.ict_dir}/{id_}/vertices_npy"
curr_n_dir=f"{self.ict_dir}/{id_}/normals_npy"
self.ict_synth_v_npy_list[id_]=sorted(glob.glob(f"{curr_v_dir}/*.npy"))
self.ict_synth_n_npy_list[id_]=sorted(glob.glob(f"{curr_n_dir}/*.npy"))
assert len(self.ict_synth_v_npy_list[id_]) == len(self.ict_synth_n_npy_list[id_]), f"{id_}"
total_len += len(self.ict_synth_v_npy_list[id_])
#id_sent_len+=1
return total_len, id_sent_len
def get_ICTcapture(self, index, select):
# sent_idx = index % self.ict_sent_len
# id_idx = index // self.ict_sent_len
id_idx = np.where(self.ict_real_count > index)[0][0] - 1
sent_idx = index - self.ict_real_count[id_idx]
o_index = self.ict_real_count[id_idx+1] - self.ict_real_count[id_idx] - self.ict_real_remain[id_idx+1]
if sent_idx >= o_index:
sent_idx = random.randint(0, o_index -1)
id_key = self.ict_data_split[self.mode][id_idx]
sent_key = self.ict_sent_key_list[sent_idx]
frame_idx = random.randint(0, len(self.ict_real_v_npy_list[id_key][sent_key]) -1)
# start_time = time.time()
id_coeff = torch.from_numpy(self.ict_real_id_vecs[id_idx]) # [100]
id_coeff = torch.cat([id_coeff, torch.zeros(28)]).float() # [128]
exp_coeff = np.load(self.ict_real_exp_coeff_list[id_key][sent_idx])[frame_idx]
exp_coeff = torch.from_numpy(exp_coeff) # [53]
exp_coeff = torch.cat([exp_coeff, torch.zeros(75)]).float() # [128]
# end_time = time.time()
# print('00time elapsed:', end_time - start_time)
# face only or full
# region = random.randint(0, 1)
# v_num, quad_f_num = self.ict_face_model.region[region]
# f_num = quad_f_num*2
# if region == 0: # full
# precompute_dir = self.ict_real_precompute
# else: # face only
# precompute_dir = self.ict_real_precompute_fo
v_num, quad_f_num = self.ict_face_model.region[0]
f_num = quad_f_num*2
precompute_dir = self.ict_real_precompute
# start_time = time.time()
## most bottleneck
v_normal = np.load(self.ict_real_n_npy_list[id_key][sent_key][frame_idx])[:v_num]
vertices = np.load(self.ict_real_v_npy_list[id_key][sent_key][frame_idx])[:v_num]
template = self.ict_real_templates_dict[id_key][:v_num]
faces = self.ict_real_templates_dict['face'][:f_num]
# end_time = time.time()
# print('01time elapsed:', end_time - start_time)
v_normal = torch.from_numpy(v_normal).float()
vertices = torch.from_numpy(vertices).float()
template = torch.from_numpy(template).float()
faces = torch.from_numpy(faces).long()
## Random Augmentation ---------------------------------------------------
# template, vertices = self.random_trans_scale(template, vertices)
## -----------------------------------------------------------------------
# start_time = time.time()
dfn_info = os.path.join(precompute_dir, f"{id_key}_dfn_info.pkl")
operators = os.path.join(precompute_dir, f"{id_key}_operators.pkl")
# end_time = time.time()
# print('02time elapsed:', end_time - start_time)
#start_time = time.time()
img = np.load(os.path.join(precompute_dir, f"{id_key}_img.npy"))
img = torch.from_numpy(img)[0]
# end_time = time.time()
#print('03time elapsed:', end_time - start_time)
# get audio feature + slice w/ window
dummy = torch.zeros(1)
# v_normal = calc_norm_torch(vertices, faces, at='v').float()
return dummy, id_coeff, exp_coeff, template, dfn_info, operators, vertices, v_normal, faces, img
def get_ICTsynthetic(self, index, select):
#e_index = index % self.expression_vecs.shape[0]
#id_idx = index // self.expression_vecs.shape[0]
# e_index = random.randint(0, self.expression_vecs.shape[0] -1)
# id_idx = index
id_idx = np.where(self.ict_synth_count > index)[0][0] - 1
e_index = random.randint(0, self.expression_vecs.shape[0] -1)
# e_index = index - self.ict_synth_count[id_idx]
# o_index = self.ict_synth_count[id_idx+1] - self.ict_synth_count[id_idx] - self.ict_synth_remain[id_idx+1]
# if e_index >= o_index:
# e_index = random.randint(0, o_index -1)
if self.use_ict_synth_single:
id_idx = 100 * self.opts.batch_size
# get id_coeff
id_coeff = torch.from_numpy(self.iden_vecs[id_idx]) #-------------------- [100]
id_coeff = torch.cat([id_coeff, torch.zeros(28)]).float() # ------------- [128]
exp_coeff = torch.from_numpy(self.expression_vecs[e_index]) # ------------ [53]
exp_coeff = torch.cat([exp_coeff, torch.zeros(75)], dim=-1).float() # ---- [128]
id_key = f'{id_idx:03d}'
#select = random.randint(0, 2)
v_num, faces = self.ict_face_model.get_random_v_and_f(select=select)
if select == 0:
precompute_dir = self.ict_synth_precompute
elif select == 1:
precompute_dir = self.ict_synth_precompute_fo
else:
precompute_dir = self.ict_synth_precompute_nf
# v_num, quad_f_num = self.ict_face_model.region[0]
# f_num = quad_f_num*2
# precompute_dir = self.ict_synth_precompute
v_normal = np.load(self.ict_synth_n_npy_list[id_key][e_index])[:v_num]
vertices = np.load(self.ict_synth_v_npy_list[id_key][e_index])[:v_num]
template = self.ict_synth_templates_dict[id_key][:v_num]
#faces = self.ict_synth_templates_dict['face'][:f_num]
v_normal = torch.from_numpy(v_normal).float()
vertices = torch.from_numpy(vertices).float()
template = torch.from_numpy(template).float()
faces = torch.from_numpy(faces).long()
## Random Augmentation ---------------------------------------------------
# template, vertices = self.random_trans_scale(template, vertices)
## -----------------------------------------------------------------------
dfn_info = os.path.join(precompute_dir, f"{id_key}_dfn_info.pkl")
operators = os.path.join(precompute_dir, f"{id_key}_operators.pkl")
img = np.load(os.path.join(precompute_dir, f"{id_idx:03d}_img.npy"))
img = torch.from_numpy(img)[0]
dummy = torch.zeros(1)
return dummy, id_coeff, exp_coeff, template, dfn_info, operators, vertices, v_normal, faces, img
def get_multiface_SEN(self, index):
id_idx = np.where(self.mf_SEN_count > index)[0][0] # num begins from 1
e_index = index - self.mf_SEN_count[id_idx-1]
o_index = self.mf_SEN_count[id_idx] - self.mf_SEN_count[id_idx-1] - self.mf_SEN_remain[id_idx]
if e_index >= o_index:
e_index = random.randint(0, o_index -1)
id_name = self.mf_data_split[self.mode][id_idx-1] # num begins from 0
SEN = list(self.mf_SEN_v_npy_list[id_name].keys())[e_index]
frame_idx = random.randint(0, len(self.mf_SEN_v_npy_list[id_name][SEN]) -1)
template = self.mf_verts[id_idx-1] # 5223
# self.mf_std['v_idx'] # 5223
vertices = np.load(self.mf_SEN_v_npy_list[id_name][SEN][frame_idx]) # 5223
v_normal = np.load(self.mf_SEN_n_npy_list[id_name][SEN][frame_idx]) # 5223
npy_file = self.mf_SEN_v_npy_list[id_name][SEN][frame_idx].replace('.npy', '')
faces = self.mf_std['new_f']
# get template dfn_info (neutral face)
dfn_info = os.path.join(self.mf_precompute_path, f"{id_name}_dfn_info.pkl")
operators = os.path.join(self.mf_precompute_path, f"{id_name}_operators.pkl")
img = np.load(os.path.join(self.mf_precompute_path, f"{id_name}_img.npy"))
img = torch.from_numpy(img)[0]
vertices = torch.from_numpy(vertices).float()
v_normal = torch.from_numpy(v_normal).float()
template = torch.from_numpy(template).float()
## Random Augmentation ---------------------------------------------------
# template, vertices = self.random_trans_scale(template, vertices)
## -----------------------------------------------------------------------
# get exp_coeff (no GT == zeros!)
exp_coeff= torch.zeros(self.WS, 128).float()
# get id_coeff (no GT == zeros!)
id_coeff = torch.zeros(128).float()
dummy = torch.zeros(1)
return dummy, id_coeff, exp_coeff, template, dfn_info, operators, vertices, v_normal, faces, img
def get_multiface_ROM(self, index):
"""
always use neutral face at 0th
"""
# e_index = index % self.expression_vecs.shape[0]
# id_idx = index // self.expression_vecs.shape[0]
# for id_idx, s in enumerate(self.mf_ROM_count):
# if s > index:
# break
id_idx = np.where(self.mf_ROM_count > index)[0][0] # num begins from 1
e_index = index - self.mf_ROM_count[id_idx-1]
o_index = self.mf_ROM_count[id_idx] - self.mf_ROM_count[id_idx-1] - self.mf_ROM_remain[id_idx]
if e_index >= o_index:
e_index = random.randint(0, o_index -1)
id_name = self.mf_data_split[self.mode][id_idx-1] # num begins from 0
ROM = list(self.mf_ROM_v_npy_list[id_name].keys())[e_index]
frame_idx = random.randint(0, len(self.mf_ROM_v_npy_list[id_name][ROM]) -1)
template = self.mf_verts[id_idx-1] # 5223
# self.mf_std['v_idx'] # 5223
vertices = np.load(self.mf_ROM_v_npy_list[id_name][ROM][frame_idx]) # 5223
v_normal = np.load(self.mf_ROM_n_npy_list[id_name][ROM][frame_idx]) # 5223
npy_file = self.mf_ROM_v_npy_list[id_name][ROM][frame_idx].replace('.npy', '')
# os.path.join(self.mf_obj_path,id_name+'_mesh.obj')
faces = self.mf_std['new_f']
# get template dfn_info (neutral face)
dfn_info = os.path.join(self.mf_precompute_path, f"{id_name}_dfn_info.pkl")
operators = os.path.join(self.mf_precompute_path, f"{id_name}_operators.pkl")
# dfn_info = pickle.load(open(os.path.join(
# self.mf_precompute_path,
# f"{id_name}_dfn_info.pkl"
# ), 'rb'))
# operators = os.path.join(
# self.mf_precompute_path,
# f"{id_name}_operators.pkl"
# )
img = np.load(os.path.join(self.mf_precompute_path, f"{id_name}_img.npy"))
img = torch.from_numpy(img)[0]
vertices = torch.from_numpy(vertices).float()
v_normal = torch.from_numpy(v_normal).float()
template = torch.from_numpy(template).float()
## Random Augmentation ---------------------------------------------------
# template, vertices = self.random_trans_scale(template, vertices)
## -----------------------------------------------------------------------
# get id_coeff and exp_coeff (no GT == zeros!)
id_coeff = torch.zeros(128)
exp_coeff = torch.zeros(self.WS, 128)
dummy = torch.zeros(1)
# v_normal = calc_norm_torch(vertices, faces, at='v')
return dummy, id_coeff, exp_coeff, template, dfn_info, operators, vertices, v_normal, faces, img
def get_voca(self, index):
id_idx = np.where(self.voca_count > index)[0][0] # num begins from 1
e_index = index - self.voca_count[id_idx-1]
o_index = self.voca_count[id_idx] - self.voca_count[id_idx-1] - self.voca_remain[id_idx]
if e_index >= o_index:
e_index = random.randint(0, o_index -1)
id_name = self.voca_data_split[self.mode][id_idx-1] # num begins from 0
sent = list(self.voca_v_npy_list[id_name].keys())[e_index]
frame_idx = random.randint(0, len(self.voca_v_npy_list[id_name][sent]) -1)
# get template vertices (neutral face)
template = self.voca_coma_templates[id_name] # 3525, 3
vertices = np.load(self.voca_v_npy_list[id_name][sent][frame_idx]) # 3525, 3
v_normal = np.load(self.voca_n_npy_list[id_name][sent][frame_idx]) # 3525, 3
faces = self.voca_coma_std['new_f'] ## removed eyeball
# get template dfn_info (neutral face)
dfn_info = os.path.join(self.voca_coma_precompute_path, f"{id_name}_dfn_info.pkl")
operators = os.path.join(self.voca_coma_precompute_path, f"{id_name}_operators.pkl")
img = np.load(os.path.join(self.voca_coma_precompute_path, f"{id_name}_img.npy")) # ----------------- [1, 256, 256, 3]
img = torch.from_numpy(img)[0].float()
vertices = torch.from_numpy(vertices).float()
v_normal = torch.from_numpy(v_normal).float()
template = torch.from_numpy(template).float()
faces = torch.from_numpy(faces).long()
id_coeff = torch.zeros(128)
exp_coeff = torch.zeros(self.WS, 128)
dummy = torch.zeros(1)
return dummy, id_coeff, exp_coeff, template, dfn_info, operators, vertices, v_normal, faces, img
def get_coma(self, index):
id_idx = np.where(self.coma_count > index)[0][0] # num begins from 1
e_index = index - self.coma_count[id_idx-1]
o_index = self.coma_count[id_idx] - self.coma_count[id_idx-1] - self.coma_remain[id_idx]
if e_index >= o_index:
e_index = random.randint(0, o_index -1)
id_name = self.voca_data_split[self.mode][id_idx-1] # num begins from 0
ROM = list(self.coma_v_npy_list[id_name].keys())[e_index]
frame_idx = random.randint(0, len(self.coma_v_npy_list[id_name][ROM]) -1)
# get template vertices (neutral face)
template = self.voca_coma_templates[id_name] # 3525, 3
vertices = np.load(self.coma_v_npy_list[id_name][ROM][frame_idx]) # 3525, 3
v_normal = np.load(self.coma_n_npy_list[id_name][ROM][frame_idx]) # 3525, 3
faces = self.voca_coma_std['new_f'] ## removed eyeball
# get template dfn_info (neutral face)
dfn_info = os.path.join(self.voca_coma_precompute_path, f"{id_name}_dfn_info.pkl")
operators = os.path.join(self.voca_coma_precompute_path, f"{id_name}_operators.pkl")
img = np.load(os.path.join(self.voca_coma_precompute_path, f"{id_name}_img.npy")) # ----------------- [1, 256, 256, 3]
img = torch.from_numpy(img)[0].float()
vertices = torch.from_numpy(vertices).float()
v_normal = torch.from_numpy(v_normal).float()
template = torch.from_numpy(template).float()
faces = torch.from_numpy(faces).long()
id_coeff = torch.zeros(128)
exp_coeff = torch.zeros(self.WS, 128)
dummy = torch.zeros(1)
return dummy, id_coeff, exp_coeff, template, dfn_info, operators, vertices, v_normal, faces, img
def get_biwi(self, index):
id_idx = np.where(self.biwi_count > index)[0][0] # num begins from 1
e_index = index - self.biwi_count[id_idx-1]
o_index = self.biwi_count[id_idx] - self.biwi_count[id_idx-1] - self.biwi_remain[id_idx]
if e_index >= o_index:
e_index = random.randint(0, o_index -1)
id_name = self.biwi_id[id_idx-1] # num begins from 0
sent = list(self.biwi_v_npy_list[id_name].keys())[e_index]
frame_idx = random.randint(0, len(self.biwi_v_npy_list[id_name]) -1)
# get template vertices (neutral face)
template = self.biwi_templates[id_name] # 2560, 3
vertices = np.load(self.biwi_v_npy_list[id_name][sent][frame_idx]) # 2560, 3
v_normal = np.load(self.biwi_n_npy_list[id_name][sent][frame_idx]) # 2560, 3
# get template face indices
faces = self.biwi_templates['face']
# get template dfn_info (neutral face)
dfn_info = os.path.join(self.biwi_precompute_path, f"{id_name}_dfn_info.pkl")
operators = os.path.join(self.biwi_precompute_path, f"{id_name}_operators.pkl")
img = np.load(os.path.join(self.biwi_precompute_path, f"{id_name}_img.npy")) # ----------------- [1, 256, 256, 3]
img = torch.from_numpy(img)[0]
vertices = torch.from_numpy(vertices).float()
v_normal = torch.from_numpy(v_normal).float()
template = torch.from_numpy(template).float()
faces = torch.from_numpy(faces).long()
id_coeff = torch.zeros(128)
exp_coeff = torch.zeros(self.WS, 128)
dummy = torch.zeros(1)
return dummy, id_coeff, exp_coeff, template, dfn_info, operators, vertices, v_normal, faces, img
def random_rotation_matrix(self, randgen=None):
"""
Borrowed from https://github.com/nmwsharp/diffusion-net/blob/master/src/diffusion_net/utils.py
Creates a random rotation matrix.
randgen: if given, a np.random.RandomState instance used for random numbers (for reproducibility)
"""
# adapted from http://www.realtimerendering.com/resources/GraphicsGems/gemsiii/rand_rotation.c
if randgen is None:
randgen = np.random.RandomState()