File size: 2,769 Bytes
ce209f5 | 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 | from __future__ import annotations
import csv
import json
from pathlib import Path
from typing import Mapping
ROOT = Path(__file__).resolve().parents[1]
METRIC_COLUMNS = [
"Model",
"Dataset",
"F1",
"IoU",
"mIoU",
"Precision",
"Recall",
"BF1",
"OA",
"Kappa",
"ParamsM",
"FLOPsG",
"FPS",
"GPUUtilMean",
"GPUMemPeakGB",
"BestEpoch",
]
def save_metrics(
model_name: str,
dataset_name: str,
split: str,
metrics_dict: Mapping,
output_dir: str | Path = "results",
) -> Path:
base = Path(output_dir)
if not base.is_absolute():
base = ROOT / base
out_dir = base / model_name / dataset_name
out_dir.mkdir(parents=True, exist_ok=True)
path = out_dir / f"metrics_{split}.json"
payload = dict(metrics_dict)
payload.setdefault("model", model_name)
payload.setdefault("dataset", dataset_name)
payload.setdefault("split", split)
with path.open("w", encoding="utf-8") as f:
json.dump(payload, f, indent=2, sort_keys=True)
return path
def append_to_comparison_table(results_dir: str | Path = "results") -> Path:
base = Path(results_dir)
if not base.is_absolute():
base = ROOT / base
rows = []
for path in sorted(base.glob("*/*/metrics_test.json")):
with path.open("r", encoding="utf-8") as f:
metrics = json.load(f)
rows.append({
"Model": metrics.get("model", path.parents[1].name),
"Dataset": metrics.get("dataset", path.parent.name),
"F1": metrics.get("f1", metrics.get("F1", "")),
"IoU": metrics.get("iou", metrics.get("IoU", "")),
"mIoU": metrics.get("miou", metrics.get("mIoU", "")),
"Precision": metrics.get("precision", metrics.get("Precision", "")),
"Recall": metrics.get("recall", metrics.get("Recall", "")),
"BF1": metrics.get("bf1", metrics.get("BF1", "")),
"OA": metrics.get("oa", metrics.get("OA", "")),
"Kappa": metrics.get("kappa", metrics.get("Kappa", "")),
"ParamsM": metrics.get("params_m", ""),
"FLOPsG": metrics.get("flops_g", ""),
"FPS": metrics.get("fps", ""),
"GPUUtilMean": metrics.get("gpu_util_mean", ""),
"GPUMemPeakGB": metrics.get("gpu_mem_reserved_peak_gb", metrics.get("gpu_mem_allocated_peak_gb", "")),
"BestEpoch": metrics.get("best_epoch", metrics.get("epoch", "")),
})
out = base / "comparison_table.csv"
base.mkdir(parents=True, exist_ok=True)
with out.open("w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=METRIC_COLUMNS)
writer.writeheader()
writer.writerows(rows)
return out
|