Gaze-LIPE / scripts /evaluate_best_gaze360.py
thanhhuyvan's picture
Initial release of LIPE V2 GOLD
a10ba7f
Raw
History Blame Contribute Delete
3.19 kB
import subprocess
import sys
import os
import glob
from tqdm import tqdm
# Configuration
H5_PATH = "data/processed/gaze360_robust_v16_test_B.h5"
SWA_DIR = "checkpoints/gold_swa"
RESULTS_CSV = "logs/gaze360_best_summary.csv"
RESULTS_LOG = "logs/gaze360_best_summary.log"
def main():
# 1. Identify all available BEST checkpoints
best_models = glob.glob(os.path.join(SWA_DIR, "best_gold_p*.pt"))
best_models.sort()
if not best_models:
print(f"No BEST models found in {SWA_DIR}")
return
print(f"Found {len(best_models)} BEST models. Starting evaluation on Gaze360...")
# 2. Prepare result storage
results = []
print(f"{'='*70}")
print(f"{'Fold':<8} | {'MAE (All Cases)':<20} | {'MAE (Frontal 45)':<20}")
print(f"{'-'*70}")
with open(RESULTS_CSV, 'w') as f_csv:
f_csv.write("Fold,MAE_All,MAE_Frontal\n")
for model_path in tqdm(best_models, desc="Evaluating Folds"):
p_id = os.path.basename(model_path).split('_')[-1].replace('.pt', '')
# Execute verify_gaze360.py (Fixed mapping internally now)
cmd = [
sys.executable, "src/verify_gaze360.py",
"--model", model_path,
"--h5", H5_PATH
]
try:
# Capture output
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
stdout, stderr = process.communicate()
# Parse output for MAE values
mae_all = "N/A"
mae_frontal = "N/A"
for line in stdout.splitlines():
if "All Cases" in line and "|" in line:
mae_all = line.split('|')[-1].strip()
if "Frontal +/- 45" in line and "|" in line:
mae_frontal = line.split('|')[-1].strip()
print(f"{p_id:<8} | {mae_all:<20} | {mae_frontal:<20}")
f_csv.write(f"{p_id},{mae_all},{mae_frontal}\n")
if mae_all != "N/A":
results.append({
'fold': p_id,
'all': float(mae_all),
'frontal': float(mae_frontal) if mae_frontal != "N/A" else 0.0
})
except Exception as e:
print(f"Error evaluating {p_id}: {e}")
# 3. Final Summary
if results:
avg_all = sum(r['all'] for r in results) / len(results)
avg_frontal = sum(r['frontal'] for r in results) / len(results)
summary_str = (
f"\n{'='*70}\n"
f" FINAL SUMMARY (Gaze360 BEST Evaluation)\n"
f"{'-'*70}\n"
f" Average MAE (All Cases): {avg_all:.4f} deg\n"
f" Average MAE (Frontal 45): {avg_frontal:.4f} deg\n"
f" Number of Folds evaluated: {len(results)}\n"
f"{'='*70}\n"
)
print(summary_str)
with open(RESULTS_LOG, 'w') as f_log:
f_log.write(summary_str)
if __name__ == "__main__":
main()