-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraining.py
More file actions
46 lines (35 loc) · 1.5 KB
/
training.py
File metadata and controls
46 lines (35 loc) · 1.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
"""
Authors: Massimiliano Todisco, Michele Panariello and chatGPT
Email: https://mailhide.io/e/Qk2FFM4a
Date: August 2024
"""
import torch
from models import CosineDistanceLoss
def train(model, batch_a, batch_b, optimizer, writer, epoch, batch_idx, total_batches, model_ecapa, feature_extractor_campp, model_campp, device, f_A):
model.train()
optimizer.zero_grad()
embedding_a = []
embedding_b = []
embeddings_a = []
embeddings_b = []
for audio_a, audio_b in zip(batch_a, batch_b):
if f_A == 'ecapa':
embedding_a = audio_a.unsqueeze(0).unsqueeze(0)
processed_b = model(audio_b.to(device).unsqueeze(1)).squeeze(1)
embedding_b = model_ecapa.encode_batch(processed_b)
elif f_A == 'campp':
embedding_a = audio_a.unsqueeze(0).unsqueeze(0)
processed_audio = model(audio_b.to(device).unsqueeze(0)).squeeze(0)
mel = feature_extractor_campp(processed_audio).unsqueeze(0)
embedding_b = model_campp(mel).unsqueeze(0)
embeddings_a.append(embedding_a)
embeddings_b.append(embedding_b)
embeddings_a = torch.cat(embeddings_a, dim=0).squeeze(1).to(device)
embeddings_b = torch.cat(embeddings_b, dim=0).squeeze(1).to(device)
loss_fn = CosineDistanceLoss()
loss = loss_fn(embeddings_a, embeddings_b)
loss.backward()
optimizer.step()
global_step = epoch * total_batches + batch_idx
writer.add_scalar('Loss/train', loss.item(), global_step)
return loss.item()