Boost: COLMAP plane refinement + junction constraints + snap fixes
Browse filesInference-only improvements (no retraining required) targeting both
corner_f1 and edge_iou:
- Integrate refine_vertices_multiview_plane in main loop. Built but
never called; snaps each vertex onto the roof plane visible at its
2D projection across views, weighted by per-view PCA quality.
Directly targets corner_f1 (currently the largest gap vs leaders).
- Integrate apply_junction_constraints after hybrid_merge: drops
collinear duplicate vertices, antiparallel duplicate edges, and
short leaf edges. Improves edge_iou by removing structurally
invalid topology. Falls back to pre-junction graph if it would
produce zero edges.
- snap_to_point_cloud now includes flashing_end_point (class 3) as
a snap target. Previously only classes 1+2 were used.
- snap_horizontal now only moves a vertex when ALL of its incident
edges are near-horizontal — prevents corruption of sloped edges
that share a vertex with an eave.
- hybrid_merge: accept track edges between two existing predicted
vertices when both are within a tight 0.3 m radius (high-confidence
topology correction). Previously only edges touching new vertices
were accepted.
Each new integration is wrapped in try/except so a failure on any
sample falls back to the previous behaviour.
Files changed (2)
5 unmodified lines6789910111213 unmodified lines262728292930313233343135363738394041424334353637384445464748495051525354555657585960616263645 unmodified lines target_classes=None): """Snap vertices to nearby point cloud clusters of specific semantic classes.""" if target_classes is None: target_classes = [1, 2] # apex, eave_end_point target_classes = [1, 2, 3] # apex, eave_end_point, flashing_end_point
snapped = vertices.copy() mask = np.isin(class_id, target_classes)13 unmodified lines
def snap_horizontal(vertices, edges, max_slope=0.05): """Snap near-horizontal edges to be exactly horizontal.""" """Snap near-horizontal edges to be exactly horizontal.
Only snaps a vertex if ALL of its incident edges are near-horizontal — otherwise we would corrupt sloped edges that share the vertex. """ verts = vertices.copy() for a, b in edges: n = len(verts) if n == 0 or len(edges) == 0: return verts
edge_is_h = [] incident = [[] for _ in range(n)] for ei, (a, b) in enumerate(edges): a, b = int(a), int(b) dy = abs(verts[a, 1] - verts[b, 1]) dxz = np.sqrt((verts[a, 0] - verts[b, 0])**2 + (verts[a, 2] - verts[b, 2])**2) if dxz > 0.1 and dy / dxz < max_slope: avg_y = 0.5 * (verts[a, 1] + verts[b, 1]) verts[a, 1] = avg_y verts[b, 1] = avg_y dxz = np.sqrt((verts[a, 0] - verts[b, 0]) ** 2 + (verts[a, 2] - verts[b, 2]) ** 2) is_h = dxz > 0.1 and dy / dxz < max_slope edge_is_h.append(is_h) incident[a].append(ei) incident[b].append(ei)
purely_h = [ len(incident[v]) > 0 and all(edge_is_h[ei] for ei in incident[v]) for v in range(n) ]
for ei, (a, b) in enumerate(edges): if not edge_is_h[ei]: continue a, b = int(a), int(b) if not (purely_h[a] and purely_h[b]): continue avg_y = 0.5 * (verts[a, 1] + verts[b, 1]) verts[a, 1] = avg_y verts[b, 1] = avg_y return verts292 unmodified lines29329429529629729829930030130229930030130230330430530630730330430530630730830931031131231331431531631731831932032132232332432532632732832933086 unmodified lines417418419400420421422423404424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460292 unmodified lines for u, v in final_e: existing_edges.add((min(u, v), max(u, v))) # Tight-confidence radius for accepting a track edge between two # already-existing predicted vertices (topology correction). tight_radius = 0.3
for u_t, v_t in track_e: u_f = track_to_final.get(u_t) v_f = track_to_final.get(v_t) if u_f is not None and v_f is not None and u_f != v_f: e = (min(u_f, v_f), max(u_f, v_f)) if e not in existing_edges: # ONLY append the tracked edge if it connects to a NEWLY DISCOVERED vertex. # This prevents the geometric tracker from aggressively re-wiring the learned model's existing topology! if u_f >= len(pred_v) or v_f >= len(pred_v): final_e.append(e) existing_edges.add(e) if u_f is None or v_f is None or u_f == v_f: continue e = (min(u_f, v_f), max(u_f, v_f)) if e in existing_edges: continue
u_is_new = u_f >= len(pred_v) v_is_new = v_f >= len(pred_v)
if u_is_new or v_is_new: # New vertex: trust the track edge. final_e.append(e) existing_edges.add(e) continue
# Both endpoints are existing predicted vertices — only accept the # track edge as a topology correction when both are very confidently # matched (tight distance). This recovers missing edges without # letting the tracker rewire the learned topology. d_u = dists[u_t] if u_t < len(dists) else np.inf d_v = dists[v_t] if v_t < len(dists) else np.inf if d_u <= tight_radius and d_v <= tight_radius: final_e.append(e) existing_edges.add(e)
return np.array(final_v), final_e
# ---------------------------------------------------------------------------86 unmodified lines from triangulation import predict_wireframe_tracks # Use min_views=3 for highly precise, conservative geometric tracks track_v, track_e = predict_wireframe_tracks(sample, min_views=3)
pred_v, pred_e = hybrid_merge(pred_v, pred_e, track_v, track_e, merge_radius=0.8) except Exception as track_e_err: print(f" Track ensemble failed for {order_id}: {track_e_err}")
# COLMAP multi-view plane refinement: snaps each vertex to the # roof plane visible at its 2D projection. Targets corner_f1. try: if isinstance(pred_v, np.ndarray) and len(pred_v) >= 1: from colmap_refine import refine_vertices_multiview_plane refined, snapped_mask = refine_vertices_multiview_plane( pred_v, sample, knn_2d_px=30.0, knn_k=12, min_neighbours=6, max_displacement=0.5, min_quality=0.5, min_views=2, ) if snapped_mask.any(): pred_v = refined except Exception as refine_err: print(f" COLMAP refine failed for {order_id}: {refine_err}")
# Junction constraints: drop collinear/duplicate/short-leaf edges try: if isinstance(pred_v, np.ndarray) and len(pred_v) >= 2 and len(pred_e) >= 1: from junction import apply_junction_constraints jv, je = apply_junction_constraints( pred_v, pred_e, collinear_cos=0.97, duplicate_cos=0.985, leaf_min_len=0.4, max_passes=3, ) # Only accept if at least one edge survives — never let # the post-processing reduce us to an empty graph. if len(je) >= 1: pred_v, pred_e = jv, je except Exception as junc_err: print(f" Junction constraints failed for {order_id}: {junc_err}")
except Exception as e: import traceback print(f" Predict failed for {order_id}:\n{traceback.format_exc()}")