TTA optimization: cache point fusion, vary only priority sampling
Browse filesThe previous TTA commit (8e33a89) timed out because it called
build_compact_scene 3x per sample (the expensive multi-view label
voting step). Refactor:
- compute_scene(sample, cfg, rng): does build_compact_scene + group/class/
center/scale computation. Called ONCE per sample.
- sample_from_scene(scene): does priority sampling + result-dict assembly.
Cheap, called K=3 times per sample.
- fuse_and_sample is preserved as a backward-compat wrapper.
Why this still gives TTA variation: _priority_sample uses the *global*
numpy random state (np.random.shuffle), not an explicit rng arg. Each
consecutive call advances the global state and produces a different
4096-point subset of the same fused scene. The model sees different
inputs across passes despite the scene being identical.
Cost: ~10% extra wall time vs single pass (3x cheap priority sampling
+ 3x cheap model forward), instead of ~200% from the previous commit.
Should fit comfortably in the 2h budget.
59 unmodified lines60616263646566676869703 unmodified lines7475767778798081828384858687888990919293944 unmodified lines99100101102103104105106107108109110111112113114115116117118119120121333 unmodified lines45545645745845946046146246346446546646746846947047147247359 unmodified linesSNAP_RADIUS = 0.5def fuse_and_sample(sample, cfg, rng):"""Run point fusion + priority sampling on a raw dataset sample.Returns a dict with xyz_norm, class_id, source, mask, center, scale, etc.ready for model inference. Returns None if fusion fails."""try:scene = build_compact_scene(sample, cfg, rng)3 unmodified linesxyz = scene["xyz"]source = scene["source"]if len(xyz) < 10:return None# Compute group_id and class_id (same as cache_scenes.py)behind_id = scene.get("behind_gest_id", np.full(len(xyz), -1, dtype=np.int16))group_id, class_id = _compute_group_and_class(scene["visible_src"], scene["visible_id"], behind_id, source)# Normalizecenter, scale = _compute_smart_center_scale(xyz, source)# Priority sampleindices, mask = _priority_sample(source, group_id, SEQ_LEN, COLMAP_QUOTA, DEPTH_QUOTA)xyz_norm = (xyz[indices] - center) / scaleresult = {4 unmodified lines"center": center.astype(np.float32),"scale": np.float32(scale),}# Optional fieldsif "behind_gest_id" in scene:behind = np.clip(scene["behind_gest_id"][indices].astype(np.int16), 0, None)result["behind"] = behind.astype(np.int64)if "n_views_voted" in scene:result["n_views_voted"] = scene["n_views_voted"][indices].astype(np.float32)if "vote_frac" in scene:result["vote_frac"] = scene["vote_frac"][indices].astype(np.float32)# Visible src/id for snap post-processingresult["visible_src"] = scene["visible_src"][indices].astype(np.int64)result["visible_id"] = scene["visible_id"][indices].astype(np.int64)return resultdef load_model(checkpoint_path, device):"""Load model from checkpoint."""ckpt = torch.load(checkpoint_path, map_location=device, weights_only=False)333 unmodified linesorder_id = sample["order_id"]try:# ---- TTA: run the learned pipeline K times, union outputstta_outputs = []for k in range(TTA_PASSES):rng_k = np.random.RandomState(TTA_BASE_SEED + k * 1000)fused_k = fuse_and_sample(sample, cfg, rng_k)if fused_k is None:continuetry:pv_k, pe_k = predict_sample(fused_k, model, device)if isinstance(pv_k, np.ndarray) and len(pv_k) >= 2 and len(pe_k) >= 1:tta_outputs.append((pv_k, pe_k))except Exception as tta_e:print(f" TTA pass {k} failed for {order_id}: {tta_e}")if torch.cuda.is_available():torch.cuda.empty_cache()59 unmodified lines60616263646566676869703 unmodified lines74757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151164 unmodified lines121122123124125126127128129130131132133134135136137138139140141142143144145146333 unmodified lines48048148248348448548648748848949049149249349449549649749849950050150259 unmodified linesSNAP_RADIUS = 0.5def compute_scene(sample, cfg, rng):"""Expensive: multi-view label voting + smart normalization. Call once per sample.Returns a dict with the full pre-priority-sampling fused scene, ready tofeed into ``sample_from_scene`` repeatedly for TTA. Returns None on failure."""try:scene = build_compact_scene(sample, cfg, rng)3 unmodified linesxyz = scene["xyz"]source = scene["source"]if len(xyz) < 10:return Nonebehind_id = scene.get("behind_gest_id", np.full(len(xyz), -1, dtype=np.int16))group_id, class_id = _compute_group_and_class(scene["visible_src"], scene["visible_id"], behind_id, source)center, scale = _compute_smart_center_scale(xyz, source)return {"xyz": xyz,"source": source,"group_id": group_id,"class_id": class_id,"center": center,"scale": scale,"behind_gest_id": scene.get("behind_gest_id"),"n_views_voted": scene.get("n_views_voted"),"vote_frac": scene.get("vote_frac"),"visible_src": scene["visible_src"],"visible_id": scene["visible_id"],}def sample_from_scene(scene):"""Cheap: priority-sample 4096 points from a fused scene.Uses the global numpy random state (advanced internally by ``_priority_sample``),so consecutive calls yield different 4096-subsets — perfect for TTA."""xyz = scene["xyz"]source = scene["source"]group_id = scene["group_id"]class_id = scene["class_id"]center = scene["center"]scale = scene["scale"]indices, mask = _priority_sample(source, group_id, SEQ_LEN, COLMAP_QUOTA, DEPTH_QUOTA)xyz_norm = (xyz[indices] - center) / scaleresult = {4 unmodified lines"center": center.astype(np.float32),"scale": np.float32(scale),}if scene.get("behind_gest_id") is not None:behind = np.clip(scene["behind_gest_id"][indices].astype(np.int16), 0, None)result["behind"] = behind.astype(np.int64)if scene.get("n_views_voted") is not None:result["n_views_voted"] = scene["n_views_voted"][indices].astype(np.float32)if scene.get("vote_frac") is not None:result["vote_frac"] = scene["vote_frac"][indices].astype(np.float32)result["visible_src"] = scene["visible_src"][indices].astype(np.int64)result["visible_id"] = scene["visible_id"][indices].astype(np.int64)return resultdef fuse_and_sample(sample, cfg, rng):"""Backward-compatible wrapper: compute scene + one priority sample."""scene = compute_scene(sample, cfg, rng)if scene is None:return Nonereturn sample_from_scene(scene)def load_model(checkpoint_path, device):"""Load model from checkpoint."""ckpt = torch.load(checkpoint_path, map_location=device, weights_only=False)333 unmodified linesorder_id = sample["order_id"]try:# ---- Build the fused scene ONCE (the expensive multi-view# label voting); then run priority sampling + model K times# for TTA. _priority_sample uses the global numpy RNG which# advances on each call, giving genuine variation cheaply.scene_rng = np.random.RandomState(TTA_BASE_SEED)scene = compute_scene(sample, cfg, scene_rng)tta_outputs = []if scene is not None:np.random.seed(TTA_BASE_SEED) # reset global RNG for reproducibilityfor k in range(TTA_PASSES):try:fused_k = sample_from_scene(scene)pv_k, pe_k = predict_sample(fused_k, model, device)if isinstance(pv_k, np.ndarray) and len(pv_k) >= 2 and len(pe_k) >= 1:tta_outputs.append((pv_k, pe_k))except Exception as tta_e:print(f" TTA pass {k} failed for {order_id}: {tta_e}")if torch.cuda.is_available():torch.cuda.empty_cache()