Download examples/visualizer_code/source/d4rt_scripts/demo/scale4d_camera_ablation.py from facebook/SCALE4D: direct link, hf CLI and curl.
- Browser
- Download file 2.72 kB
-
https://huggingface.co/facebook/SCALE4D/resolve/main/examples/visualizer_code/source/d4rt_scripts/demo/scale4d_camera_ablation.py
- Command line
-
hf download hf://facebook/SCALE4D/examples/visualizer_code/source/d4rt_scripts/demo/scale4d_camera_ablation.py
-
curl -L -o scale4d_camera_ablation.py https://huggingface.co/facebook/SCALE4D/resolve/main/examples/visualizer_code/source/d4rt_scripts/demo/scale4d_camera_ablation.py
2.72 kB
| """Compare camera conditioning ON/OFF with the same frozen SCALE4D checkpoint.""" | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| def main() -> None: | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--input", type=Path, required=True) | |
| parser.add_argument("--output", type=Path, required=True) | |
| parser.add_argument("--checkpoint", default="facebook/SCALE4D") | |
| parser.add_argument("--revision") | |
| parser.add_argument("--device", default="cuda:0") | |
| parser.add_argument("--views", type=int, choices=range(1, 11), default=3) | |
| parser.add_argument("--labels", type=Path) | |
| parser.add_argument("--no-flash-attn", action="store_true") | |
| args = parser.parse_args() | |
| if args.output.exists(): | |
| parser.error("The output directory already exists") | |
| import numpy as np | |
| from evaluation.scale4d import SCALE4D | |
| from evaluation.scale4d_inputs import npz_clip | |
| from evaluation.ucpe_inputs import ClipLabels | |
| from evaluation.ucpe_scores import score_tracks | |
| clip = npz_clip(args.input) | |
| if clip.rgbs.shape[0] < args.views: | |
| parser.error("The input does not contain enough calibrated camera videos") | |
| labels = None | |
| if args.labels: | |
| with np.load(args.labels, allow_pickle=False) as data: | |
| labels = ClipLabels( | |
| data["gt_tracks_target_camera"], | |
| data["visibility"], | |
| data.get("visibility_known", None), | |
| ) | |
| model = SCALE4D( | |
| args.checkpoint, | |
| revision=args.revision, | |
| device=args.device, | |
| use_flash_attn=not args.no_flash_attn, | |
| max_input_views=max(3, args.views), | |
| ) | |
| args.output.mkdir(parents=True) | |
| scores = {} | |
| evaluation_mask = ( | |
| np.arange(clip.rgbs.shape[1])[:, None] > clip.query_times.numpy()[None] | |
| if clip.metadata.get("query_policy") | |
| in ("first_visible_left", "first_visible_anchor") | |
| else None | |
| ) | |
| for camera in ("on", "off"): | |
| prediction = model.predict_clip(clip, views=args.views, camera=camera) | |
| path = prediction.save(args.output / f"camera_{camera}.npz") | |
| if labels is not None: | |
| scores[camera] = score_tracks( | |
| labels, | |
| prediction.uv_depth.numpy(), | |
| prediction.visibility.numpy(), | |
| evaluation_mask=evaluation_mask, | |
| )["metric"] | |
| print(f"Saved camera {camera.upper()}: {path}", flush=True) | |
| if scores: | |
| scores["aj_gain_points"] = scores["on"]["aj"] - scores["off"]["aj"] | |
| (args.output / "scores.json").write_text(json.dumps(scores, indent=2) + "\n") | |
| if __name__ == "__main__": | |
| main() | |