Download examples/visualizer_code/source/nets/ucpe_cd50_stable/ucpe_transition.py from facebook/SCALE4D: direct link, hf CLI and curl.
- Browser
- Download file 2.1 kB
-
https://huggingface.co/facebook/SCALE4D/resolve/main/examples/visualizer_code/source/nets/ucpe_cd50_stable/ucpe_transition.py
- Command line
-
hf download hf://facebook/SCALE4D/examples/visualizer_code/source/nets/ucpe_cd50_stable/ucpe_transition.py
-
curl -L -o ucpe_transition.py https://huggingface.co/facebook/SCALE4D/resolve/main/examples/visualizer_code/source/nets/ucpe_cd50_stable/ucpe_transition.py
2.1 kB
| # (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary. | |
| """Shared-parameter interpolation between first-frame and two-frame UCPE.""" | |
| from __future__ import annotations | |
| import torch | |
| from nets.ucpe_cd50_stable.ucpe import RelativeRayAttention as _FirstFrameAttention | |
| from nets.ucpe_cd50_stable.ucpe_tubelet import ( | |
| RelativeRayAttention as _TwoFrameAttention, | |
| ) | |
| from nets.ucpe_cd50_stable.ucpe_tubelet import ( | |
| build_lat_up_encoding, | |
| build_relative_ray_transforms, | |
| ) | |
| __all__ = [ | |
| "RelativeRayAttention", | |
| "build_lat_up_encoding", | |
| "build_relative_ray_transforms", | |
| ] | |
| class RelativeRayAttention(_TwoFrameAttention): | |
| transition_scale: float = 1.0 | |
| def forward( | |
| self, | |
| tokens: torch.Tensor, | |
| ray_transforms: torch.Tensor, | |
| *, | |
| spatial_height: int, | |
| spatial_width: int, | |
| lat_up_encoding: torch.Tensor | None = None, | |
| lat_up_valid: torch.Tensor | None = None, | |
| ) -> torch.Tensor: | |
| scale = self.transition_scale | |
| if not 0.0 <= scale <= 1.0: | |
| raise ValueError("UCPE transition scale must be in [0, 1]") | |
| self._validate_geometry( | |
| tokens, ray_transforms, spatial_height, spatial_width, lat_up_encoding | |
| ) | |
| first = None | |
| if scale < 1.0: | |
| first = _FirstFrameAttention.forward( | |
| self, | |
| tokens, | |
| ray_transforms[:, :, 0], | |
| spatial_height=spatial_height, | |
| spatial_width=spatial_width, | |
| lat_up_encoding=None | |
| if lat_up_encoding is None | |
| else lat_up_encoding[:, :, 0], | |
| lat_up_valid=lat_up_valid, | |
| ) | |
| if scale == 0.0: | |
| return first | |
| both = super().forward( | |
| tokens, | |
| ray_transforms, | |
| spatial_height=spatial_height, | |
| spatial_width=spatial_width, | |
| lat_up_encoding=lat_up_encoding, | |
| lat_up_valid=lat_up_valid, | |
| ) | |
| return both if first is None else first + scale * (both - first) | |