-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_disentangle.py
More file actions
1336 lines (1126 loc) · 51.4 KB
/
Copy pathtrain_disentangle.py
File metadata and controls
1336 lines (1126 loc) · 51.4 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 datetime
import math
import os
from collections import defaultdict
import json
import random
import numpy as np
import librosa
import pytorch_lightning as pl
import torch
import torch.nn.functional as F
from peft import LoraConfig, get_peft_model
from torch import nn
# from torch.nn import TransformerEncoder, TransformerEncoderLayer
from torch.optim.lr_scheduler import LambdaLR
from torch.utils.data import DataLoader
from pytorch_lightning import Trainer, seed_everything
from pytorch_lightning.callbacks import ModelCheckpoint
from pytorch_lightning.loggers import WandbLogger
from pytorch_lightning.strategies.ddp import DDPStrategy
import wandb
from audioldm2.latent_diffusion.models.ddpm import LatentDiffusion
from audioldm2.latent_diffusion.modules.audiomae import models_mae
from audioldm2.utils import plot_mel, save_wave, default_audioldm_config
from calc_metrics import calc_metric, calc_cer
from codebook_clustering import create_reduced_codebook, vote_with_mode
from dataset import mel_spectrogram, read_jsonlines, Meg2MelDataset
from inference import load_models
from brain_module import BrainModule
from ns3_facodec import TransformerEncoder, ResidualVQ
from ns3_facodec.quantize.fvq import FactorizedVectorQuantize
from projector import FeatureProjector, ProjectorLoss
from train_proj import WarmupScheduler
def fix_seed(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
class BrainFVQ(nn.Module):
def __init__(self, fvq: FactorizedVectorQuantize, feature_dim=256, num_clusters=64, pool_scale=4, temperature=0.2):
super().__init__()
# 注册codebook为缓冲区(不参与梯度更新)
reduced_codebook, mapping = create_reduced_codebook(
fvq.codebook.weight, num_clusters=num_clusters
)
self.register_buffer('codebook', reduced_codebook)
self.codebook_mapping = mapping
self.in_proj = fvq.in_proj
self.out_proj = fvq.out_proj
for p in self.in_proj.parameters():
p.requires_grad = False
for p in self.out_proj.parameters():
p.requires_grad = False
self.codebook_size = self.codebook.shape[0]
self.codebook_dim = self.codebook.shape[1]
# self.sub_num = sub_num
self.pool_scale = pool_scale
self.temperature = temperature
def forward(self, z):
# z = self.encoder(z)
z_e = self.in_proj(z)
z_q, indices = self.vector_quantization(z_e)
z_q = (
z_e + (z_q - z_e).detach()
) # noop in forward pass, straight-through gradient estimator in backward pass
# z_q = z_e
z_out = self.out_proj(z_q) # [B, S, H]
return z_out, z_e
def vector_quantization(self, z_e, normalize=True):
bs, t, d = z_e.shape
z_e = z_e.reshape(-1, d)
# 计算输入向量与 codebook 向量之间的欧式距离
if normalize:
z_e = F.normalize(z_e)
codebook = F.normalize(self.codebook)
else:
codebook = self.codebook
distances = torch.cdist(z_e, codebook, p=2) # (batch_size, codebook_size)
# 找到最小距离对应的索引
indices = torch.argmin(distances, dim=1) # (batch_size,)
# 根据索引从 codebook 中取出量化向量
z_q = self.codebook[indices] # (batch_size, d)
z_q = z_q.reshape(bs, t, -1)
indices = indices.reshape(bs, t)
return z_q, indices
def cal_contrastive_loss(self, z_pred, vq_id, normalize=True):
"""
Args:
z_pred: (bs*sub, t, d)
vq_id: (bs, t)
normalize: if True, use cosine similarity
Returns:
contrastive_loss
"""
# (bs*sub, t, d) -> (bs*sub, d, t) -> (bs*t*sub, d)
bs_sub = z_pred.shape[0]
bs = vq_id.shape[0]
sub_num = bs_sub // bs
z_pred = z_pred.permute(0, 2, 1)
z_pred = F.avg_pool1d(z_pred, kernel_size=self.pool_scale, stride=self.pool_scale)
d, t = z_pred.shape[-2], z_pred.shape[-1]
z_pred = z_pred.reshape(bs, sub_num, d, t).permute(0, 3, 1, 2).reshape(-1, d)
# z_target == codebook (codebook_size, d)
if normalize:
z_pred = F.normalize(z_pred)
codebook = F.normalize(self.codebook)
else:
codebook = self.codebook
sim_matrix = -torch.cdist(z_pred, codebook) # 使用负欧氏距离作为相似度
sim_matrix = sim_matrix / self.temperature
# (bs, t) -> (bs*t) -> (bs*t*sub)
mapped_id = [self.codebook_mapping[int(id)] for id in vq_id.reshape(-1)]
label_id = vote_with_mode(mapped_id, pool=self.pool_scale)
targets = torch.tensor(label_id, dtype=torch.int64, device=z_pred.device)\
.unsqueeze(1).repeat(1, sub_num).reshape(-1)
# 创建标签矩阵 (one-hot)
target_matrix = F.one_hot(targets, num_classes=self.codebook_size).float()
# 计算InfoNCE损失 (对数似然)
log_probs = F.log_softmax(sim_matrix, dim=1)
loss = -(target_matrix * log_probs).sum(dim=1).mean()
# target_code = codebook[targets]
# loss = F.mse_loss(z_pred, target_code)
# # loss = 1 - F.cosine_similarity(z_pred, target_code, dim=1).mean()
return loss
class BrainRVQ(nn.Module):
def __init__(
self,
rvq: ResidualVQ,
feature_dim=256,
sub_num=1,
num_clusters=64,
pool_scale=4,
temperature=0.2,
):
super().__init__()
self.layers = nn.ModuleList()
for fvq in rvq.layers:
self.layers.append(BrainFVQ(fvq=fvq,
feature_dim=feature_dim,
num_clusters=num_clusters,
pool_scale=pool_scale, temperature=temperature))
def forward(self, z):
quantized_out = 0.0
residual = z
z_projected = []
for layer in self.layers:
z_out, z_e = layer(residual)
residual = residual - z_out
quantized_out = quantized_out + z_out
z_projected.append(z_e.unsqueeze(0))
z_projected = torch.vstack(z_projected)
return quantized_out, z_projected
def cal_contrastive_loss(self, z_pred, vq_id):
loss = 0.0
for i, layer in enumerate(self.layers):
loss += layer.cal_contrastive_loss(z_pred[i], vq_id[i])
return loss
class BrainRVQEncoders(nn.Module):
def __init__(
self,
fa_decoder,
feature_dim=256,
sub_num=1,
num_clusters=64,
pool_scale=4,
temperature=0.2,
use_prosody=True,
use_content=True,
use_detail=True,
use_timbre=True,
):
super().__init__()
self.use_prosody = use_prosody
self.use_content = use_content
self.use_detail = use_detail
self.use_timbre = use_timbre
if use_prosody:
# self.prosody_encoder = TransformerEncoder(
# enc_emb_tokens=None,
# encoder_layer=4,
# encoder_hidden=feature_dim,
# encoder_head=4,
# conv_filter_size=1024,
# conv_kernel_size=5,
# encoder_dropout=0.1,
# use_cln=False,
# )
self.prosody_vq = BrainRVQ(rvq=fa_decoder.quantizer[0],
feature_dim=feature_dim,
sub_num=sub_num, num_clusters=num_clusters,
pool_scale=pool_scale, temperature=temperature)
if use_content:
# self.content_encoder = TransformerEncoder(
# enc_emb_tokens=None,
# encoder_layer=4,
# encoder_hidden=feature_dim,
# encoder_head=4,
# conv_filter_size=1024,
# conv_kernel_size=5,
# encoder_dropout=0.1,
# use_cln=False,
# )
self.content_vq = BrainRVQ(rvq=fa_decoder.quantizer[1],
feature_dim=feature_dim,
sub_num=sub_num, num_clusters=num_clusters,
pool_scale=pool_scale, temperature=temperature)
if use_detail:
self.detail_vq = BrainRVQ(rvq=fa_decoder.quantizer[2],
feature_dim=feature_dim,
sub_num=sub_num, num_clusters=num_clusters,
pool_scale=1, temperature=temperature)
if use_timbre:
# self.timbre_encoder = TransformerEncoder(
# encoder_layer=TransformerEncoderLayer(d_model=vq_dim, nhead=4), num_layers=4)
self.timbre_encoder = TransformerEncoder(
enc_emb_tokens=None,
encoder_layer=4,
encoder_hidden=feature_dim,
encoder_head=4,
conv_filter_size=1024,
conv_kernel_size=5,
encoder_dropout=0.1,
use_cln=False,
)
def forward(self, x, return_ortho_loss=False):
outs = 0
buf = {}
z_projected_buf = {}
if self.use_timbre:
h_t = self.timbre_encoder(x).mean(1)
buf['timbre'] = h_t
p_input = x
c_input = x
if self.use_prosody:
# p_hidden = self.prosody_encoder(p_input)
z_p, z_projected_p = self.prosody_vq(p_input)
outs += z_p
x = x - z_p
buf['prosody'] = z_p
z_projected_buf['prosody'] = z_projected_p
if self.use_content:
# c_hidden = self.content_encoder(c_input)
z_c, z_projected_c = self.content_vq(c_input)
outs += z_c
x = x - z_c
buf['content'] = z_c
z_projected_buf['content'] = z_projected_c
if self.use_detail:
z_d, z_projected_d = self.detail_vq(x)
outs += z_d
buf['detail'] = z_d
z_projected_buf['detail'] = z_projected_d
if not isinstance(outs, torch.Tensor):
outs = x
if return_ortho_loss and self.use_prosody and self.use_content:
ortho_loss = self.cal_ortho_loss(p_hidden, c_hidden)
return outs, buf, z_projected_buf, ortho_loss
return outs, buf, z_projected_buf
def cal_ortho_loss(self, prosody_features, content_features):
n_batch, n_t, d = prosody_features.shape
prosody_features = prosody_features.reshape(-1, d)
content_features = content_features.reshape(-1, d)
# 归一化特征向量
p_norm = F.normalize(prosody_features, p=2, dim=1)
c_norm = F.normalize(content_features, p=2, dim=1)
# 计算批次内样本的内积矩阵
similarity = torch.mm(p_norm, c_norm.t())
# 最小化绝对内积值(促进正交性)
ortho_loss = torch.mean(torch.abs(similarity))
return ortho_loss
def cal_contrastive_loss(self, z_projected_buf, vq_id):
loss = 0.0
log_dic = {}
if self.use_prosody:
z_projected_p = z_projected_buf['prosody']
vq_id_p = vq_id[:1]
p_loss = self.prosody_vq.cal_contrastive_loss(z_projected_p, vq_id_p)
loss += p_loss
log_dic.update({"p": p_loss})
if self.use_content:
z_projected_c = z_projected_buf['content']
vq_id_c = vq_id[1:3]
c_loss = self.content_vq.cal_contrastive_loss(z_projected_c, vq_id_c)
loss += c_loss
log_dic.update({"c": c_loss})
if self.use_detail:
z_projected_d = z_projected_buf['detail']
vq_id_d = vq_id[3:]
d_loss = self.detail_vq.cal_contrastive_loss(z_projected_d, vq_id_d)
loss += d_loss
log_dic.update({"d": d_loss})
return loss, log_dic
class BrainFACodec(pl.LightningModule):
def __init__(self,
modal='fmri',
facodecldm_ckpt_path=None,
d_t=112,
num_patches=56,
hidden_dim=768,
num_clusters=64,
use_prosody=True,
use_content=True,
use_detail=True,
use_timbre=True,
use_detail_cycle_loss=True,
use_timbre_cycle_loss=True,
use_cycle_loss=False,
sup_temperature=0.2,
cycle_temperature=0.5,
pool_scale=4,
lambda_cycle=5.0,
lambda_sup=5.0,
lambda_recon=0.1,
sub_num=1,
val_sub_list=None,
test_sub_list=None,
use_clip_loss=False,
log_dir=None,
val_wav_dir=None,
test_wav_dir=None,
val_trans_path=None,
test_trans_path=None,
layout=None
):
super().__init__()
# self.multi_sub = multi_sub
# self.d_t = d_t
self.num_patches = num_patches
self.use_prosody = use_prosody
self.use_content = use_content
self.use_detail = use_detail
self.use_timbre = use_timbre
self.use_detail_cycle_loss = use_detail_cycle_loss
self.use_timbre_cycle_loss = use_timbre_cycle_loss
self.use_cycle_loss = use_cycle_loss
self.sup_temperature = sup_temperature
self.cycle_temperature = cycle_temperature
self.pool_scale = pool_scale
self.lambda_cycle = lambda_cycle
self.lambda_sup = lambda_sup
self.sub_num = sub_num
self.val_sub_list = val_sub_list
self.test_sub_list = test_sub_list
self.use_clip_loss = use_clip_loss
self.modal = modal
if self.modal == 'fmri':
self.encoder = FmriEncoder(ch2=hidden_dim, d_t=d_t)
else:
self.encoder = BrainModule(out_channels=hidden_dim, loaded_layout=layout,
subject_layers=True, n_subjects=sub_num)
self.fa_encoder, self.fa_decoder = self.init_facodec()
self.brain_rvq = BrainRVQEncoders(self.fa_decoder,
feature_dim=hidden_dim,
sub_num=sub_num,
num_clusters=num_clusters,
pool_scale=pool_scale,
temperature=sup_temperature,
use_prosody=use_prosody,
use_content=use_content,
use_detail=use_detail,
use_timbre=use_timbre,
)
# self.crm = CRM(in_channels=hidden_dim)
self.log_dir = log_dir
self.val_wav_dir = val_wav_dir
self.test_wav_dir = test_wav_dir
self.val_trans_path = val_trans_path
self.test_trans_path = test_trans_path
self.val_metrics = ["similarity", "fpc", "fad", "clap_score", "per"]
self.test_metrics = ["similarity", "fpc", "mcd", "fad", "clap_score", "per"]
self.ldm = self.init_ldm(num_patches=num_patches)
# self.audiomae = self.init_audiomae()
self.fa_encoder, self.fa_decoder = self.init_facodec()
# self.dynamic_weight = DynamicWeightedLoss(use_prosody, use_content, use_detail)
self.lambda_recon = lambda_recon
self.projector = FeatureProjector(input_seq_len=d_t, output_seq_len=num_patches, input_dim=hidden_dim)
if facodecldm_ckpt_path is not None:
self.init_projector(ckpt_path=facodecldm_ckpt_path)
# self.projector_loss = ProjectorLoss()
# target_modules = get_target_modules(self.generator.adaptor)
# prepare_model_for_lora(self.generator.adaptor, target_modules)
def configure_optimizers(self):
# return torch.optim.AdamW(self.parameters(), lr=1e-4)
# return torch.optim.Adam(self.parameters(), lr=2e-4, betas=(0.5, 0.9))
optimizer = torch.optim.Adam(self.parameters(), lr=2e-4, betas=(0.5, 0.9), weight_decay=1e-4)
scheduler = WarmupScheduler(optimizer, warmup_steps=1000)
# scheduler = WarmupExponentialDecayScheduler(optimizer, warmup_steps=self.warmup_steps)
scheduler_config = {
"scheduler": scheduler,
"interval": "step", # 每一步更新学习率
"frequency": 1, # 每步执行一次
}
return {"optimizer": optimizer, "lr_scheduler": scheduler_config}
def init_projector(self, ckpt_path):
ckpt = torch.load(ckpt_path)["state_dict"]
new_state_dict = {}
for key, value in ckpt.items():
if key.startswith('projector.'):
new_key = key[len('projector.'):] # 去掉前缀
new_state_dict[new_key] = value
self.projector.load_state_dict(new_state_dict)
for p in self.projector.parameters():
p.requires_grad = False
def init_facodec(self):
fa_encoder, fa_decoder = load_models()
for p in fa_encoder.parameters():
p.requires_grad = False
for p in fa_decoder.parameters():
p.requires_grad = False
return fa_encoder, fa_decoder
def init_audiomae(self, ckpt_path="checkpoint/audiomae/pretrained.pth"):
audiomae = models_mae.__dict__["mae_vit_base_patch16"](
in_chans=1, audio_exp=True, img_size=(1024, 128)
)
audiomae_ckpt = torch.load(ckpt_path)['model']
audiomae.load_state_dict(audiomae_ckpt, strict=False)
for p in audiomae.parameters():
p.requires_grad = False
return audiomae
def init_ldm(self, num_patches=208, ckpt_path='checkpoint/audioldm2-speech-gigaspeech.pth'):
model_config = default_audioldm_config(model_name='audioldm2-speech-gigaspeech')
latent_diffusion = LatentDiffusion(**model_config["model"]["params"])
latent_diffusion.latent_t_size = num_patches // 2
ckpt = torch.load(ckpt_path)["state_dict"]
latent_diffusion.load_state_dict(ckpt, strict=False)
latent_diffusion.use_ema = False
for p in latent_diffusion.parameters():
p.requires_grad = False
# for p in latent_diffusion.model.parameters():
# p.requires_grad = True
return latent_diffusion
def forward(self, x):
h_d, disentangled_buf = self.disentangle(x)
if self.use_timbre:
c = self.crm(h_d, disentangled_buf['timbre'])
else:
c = h_d
c = self.projector(c)
recon_audio, recon_mel = self.ldm.recon(c) # (B, F, T)
return recon_audio, recon_mel
def disentangle(self, x, subj_idx):
latent_embed = self.encoder(x, subj_idx)
h_d, disentangled_buf, _ = self.brain_rvq(latent_embed)
return h_d, disentangled_buf
def recon(self, h_d, h_t):
# if self.use_timbre:
# c = self.crm(h_d, h_t)
# else:
# c = h_d
# c = self.projector(c)
# recon_audio, recon_mel = self.ldm.recon(c) # (B, F, T)
# return recon_audio, recon_mel
recon_audio, recon_mel = self.generator.recon(h_d.transpose(1, 2), h_t)
return recon_audio, recon_mel
def generate_gt_cond(self, ta_kaldi_fbank):
emb_enc = self.audiomae.forward_encoder_no_mask(ta_kaldi_fbank)
cond_audiomae = emb_enc[:, 1:1+self.num_patches, :].detach() # remove cls
return cond_audiomae
def multi_sub_contrastive_loss(self, z_buf, audio, loss_fn, include_timbre=True):
losses = []
loss = 0.0
log_dic = {}
enc_out = self.fa_encoder(audio)
vq_post_emb, _, _, quantized, spk_embs = self.fa_decoder(enc_out, eval_vq=False, vq=True)
if self.use_prosody:
z_p = z_buf['prosody']
z_p_rec = quantized[0]
p_cycle_loss = loss_fn(z_p, z_p_rec)
loss += p_cycle_loss
# losses.append(p_cycle_loss)
log_dic.update({"p": p_cycle_loss})
if self.use_content:
z_c = z_buf['content']
z_c_rec = quantized[1]
c_cycle_loss = loss_fn(z_c, z_c_rec)
loss += c_cycle_loss
# losses.append(c_cycle_loss)
log_dic.update({"c": c_cycle_loss})
if self.use_detail and self.use_detail_cycle_loss:
z_d = z_buf['detail']
z_d_rec = quantized[2]
d_cycle_loss = loss_fn(z_d, z_d_rec)
loss += d_cycle_loss
# losses.append(d_cycle_loss)
log_dic.update({"d": d_cycle_loss})
if include_timbre and self.use_timbre and self.use_timbre_cycle_loss:
timbre_embed = z_buf['timbre']
t_cycle_loss = loss_fn(timbre_embed, spk_embs, is_timbre=True)
loss += t_cycle_loss
# losses.append(t_cycle_loss)
log_dic.update({"t": t_cycle_loss})
# loss = self.auto_weight(losses)
return loss, log_dic
def compute_cross_cycle_loss(self, encoder, decoder, z_prosody, z_content, z_detail):
"""
Compute cross-cycle consistency loss for disentangled representations.
Args:
encoder: Function to encode audio into (z_prosody, z_content, z_detail).
decoder: Function to decode (z_prosody, z_content, z_detail) into audio.
z_prosody: Tensor of shape (batch_size, time_steps, feature_dim) for prosody.
z_content: Tensor of shape (batch_size, time_steps, feature_dim) for content.
z_detail: Tensor of shape (batch_size, time_steps, feature_dim) for detail.
Returns:
cross_cycle_loss: Scalar tensor representing the cross-cycle loss.
"""
batch_size = z_prosody.size(0)
cross_cycle_loss = 0.0
# Replace prosody
for i in range(batch_size):
z_prosody_mix = z_prosody.clone()
z_prosody_mix[i] = z_prosody[(i + 1) % batch_size] # Replace with next batch's prosody
mixed_audio = decoder(z_prosody_mix, z_content, z_detail)
z_p_mix, z_c_mix, z_d_mix = encoder(mixed_audio)
cross_cycle_loss += F.mse_loss(z_prosody[i], z_p_mix[i]) # ||z_prosody_1 - z_p_mix||
# Replace content
for i in range(batch_size):
z_content_mix = z_content.clone()
z_content_mix[i] = z_content[(i + 1) % batch_size] # Replace with next batch's content
mixed_audio = decoder(z_prosody, z_content_mix, z_detail)
z_p_mix, z_c_mix, z_d_mix = encoder(mixed_audio)
cross_cycle_loss += F.mse_loss(z_content[i], z_c_mix[i]) # ||z_content_2 - z_c_mix||
# Replace detail
for i in range(batch_size):
z_detail_mix = z_detail.clone()
z_detail_mix[i] = z_detail[(i + 1) % batch_size] # Replace with next batch's detail
mixed_audio = decoder(z_prosody, z_content, z_detail_mix)
z_p_mix, z_c_mix, z_d_mix = encoder(mixed_audio)
cross_cycle_loss += F.mse_loss(z_detail[i], z_d_mix[i]) # ||z_detail_1 - z_d_mix||
return cross_cycle_loss
def compute_cosine_loss(self, z_pred, z_target, is_timbre=False, add_noise=False):
"""
计算余弦相似度损失。
参数:
- z_pred: 形状 (bs * sub, t, d)
- z_target: 形状 (bs, d, t)
- sub: 子维度 sub 的大小
返回:
- 平均余弦损失
"""
if is_timbre:
bs_sub, d = z_pred.shape
bs, d = z_target.shape
sub_num = bs_sub // bs
z_pred = z_pred.view(bs, sub_num, d).permute(1, 0, 2)
else:
bs_sub, t, d = z_pred.shape
bs, d, t = z_target.shape
sub_num = bs_sub // bs
# 重新调整形状
z_pred = z_pred.view(bs, sub_num, t, d).permute(1, 0, 2, 3).reshape(sub_num, bs * t, d)
z_target = z_target.permute(0, 2, 1).reshape(bs * t, d) # (bs*t, d)
if add_noise:
noise = torch.normal(mean=0.0, std=0.004, size=z_target.size(), device=z_target.device)
z_target = z_target.detach() + noise # * self.alpha
# 计算余弦损失
cosine_losses = []
for i in range(sub_num):
cosine_loss = 1 - F.cosine_similarity(z_pred[i], z_target, dim=1).mean()
cosine_losses.append(cosine_loss)
# 计算 sub 维度的均值
return torch.stack(cosine_losses).mean()
def sup_loss(self, z_pred, z_target):
# (bs*sub, t, d) -> (bs*sub, d, t) -> (bs*t*sub, d)
z_pred = z_pred.permute(0, 2, 1)
z_pred = F.avg_pool1d(z_pred, kernel_size=self.pool_scale, stride=self.pool_scale)
d, t = z_pred.shape[-2], z_pred.shape[-1]
z_pred = z_pred.reshape(-1, self.sub_num, d, t).permute(0, 3, 1, 2).reshape(-1, d)
# (bs, d, t) -> (bs*t, d)
d, t = z_target.shape[-2], z_target.shape[-1]
z_target = F.avg_pool1d(z_target, kernel_size=self.pool_scale, stride=self.pool_scale)
z_target = z_target.permute(0, 2, 1).reshape(-1, d)
noise = torch.normal(mean=0.0, std=0.004, size=z_target.size(), device=z_target.device)
z_target = z_target.detach() + noise # * self.alpha
bs = z_pred.shape[0] // self.sub_num
sub_num = self.sub_num
similarity_matrix = torch.cosine_similarity(
z_pred.unsqueeze(1),
z_target.unsqueeze(0),
dim=-1
) / self.sup_temperature
similarity_matrix = torch.exp(similarity_matrix)
pos_sim_index_1 = torch.arange(bs, device=similarity_matrix.device) \
.view(bs, 1).unsqueeze(1) \
.expand(bs, sub_num, 1).contiguous().view(bs * sub_num, 1)
pos_sim_1 = torch.gather(input=similarity_matrix, dim=1, index=pos_sim_index_1)
loss_1 = torch.mean(
-torch.div(pos_sim_1.sum(dim=1), similarity_matrix.sum(dim=1)).log())
pos_sim_index_2 = torch.arange(bs * sub_num, device=similarity_matrix.device).view(bs, sub_num).T
pos_sim_2 = torch.gather(input=similarity_matrix, dim=0, index=pos_sim_index_2)
loss_2 = torch.mean(
-torch.div(pos_sim_2.sum(dim=0), similarity_matrix.sum(dim=0)).log())
return loss_1 + loss_2
def cycle_loss(self, z_pred, z_target):
# (bs*sub, t, d) -> (bs*sub, d, t) -> (sub, bs*t, d)
z_pred = z_pred.permute(0, 2, 1)
z_pred = F.avg_pool1d(z_pred, kernel_size=self.pool_scale, stride=self.pool_scale)
d, t = z_pred.shape[-2], z_pred.shape[-1]
z_pred = z_pred.reshape(-1, self.sub_num, d, t).permute(1, 0, 3, 2).reshape(self.sub_num, -1, d)
# (bs*sub, d, t) -> (sub, bs*t, d)
z_target = F.avg_pool1d(z_target, kernel_size=self.pool_scale, stride=self.pool_scale)
d, t = z_target.shape[-2], z_target.shape[-1]
z_target = z_target.reshape(-1, self.sub_num, d, t).permute(1, 0, 3, 2).reshape(self.sub_num, -1, d)
bs_t = z_pred.shape[1]
cont_losses = []
for i in range(self.sub_num):
similarity_matrix = torch.cosine_similarity(
z_pred[i].unsqueeze(1),
z_target[i].unsqueeze(0),
dim=-1
) / self.sup_temperature
similarity_matrix = torch.exp(similarity_matrix)
pos_sim_index_1 = torch.arange(bs_t, device=similarity_matrix.device).view(bs_t, 1)
pos_sim_1 = torch.gather(input=similarity_matrix, dim=1, index=pos_sim_index_1)
loss_1 = torch.mean(
-torch.div(pos_sim_1.sum(dim=1), similarity_matrix.sum(dim=1)).log())
cont_losses.append(loss_1)
return torch.stack(cont_losses).mean()
def training_step(self, batch, batch_idx):
loss = 0.0
log_dic = {}
brain_data, audio, _, _, mel_padded, ta_kaldi_fbank, wav_name, subj = batch
if self.modal == 'fmri':
bs, sub_num, d, h, w = brain_data.shape
assert sub_num == self.sub_num
brain_data = brain_data.view(-1, d, h, w)
else:
bs, sub_num, ch, t = brain_data.shape
assert sub_num == self.sub_num
brain_data = brain_data.view(-1, ch, t)
subj = subj.view(-1)
subj_idx = {'subject_index': subj}
mel_padded = mel_padded.unsqueeze(1).repeat(1, sub_num, 1, 1, 1) \
.view(-1, mel_padded.shape[-3], mel_padded.shape[-2], mel_padded.shape[-1])
latent_embed = self.encoder(brain_data, subj_idx)
outs, out_buf, projected_buf = self.brain_rvq(latent_embed)
if self.use_timbre:
# h_d = self.crm(outs, out_buf['timbre'])
style = self.fa_decoder.timbre_linear(out_buf['timbre']).unsqueeze(1) # (B, 1, 2d)
gamma, beta = style.chunk(2, dim=2) # (B, 1, d)
h_d = outs
h_d = self.fa_decoder.timbre_norm(h_d)
h_d = h_d * gamma + beta
else:
h_d = outs
c = self.projector(h_d)
recon_loss, recon_mel = self.ldm(mel=mel_padded, cond=c, return_mel_recon=True)
recon_audio, recon_mel = self.ldm.mel_spectrogram_to_waveform(
recon_mel, savepath='', to_numpy=False, name='', save=False
)
recon_audio = recon_audio[:, :, :audio.shape[-1]]
loss += recon_loss * self.lambda_recon
log_dic.update({"recon_loss": recon_loss})
enc_out = self.fa_encoder(audio)
vq_post_emb, vq_id, _, quantized, spk_embs = self.fa_decoder(enc_out, eval_vq=False, vq=True)
# supervision loss
cont_loss, cont_log_dict = self.brain_rvq.cal_contrastive_loss(projected_buf, vq_id)
# cont_loss = self.dynamic_weight(cont_log_dict)
# log_dic.update(self.dynamic_weight.log_vars())
loss += cont_loss
log_dic.update({("cont_loss/" + k): float(v) for k, v in cont_log_dict.items()})
if self.use_timbre and self.use_timbre_cycle_loss:
timbre_embed = out_buf['timbre']
cosine_loss = self.compute_cosine_loss(timbre_embed, spk_embs, is_timbre=True, add_noise=True)
loss += cosine_loss
log_dic.update({"sim_loss/t": cosine_loss})
latent_loss = self.sup_loss(latent_embed, enc_out)
loss += latent_loss
log_dic.update({"cont_loss/latent": latent_loss})
self.log_dict(
{("train/" + k): float(v) for k, v in log_dic.items()},
prog_bar=True,
logger=True,
on_step=True,
on_epoch=False,
sync_dist=True
)
self.log(
"global_step",
float(self.global_step),
prog_bar=True,
logger=True,
on_step=True,
on_epoch=False,
)
lr = self.trainer.optimizers[0].param_groups[0]["lr"]
self.log(
"lr_abs",
float(lr),
prog_bar=True,
logger=True,
on_step=True,
on_epoch=False,
)
return loss
def get_validation_folder_name(self):
now = datetime.datetime.now()
timestamp = now.strftime("%m-%d-%H:%M")
return "val_%s_%s" % (
self.global_step,
timestamp,
)
def on_validation_epoch_start(self) -> None:
self.save_path = os.path.join(self.log_dir, self.get_validation_folder_name())
self.save_path_list = [os.path.join(self.save_path, sub) for sub in self.val_sub_list]
for save_path in self.save_path_list:
os.makedirs(os.path.join(save_path, 'img'), exist_ok=True)
os.makedirs(os.path.join(save_path, 'wav'), exist_ok=True)
# print("Mel save path: ", self.img_save_path)
# print("Wav save path: ", self.wav_save_path)
@torch.no_grad()
def validation_step(self, batch, batch_idx):
log_dic = {}
brain_data, audio, _, mel, _, _, wav_name, subj = batch
if self.modal == 'fmri':
bs, sub_num, d, h, w = brain_data.shape
# assert sub_num == self.sub_num
brain_data = brain_data.view(-1, d, h, w)
else:
bs, sub_num, ch, t = brain_data.shape
# assert sub_num == self.sub_num
brain_data = brain_data.view(-1, ch, t)
subj = subj.view(-1)
subj_idx = {'subject_index': subj}
mel = mel.unsqueeze(1).repeat(1, sub_num, 1, 1, 1) \
.view(-1, mel.shape[-3], mel.shape[-2], mel.shape[-1])
latent_embed = self.encoder(brain_data, subj_idx)
outs, out_buf, quantize_buf = self.brain_rvq(latent_embed)
# recon_audio = self.fa_decoder.inference(outs.transpose(1, 2), quantized_buf['timbre'])
if self.use_timbre:
# h_d = self.crm(outs, out_buf['timbre'])
style = self.fa_decoder.timbre_linear(out_buf['timbre']).unsqueeze(1) # (B, 1, 2d)
gamma, beta = style.chunk(2, dim=2) # (B, 1, d)
h_d = outs
h_d = self.fa_decoder.timbre_norm(h_d)
h_d = h_d * gamma + beta
else:
h_d = outs
c = self.projector(h_d)
recon_audio, recon_mel = self.ldm.recon(c) # (B, F, T)
recon_audio = recon_audio[:, :, :audio.shape[-1]]
recon_mel = recon_mel[:, :, :mel.shape[-2]]
# # supervision loss
# # quantized_buf['timbre'] = timbre_embed
# sup_loss, sup_log_dic = self.multi_sub_contrastive_loss(quantized_buf, audio, loss_fn=self.sup_loss)
# log_dic.update({("sup_loss/" + k): float(v) for k, v in sup_log_dic.items()})
#
# # cycle loss
# # quantized_buf['timbre'] = timbre_embed
# cycle_loss, cycle_log_dic = self.multi_sub_contrastive_loss(quantized_buf, recon_audio, loss_fn=self.cycle_loss)
# log_dic.update({("cycle_loss/" + k): float(v) for k, v in cycle_log_dic.items()})
# sim loss
sim_loss, sim_log_dic = self.multi_sub_contrastive_loss(out_buf, audio, loss_fn=self.compute_cosine_loss)
log_dic.update({("sim_loss/" + k): float(v) for k, v in sim_log_dic.items()})
self.log_dict(
{("val/" + k): float(v) for k, v in log_dic.items()},
prog_bar=True,
logger=True,
on_step=False,
on_epoch=True,
sync_dist=True
)
# recon_mel = torch.cat([mel_spectrogram(recon_audio_seg) for recon_audio_seg in recon_audio])
recon_audio = recon_audio.cpu().detach().numpy()
mel = mel[:, 0, :, :].permute(0, 2, 1).cpu().detach().numpy() # (B, F, T)
recon_mel = recon_mel.cpu().detach().numpy() # (B, F, T)
for i in range(len(wav_name)):
sample_name = wav_name[i]
for sub_id, save_path in enumerate(self.save_path_list):
img_save_name = os.path.join(save_path, 'img', sample_name + '.png')
plot_mel(mel[i * sub_num + sub_id], recon_mel[i * sub_num + sub_id], fig_path=img_save_name)
wav_save_name = os.path.join(save_path, 'wav', sample_name + '.wav')
save_wave(recon_audio[i * sub_num + sub_id], wav_path=wav_save_name)
# else:
# sample_name = wav_name[i]
# img_save_name = os.path.join(self.img_save_path, sample_name + '.png')
# plot_mel(mel[i], recon_mel[i], fig_path=img_save_name)
# wav_save_name = os.path.join(self.wav_save_path, sample_name + '.wav')
# save_wave(recon_audio[i], wav_path=wav_save_name)
def on_validation_epoch_end(self) -> None:
values_dict = defaultdict(list)
for save_path in self.save_path_list:
print('Calculate Metrics:', save_path)
result = calc_metric(
self.val_wav_dir,
os.path.join(save_path, 'wav'),
save_path,
self.val_metrics,
fs=16000,
method="dtw",
db_scale=True,
need_mean=True,
model_name="wavlm",
similarity_mode="pairwith",
ltr_path="None",
intelligibility_mode="gt_audio",
language="english",
)
for key, value in result.items():
values_dict[key].append(value)
mean_dict = {key: sum(values) / len(values) for key, values in values_dict.items()}
data = json.dumps(mean_dict, indent=4)
with open(os.path.join(self.save_path, "result.json"), "w", newline="\n") as f:
f.write(data)
self.log_dict(
{("val/metric/" + k): v for k, v in mean_dict.items()},
prog_bar=True,
logger=True,
on_step=False,
on_epoch=True,
sync_dist=True
)
def on_test_epoch_start(self) -> None:
self.test_save_path = os.path.join(self.log_dir, "test")
self.test_save_path_list = [os.path.join(self.test_save_path, sub) for sub in self.test_sub_list]
for save_path in self.test_save_path_list:
os.makedirs(os.path.join(save_path, 'img'), exist_ok=True)
os.makedirs(os.path.join(save_path, 'wav'), exist_ok=True)
# print("Mel save path: ", self.test_img_save_path)
# print("Wav save path: ", self.test_wav_save_path)
@torch.no_grad()
def test_step(self, batch, batch_idx):
brain_data, audio, _, mel, _, _, wav_name, subj = batch
if self.modal == 'fmri':
bs, sub_num, d, h, w = brain_data.shape
assert sub_num == len(self.test_sub_list)
brain_data = brain_data.view(-1, d, h, w)
else:
bs, sub_num, ch, t = brain_data.shape
assert sub_num == len(self.test_sub_list)
brain_data = brain_data.view(-1, ch, t)
subj = subj.view(-1)
subj_idx = {'subject_index': subj}
mel = mel.unsqueeze(1).repeat(1, sub_num, 1, 1, 1) \
.view(-1, mel.shape[-3], mel.shape[-2], mel.shape[-1])
latent_embed = self.encoder(brain_data, subj_idx)
outs, out_buf, quantize_buf = self.brain_rvq(latent_embed)
# recon_audio = self.fa_decoder.inference(outs.transpose(1, 2), quantized_buf['timbre'])
if self.use_timbre:
# h_d = self.crm(outs, out_buf['timbre'])
style = self.fa_decoder.timbre_linear(out_buf['timbre']).unsqueeze(1) # (B, 1, 2d)
gamma, beta = style.chunk(2, dim=2) # (B, 1, d)
h_d = outs
h_d = self.fa_decoder.timbre_norm(h_d)
h_d = h_d * gamma + beta
else:
h_d = outs
c = self.projector(h_d)
recon_audio, recon_mel = self.ldm.recon(c) # (B, F, T)
recon_audio = recon_audio[:, :, :audio.shape[-1]]
recon_mel = recon_mel[:, :, :mel.shape[-2]]