Download examples/visualizer_code/source/demo/scale4d_view_count/teacher.py from facebook/SCALE4D: direct link, hf CLI and curl.
- Browser
- Download file 4.46 kB
-
https://huggingface.co/facebook/SCALE4D/resolve/main/examples/visualizer_code/source/demo/scale4d_view_count/teacher.py
- Command line
-
hf download hf://facebook/SCALE4D/examples/visualizer_code/source/demo/scale4d_view_count/teacher.py
-
curl -L -o teacher.py https://huggingface.co/facebook/SCALE4D/resolve/main/examples/visualizer_code/source/demo/scale4d_view_count/teacher.py
4.46 kB
| """Display saved EMA teacher predictions from matched distillation runs.""" | |
| from __future__ import annotations | |
| import numpy as np | |
| from demo.scale4d_view_count.bundle import validate_scene | |
| from demo.scale4d_view_count.camera import camera_scene_payload | |
| from demo.scale4d_view_count.export import track_layer | |
| def teacher_scene_payload( | |
| inputs: dict, labels: dict, predictions: dict, record: dict | |
| ) -> dict: | |
| first = predictions["b3_ema"] | |
| checkpoint_step = first["metadata"]["checkpoint_step"] | |
| if not isinstance(checkpoint_step, int) or checkpoint_step <= 0: | |
| raise ValueError("Expected a positive EMA checkpoint step") | |
| base_record = { | |
| **record, | |
| "position_source": "xyz", | |
| **{ | |
| mode: first["metadata"]["scores"][mode] | |
| for mode in first["camera_modes"].tolist() | |
| }, | |
| } | |
| base_results = { | |
| "xyz": first["positions"], | |
| "camera_modes": first["camera_modes"], | |
| "gt_tracks_target_camera": labels["gt_tracks_target_camera"], | |
| "gt_visibility": labels["visibility"], | |
| } | |
| scene = camera_scene_payload(inputs, base_results, base_record) | |
| visible_points = labels["gt_tracks_target_camera"][labels["visibility"]] | |
| scene["extent"] = max( | |
| float( | |
| np.percentile( | |
| np.linalg.norm(visible_points - np.asarray(scene["target"]), axis=-1), | |
| 95, | |
| ) | |
| ), | |
| 0.06, | |
| ) | |
| scene["fov"] = 55.0 | |
| scene["states"] = [] | |
| provenance = {} | |
| for model, values in predictions.items(): | |
| metadata = values["metadata"] | |
| if ( | |
| metadata["weight_role"] != "ema_teacher" | |
| or metadata["checkpoint_step"] != checkpoint_step | |
| ): | |
| raise ValueError("Expected saved EMA teachers from the same checkpoint step") | |
| if metadata.get("query_fingerprint") != record.get("query_fingerprint"): | |
| raise ValueError("Teacher predictions do not match the scored query set") | |
| provenance[model] = { | |
| "teacher_state_sha256": metadata["teacher_state_sha256"], | |
| "checkpoint_step": metadata["checkpoint_step"], | |
| "weight_role": metadata["weight_role"], | |
| } | |
| for index, camera in enumerate(values["camera_modes"].tolist()): | |
| positions = values["positions"][index] | |
| valid = np.isfinite(positions).all(-1) & (positions[..., 2] > 0) | |
| valid &= np.abs(positions).max(-1) < 32 | |
| scores = metadata["scores"][camera] | |
| state = { | |
| "model": model, | |
| "camera": camera, | |
| "views": 3, | |
| **track_layer(positions, valid), | |
| "aj": scores["aj"], | |
| "apd": scores["apd"], | |
| "mean_error_cm": scores["epe"], | |
| } | |
| if "dense_positions" in values: | |
| state["dense"] = { | |
| **track_layer( | |
| values["dense_positions"][index], | |
| values["dense_valid"][index], | |
| ), | |
| "colors": values["dense_colors"].tolist(), | |
| } | |
| scene["states"].append(state) | |
| scene["model_labels"] = {"b2_ema": "EMA teacher B2", "b3_ema": "EMA teacher B3"} | |
| scene["model_description"] = ( | |
| f"Saved EMA teacher weights at {checkpoint_step:,} fine-tuning steps " | |
| "from the common pre-Forge 50k initializer" | |
| ) | |
| scene["comparison_description"] = ( | |
| "B2 and B3 are separately trained EMA teachers. ON/OFF states within each " | |
| "teacher use the same weights, images, queries, and three views. The default " | |
| "compares B3 OFF with B2 OFF to evaluate the effect of camera distillation " | |
| "when camera conditioning is unavailable at inference." | |
| ) | |
| scene["checkpoint_iteration"] = checkpoint_step | |
| scene["match_teacher_inputs"] = True | |
| scene["checkpoint_provenance"] = provenance | |
| scene["default_comparison"] = { | |
| "views": 3, | |
| "primary_model": "b3_ema", | |
| "primary_camera": "off", | |
| "comparison_model": "b2_ema", | |
| "comparison_camera": "off", | |
| } | |
| scene["benchmark_scores"] = [ | |
| { | |
| "model": state["model"], | |
| "camera": state["camera"], | |
| "views": 3, | |
| "aj": state["aj"], | |
| "apd": state["apd"], | |
| } | |
| for state in scene["states"] | |
| ] | |
| validate_scene(scene) | |
| return scene | |