File size: 4,270 Bytes
77731f3 | 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 | #!/usr/bin/env python3
from __future__ import annotations
import argparse
from pathlib import Path
from _common import (
cell_set,
ensure_dir,
read_jsonl,
safe_div,
universe_cells,
valid_candidates,
write_json,
write_jsonl,
)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Run deterministic greedy CM-EVS view selection.")
parser.add_argument("--candidates", type=Path, required=True, help="Candidate JSONL.")
parser.add_argument("--output-dir", type=Path, default=Path("outputs/tiny"), help="Run output directory.")
parser.add_argument("--budget", type=int, default=4, help="Maximum number of selected views.")
parser.add_argument("--lambda-conflict", type=float, default=0.35, help="Conflict-prior penalty weight.")
parser.add_argument("--min-gain", type=float, default=0.01, help="Stop when marginal coverage falls below this value.")
return parser.parse_args()
def main() -> None:
args = parse_args()
rows = read_jsonl(args.candidates)
candidates = valid_candidates(rows)
universe = universe_cells(candidates)
universe_size = max(1, len(universe))
covered: set[str] = set()
selected: list[dict] = []
log_rows: list[dict] = []
used: set[str] = set()
for step in range(args.budget):
best = None
best_tuple = None
for candidate in candidates:
cid = str(candidate["candidate_id"])
if cid in used:
continue
cells = cell_set(candidate)
new_cells = cells - covered
marginal_gain = safe_div(len(new_cells), universe_size)
probe = float(candidate.get("single_view_probe_coverage", 0.0))
conflict = float(candidate.get("conflict_prior", 0.0))
score = marginal_gain + 0.15 * probe - args.lambda_conflict * conflict
score_tuple = (score, marginal_gain, -conflict, cid)
if best_tuple is None or score_tuple > best_tuple:
best_tuple = score_tuple
best = (candidate, new_cells, score, marginal_gain)
if best is None:
break
candidate, new_cells, score, marginal_gain = best
if selected and marginal_gain < args.min_gain:
break
cid = str(candidate["candidate_id"])
used.add(cid)
covered.update(new_cells)
rank = len(selected)
selected.append(
{
"candidate_id": cid,
"rank": rank,
"position": candidate.get("position", [0.0, 0.0, 0.0]),
"yaw_deg": float(candidate.get("yaw_deg", 0.0)),
"score": round(float(score), 6),
"marginal_gain": round(float(marginal_gain), 6),
}
)
log_rows.append(
{
"step": step,
"candidate_id": cid,
"score": round(float(score), 6),
"marginal_gain": round(float(marginal_gain), 6),
"coverage_after": round(safe_div(len(covered), universe_size), 6),
"conflict_prior": float(candidate.get("conflict_prior", 0.0)),
}
)
scene_id = str(candidates[0].get("scene_id", "unknown")) if candidates else "unknown"
metadata_dir = ensure_dir(args.output_dir / "metadata")
write_json(
metadata_dir / "selected_viewpoints.json",
{
"scene_id": scene_id,
"method": "cmevs_greedy_conflict_minimized",
"selected_viewpoints": selected,
"summary": {
"budget": args.budget,
"lambda_conflict": args.lambda_conflict,
"min_gain": args.min_gain,
"num_candidates": len(rows),
"num_valid_candidates": len(candidates),
"num_selected": len(selected),
"coverage": round(safe_div(len(covered), universe_size), 6),
"universe_cells": len(universe),
},
},
)
write_jsonl(metadata_dir / "per_step_log.jsonl", log_rows)
print(f"Selected {len(selected)} viewpoints; final coverage={safe_div(len(covered), universe_size):.3f}")
if __name__ == "__main__":
main()
|