-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmuse-project.py
450 lines (358 loc) · 17.2 KB
/
muse-project.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
import os
import pandas as pd
import numpy as np
import torch
import torch.nn as nn
from sklearn.metrics import classification_report, balanced_accuracy_score
import joblib
from torch.utils.data import DataLoader
from tqdm import tqdm
import warnings
from backbone import resnet18
from model import MultimodalClassifier
from dataset import MELDDataset
from utils import plot_metrics, analyze_results_per_class
def process_task(model, a, t, classifier, weight_size, labels, criterion, device='cuda'):
softmax = nn.Softmax(dim=1)
fused = torch.cat([a, t], dim=-1)
# --- Compute logits ---
logits_fused = classifier(fused)
# 2) Audio-only
logits_a = (
torch.mm(a, torch.transpose(classifier.weight[:, :weight_size / 2], 0, 1))
+ classifier.bias / 2
)
# 3) Text-only
logits_t = (
torch.mm(t, torch.transpose(classifier.weight[:, weight_size / 2 :], 0, 1))
+ classifier.bias / 2
)
# --- Compute loss ---
loss = criterion(logits_fused, labels)
# --- Predictions ---
preds_fused = torch.argmax(softmax(logits_fused), dim=1).detach().cpu().numpy()
preds_audio = torch.argmax(softmax(logits_a), dim=1).detach().cpu().numpy()
preds_text = torch.argmax(softmax(logits_t), dim=1).detach().cpu().numpy()
return loss, preds_fused, preds_audio, preds_text
def train_one_epoch(model, dataloader, optimizer, criterions, emotion_reg=0.6, sentiment_reg=0.4, device='cuda'):
model.train()
# tracked measures
losses = {'emotion': 0.0, 'sentiment': 0.0}
metrics = {'emotion': {'fused': [], 'audio': [], 'text': [], 'labels': []},
'sentiment': {'fused': [], 'audio': [], 'text': [], 'labels': []}}
# Wrap dataloader with tqdm
loop = tqdm(dataloader, desc="Training", leave=False)
for audio_arrays, texts, emotion_labels, sentiment_labels in loop:
# Move data to device
audio_arrays = audio_arrays.to(device)
emotion_labels = emotion_labels.to(device)
sentiment_labels = sentiment_labels.to(device)
optimizer.zero_grad()
# Forward pass for audio and text
a, t = model(audio_arrays, texts)
# EMOTION TASK
emotion_loss, e_fused, e_audio, e_text = process_task(
model=model,
a=a,
t=t,
classifier=model.emotion_classifier,
weight_size=model.emotion_classifier.weight.size(1),
labels=emotion_labels,
criterion=criterions['emotion']
)
losses['emotion'] += emotion_loss.item()
metrics['emotion']['fused'].extend(e_fused)
metrics['emotion']['audio'].extend(e_audio)
metrics['emotion']['text'].extend(e_text)
metrics['emotion']['labels'].extend(emotion_labels.cpu().numpy())
# SENTIMENT TASK
sentiment_loss, s_fused, s_audio, s_text = process_task(
model=model,
a=a,
t=t,
classifier=model.sentiment_classifier,
weight_size=model.sentiment_classifier.weight.size(1),
labels=sentiment_labels,
criterion=criterions['sentiment']
)
losses['sentiment'] += sentiment_loss.item()
metrics['sentiment']['fused'].extend(s_fused)
metrics['sentiment']['audio'].extend(s_audio)
metrics['sentiment']['text'].extend(s_text)
metrics['sentiment']['labels'].extend(sentiment_labels.cpu().numpy())
# Backprop on weighted loss
combined_loss = emotion_reg * emotion_loss + sentiment_reg * sentiment_loss
combined_loss.backward()
optimizer.step()
# Average losses
losses['emotion'] /= len(dataloader)
losses['sentiment'] /= len(dataloader)
# compute metrics per modality
emotion_metrics = {
'fused': compute_metrics(metrics['emotion']['labels'], metrics['emotion']['fused']),
'audio': compute_metrics(metrics['emotion']['labels'], metrics['emotion']['audio']),
'text': compute_metrics(metrics['emotion']['labels'], metrics['emotion']['text']),
}
sentiment_metrics = {
'fused': compute_metrics(metrics['sentiment']['labels'], metrics['sentiment']['fused']),
'audio': compute_metrics(metrics['sentiment']['labels'], metrics['sentiment']['audio']),
'text': compute_metrics(metrics['sentiment']['labels'], metrics['sentiment']['text']),
}
return losses, {'emotion': emotion_metrics, 'sentiment': sentiment_metrics}
def validate_one_epoch(model, dataloader, criterions, device='cuda'):
model.eval()
losses = {'emotion': 0.0, 'sentiment': 0.0}
metrics = {'emotion': {'fused': [], 'audio': [], 'text': [], 'labels': []},
'sentiment': {'fused': [], 'audio': [], 'text': [], 'labels': []}}
with torch.no_grad():
# Also wrap validation dataloader with tqdm
loop = tqdm(dataloader, desc="Validation", leave=False)
for audio_arrays, texts, emotion_labels, sentiment_labels in loop:
audio_arrays = audio_arrays.to(device)
emotion_labels = emotion_labels.to(device)
sentiment_labels = sentiment_labels.to(device)
a, t = model(audio_arrays, texts)
emotion_loss, e_fused, e_audio, e_text = process_task(
model=model,
a=a,
t=t,
classifier=model.emotion_classifier,
weight_size=model.emotion_classifier.weight.size(1),
labels=emotion_labels,
criterion=criterions['emotion']
)
losses['emotion'] += emotion_loss.item()
metrics['emotion']['fused'].extend(e_fused)
metrics['emotion']['audio'].extend(e_audio)
metrics['emotion']['text'].extend(e_text)
metrics['emotion']['labels'].extend(emotion_labels.cpu().numpy())
sentiment_loss, s_fused, s_audio, s_text = process_task(
model=model,
a=a,
t=t,
classifier=model.sentiment_classifier,
weight_size=model.sentiment_classifier.weight.size(1),
labels=sentiment_labels,
criterion=criterions['sentiment']
)
losses['sentiment'] += sentiment_loss.item()
metrics['sentiment']['fused'].extend(s_fused)
metrics['sentiment']['audio'].extend(s_audio)
metrics['sentiment']['text'].extend(s_text)
metrics['sentiment']['labels'].extend(sentiment_labels.cpu().numpy())
losses['emotion'] /= len(dataloader)
losses['sentiment'] /= len(dataloader)
emotion_metrics = {
'fused': compute_metrics(metrics['emotion']['labels'], metrics['emotion']['fused']),
'audio': compute_metrics(metrics['emotion']['labels'], metrics['emotion']['audio']),
'text': compute_metrics(metrics['emotion']['labels'], metrics['emotion']['text']),
}
sentiment_metrics = {
'fused': compute_metrics(metrics['sentiment']['labels'], metrics['sentiment']['fused']),
'audio': compute_metrics(metrics['sentiment']['labels'], metrics['sentiment']['audio']),
'text': compute_metrics(metrics['sentiment']['labels'], metrics['sentiment']['text']),
}
return losses, {'emotion': emotion_metrics, 'sentiment': sentiment_metrics}
def compute_metrics(true_labels, predictions):
"""
Compute classification metrics including accuracy, per-class F1 scores, and weighted average F1 score.
"""
# Compute overall accuracy
overall_accuracy = balanced_accuracy_score(true_labels, predictions)
# Compute F1 scores
report = classification_report(
true_labels, predictions, output_dict=True, zero_division=0
)
per_class_f1 = {label: values["f1-score"] for label, values in report.items() if label.isdigit()}
macro_f1 = report["macro avg"]["f1-score"]
# Compile metrics into a dictionary
metrics = {
"balanced_acc": overall_accuracy,
"macro_f1": macro_f1,
"per_class_f1": per_class_f1,
}
return metrics
def train_and_validate(model, train_loader, val_loader, optimizer, criterions, num_epochs, experiment_name, device='cuda', save_dir='./results'):
"""
Train and validate the model for multiple epochs, and display results in a summary table at the end.
:param model: The model to train.
:param train_dataloader: Training DataLoader.
:param val_dataloader: Validation DataLoader.
:param optimizer: Optimizer.
:param criterion: Loss function.
:param num_epochs: Number of epochs.
:param model_name: Name of the model (for display in results).
:param device: Device to use for training ('cuda' or 'cpu').
"""
os.makedirs(save_dir, exist_ok=True)
results = {
'experiment_name': experiment_name,
'model_state_dict': None,
'results_emotions': [],
'results_sentiments': []
}
for epoch in range(num_epochs):
train_losses, train_metrics = train_one_epoch(model, train_loader, optimizer, criterions, device=device)
val_losses, val_metrics = validate_one_epoch(model, val_loader, criterions, device=device)
print(f"Epoch [{epoch+1}/{num_epochs}]")
for task in ['emotion', 'sentiment']:
print(f"\t{task.capitalize()} Train Loss: {train_losses[task]:.4f}")
print(f"\t{task.capitalize()} Val Loss: {val_losses[task]:.4f}")
for modality in ['fused', 'audio', 'text']:
train_acc = train_metrics[task][modality]['balanced_acc'] * 100
val_acc = val_metrics[task][modality]['balanced_acc'] * 100
train_f1 = train_metrics[task][modality]['macro_f1'] * 100
val_f1 = val_metrics[task][modality]['macro_f1'] * 100
print(f"\t\t{modality.capitalize()} Train Balanced Acc: {train_acc:.2f}%, Train F1 (macro): {train_f1:.2f}%")
print(f"\t\t{modality.capitalize()} Val Balanced Acc: {val_acc:.2f}%, Val F1 (macro): {val_f1:.2f}%")
print('\n')
print("---------------------------------------------------------------\n")
# Save results for both tasks
results['results_emotions'].append({
'epoch': epoch + 1,
'train_loss': train_losses['emotion'],
'val_loss': val_losses['emotion'],
'train_metrics': train_metrics['emotion'],
'val_metrics': val_metrics['emotion']
})
results['results_sentiments'].append({
'epoch': epoch + 1,
'train_loss': train_losses['sentiment'],
'val_loss': val_losses['sentiment'],
'train_metrics': train_metrics['sentiment'],
'val_metrics': val_metrics['sentiment']
})
# Save model state
results['model_state_dict'] = model.state_dict()
results['optimizer'] = optimizer
# save all the results as pkl file
results_joblib_path = os.path.join(save_dir, f"{experiment_name}_results.pkl")
joblib.dump(results, results_joblib_path)
# display the summary of the training as a Datframe (a table per task)
# print('\n\n SUMMARY OF THE RESULTS\n')
# for task in ['emotions', 'sentiments']:
# print(f'Results for the {task} task:')
# display(task_result_to_table(results[f'results_{task}_bsize{train_dataloader.batch_size}']))
# print(f"Results saved as Joblib file: {results_joblib_path}.")
# Display final results in a table
return model, results
def test_inference(model, test_loader, criterions, experiment_name, device='cuda'):
"""
Perform inference on the test set and save the true and predicted labels to a file.
:param model: The trained model.
:param test_loader: DataLoader for the test set (the embedding one)
:param criterions: Dictionary of loss functions for each task.
:param save_path: Path to save the true and predicted labels.
:param device: Device to use for inference ('cuda' or 'cpu').
"""
model.eval()
true_and_pred_labels = {
'emotion': {'true': [], 'pred': []},
'sentiment': {'true': [], 'pred': []},
}
with torch.no_grad():
for audio_arrays, texts, emotion_labels, sentiment_labels in test_loader:
audio_arrays = audio_arrays.to(device)
emotion_labels = emotion_labels.to(device)
sentiment_labels = sentiment_labels.to(device)
a, t = model(audio_arrays, texts)
# ------------------- EMOTION TASK -------------------
_, e_fused, _, _ = process_task(
model=model, a=a, t=t, classifier=model.emotion_classifier,
weight_size=model.emotion_classifier.weight.size(1),
labels=emotion_labels, criterion=criterions['emotion']
)
true_and_pred_labels['emotion']['true'].extend(emotion_labels.cpu().numpy())
true_and_pred_labels['emotion']['pred'].extend(e_fused)
# ------------------ SENTIMENT TASK ------------------
_, s_fused, _, _ = process_task(
model=model, a=a, t=t, classifier=model.sentiment_classifier,
weight_size=model.sentiment_classifier.weight.size(1),
labels=sentiment_labels, criterion=criterions['sentiment']
)
true_and_pred_labels['sentiment']['true'].extend(sentiment_labels.cpu().numpy())
true_and_pred_labels['sentiment']['pred'].extend(s_fused)
# save all the results as pkl file
results_path = os.path.join(
'test_inference_results',
f"{experiment_name}_test_inference_results.pkl"
)
joblib.dump(true_and_pred_labels, results_path)
return true_and_pred_labels
def main():
torch.manual_seed(42)
warnings.filterwarnings("ignore", category=UserWarning, module="transformers")
warnings.filterwarnings("ignore", category=UserWarning, module="torch")
device = 'cuda'
print("Loading data...")
train_set = MELDDataset(csv_file="train_sent_emo.csv", root_dir="./meld-train-muse", mode="train")
dev_set = MELDDataset(csv_file="dev_sent_emo.csv", root_dir="./meld-dev-muse", mode="dev")
test_set = MELDDataset(csv_file='test_sent_emo.csv', root_dir='./meld-test-muse', mode='test')
print("Data loaded.")
train_loader = DataLoader(train_set, batch_size=64, shuffle=True, num_workers=16)
dev_loader = DataLoader(dev_set, batch_size=64, shuffle=True, num_workers=16)
test_loader = DataLoader(test_set, batch_size=64, shuffle=True, num_workers=16)
print("Data loaders created.")
audio_model_name = "facebook/wav2vec2-base"
text_model_name = "bert-base-uncased"
# Gather all emotion labels in the train set
emotion_labels = [sample[2] for sample in train_set.samples]
# Count how many samples of each class (returns unique labels and their counts)
unique_classes, counts = np.unique(emotion_labels, return_counts=True)
print("Class labels:", unique_classes)
print("Class counts:", counts)
_, str_to_int = train_set.get_emotions_dicts()
num_classes = len(str_to_int)
ordered_counts = [0] * num_classes
for class_label, count in zip(unique_classes, counts):
class_idx = str_to_int[class_label] # Convert 'neutral' → 0, 'joy' → 1, etc.
ordered_counts[class_idx] = count
ordered_counts = np.array(ordered_counts)
print("Ordered counts:", ordered_counts)
# Avoid division by zero
inverse_freq = 1.0 / np.maximum(ordered_counts, 1)
emotions_class_weights = torch.tensor(inverse_freq, dtype=torch.float32)
# or normalize them
emotions_class_weights = emotions_class_weights / emotions_class_weights.sum() #* num_classes
print("Class weights:", emotions_class_weights)
emotions_class_weights = emotions_class_weights.to(device)
lr = 0.0001
criterions = {
'emotion': nn.CrossEntropyLoss(weight=emotions_class_weights),
'sentiment': nn.CrossEntropyLoss()
}
num_epochs = 5
num_emotions = len(train_set.get_emotions_dicts()[0].values())
num_sentiments = len(train_set.get_sentiments_dicts()[0].values())
model = MultimodalClassifier(
text_model_name=text_model_name,
text_fine_tune=True,
unfreeze_last_n_text=6,
audio_encoder=resnet18(modality='audio'),
num_emotions=num_emotions,
num_sentiments=num_sentiments
).to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=lr)
experiment_name = 'TEST'
print("Training model...")
model, results = train_and_validate(
model, train_loader, dev_loader,
optimizer, criterions, num_epochs,
experiment_name=experiment_name,
device='cuda', save_dir='./saved_results'
)
print("Training complete.")
#load from saved_results
loaded_results = joblib.load('./saved_results/TEST_results.pkl')
plot_metrics(loaded_results, metric='macro_f1', modality='fused', task='emotions')
true_and_pred_labels = test_inference(model, test_loader, criterions, experiment_name)
for mode in ['confusion_matrix', 'classification_report', 'roc_curve']:
analyze_results_per_class(
true_and_pred_labels['emotion']['true'],
true_and_pred_labels['emotion']['pred'],
unique_classes,
task_name="Emotions",
mode=mode
)
if __name__ == "__main__":
main()