Datasets:
Upload segmentation/pipeline_60s_run2/scripts/phase6_fuse.py with huggingface_hub
Browse files
segmentation/pipeline_60s_run2/scripts/phase6_fuse.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import csv
|
| 2 |
+
import glob
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
import cv2
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
ROOT = "/root/bdmc_pipeline"
|
| 9 |
+
H, W = 1920, 1080
|
| 10 |
+
fx = fy = H / (2 * np.tan(np.radians(72.0 / 2)))
|
| 11 |
+
K = np.array([[fx, 0, W / 2], [0, fy, H / 2], [0, 0, 1.0]])
|
| 12 |
+
RADIUS_PX = 110
|
| 13 |
+
|
| 14 |
+
devmaps = np.load(f"{ROOT}/outputs/devmaps_small.npz")["dev"].astype(np.float32)
|
| 15 |
+
devmaps_m = (devmaps - 128.0) / 1000.0
|
| 16 |
+
defects = list(csv.DictReader(open(f"{ROOT}/outputs/defects.csv")))
|
| 17 |
+
wins = {}
|
| 18 |
+
for p in sorted(glob.glob(f"{ROOT}/outputs/tracks/win_*.npz")):
|
| 19 |
+
z = np.load(p)
|
| 20 |
+
wins[int(z["start"])] = {"tracks": z["tracks"], "vis": z["vis"].reshape(z["vis"].shape[0], -1)}
|
| 21 |
+
|
| 22 |
+
tri_meta = {}
|
| 23 |
+
for p in sorted(glob.glob(f"{ROOT}/outputs/tracks/triangulated_*.npz")):
|
| 24 |
+
z = np.load(p)
|
| 25 |
+
tri_meta[int(z["start"])] = {"xyz": z["xyz"], "Rs": z["Rs"], "ts": z["ts"],
|
| 26 |
+
"rmse_med": float(np.median(z["rmse"]))}
|
| 27 |
+
|
| 28 |
+
def track_support(fst, dur, cxc, cyc):
|
| 29 |
+
dvs = []
|
| 30 |
+
for s, w in wins.items():
|
| 31 |
+
lo, hi = max(fst, s), min(fst + dur - 1, s + w["tracks"].shape[0] - 1)
|
| 32 |
+
for f in range(lo, hi + 1):
|
| 33 |
+
tr = w["tracks"][f - s]
|
| 34 |
+
v = w["vis"][f - s].astype(bool)
|
| 35 |
+
xi = np.clip(tr[:, 0].round().astype(int), 0, W - 1)
|
| 36 |
+
yi = np.clip(tr[:, 1].round().astype(int), 0, H - 1)
|
| 37 |
+
dist = np.hypot(xi - cxc, yi - cyc)
|
| 38 |
+
near = v & (dist < RADIUS_PX)
|
| 39 |
+
if near.sum() == 0:
|
| 40 |
+
continue
|
| 41 |
+
dvs.append(devmaps_m[f - 1][yi[near] // 4, xi[near] // 4])
|
| 42 |
+
if not dvs:
|
| 43 |
+
return None
|
| 44 |
+
dv = np.concatenate(dvs)
|
| 45 |
+
return {"n_obs": int(len(dv)),
|
| 46 |
+
"support_frac_lt3cm": round(float((dv < -0.03).mean()), 3),
|
| 47 |
+
"median_dev_cm": round(float(np.median(dv)) * 100, 2),
|
| 48 |
+
"p25_dev_cm": round(float(np.percentile(dv, 25)) * 100, 2)}
|
| 49 |
+
|
| 50 |
+
rows_out = []
|
| 51 |
+
for r in defects:
|
| 52 |
+
c = str(r["centroid_px"]).replace("[", "").replace("]", "").replace(",", " ").split()
|
| 53 |
+
cxc, cyc = float(c[0]), float(c[1])
|
| 54 |
+
fst, dur = int(r["first_frame"]), int(r["duration_frames"])
|
| 55 |
+
rec = dict(r)
|
| 56 |
+
ts = track_support(fst, dur, cxc, cyc)
|
| 57 |
+
if ts is None or ts["n_obs"] < 5:
|
| 58 |
+
rec.update({"track_n_obs": ts["n_obs"] if ts else 0, "track_median_dev_cm": None,
|
| 59 |
+
"track_support_frac": None, "confidence": "no_coverage"})
|
| 60 |
+
else:
|
| 61 |
+
med, sf = ts["median_dev_cm"], ts["support_frac_lt3cm"]
|
| 62 |
+
conf = ("agree" if (med <= -3.0 and sf >= 0.4)
|
| 63 |
+
else "partial" if med <= -1.5 else "disagree")
|
| 64 |
+
rec.update({**ts, "track_support_frac": sf, "track_median_dev_cm": med,
|
| 65 |
+
"confidence": conf})
|
| 66 |
+
rows_out.append(rec)
|
| 67 |
+
|
| 68 |
+
cols = ["first_frame", "t_sec", "duration_frames", "duration_s", "peak_dev_cm", "area_px",
|
| 69 |
+
"centroid_px", "n_obs", "median_dev_cm", "p25_dev_cm", "support_frac_lt3cm",
|
| 70 |
+
"confidence"]
|
| 71 |
+
with open(f"{ROOT}/outputs/defects_fused.csv", "w", newline="") as fh:
|
| 72 |
+
wr = csv.DictWriter(fh, fieldnames=cols)
|
| 73 |
+
wr.writeheader()
|
| 74 |
+
wr.writerows([{k: r.get(k) for k in cols} for r in rows_out])
|
| 75 |
+
|
| 76 |
+
tri_files = glob.glob(f"{ROOT}/outputs/tracks/triangulated_*.npz")
|
| 77 |
+
if tri_files:
|
| 78 |
+
med_rmse = float(np.median(np.concatenate([np.load(p)["rmse"] for p in tri_files])))
|
| 79 |
+
else:
|
| 80 |
+
med_rmse = 0.0
|
| 81 |
+
|
| 82 |
+
report = {
|
| 83 |
+
"n_defects": len(rows_out),
|
| 84 |
+
"confidence_counts": {c: sum(1 for r in rows_out if r.get("confidence") == c)
|
| 85 |
+
for c in ["agree", "partial", "disagree", "no_coverage"]},
|
| 86 |
+
"method": "per-defect: gather all CoTracker3 observations passing through defect region during its confirmed lifetime; compare their depth-residual samples against the flag",
|
| 87 |
+
"median_triangulated_rmse_px": round(med_rmse, 2),
|
| 88 |
+
"sigma_proxy_triangulated_cm_at_5m": round(float(med_rmse * 5.0 / fx * 100), 2),
|
| 89 |
+
"depth_tile_sigma_cm": json.load(open(f"{ROOT}/outputs/phase3_summary.json"))["tile_sigma_median_cm"],
|
| 90 |
+
"note": "no GPS/IMU: egomotion estimated visually (Kabsch on depth-anchored correspondences)",
|
| 91 |
+
}
|
| 92 |
+
json.dump(report, open(f"{ROOT}/outputs/phase6_report.json", "w"), indent=1)
|
| 93 |
+
print(json.dumps(report, indent=1))
|
| 94 |
+
print("Phase 6 fusion complete", flush=True)
|