-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval.py
More file actions
370 lines (288 loc) · 12.6 KB
/
eval.py
File metadata and controls
370 lines (288 loc) · 12.6 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
import os
import json
import glob
import argparse
import logging
import numpy as np
import pandas as pd
import pyarrow.parquet as pq
from datetime import datetime
from tqdm import tqdm
from scipy import stats
import torch
import torch.nn as nn
# CONFIG
NUM_EXAMPLES = 10
POPULARITY_TASKS = ["score_streams", "score_likes"]
SONGEVAL_TASKS = ["coherence", "musicality", "memorability", "clarity", "naturalness"]
ALL_TASKS = POPULARITY_TASKS + SONGEVAL_TASKS
# MODEL
class SharedBlock(nn.Module):
def __init__(self, in_dim, out_dim, dropout):
super().__init__()
self.block = nn.Sequential(
nn.Linear(in_dim, out_dim),
nn.BatchNorm1d(out_dim),
nn.GELU(),
nn.Dropout(dropout)
)
def forward(self, x):
return self.block(x)
class BranchBlock(nn.Module):
def __init__(self, in_dim, out_dim, dropout, use_bn=True):
super().__init__()
layers = [nn.Linear(in_dim, out_dim)]
if use_bn:
layers.append(nn.BatchNorm1d(out_dim))
layers += [nn.GELU(), nn.Dropout(dropout)]
self.block = nn.Sequential(*layers)
def forward(self, x):
return self.block(x)
class TaskBranch(nn.Module):
def __init__(self, scale, shift):
super().__init__()
self.branch = nn.Sequential(
BranchBlock(256, 128, dropout=0.1, use_bn=True),
BranchBlock(128, 64, dropout=0.1, use_bn=True),
nn.Linear(64, 1)
)
self.scale = scale
self.shift = shift
def forward(self, x):
return torch.sigmoid(self.branch(x)) * self.scale + self.shift
class apexMLP(nn.Module):
def __init__(self, task, n_shared):
super().__init__()
self.task = task
if n_shared == 2:
self.shared = nn.Sequential(
SharedBlock(768, 512, dropout=0.3),
SharedBlock(512, 256, dropout=0.3)
)
elif n_shared == 3:
self.shared = nn.Sequential(
SharedBlock(768, 512, dropout=0.3),
SharedBlock(512, 384, dropout=0.3),
SharedBlock(384, 256, dropout=0.3)
)
else:
raise ValueError(f"n_shared must be 2 or 3, got {n_shared}")
self.branch_score_streams = TaskBranch(scale=100, shift=0)
self.branch_score_likes = TaskBranch(scale=100, shift=0)
if task == "full":
self.branch_coherence = TaskBranch(scale=4, shift=1)
self.branch_musicality = TaskBranch(scale=4, shift=1)
self.branch_memorability = TaskBranch(scale=4, shift=1)
self.branch_clarity = TaskBranch(scale=4, shift=1)
self.branch_naturalness = TaskBranch(scale=4, shift=1)
def forward(self, x):
shared = self.shared(x)
out = {
"score_streams": self.branch_score_streams(shared).squeeze(1),
"score_likes" : self.branch_score_likes(shared).squeeze(1),
}
if self.task == "full":
out["coherence"] = self.branch_coherence(shared).squeeze(1)
out["musicality"] = self.branch_musicality(shared).squeeze(1)
out["memorability"] = self.branch_memorability(shared).squeeze(1)
out["clarity"] = self.branch_clarity(shared).squeeze(1)
out["naturalness"] = self.branch_naturalness(shared).squeeze(1)
return out
# LOAD TEST DATA
def load_test_data(test_folder, mode, task):
files = sorted(glob.glob(os.path.join(test_folder, "*.parquet")))
dfs = []
for f in tqdm(files, desc="Loading test parquets"):
dfs.append(pq.read_table(f).to_pandas())
df = pd.concat(dfs, ignore_index=True)
logger.info(f"Loaded {len(df):,} segments | {df['audio_id'].nunique():,} unique songs")
if mode == "song":
logger.info("Aggregating segments to song level...")
df = aggregate_to_song(df)
logger.info(f"Aggregated to {len(df):,} songs")
return df
def aggregate_to_song(df):
return df.groupby(["audio_id", "platform"]).agg(
segment_embedding = ("segment_embedding", lambda x: np.stack(x.values).mean(axis=0)),
score_streams = ("score_streams", "first"),
score_likes = ("score_likes", "first"),
songeval_scores = ("songeval_scores", "first")
).reset_index()
# RUN INFERENCE
def run_inference(model, df, task, device, batch_size=512):
model.eval()
embeddings = np.stack(df["segment_embedding"].values).astype(np.float32)
all_preds = {t: [] for t in (POPULARITY_TASKS if task == "popularity" else ALL_TASKS)}
with torch.no_grad():
for start in tqdm(range(0, len(embeddings), batch_size), desc="Inference"):
batch = torch.tensor(embeddings[start:start + batch_size]).to(device)
preds = model(batch)
for t in all_preds:
all_preds[t].extend(preds[t].cpu().numpy().tolist())
return {t: np.array(v) for t, v in all_preds.items()}
# AGGREGATE SEGMENT PREDICTIONS TO SONG LEVEL
def aggregate_segment_predictions(df, preds):
df = df.copy()
for t, v in preds.items():
df[f"pred_{t}"] = v
return df.groupby(["audio_id", "platform"]).agg(
score_streams = ("score_streams", "first"),
score_likes = ("score_likes", "first"),
songeval_scores = ("songeval_scores", "first"),
**{f"pred_{t}": (f"pred_{t}", "mean") for t in preds.keys()}
).reset_index()
# GET GROUND TRUTH
def get_ground_truth(df, task):
gt = {
"score_streams": df["score_streams"].values.astype(np.float32),
"score_likes" : df["score_likes"].values.astype(np.float32),
}
if task == "full":
songeval = df["songeval_scores"].apply(
lambda x: x if isinstance(x, dict) else json.loads(x)
)
gt["coherence"] = songeval.apply(lambda x: x["coherence"]).values.astype(np.float32)
gt["musicality"] = songeval.apply(lambda x: x["musicality"]).values.astype(np.float32)
gt["memorability"] = songeval.apply(lambda x: x["memorability"]).values.astype(np.float32)
gt["clarity"] = songeval.apply(lambda x: x["clarity"]).values.astype(np.float32)
gt["naturalness"] = songeval.apply(lambda x: x["naturalness"]).values.astype(np.float32)
return gt
# COMPUTE METRICS
def compute_metrics(actual, predicted):
return {
"mse" : float(np.mean((actual - predicted) ** 2)),
"mae" : float(np.mean(np.abs(actual - predicted))),
"pearson" : float(stats.pearsonr(actual, predicted)[0]),
"spearman": float(stats.spearmanr(actual, predicted)[0])
}
# GET EXAMPLES
def get_examples(df, actual, predicted, n=10):
temp = pd.DataFrame({
"audio_id" : df["audio_id"].values,
"platform" : df["platform"].values,
"actual" : actual,
"predicted": predicted
})
sorted_df = temp.sort_values("actual", ascending=False)
top_n = sorted_df.head(n)
bottom_n = sorted_df.tail(n).sort_values("actual", ascending=True)
def to_records(d):
return [
{
"audio_id" : row["audio_id"],
"platform" : row["platform"],
"actual" : round(float(row["actual"]), 4),
"predicted": round(float(row["predicted"]), 4),
"error" : round(float(abs(row["actual"] - row["predicted"])), 4)
}
for _, row in d.iterrows()
]
return {
"top_10_highest": to_records(top_n),
"top_10_lowest" : to_records(bottom_n)
}
# PRINT METRICS
def print_metrics(metrics, mode, task):
tasks = POPULARITY_TASKS if task == "popularity" else ALL_TASKS
print(f"\n{'-'*60}")
print(f" EVALUATION RESULTS | mode={mode} | task={task}")
print(f"{'─'*60}")
print(f" {'Task':<20} {'MSE':>10} {'MAE':>10} {'Pearson':>10} {'Spearman':>10}")
print(f" {'-'*20} {'-'*10} {'-'*10} {'-'*10} {'-'*10}")
for t in tasks:
m = metrics[t]
print(f" {t:<20} {m['mse']:>10.4f} {m['mae']:>10.4f} {m['pearson']:>10.4f} {m['spearman']:>10.4f}")
print(f"{'-'*60}\n")
# MAIN
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="APEX Evaluation Script")
parser.add_argument("--checkpoint", type=str, required=True, help="Path to checkpoint file")
parser.add_argument("--test_folder", type=str, required=True, help="Path to test parquet folder")
parser.add_argument("--results_folder", type=str, default="eval_results", help="Path to save results")
args = parser.parse_args()
os.makedirs(args.results_folder, exist_ok=True)
# Setup logging
log_file = os.path.join(args.results_folder, "eval.log")
logging.basicConfig(
level = logging.INFO,
format = "%(asctime)s [%(levelname)s] %(message)s",
handlers = [
logging.FileHandler(log_file),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
# Load checkpoint — auto detect all settings
logger.info(f"Loading checkpoint: {args.checkpoint}")
checkpoint = torch.load(args.checkpoint, map_location="cpu")
mode = checkpoint["mode"]
task = checkpoint["task"]
n_shared = checkpoint.get("shared", 2) # default 2 for old checkpoints
loss = checkpoint.get("loss", "equal")
logger.info(f"Auto-detected — mode: {mode} | task: {task} | shared: {n_shared} | loss: {loss}")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
logger.info(f"Using device: {device}")
# Load model
model = apexMLP(task=task, n_shared=n_shared)
model.load_state_dict(checkpoint["model_state"])
model.to(device)
model.eval()
logger.info(f"Model loaded from epoch {checkpoint['epoch']} | Val Loss: {checkpoint['val_loss']:.4f}")
# Load test data
df = load_test_data(args.test_folder, mode, task)
preds = run_inference(model, df, task, device)
# Aggregate if segment mode
if mode == "segment":
logger.info("Aggregating segment predictions to song level...")
song_df = aggregate_segment_predictions(df, preds)
eval_df = song_df
tasks = POPULARITY_TASKS if task == "popularity" else ALL_TASKS
preds = {t: song_df[f"pred_{t}"].values for t in tasks}
else:
eval_df = df
# Ground truth
gt = get_ground_truth(eval_df, task)
tasks = POPULARITY_TASKS if task == "popularity" else ALL_TASKS
# Compute metrics
metrics = {}
for t in tasks:
metrics[t] = compute_metrics(gt[t], preds[t])
logger.info(f"{t}: MSE={metrics[t]['mse']:.4f} | MAE={metrics[t]['mae']:.4f} | Pearson={metrics[t]['pearson']:.4f} | Spearman={metrics[t]['spearman']:.4f}")
print_metrics(metrics, mode, task)
# Examples
examples = {}
for t in tasks:
examples[t] = get_examples(eval_df, gt[t], preds[t], n=NUM_EXAMPLES)
# Print examples
for t in tasks:
print(f"\n── {t} — Top 10 Highest ────────────────────────────")
print(f" {'audio_id':<40} {'platform':<8} {'actual':>8} {'predicted':>10} {'error':>8}")
print(f" {'─'*40} {'─'*8} {'─'*8} {'─'*10} {'─'*8}")
for ex in examples[t]["top_10_highest"]:
print(f" {ex['audio_id']:<40} {ex['platform']:<8} {ex['actual']:>8.4f} {ex['predicted']:>10.4f} {ex['error']:>8.4f}")
print(f"\n── {t} — Top 10 Lowest ─────────────────────────────")
print(f" {'audio_id':<40} {'platform':<8} {'actual':>8} {'predicted':>10} {'error':>8}")
print(f" {'─'*40} {'─'*8} {'─'*8} {'─'*10} {'─'*8}")
for ex in examples[t]["top_10_lowest"]:
print(f" {ex['audio_id']:<40} {ex['platform']:<8} {ex['actual']:>8.4f} {ex['predicted']:>10.4f} {ex['error']:>8.4f}")
# Save results
results = {
"checkpoint" : args.checkpoint,
"mode" : mode,
"task" : task,
"n_shared" : n_shared,
"loss" : loss,
"epoch" : checkpoint["epoch"],
"val_loss" : checkpoint["val_loss"],
"evaluated_at" : datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"num_songs" : int(eval_df["audio_id"].nunique()),
"metrics" : metrics,
"examples" : examples
}
results_path = os.path.join(
args.results_folder,
f"eval_results_mode-{mode}_task-{task}_shared-{n_shared}_loss-{loss}.json"
)
with open(results_path, "w") as f:
json.dump(results, f, indent=2)
logger.info(f"Results saved to {results_path}")