-
Notifications
You must be signed in to change notification settings - Fork 9
/
processor.py
1495 lines (1307 loc) · 76.8 KB
/
processor.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
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 datetime
import lmdb
import math
import matplotlib.pyplot as plt
import numpy as np
import os
import pickle
import pyarrow
import python_speech_features as ps
import time
import torch
import torch.optim as optim
import torch.nn as nn
import torch.nn.functional as F
from os.path import join as jn
from torchlight.torchlight.io import IO
import utils.common as cmn
from net.embedding_space_evaluator import EmbeddingSpaceEvaluator
from net.ser_att_conv_rnn_v1 import AttConvRNN
from net.multimodal_context_net_v1 import PoseGeneratorTriModal as PGT, ConvDiscriminatorTriModal as CDT
from net.multimodal_context_net_v1 import PoseGenerator, AffDiscriminator
from utils.average_meter import AverageMeter
from utils.data_preprocessor import DataPreprocessor
from utils.gen_utils import create_video_and_save
from utils import losses
from utils.ted_db_utils import *
torch.manual_seed(1234)
rec_loss = losses.quat_angle_loss
def find_all_substr(a_str, sub):
start = 0
while True:
start = a_str.find(sub, start)
if start == -1:
return
yield start
start += len(sub) # use start += 1 to find overlapping matches
def get_epoch_and_loss(path_to_model_files, phase, emo_as_cats, epoch='best'):
all_models = os.listdir(path_to_model_files)
if len(all_models) < 2:
if phase == 'ser':
return '', None, 0. if emo_as_cats else -np.inf, np.inf
if phase == 's2eg':
return '', None, np.inf
if epoch == 'best':
loss_list = -1. * np.ones(len(all_models))
for i, model in enumerate(all_models):
loss_val = str.split(model, '_')
if len(loss_val) > 1:
loss_list[i] = float(loss_val[3])
if len(loss_list) < 3:
best_model = all_models[np.argwhere(loss_list == min([n for n in loss_list if n > 0]))[0, 0]]
else:
loss_idx = np.argpartition(loss_list, 2)
best_model = all_models[loss_idx[1]]
all_underscores = list(find_all_substr(best_model, '_'))
# return model name, best loss
if phase == 'ser':
return best_model, int(best_model[all_underscores[0] + 1:all_underscores[1]]),\
float(best_model[all_underscores[2] + 1:all_underscores[3]]), \
float(best_model[all_underscores[4] + 1:all_underscores[5]])
if phase == 's2eg':
return best_model, int(best_model[all_underscores[0] + 1:all_underscores[1]]), \
float(best_model[all_underscores[2] + 1:all_underscores[3]])
assert isinstance(epoch, int)
found_model = None
for i, model in enumerate(all_models):
model_epoch = str.split(model, '_')
if len(model_epoch) > 1 and epoch == int(model_epoch[1]):
found_model = model
break
if found_model is None:
if phase == 'ser':
return '', None, 0. if emo_as_cats else -np.inf, np.inf
if phase == 'se2g':
return '', None, np.inf
all_underscores = list(find_all_substr(found_model, '_'))
if phase == 'ser':
return found_model, int(found_model[all_underscores[0] + 1:all_underscores[1]]),\
float(found_model[all_underscores[2] + 1:all_underscores[3]]),\
float(found_model[all_underscores[4] + 1:all_underscores[5]])
if phase == 's2eg':
return found_model, int(found_model[all_underscores[0] + 1:all_underscores[1]]),\
float(found_model[all_underscores[2] + 1:all_underscores[3]])
class Processor(object):
"""
Processor for emotive gesture generation
"""
def __init__(self, args, config_args, data_path, data_loader,
C, H, W, EC, ED, P, T,
min_train_epochs=20,
zfill=6,
save_path=None):
self.device = torch.device('cuda:{}'.format(torch.cuda.current_device())
if torch.cuda.is_available() else 'cpu')
self.args = args
self.config_args = config_args
self.dataset = args.dataset_ser
self.channel_map = {
'Xrotation': 'x',
'Yrotation': 'y',
'Zrotation': 'z'
}
self.data_loader = data_loader
self.result = dict()
self.iter_info = dict()
self.epoch_info = dict()
self.meta_info = dict(epoch=0, iter=0)
self.io = IO(
self.args.work_dir_ser,
save_log=self.args.save_log,
print_log=self.args.print_log)
# model
self.C = C
self.H = H
self.W = W
self.EC = EC
self.ED = ED
self.P = P
self.T = T
self.num_labels = self.EC if self.args.emo_as_cats else self.ED
if self.args.emo_as_cats:
self.L1 = 128
self.L2 = 256
self.L3 = 256
self.L4 = 256
self.gru_cell_units = 128
self.attention_size = 5
self.pool_stride_height = 2
self.pool_stride_width = 4
self.F1 = 768
self.F2 = 64
self.bidirectional = True
self.dropout_prob = 0.
else:
self.L1 = 16
self.L2 = 8
self.L3 = 8
self.L4 = 8
self.gru_cell_units = 16
self.attention_size = 32
self.pool_stride_height = 2
self.pool_stride_width = 4
self.F1 = 32
self.F2 = 8
self.bidirectional = False
self.dropout_prob = 0.
self.pred_loss_func = nn.CrossEntropyLoss() if self.args.emo_as_cats else nn.L1Loss()
self.best_ser_accu = 0. if self.args.emo_as_cats else -np.inf
self.ser_accu_updated = False
self.ser_step_epochs = [math.ceil(float(self.args.ser_num_epoch * x)) for x in self.args.step]
self.best_ser_accu_epoch = None
self.best_ser_accu_loss = None
self.best_s2eg_loss = np.inf
self.best_s2eg_loss_epoch = None
self.s2eg_loss_updated = False
self.min_train_epochs = min_train_epochs
self.zfill = zfill
self.ser_model = AttConvRNN(C=self.C, H=self.H, W=self.W, EC=self.num_labels,
L1=self.L1, L2=self.L2, gru_cell_units=self.gru_cell_units,
attention_size=self.attention_size, F1=self.F1,
pool_stride_height=self.pool_stride_height,
pool_stride_width=self.pool_stride_width,
F2=self.F2, bidirectional=self.bidirectional,
dropout_prob=self.dropout_prob)
if not self.args.train_ser:
self.lang_model = self.data_loader['train_data_s2eg'].lang_model
self.train_speaker_model = self.data_loader['train_data_s2eg'].speaker_model
self.eval_speaker_model = self.data_loader['eval_data_s2eg'].speaker_model
self.test_speaker_model = self.data_loader['test_data_s2eg'].speaker_model
self.trimodal_generator = PGT(self.config_args,
pose_dim=self.P,
n_words=self.lang_model.n_words,
word_embed_size=self.config_args.wordembed_dim,
word_embeddings=self.lang_model.word_embedding_weights,
z_obj=self.train_speaker_model)
self.trimodal_discriminator = CDT(self.P)
self.s2eg_generator = PoseGenerator(self.config_args,
n_words=self.lang_model.n_words,
word_embed_size=self.config_args.wordembed_dim,
word_embeddings=self.lang_model.word_embedding_weights,
labels_size=self.num_labels,
z_obj=self.train_speaker_model,
pose_dim=self.P)
self.s2eg_discriminator = AffDiscriminator(self.P, self.EC if self.args.emo_as_cats else self.ED)
self.evaluator = EmbeddingSpaceEvaluator(self.config_args, self.P, self.lang_model, self.device)
else:
self.lang_model, self.train_speaker_model,\
self.eval_speaker_model, self.test_speaker_model,\
self.s2eg_generator, self.s2eg_discriminator = [None] * 6
if self.args.use_multiple_gpus and torch.cuda.device_count() > 1:
self.args.batch_size *= torch.cuda.device_count()
self.ser_model = nn.DataParallel(self.ser_model)
if not self.args.train_ser:
self.trimodal_generator = nn.DataParallel(self.trimodal_generator)
self.trimodal_discriminator = nn.DataParallel(self.trimodal_discriminator)
self.s2eg_generator = nn.DataParallel(self.s2eg_generator)
self.s2eg_discriminator = nn.DataParallel(self.s2eg_discriminator)
self.ser_model.to(self.device)
if not self.args.train_ser:
self.trimodal_generator.to(self.device)
self.trimodal_discriminator.to(self.device)
self.s2eg_generator.to(self.device)
self.s2eg_discriminator.to(self.device)
self.conv2_weights = []
if self.args.train_ser:
print('Total ser training data:\t\t{}'.format(len(self.data_loader['train_data_ser'])))
print('Total ser evaluation data:\t\t{}'.format(len(self.data_loader['eval_data_ser'])))
print('Total ser testing data:\t\t\t{}'.format(len(self.data_loader['test_data_ser'])))
print('Training ser with batch size:\t{}'.format(self.args.batch_size))
if self.args.train_s2eg:
print('Total s2eg training data:\t\t{}'.format(len(self.data_loader['train_data_s2eg_wav'])))
print('Total s2eg evaluation data:\t\t{}'.format(len(self.data_loader['eval_data_s2eg_wav'])))
print('Total s2eg testing data:\t\t\t{}'.format(len(self.data_loader['test_data_s2eg_wav'])))
print('Training s2eg with batch size:\t{}'.format(self.args.batch_size))
# ser optimizer
if self.args.ser_optimizer == 'SGD':
self.ser_optimizer = optim.SGD(
self.ser_model.parameters(),
lr=self.args.base_lr,
momentum=0.9,
nesterov=self.args.nesterov,
weight_decay=self.args.weight_decay)
elif self.args.ser_optimizer == 'Adam':
self.ser_optimizer = optim.Adam(
self.ser_model.parameters(),
lr=self.args.base_lr_ser,
weight_decay=self.args.weight_decay)
else:
raise ValueError()
self.lr_ser = self.args.base_lr_ser
self.lr_s2eg_gen = self.config_args.learning_rate
self.lr_s2eg_dis = self.config_args.learning_rate * self.config_args.discriminator_lr_weight
# s2eg optimizers
if not self.args.train_ser:
self.s2eg_gen_optimizer = optim.Adam(self.s2eg_generator.parameters(),
lr=self.lr_s2eg_gen, betas=(0.5, 0.999))
self.s2eg_dis_optimizer = torch.optim.Adam(
self.s2eg_discriminator.parameters(),
lr=self.lr_s2eg_dis,
betas=(0.5, 0.999))
def process_data(self, data, poses, quat, trans, affs):
data = data.float().to(self.device)
poses = poses.float().to(self.device)
quat = quat.float().to(self.device)
trans = trans.float().to(self.device)
affs = affs.float().to(self.device)
return data, poses, quat, trans, affs
def load_model_at_epoch(self, phase, epoch='best'):
work_dir = self.args.work_dir_ser if phase == 'ser'\
else (self.args.work_dir_s2eg if phase == 's2eg' else None)
model_name = None
if phase == 'ser':
model_name, self.best_ser_accu_epoch, \
self.best_ser_accu, self.best_ser_accu_loss =\
get_epoch_and_loss(work_dir, 'ser', emo_as_cats=self.args.emo_as_cats, epoch=epoch)
elif phase == 's2eg':
model_name, self.best_s2eg_loss_epoch, self.best_s2eg_loss =\
get_epoch_and_loss(work_dir, 's2eg', emo_as_cats=self.args.emo_as_cats, epoch=epoch)
model_found = False
try:
loaded_vars = torch.load(jn(work_dir, model_name))
if phase == 'ser':
self.ser_model.load_state_dict(loaded_vars['ser_model_dict'])
elif phase == 's2eg':
self.s2eg_generator.load_state_dict(loaded_vars['gen_model_dict'])
self.s2eg_discriminator.load_state_dict(loaded_vars['dis_model_dict'])
model_found = True
except (FileNotFoundError, IsADirectoryError):
if epoch == 'best':
print('Warning! No saved model found.')
else:
print('Warning! No saved model found at epoch {:d}.'.format(epoch))
return model_found
def adjust_lr_ser(self):
self.lr_ser = self.lr_ser * self.args.lr_ser_decay
for param_group in self.ser_optimizer.param_groups:
param_group['lr'] = self.lr_ser
def adjust_lr_s2eg(self):
self.lr_s2eg_gen = self.lr_s2eg_gen * self.args.lr_s2eg_decay
for param_group in self.s2eg_gen_optimizer.param_groups:
param_group['lr'] = self.lr_s2eg_gen
self.lr_s2eg_dis = self.lr_s2eg_dis * self.args.lr_s2eg_decay
for param_group in self.s2eg_dis_optimizer.param_groups:
param_group['lr'] = self.lr_s2eg_dis
def show_epoch_info(self):
best_metrics = []
print_epochs = []
if self.args.train_ser:
best_metrics = [self.best_ser_accu, self.best_ser_accu_loss]
print_epochs = [self.best_ser_accu_epoch
if self.best_ser_accu_epoch is not None else 0] * len(best_metrics)
if self.args.train_s2eg:
best_metrics = [self.best_s2eg_loss]
print_epochs = [self.best_s2eg_loss_epoch
if self.best_s2eg_loss_epoch is not None else 0] * len(best_metrics)
i = 0
for k, v in self.epoch_info.items():
self.io.print_log('\t{}: {}. Best so far: {:.4f} (epoch: {:d}).'.
format(k, v, best_metrics[i], print_epochs[i]))
i += 1
if self.args.pavi_log:
self.io.log('train', self.meta_info['iter'], self.epoch_info)
def show_iter_info(self):
if self.meta_info['iter'] % self.args.log_interval == 0:
info = '\tIter {} Done.'.format(self.meta_info['iter'])
for k, v in self.iter_info.items():
if isinstance(v, float):
info = info + ' | {}: {:.4f}'.format(k, v)
else:
info = info + ' | {}: {}'.format(k, v)
self.io.print_log(info)
if self.args.pavi_log:
self.io.log('train', self.meta_info['iter'], self.iter_info)
def count_parameters(self):
return sum(p.numel() for p in self.ser_model.parameters() if p.requires_grad)
def yield_batch(self, train):
batch_data_ser = torch.zeros((self.args.batch_size, self.C, self.H, self.W)).to(self.device)
batch_data_s2eg = torch.zeros((self.args.batch_size, self.C, self.H, self.W)).to(self.device)
batch_labels_cat = torch.zeros(self.args.batch_size).long().to(self.device)
batch_labels_dim = torch.zeros((self.args.batch_size, self.ED)).float().to(self.device)
if not self.args.train_ser:
batch_word_seq_tensor = torch.zeros((self.args.batch_size, self.T)).long().to(self.device)
batch_word_seq_lengths = torch.zeros(self.args.batch_size).long().to(self.device)
batch_extended_word_seq = torch.zeros((self.args.batch_size, self.T)).long().to(self.device)
batch_pose_seq = torch.zeros((self.args.batch_size, self.T, self.P + self.C)).float().to(self.device)
batch_vec_seq = torch.zeros((self.args.batch_size, self.T, self.P)).float().to(self.device)
batch_audio = torch.zeros((self.args.batch_size, 36267)).float().to(self.device)
batch_spectrogram = torch.zeros((self.args.batch_size, 128, 70)).float().to(self.device)
batch_vid_indices = torch.zeros(self.args.batch_size).long().to(self.device)
else:
batch_word_seq_tensor, batch_word_seq_lengths,\
batch_extended_word_seq, batch_pose_seq,\
batch_vec_seq, batch_audio,\
batch_spectrogram, batch_vid_indices = [None] * 8
if train:
data_ser_np = self.data_loader['train_data_ser']
data_s2eg_np = self.data_loader['train_data_s2eg_wav']
data_s2eg = self.data_loader['train_data_s2eg']
labels_cat_np = self.data_loader['train_labels_cat']
labels_dim_np = self.data_loader['train_labels_dim']
else:
data_ser_np = self.data_loader['eval_data_ser']
data_s2eg_np = self.data_loader['eval_data_s2eg_wav']
data_s2eg = self.data_loader['eval_data_s2eg']
labels_cat_np = self.data_loader['eval_labels_cat']
labels_dim_np = self.data_loader['eval_labels_dim']
num_data = len(data_ser_np)
pseudo_passes = (num_data + self.args.batch_size - 1) // self.args.batch_size
prob_dist = np.ones(num_data) / float(num_data)
def extend_word_seq(lang, words, end_time=None):
n_frames = data_s2eg.n_poses
if end_time is None:
end_time = aux_info['end_time']
frame_duration = (end_time - aux_info['start_time']) / n_frames
extended_word_indices = np.zeros(n_frames) # zero is the index of padding token
if data_s2eg.remove_word_timing:
n_words = 0
for word in words:
idx = max(0, int(np.floor((word[1] - aux_info['start_time']) / frame_duration)))
if idx < n_frames:
n_words += 1
space = int(n_frames / (n_words + 1))
for word_idx in range(n_words):
idx = (word_idx + 1) * space
extended_word_indices[idx] = lang.get_word_index(words[word_idx][0])
else:
prev_idx = 0
for word in words:
idx = max(0, int(np.floor((word[1] - aux_info['start_time']) / frame_duration)))
if idx < n_frames:
extended_word_indices[idx] = lang.get_word_index(word[0])
# extended_word_indices[prev_idx:idx+1] = lang.get_word_index(word[0])
prev_idx = idx
return torch.Tensor(extended_word_indices).long()
def words_to_tensor(lang, words, end_time=None):
indexes = [lang.SOS_token]
for word in words:
if end_time is not None and word[1] > end_time:
break
indexes.append(lang.get_word_index(word[0]))
indexes.append(lang.EOS_token)
return torch.Tensor(indexes).long()
for p in range(pseudo_passes):
rand_keys = np.random.choice(num_data, size=self.args.batch_size, replace=True, p=prob_dist)
for i, k in enumerate(rand_keys):
batch_data_ser[i] = torch.from_numpy(data_ser_np[k])
batch_labels_cat[i] = torch.from_numpy(np.where(labels_cat_np[k])[0])
batch_labels_dim[i] = torch.from_numpy(labels_dim_np[k])
if not self.args.train_ser:
with data_s2eg.lmdb_env.begin(write=False) as txn:
key = '{:010}'.format(k).encode('ascii')
sample = txn.get(key)
sample = pyarrow.deserialize(sample)
word_seq, pose_seq, vec_seq, audio, spectrogram, aux_info = sample
# vid_name = sample[-1]['vid']
# clip_start = str(sample[-1]['start_time'])
# clip_end = str(sample[-1]['end_time'])
batch_data_s2eg[i] = torch.from_numpy(data_s2eg_np[k])
duration = aux_info['end_time'] - aux_info['start_time']
do_clipping = True
if do_clipping:
sample_end_time = aux_info['start_time'] + duration * data_s2eg.n_poses / vec_seq.shape[0]
audio = make_audio_fixed_length(audio, data_s2eg.expected_audio_length)
spectrogram = spectrogram[:, 0:data_s2eg.expected_spectrogram_length]
vec_seq = vec_seq[0:data_s2eg.n_poses]
pose_seq = pose_seq[0:data_s2eg.n_poses]
else:
sample_end_time = None
# to tensors
word_seq_tensor = words_to_tensor(data_s2eg.lang_model, word_seq, sample_end_time)
extended_word_seq = extend_word_seq(data_s2eg.lang_model, word_seq, sample_end_time)
vec_seq = torch.from_numpy(vec_seq).reshape((vec_seq.shape[0], -1)).float()
pose_seq = torch.from_numpy(pose_seq).reshape((pose_seq.shape[0], -1)).float()
audio = torch.from_numpy(audio).float()
spectrogram = torch.from_numpy(spectrogram)
batch_word_seq_tensor[i, :len(word_seq_tensor)] = word_seq_tensor
batch_word_seq_lengths[i] = len(word_seq_tensor)
batch_extended_word_seq[i] = extended_word_seq
batch_pose_seq[i] = pose_seq
batch_vec_seq[i] = vec_seq
batch_audio[i] = audio
batch_spectrogram[i] = spectrogram
# speaker input
if train:
if self.train_speaker_model and self.train_speaker_model.__class__.__name__ == 'Vocab':
batch_vid_indices[i] =\
torch.LongTensor([self.train_speaker_model.word2index[aux_info['vid']]])
else:
if self.eval_speaker_model and self.eval_speaker_model.__class__.__name__ == 'Vocab':
batch_vid_indices[i] =\
torch.LongTensor([self.eval_speaker_model.word2index[aux_info['vid']]])
yield batch_data_ser, batch_labels_cat, batch_labels_dim,\
batch_word_seq_tensor, batch_word_seq_lengths, batch_extended_word_seq,\
batch_pose_seq, batch_vec_seq, batch_audio, batch_spectrogram, batch_vid_indices
def return_batch(self, batch_size, randomized=True):
data_ser_np = self.data_loader['test_data_ser']
data_s2eg_np = self.data_loader['test_data_s2eg_wav']
data_s2eg = self.data_loader['test_data_s2eg']
labels_cat_np = self.data_loader['test_labels_cat']
labels_dim_np = self.data_loader['test_labels_dim']
if len(batch_size) > 1:
rand_keys = np.copy(batch_size)
batch_size = len(batch_size)
else:
batch_size = batch_size[0]
num_data = len(data_ser_np)
prob_dist = np.ones(num_data) / float(num_data)
if randomized:
rand_keys = np.random.choice(num_data, size=batch_size, replace=False, p=prob_dist)
else:
rand_keys = np.arange(batch_size)
batch_data_ser = torch.zeros((batch_size, self.C, self.H, self.W)).to(self.device)
batch_data_s2eg = torch.zeros((batch_size, self.C, self.H, self.W)).to(self.device)
batch_labels_cat = torch.zeros(batch_size).long().to(self.device)
batch_labels_dim = torch.zeros((batch_size, self.ED)).float().to(self.device)
batch_words = [[] for _ in range(batch_size)]
batch_aux_info = [[] for _ in range(batch_size)]
batch_word_seq_tensor = torch.zeros((batch_size, self.T)).long().to(self.device)
batch_word_seq_lengths = torch.zeros(batch_size).long().to(self.device)
batch_extended_word_seq = torch.zeros((batch_size, self.T)).long().to(self.device)
batch_pose_seq = torch.zeros((batch_size, self.T, self.P + self.C)).float().to(self.device)
batch_vec_seq = torch.zeros((batch_size, self.T, self.P)).float().to(self.device)
batch_target_seq = torch.zeros((batch_size, self.T, self.P)).float().to(self.device)
batch_audio = torch.zeros((batch_size, 36267)).float().to(self.device)
batch_spectrogram = torch.zeros((batch_size, 128, 70)).float().to(self.device)
batch_vid_indices = torch.zeros(batch_size).long().to(self.device)
def extend_word_seq(lang, words, end_time=None):
n_frames = data_s2eg.n_poses
if end_time is None:
end_time = aux_info['end_time']
frame_duration = (end_time - aux_info['start_time']) / n_frames
extended_word_indices = np.zeros(n_frames) # zero is the index of padding token
if data_s2eg.remove_word_timing:
n_words = 0
for word in words:
idx = max(0, int(np.floor((word[1] - aux_info['start_time']) / frame_duration)))
if idx < n_frames:
n_words += 1
space = int(n_frames / (n_words + 1))
for word_idx in range(n_words):
idx = (word_idx + 1) * space
extended_word_indices[idx] = lang.get_word_index(words[word_idx][0])
else:
prev_idx = 0
for word in words:
idx = max(0, int(np.floor((word[1] - aux_info['start_time']) / frame_duration)))
if idx < n_frames:
extended_word_indices[idx] = lang.get_word_index(word[0])
# extended_word_indices[prev_idx:idx+1] = lang.get_word_index(word[0])
prev_idx = idx
return torch.Tensor(extended_word_indices).long()
def words_to_tensor(lang, words, end_time=None):
indexes = [lang.SOS_token]
for word in words:
if end_time is not None and word[1] > end_time:
break
indexes.append(lang.get_word_index(word[0]))
indexes.append(lang.EOS_token)
return torch.Tensor(indexes).long()
for i, k in enumerate(rand_keys):
batch_data_ser[i] = torch.from_numpy(data_ser_np[k])
batch_labels_cat[i] = torch.from_numpy(np.where(labels_cat_np[k])[0])
batch_labels_dim[i] = torch.from_numpy(labels_dim_np[k])
if not self.args.train_ser:
with data_s2eg.lmdb_env.begin(write=False) as txn:
key = '{:010}'.format(k).encode('ascii')
sample = txn.get(key)
sample = pyarrow.deserialize(sample)
word_seq, pose_seq, vec_seq, audio, spectrogram, aux_info = sample
batch_data_s2eg[i] = torch.from_numpy(data_s2eg_np[k])
# for selected_vi in range(len(word_seq)): # make start time of input text zero
# word_seq[selected_vi][1] -= aux_info['start_time'] # start time
# word_seq[selected_vi][2] -= aux_info['start_time'] # end time
batch_words[i] = [word_seq[i][0] for i in range(len(word_seq))]
batch_aux_info[i] = aux_info
duration = aux_info['end_time'] - aux_info['start_time']
do_clipping = True
if do_clipping:
sample_end_time = aux_info['start_time'] + duration * data_s2eg.n_poses / vec_seq.shape[0]
audio = make_audio_fixed_length(audio, data_s2eg.expected_audio_length)
spectrogram = spectrogram[:, 0:data_s2eg.expected_spectrogram_length]
vec_seq = vec_seq[0:data_s2eg.n_poses]
pose_seq = pose_seq[0:data_s2eg.n_poses]
else:
sample_end_time = None
# to tensors
word_seq_tensor = words_to_tensor(data_s2eg.lang_model, word_seq, sample_end_time)
extended_word_seq = extend_word_seq(data_s2eg.lang_model, word_seq, sample_end_time)
vec_seq = torch.from_numpy(vec_seq).reshape((vec_seq.shape[0], -1)).float()
pose_seq = torch.from_numpy(pose_seq).reshape((pose_seq.shape[0], -1)).float()
target_seq = convert_pose_seq_to_dir_vec(pose_seq)
target_seq = target_seq.reshape(target_seq.shape[0], -1)
target_seq -= np.reshape(self.config_args.mean_dir_vec, -1)
audio = torch.from_numpy(audio).float()
spectrogram = torch.from_numpy(spectrogram)
batch_word_seq_tensor[i, :len(word_seq_tensor)] = word_seq_tensor
batch_word_seq_lengths[i] = len(word_seq_tensor)
batch_extended_word_seq[i] = extended_word_seq
batch_pose_seq[i] = pose_seq
# batch_vec_seq[i] = vec_seq
batch_target_seq[i] = torch.from_numpy(target_seq).float()
batch_audio[i] = audio
batch_spectrogram[i] = spectrogram
# speaker input
if self.test_speaker_model and self.test_speaker_model.__class__.__name__ == 'Vocab':
batch_vid_indices[i] =\
torch.LongTensor([self.test_speaker_model.word2index[aux_info['vid']]])
return batch_data_ser, batch_labels_cat, batch_labels_dim, batch_words,\
batch_aux_info, batch_word_seq_tensor, batch_word_seq_lengths, batch_extended_word_seq,\
batch_pose_seq, batch_vec_seq, batch_target_seq, batch_audio, batch_spectrogram, batch_vid_indices
def forward_pass_ser(self, data, labels_gt=None):
self.ser_optimizer.zero_grad()
with torch.autograd.detect_anomaly():
labels_pred_raw = self.ser_model(data)
# labels_pred_np = labels_pred.detach().cpu().numpy()
# labels_gt_np = labels_gt.detach().cpu().numpy()
if self.args.emo_as_cats:
labels_pred = labels_pred_raw
else:
# labels_pred = torch.sigmoid(labels_pred_raw)
labels_pred = labels_pred_raw
labels_pred_diff = labels_pred[1:] - labels_pred[:-1]
# total_loss = None if labels_gt is None else self.pred_loss_func(labels_pred, labels_gt)
total_loss = None if labels_gt is None else ((self.pred_loss_func(labels_pred, labels_gt) +
(0. if self.args.emo_as_cats else
self.pred_loss_func(labels_pred_diff,
labels_gt[1:] - labels_gt[:-1]))) * 1.)
max_idx = torch.argmax(labels_pred, -1, keepdim=True)
labels_one_hot = torch.FloatTensor(labels_pred.shape).to(self.device)
labels_one_hot.zero_()
labels_one_hot.scatter_(1, max_idx, 1)
return total_loss, torch.argmax(labels_pred, dim=-1) if self.args.emo_as_cats else labels_pred, labels_one_hot
@staticmethod
def add_noise(data):
noise = torch.randn_like(data) * 0.1
return data + noise
def push_samples(self, target, out_dir_vec, in_text_padded, in_audio,
losses_all, joint_mae, accel):
batch_size = len(target)
if self.evaluator:
self.evaluator.reset()
loss = F.l1_loss(out_dir_vec, target)
losses_all.update(loss.item(), batch_size)
if self.evaluator:
self.evaluator.push_samples(in_text_padded, in_audio, out_dir_vec, target)
# calculate MAE of joint coordinates
out_dir_vec_np = out_dir_vec.detach().cpu().numpy()
out_dir_vec_np += np.array(self.config_args.mean_dir_vec).squeeze()
out_joint_poses = convert_dir_vec_to_pose(out_dir_vec_np)
target_vec = target.detach().cpu().numpy()
target_vec += np.array(self.config_args.mean_dir_vec).squeeze()
target_poses = convert_dir_vec_to_pose(target_vec)
if out_joint_poses.shape[1] == self.config_args.n_poses:
diff = out_joint_poses[:, self.config_args.n_pre_poses:] - target_poses[:, self.config_args.n_pre_poses:]
else:
diff = out_joint_poses - target_poses[:, self.config_args.n_pre_poses:]
mae_val = np.mean(np.absolute(diff))
joint_mae.update(mae_val, batch_size)
# accel
target_acc = np.diff(target_poses, n=2, axis=1)
out_acc = np.diff(out_joint_poses, n=2, axis=1)
accel.update(np.mean(np.abs(target_acc - out_acc)), batch_size)
return losses_all, joint_mae, accel
def forward_pass_s2eg(self, in_text, in_audio, in_emo_labels, target_poses, vid_indices, train,
target_seq=None, words=None, aux_info=None, save_path=None, make_video=False,
calculate_metrics=False, losses_all=None, joint_mae=None, accel=None):
warm_up_epochs = self.config_args.loss_warmup
use_noisy_target = False
# make pre seq input
pre_seq = target_poses.new_zeros((target_poses.shape[0], target_poses.shape[1], target_poses.shape[2] + 1))
pre_seq[:, 0:self.config_args.n_pre_poses, :-1] = target_poses[:, 0:self.config_args.n_pre_poses]
pre_seq[:, 0:self.config_args.n_pre_poses, -1] = 1 # indicating bit for constraints
###########################################################################################
# train D
dis_error = None
if self.meta_info['epoch'] > warm_up_epochs and self.config_args.loss_gan_weight > 0.0:
self.s2eg_dis_optimizer.zero_grad()
# out shape (batch x seq x dim)
out_dir_vec, *_ = self.s2eg_generator(pre_seq, in_text, in_audio, in_emo_labels, vid_indices)
if use_noisy_target:
noise_target = Processor.add_noise(target_poses)
noise_out = Processor.add_noise(out_dir_vec.detach())
dis_real = self.s2eg_discriminator(noise_target, in_emo_labels, in_text)
dis_fake = self.s2eg_discriminator(noise_out, in_emo_labels, in_text)
else:
dis_real = self.s2eg_discriminator(target_poses, in_emo_labels, in_text)
dis_fake = self.s2eg_discriminator(out_dir_vec.detach(), in_emo_labels, in_text)
dis_error = torch.sum(-torch.mean(torch.log(dis_real + 1e-8) + torch.log(1 - dis_fake + 1e-8))) # ns-gan
if train:
dis_error.backward()
self.s2eg_dis_optimizer.step()
###########################################################################################
# train G
self.s2eg_gen_optimizer.zero_grad()
# decoding
out_dir_vec_trimodal, *_ = self.trimodal_generator(pre_seq, in_text, in_audio, vid_indices)
out_dir_vec, z, z_mu, z_log_var = self.s2eg_generator(pre_seq, in_text, in_audio, in_emo_labels, vid_indices)
# make a video
assert not make_video or (make_video and target_seq is not None), \
'target_seq cannot be None when make_video is True'
assert not make_video or (make_video and words is not None), \
'words cannot be None when make_video is True'
assert not make_video or (make_video and aux_info is not None), \
'aux_info cannot be None when make_video is True'
assert not make_video or (make_video and save_path is not None), \
'save_path cannot be None when make_video is True'
if make_video:
sentence_words = []
for word in words:
sentence_words.append(word)
sentences = [' '.join(sentence_word) for sentence_word in sentence_words]
num_videos = len(aux_info)
for vid_idx in range(num_videos):
start_time = time.time()
filename_prefix = '{}_{}'.format(aux_info[vid_idx]['vid'], vid_idx)
filename_prefix_for_video = filename_prefix
aux_str = '({}, time: {}-{})'.format(aux_info[vid_idx]['vid'],
str(datetime.timedelta(
seconds=aux_info[vid_idx]['start_time'])),
str(datetime.timedelta(
seconds=aux_info[vid_idx]['end_time'])))
create_video_and_save(
save_path, 0, filename_prefix_for_video, 0,
target_seq[vid_idx].cpu().numpy(),
out_dir_vec_trimodal[vid_idx].cpu().numpy(), out_dir_vec[vid_idx].cpu().numpy(),
np.reshape(self.config_args.mean_dir_vec, -1), sentences[vid_idx],
audio=in_audio[vid_idx].cpu().numpy(), aux_str=aux_str,
clipping_to_shortest_stream=True, delete_audio_file=False)
print('\rRendered {} of {} videos. Last one took {:.2f} seconds.'.format(vid_idx + 1,
num_videos,
time.time() - start_time),
end='')
print()
# calculate metrics
assert not calculate_metrics or (calculate_metrics and target_seq is not None), \
'target_seq cannot be None when calculate_metrics is True'
assert not calculate_metrics or (calculate_metrics and losses_all is not None), \
'losses_all cannot be None when calculate_metrics is True'
assert not calculate_metrics or (calculate_metrics and joint_mae is not None), \
'joint_mae cannot be None when calculate_metrics is True'
assert not calculate_metrics or (calculate_metrics and accel is not None), \
'accel cannot be None when calculate_metrics is True'
if calculate_metrics:
losses_all, joint_mae, accel = self.push_samples(target_seq, out_dir_vec_trimodal, in_text, in_audio,
losses_all, joint_mae, accel)
# self.push_samples(target_seq, out_dir_vec, in_text, in_audio)
# loss
beta = 0.1
huber_loss = F.smooth_l1_loss(out_dir_vec / beta, target_poses / beta) * beta
dis_output = self.s2eg_discriminator(out_dir_vec, in_emo_labels, in_text)
gen_error = -torch.mean(torch.log(dis_output + 1e-8))
kld = div_reg = None
if (self.config_args.z_type == 'speaker' or self.config_args.z_type == 'random') and\
self.config_args.loss_reg_weight > 0.0:
if self.config_args.z_type == 'speaker':
# enforcing divergent gestures btw original vid and other vid
rand_idx = torch.randperm(vid_indices.shape[0])
rand_vids = vid_indices[rand_idx]
else:
rand_vids = None
out_dir_vec_rand_vid, z_rand_vid, _, _ = self.s2eg_generator(pre_seq, in_text, in_audio,
in_emo_labels, rand_vids)
beta = 0.05
pose_l1 = F.smooth_l1_loss(out_dir_vec / beta, out_dir_vec_rand_vid.detach() / beta,
reduction='none') * beta
pose_l1 = pose_l1.sum(dim=1).sum(dim=1)
pose_l1 = pose_l1.view(pose_l1.shape[0], -1).mean(1)
z_l1 = F.l1_loss(z.detach(), z_rand_vid.detach(), reduction='none')
z_l1 = z_l1.view(z_l1.shape[0], -1).mean(1)
div_reg = -(pose_l1 / (z_l1 + 1.0e-5))
div_reg = torch.clamp(div_reg, min=-1000)
div_reg = div_reg.mean()
if self.config_args.z_type == 'speaker':
# speaker embedding KLD
kld = -0.5 * torch.mean(1 + z_log_var - z_mu.pow(2) - z_log_var.exp())
loss = self.config_args.loss_regression_weight * huber_loss +\
self.config_args.loss_kld_weight * kld +\
self.config_args.loss_reg_weight * div_reg
else:
loss = self.config_args.loss_regression_weight * huber_loss +\
self.config_args.loss_reg_weight * div_reg
else:
loss = self.config_args.loss_regression_weight * huber_loss # + var_loss
if self.meta_info['epoch'] > warm_up_epochs:
loss += self.config_args.loss_gan_weight * gen_error
if train:
loss.backward()
self.s2eg_gen_optimizer.step()
loss_dict = {'loss': self.config_args.loss_regression_weight * huber_loss.item()}
if kld:
loss_dict['KLD'] = self.config_args.loss_kld_weight * kld.item()
if div_reg:
loss_dict['DIV_REG'] = self.config_args.loss_reg_weight * div_reg.item()
if self.meta_info['epoch'] > warm_up_epochs and self.config_args.loss_gan_weight > 0.0:
loss_dict['gen'] = self.config_args.loss_gan_weight * gen_error.item()
loss_dict['dis'] = dis_error.item()
loss_dict['total_loss'] = 0.
for loss in loss_dict.keys():
loss_dict['total_loss'] += loss_dict[loss]
return loss_dict, losses_all, joint_mae, accel
def per_train(self):
batch_ser_loss = 0.
batch_ser_accu = 0.
batch_s2eg_loss = 0.
num_batches = 0.
for train_data_wav, train_labels_cat, train_labels_dim,\
word_seq_tensor, word_seq_lengths, extended_word_seq,\
pose_seq, vec_seq, audio, spectrogram, vid_indices in self.yield_batch(train=True):
if self.args.train_ser:
self.ser_model.train()
ser_loss, train_labels_pred, train_labels_oh =\
self.forward_pass_ser(train_data_wav,
train_labels_cat if self.args.emo_as_cats else train_labels_dim)
ser_loss.backward()
# nn.utils.clip_grad_norm_(self.ser_model.parameters(), self.args.gradient_clip)
self.ser_optimizer.step()
if torch.max(torch.abs(self.ser_model.linear3.weight.grad.data)) < 1e-10:
stop = 1
if self.args.emo_as_cats:
train_accu = torch.sum((train_labels_cat - train_labels_pred) == 0) / len(train_labels_pred)
else:
train_accu = - ser_loss.clone()
# Compute statistics
batch_ser_loss += ser_loss.item()
batch_ser_accu += train_accu.item()
self.iter_info['ser_loss'] = ser_loss.data.item()
self.iter_info['ser_accu'] = train_accu.data.item()
self.iter_info['lr_ser'] = '{:.6f}'.format(self.lr_ser)
self.show_iter_info()
else:
self.ser_model.eval()
with torch.no_grad():
_, train_labels_pred, train_labels_oh = self.forward_pass_ser(train_data_wav,
train_labels_cat)
if self.args.train_s2eg:
self.s2eg_generator.train()
self.s2eg_discriminator.train()
loss_dict, *_ = self.forward_pass_s2eg(extended_word_seq, audio, train_labels_oh,
vec_seq, vid_indices, train=True)
# Compute statistics
batch_s2eg_loss += loss_dict['total_loss']
self.iter_info['s2eg_loss'] = loss_dict['total_loss']
self.iter_info['lr_gen'] = '{}'.format(self.lr_s2eg_gen)
self.iter_info['lr_dis'] = '{}'.format(self.lr_s2eg_dis)
self.show_iter_info()
self.meta_info['iter'] += 1
num_batches += 1
if self.args.train_ser:
batch_ser_loss /= num_batches
batch_ser_accu /= num_batches
self.epoch_info['mean_ser_accu'] = batch_ser_accu
self.epoch_info['mean_ser_loss'] = batch_ser_loss
if self.args.train_s2eg:
batch_s2eg_loss /= num_batches
self.epoch_info['mean_s2eg_loss'] = batch_s2eg_loss
self.show_epoch_info()
self.io.print_timer()
if self.args.train_ser:
self.adjust_lr_ser()
if self.args.train_s2eg:
self.adjust_lr_s2eg()
def per_eval(self):
batch_ser_loss = 0.
batch_ser_accu = 0.
batch_s2eg_loss = 0.
num_batches = 0.
for eval_data_wav, eval_labels_cat, eval_labels_dim,\
word_seq_tensor, word_seq_lengths, extended_word_seq, \
pose_seq, vec_seq, audio, spectrogram, vid_indices in self.yield_batch(train=False):
self.ser_model.eval()
with torch.no_grad():
ser_loss, eval_labels_pred, eval_labels_oh =\
self.forward_pass_ser(eval_data_wav,
eval_labels_cat if self.args.emo_as_cats else eval_labels_dim)
if self.args.emo_as_cats:
eval_accu = torch.sum((eval_labels_cat - eval_labels_pred) == 0) / len(eval_labels_pred)
else:
eval_accu = - ser_loss.clone()
if self.args.train_ser:
# Compute statistics
batch_ser_loss += ser_loss.item()
batch_ser_accu += eval_accu.item()
self.iter_info['ser_loss'] = ser_loss.data.item()
self.iter_info['ser_accu'] = eval_accu.data.item()
self.iter_info['lr_ser'] = '{:.6f}'.format(self.lr_ser)
self.show_iter_info()
if self.args.train_s2eg:
self.s2eg_generator.eval()
self.s2eg_discriminator.eval()
with torch.no_grad():
loss_dict, *_ = self.forward_pass_s2eg(extended_word_seq, audio, eval_labels_oh,
vec_seq, vid_indices, train=False)
# Compute statistics
batch_s2eg_loss += loss_dict['total_loss']
self.iter_info['s2eg_loss'] = loss_dict['total_loss']
self.iter_info['lr_gen'] = '{:.6f}'.format(self.lr_s2eg_gen)
self.iter_info['lr_dis'] = '{:.6f}'.format(self.lr_s2eg_dis)
self.show_iter_info()
self.meta_info['iter'] += 1
num_batches += 1
if self.args.train_ser:
batch_ser_loss /= num_batches
batch_ser_accu /= num_batches
self.epoch_info['mean_ser_accu'] = batch_ser_accu
self.epoch_info['mean_ser_loss'] = batch_ser_loss
if self.epoch_info['mean_ser_accu'] > self.best_ser_accu and \
self.meta_info['epoch'] > self.min_train_epochs:
self.best_ser_accu = self.epoch_info['mean_ser_accu']
self.best_ser_accu_loss = self.epoch_info['mean_ser_loss']
self.best_ser_accu_epoch = self.meta_info['epoch']
self.ser_accu_updated = True
else:
self.ser_accu_updated = False
if self.args.train_s2eg:
batch_s2eg_loss /= num_batches
self.epoch_info['mean_s2eg_loss'] = batch_s2eg_loss
if self.epoch_info['mean_s2eg_loss'] < self.best_s2eg_loss and \
self.meta_info['epoch'] > self.min_train_epochs:
self.best_s2eg_loss = self.epoch_info['mean_s2eg_loss']
self.best_s2eg_loss_epoch = self.meta_info['epoch']
self.s2eg_loss_updated = True
else:
self.s2eg_loss_updated = False
self.show_epoch_info()
self.io.print_timer()
def train(self):
if self.args.ser_load_last_best:
ser_model_found = self.load_model_at_epoch('ser', epoch=self.args.ser_start_epoch)
if not ser_model_found and self.args.ser_start_epoch is not 'best':
print('Warning! Trying to load best known model for ser: '.format(self.args.ser_start_epoch),
end='')
ser_model_found = self.load_model_at_epoch('ser', epoch='best')
self.args.ser_start_epoch = self.best_ser_accu_epoch if ser_model_found else 0
print('loaded.')
if not ser_model_found:
print('Warning! Starting at epoch 0')
self.args.ser_start_epoch = 0
else:
self.args.ser_start_epoch = 0