Download examples/visualizer_code/source/evaluation/scale4d.py from facebook/SCALE4D: direct link, hf CLI and curl.
- Browser
- Download file 4.17 kB
-
https://huggingface.co/facebook/SCALE4D/resolve/main/examples/visualizer_code/source/evaluation/scale4d.py
- Command line
-
hf download hf://facebook/SCALE4D/examples/visualizer_code/source/evaluation/scale4d.py
-
curl -L -o scale4d.py https://huggingface.co/facebook/SCALE4D/resolve/main/examples/visualizer_code/source/evaluation/scale4d.py
4.17 kB
| """Direct SCALE4D inference with the checkpoint's recorded model configuration.""" | |
| from __future__ import annotations | |
| from dataclasses import asdict | |
| from pathlib import Path | |
| import numpy as np | |
| import torch | |
| from evaluation.scale4d_inputs import array_clip, npz_clip, video_clip | |
| from evaluation.ucpe_inference import Prediction, UCPEEvaluator | |
| from evaluation.ucpe_inputs import ClipInput | |
| from nets.scale4d import HF_REPO_ID, HF_REVISION, load_model | |
| class SCALE4D: | |
| def __init__( | |
| self, | |
| checkpoint: str | Path = HF_REPO_ID, | |
| *, | |
| device: str | torch.device = "cuda:0", | |
| revision: str | None = None, | |
| config_path: str | Path | None = None, | |
| use_flash_attn: bool = True, | |
| decoder_chunk: int = 512, | |
| max_input_views: int = 3, | |
| ) -> None: | |
| device = torch.device(device) | |
| if device.type == "cuda": | |
| if not torch.cuda.is_available(): | |
| raise RuntimeError( | |
| "CUDA is unavailable; install a CUDA-compatible PyTorch build" | |
| ) | |
| torch.cuda.set_device(device) | |
| model, self.info = load_model( | |
| checkpoint, | |
| revision=revision, | |
| config_path=config_path, | |
| use_flash_attn=use_flash_attn, | |
| ) | |
| from evaluation.scale4d_view_count import ( | |
| decoder_slot_indices, | |
| extend_decoder_view_embeddings, | |
| ) | |
| self.decoder_slots = decoder_slot_indices(max_input_views) | |
| if max_input_views > 3: | |
| extend_decoder_view_embeddings(model, max_input_views) | |
| self.evaluator = UCPEEvaluator( | |
| model, | |
| device, | |
| decoder_chunk=decoder_chunk, | |
| camera_gate_m=self.info.camera_condition_max_diameter_m, | |
| max_input_views=max_input_views, | |
| ) | |
| self.checkpoint = str(checkpoint) | |
| self.revision = revision or ( | |
| HF_REVISION if self.checkpoint == HF_REPO_ID else None | |
| ) | |
| def predict_clip( | |
| self, clip: ClipInput, *, views: int | None = None, camera: str = "off" | |
| ) -> Prediction: | |
| result = self.evaluator.predict( | |
| clip, views=clip.rgbs.shape[0] if views is None else views, camera=camera | |
| ) | |
| result.metadata.update( | |
| checkpoint=self.checkpoint, | |
| checkpoint_step=self.info.step, | |
| checkpoint_revision=self.revision, | |
| model_config=asdict(self.info), | |
| decoder_slot_indices=list(self.decoder_slots[: len(result.camera_ids)]), | |
| ) | |
| return result | |
| def predict( | |
| self, | |
| rgbs: np.ndarray | torch.Tensor, | |
| *, | |
| query_uv: np.ndarray | torch.Tensor | None = None, | |
| query_times: np.ndarray | torch.Tensor | None = None, | |
| cameras_to_world: np.ndarray | torch.Tensor | None = None, | |
| intrinsics: np.ndarray | torch.Tensor | None = None, | |
| world_up: np.ndarray | torch.Tensor | None = None, | |
| grid_size: int = 16, | |
| views: int | None = None, | |
| camera: str = "off", | |
| metadata: dict | None = None, | |
| ) -> Prediction: | |
| if camera == "on" and any( | |
| value is None for value in (cameras_to_world, intrinsics, world_up) | |
| ): | |
| raise ValueError( | |
| "Camera ON requires cameras_to_world, intrinsics, and world_up" | |
| ) | |
| clip = array_clip( | |
| rgbs, | |
| query_uv=query_uv, | |
| query_times=query_times, | |
| cameras_to_world=cameras_to_world, | |
| intrinsics=intrinsics, | |
| world_up=world_up, | |
| grid_size=grid_size, | |
| metadata=metadata, | |
| ) | |
| return self.predict_clip(clip, views=views, camera=camera) | |
| def predict_video(self, path: str | Path, *, grid_size: int = 16) -> Prediction: | |
| clip = video_clip(path, self.evaluator.model.encoder.num_frames, grid_size) | |
| return self.predict_clip(clip, camera="off") | |
| def predict_npz( | |
| self, path: str | Path, *, views: int | None = None, camera: str = "off" | |
| ) -> Prediction: | |
| return self.predict_clip(npz_clip(path), views=views, camera=camera) | |