Upload s23dr_2026/eval.py with huggingface_hub
Browse files- s23dr_2026/eval.py +124 -0
s23dr_2026/eval.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
|
| 4 |
+
import hoho2025.metric_helper
|
| 5 |
+
import numpy as np
|
| 6 |
+
import torch
|
| 7 |
+
from datasets import load_dataset
|
| 8 |
+
from tqdm import tqdm
|
| 9 |
+
|
| 10 |
+
from .model import get_model, load_checkpoint_compat
|
| 11 |
+
from .inference import predict_wireframe_v2, predict_wireframe_v2_tta
|
| 12 |
+
from .scene import Scene
|
| 13 |
+
from .utils import set_random_seed, start_debug
|
| 14 |
+
|
| 15 |
+
# Scenes with known pose/annotation misalignment, grouped by severity.
|
| 16 |
+
# Used to produce a clean validation2 split from the public validation set.
|
| 17 |
+
_MISALIGNED_FILE = Path(__file__).resolve().parent.parent / "assets" / "misaligned.txt"
|
| 18 |
+
_SEVERITY_ORDER = ("extreme", "severe", "medium")
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def load_validation2(stream: bool = False, misalignment_severity: str = "medium") -> object:
|
| 22 |
+
"""Load the validation split filtered to validation2 (misalignment-cleaned subset)."""
|
| 23 |
+
dataset = load_dataset("usm3d/hoho22k_2026_trainval", trust_remote_code=True, streaming=stream)
|
| 24 |
+
return _drop_misaligned(dataset["validation"], misalignment_severity)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def _drop_misaligned(data, severity: str = "medium"):
|
| 28 |
+
"""Drop scenes with pose/annotation misalignment up to the given severity level."""
|
| 29 |
+
min_idx = _SEVERITY_ORDER.index(severity)
|
| 30 |
+
drop: set[str] = set()
|
| 31 |
+
section = None
|
| 32 |
+
for line in _MISALIGNED_FILE.read_text().splitlines():
|
| 33 |
+
line = line.strip()
|
| 34 |
+
if not line or line.startswith("#"):
|
| 35 |
+
section = line.lstrip("#").strip().lower() if line.startswith("#") else section
|
| 36 |
+
continue
|
| 37 |
+
if section in _SEVERITY_ORDER and _SEVERITY_ORDER.index(section) <= min_idx:
|
| 38 |
+
drop.add(line)
|
| 39 |
+
print(f"Dropping {len(drop)} misaligned scenes (severity <= {severity})")
|
| 40 |
+
return data.filter(lambda s: s["order_id"] not in drop)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def run_eval(model, dataset, device, args) -> list[tuple[str, object]]:
|
| 44 |
+
results = []
|
| 45 |
+
for data in tqdm(dataset, desc="Evaluating"):
|
| 46 |
+
scene = Scene(data)
|
| 47 |
+
if args.tta_rotations > 1:
|
| 48 |
+
verts, edges = predict_wireframe_v2_tta(
|
| 49 |
+
scene, model, device,
|
| 50 |
+
pt_type=args.pt_type,
|
| 51 |
+
num_points=args.num_points,
|
| 52 |
+
score_threshold=args.threshold,
|
| 53 |
+
merge_distance_threshold=args.merge_distance_threshold,
|
| 54 |
+
|
| 55 |
+
n_rotations=args.tta_rotations,
|
| 56 |
+
)
|
| 57 |
+
else:
|
| 58 |
+
verts, edges = predict_wireframe_v2(
|
| 59 |
+
scene, model, device,
|
| 60 |
+
pt_type=args.pt_type,
|
| 61 |
+
num_points=args.num_points,
|
| 62 |
+
score_threshold=args.threshold,
|
| 63 |
+
|
| 64 |
+
merge_distance_threshold=args.merge_distance_threshold,
|
| 65 |
+
merge_method=args.merge_method,
|
| 66 |
+
feature_alpha=args.feature_alpha,
|
| 67 |
+
)
|
| 68 |
+
result = hoho2025.metric_helper.hss(verts, edges, scene.verts, scene.edges)
|
| 69 |
+
results.append((scene.order_id, result))
|
| 70 |
+
print(f"{scene.order_id}: HSS={result.hss:.4f} F1={result.f1:.4f} IoU={result.iou:.4f}")
|
| 71 |
+
return results
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
if __name__ == "__main__":
|
| 75 |
+
parser = argparse.ArgumentParser(description="Evaluate WireframeDETR on a dataset split.")
|
| 76 |
+
parser.add_argument("--split", required=True, help="Dataset split (train / validation / validation2).")
|
| 77 |
+
parser.add_argument("--checkpoint", type=Path, required=True, help="Trained model checkpoint.")
|
| 78 |
+
parser.add_argument("--device", default="cuda")
|
| 79 |
+
parser.add_argument("--seed", type=int, help="Shuffle seed.")
|
| 80 |
+
parser.add_argument("--stream", action="store_true", help="Stream dataset instead of downloading.")
|
| 81 |
+
parser.add_argument("-d", "--debug", action="store_true")
|
| 82 |
+
parser.add_argument("--num_points", "-np", type=int, default=7168)
|
| 83 |
+
parser.add_argument("--threshold", "-t", type=float, default=0.9, help="Edge confidence threshold.")
|
| 84 |
+
parser.add_argument("--merge_distance_threshold", type=float, default=0.5)
|
| 85 |
+
parser.add_argument("--pt_type", type=str, default="colmap", choices=["monodepth", "colmap"])
|
| 86 |
+
parser.add_argument("--merge_method", default="distance", choices=["distance", "feature", "auto"])
|
| 87 |
+
parser.add_argument("--feature_alpha", type=float, default=0.95)
|
| 88 |
+
parser.add_argument("--tta_rotations", type=int, default=1,
|
| 89 |
+
help="Y-axis TTA rotations (1=off, 4=0/90/180/270deg).")
|
| 90 |
+
parser.add_argument("--misalignment_severity", default="medium", choices=list(_SEVERITY_ORDER),
|
| 91 |
+
help="Max misalignment severity to drop when split=validation2.")
|
| 92 |
+
parser.add_argument("--results", type=Path, help="CSV path for per-scene scores.")
|
| 93 |
+
args = parser.parse_args()
|
| 94 |
+
|
| 95 |
+
if args.debug:
|
| 96 |
+
start_debug()
|
| 97 |
+
|
| 98 |
+
if args.split == "validation2":
|
| 99 |
+
dataset = load_validation2(stream=args.stream, misalignment_severity=args.misalignment_severity)
|
| 100 |
+
else:
|
| 101 |
+
dataset = load_dataset("usm3d/hoho22k_2026_trainval", trust_remote_code=True, streaming=args.stream)[args.split]
|
| 102 |
+
|
| 103 |
+
set_random_seed(args.seed or 0)
|
| 104 |
+
if args.seed is not None:
|
| 105 |
+
dataset = dataset.shuffle(seed=args.seed)
|
| 106 |
+
|
| 107 |
+
checkpoint = torch.load(args.checkpoint, map_location=args.device)
|
| 108 |
+
model = get_model(checkpoint, num_classes=1)
|
| 109 |
+
load_checkpoint_compat(model, checkpoint)
|
| 110 |
+
model.to(args.device).eval()
|
| 111 |
+
|
| 112 |
+
results = run_eval(model, dataset, args.device, args)
|
| 113 |
+
|
| 114 |
+
mean_hss = np.mean([r.hss for _, r in results])
|
| 115 |
+
mean_f1 = np.mean([r.f1 for _, r in results])
|
| 116 |
+
mean_iou = np.mean([r.iou for _, r in results])
|
| 117 |
+
print(f"\nHSS={mean_hss:.4f} F1={mean_f1:.4f} IoU={mean_iou:.4f}")
|
| 118 |
+
|
| 119 |
+
if args.results:
|
| 120 |
+
with open(args.results, "w") as f:
|
| 121 |
+
f.write("order_id,hss,f1,iou\n")
|
| 122 |
+
for order_id, result in results:
|
| 123 |
+
f.write(f"{order_id},{result.hss:.4f},{result.f1:.4f},{result.iou:.4f}\n")
|
| 124 |
+
print(f"Saved to {args.results}")
|