| import torch, json, os, sys |
| import numpy as np |
| from collections import defaultdict |
| import warnings |
| warnings.filterwarnings('ignore') |
|
|
| os.chdir('/weka/scratch/melhila1/karan/EMMA2_text_conditioning_contextual') |
|
|
| BASE = 'experiments_final' |
|
|
| MODELS = [ |
| ('combined_v1', 'combined_v1'), |
| ('no_TSDL', 'no_TSDL_old_mixtures'), |
| ] |
|
|
| TEST_SETS = [ |
| ('In-Domain (old)', 'eval_outputs_test_3k'), |
| ('OOD Backgrounds', 'eval_outputs_OOD_backgrounds'), |
| ('OOD Both', 'eval_outputs_OOD_both'), |
| ('OOD Distractors', 'eval_outputs_OOD_distractors'), |
| ] |
|
|
| |
| ALL_METRICS = [ |
| 'scale_invariant_signal_noise_ratio', |
| 'signal_noise_ratio', |
| 'si_snr', |
| 'td_loss', |
| 'delta_ILD', |
| 'delta_ITD', |
| 'delta_ITD_gcc', |
| 'spatial_clap_score', |
| 'msclap_score', |
| ] |
| METRIC_NAMES = { |
| 'scale_invariant_signal_noise_ratio': 'SI-SNRi(dB)', |
| 'signal_noise_ratio': 'SNRi(dB)', |
| 'si_snr': 'SI-SNR(dB)', |
| 'td_loss': 'TD Loss', |
| 'delta_ILD': 'd_ILD', |
| 'delta_ITD': 'd_ITD(xcorr)', |
| 'delta_ITD_gcc': 'd_ITD(gcc)', |
| 'spatial_clap_score': 'CLAP(spat)', |
| 'msclap_score': 'CLAP(ms)', |
| } |
| LOWER_BETTER = {'td_loss', 'delta_ILD', 'delta_ITD', 'delta_ITD_gcc'} |
|
|
| COL_W = 20 |
|
|
| def load_results(pth_path): |
| batches = torch.load(pth_path, map_location='cpu', weights_only=False) |
| all_metrics = defaultdict(list) |
| all_cmd_types = [] |
| all_dist_counts = [] |
| available_metrics = set(batches[0].keys()) - {'metadata'} |
| for batch in batches: |
| n = len(batch['signal_noise_ratio']) |
| for m in ALL_METRICS: |
| if m in batch: |
| all_metrics[m].extend(batch[m]) |
| for i in range(n): |
| meta = batch['metadata'][i] |
| cmd = meta.get('command_variant', {}).get('command_type', 'unknown') |
| dist_count = meta.get('distractor_count', 0) |
| all_cmd_types.append(cmd) |
| all_dist_counts.append(dist_count) |
| return {m: np.array(v) for m, v in all_metrics.items()}, all_cmd_types, all_dist_counts, available_metrics |
|
|
|
|
| def print_table(title, count, model_data, model_names, mask_fn): |
| print(f"\n{'='*170}", flush=True) |
| print(f"{title} ({count} samples)", flush=True) |
| print('='*170, flush=True) |
|
|
| |
| active_metrics = [m for m in ALL_METRICS |
| if any(m in model_data[nm][0] for nm in model_names)] |
|
|
| header = f"{'Model':<16}" |
| for m in active_metrics: |
| header += f"{METRIC_NAMES[m]:>{COL_W}}" |
| print(header, flush=True) |
| print("-"*len(header), flush=True) |
|
|
| |
| best = {} |
| for m in active_metrics: |
| vals = [] |
| for nm in model_names: |
| mets = model_data[nm][0] |
| if m not in mets: |
| continue |
| cl, dl = model_data[nm][1], model_data[nm][2] |
| mask = mask_fn(cl, dl) |
| if mask.sum() == 0: |
| continue |
| vals.append(float(np.mean(mets[m][mask]))) |
| if not vals: |
| continue |
| best[m] = min(vals) if m in LOWER_BETTER else max(vals) |
|
|
| for nm in model_names: |
| mets, cl, dl = model_data[nm] |
| mask = mask_fn(cl, dl) |
| row = f"{nm:<16}" |
| for m in active_metrics: |
| if m not in mets: |
| row += f"{'N/A':>{COL_W}}" |
| else: |
| vals = mets[m][mask] |
| if len(vals) == 0: |
| row += f"{'N/A':>{COL_W}}" |
| continue |
| mean_val = float(np.mean(vals)) |
| std_val = float(np.std(vals)) |
| is_best = m in best and abs(mean_val - best[m]) < 1e-6 |
| marker = "*" if is_best else " " |
| cell = f"{mean_val:.2f}\u00b1{std_val:.2f}" |
| row += f"{cell:>{COL_W-1}}{marker}" |
| print(row, flush=True) |
|
|
|
|
| |
|
|
| for test_label, eval_dir_name in TEST_SETS: |
| |
| model_data = {} |
| model_names = [] |
|
|
| for display_name, folder_name in MODELS: |
| pth = os.path.join(BASE, folder_name, eval_dir_name, 'results.eval.pth') |
| if not os.path.exists(pth): |
| print(f"[SKIP] {display_name} / {eval_dir_name}: results not found", flush=True) |
| continue |
| metrics, cmd_types, dist_counts, avail = load_results(pth) |
| model_data[display_name] = (metrics, cmd_types, dist_counts) |
| model_names.append(display_name) |
| print(f"Loaded {display_name} / {eval_dir_name}: {len(cmd_types)} samples, " |
| f"metrics: {sorted(avail)}", flush=True) |
|
|
| if len(model_names) == 0: |
| print(f"\n[SKIP] No results available for test set: {test_label}\n", flush=True) |
| continue |
|
|
| n_samples = len(model_data[model_names[0]][1]) |
|
|
| |
| print_table(f"{test_label} — GLOBAL PERFORMANCE", n_samples, |
| model_data, model_names, |
| lambda c, d: np.ones(len(c), dtype=bool)) |
|
|
| |
| all_cmd = model_data[model_names[0]][1] |
| cmd_types_set = sorted(set(all_cmd)) |
| for i, cmd in enumerate(cmd_types_set): |
| n_cmd = sum(1 for c in all_cmd if c == cmd) |
| print_table(f"{test_label} — COMMAND TYPE = '{cmd}'", n_cmd, |
| model_data, model_names, |
| lambda c, d, _cmd=cmd: np.array([x == _cmd for x in c])) |
|
|
| |
| all_dist = model_data[model_names[0]][2] |
| dist_set = sorted(set(all_dist)) |
| for i, dc in enumerate(dist_set): |
| n_dc = sum(1 for d in all_dist if d == dc) |
| print_table(f"{test_label} — DISTRACTOR COUNT = {dc}", n_dc, |
| model_data, model_names, |
| lambda c, d, _dc=dc: np.array([x == _dc for x in d])) |
|
|
| print(f"\n{'='*170}", flush=True) |
| print("LEGEND: * = best model for that metric", flush=True) |
| print(" SI-SNRi(dB) = SI-SNR improvement over input mixture", flush=True) |
| print(" SNRi(dB) = SNR improvement over input mixture", flush=True) |
| print(" SI-SNR(dB) = Absolute SI-SNR of output", flush=True) |
| print(" Higher=better: SI-SNRi(dB), SNRi(dB), SI-SNR(dB), CLAP(spat), CLAP(ms)", flush=True) |
| print(" Lower=better: TD Loss, d_ILD, d_ITD(xcorr), d_ITD(gcc)", flush=True) |
| print(" N/A = metric not available for that model", flush=True) |
| print('='*170, flush=True) |
|
|