Spaces:
Running on Zero

StreamTAP3D / models /utils /cotracker_utils.py
Charlie839242's picture
Add StreamTAP3D mask-only ZeroGPU demo
7547a6b
Raw History Blame Contribute Delete
5.42 kB
import torch
import torch.nn.functional as F
# Creating the scales from a python list implies a host-to-device copy, which would block on every call
_POSENC_SCALES_CACHE = {}
_GRID_SCALE_CACHE = {}
def _grid_sample_scales(sizes, align_corners, device):
"""Pixel-to-grid scales. A host copy on every sample cannot be captured in a CUDA graph."""
key = (tuple(sizes), bool(align_corners), device)
scales = _GRID_SCALE_CACHE.get(key)
if scales is None:
if align_corners:
values = [2 / max(size - 1, 1) for size in reversed(sizes)]
else:
values = [2 / size for size in reversed(sizes)]
scales = torch.tensor(values, device=device, dtype=torch.float32)
_GRID_SCALE_CACHE[key] = scales
return scales
def _get_posenc_scales(min_deg, max_deg, dtype, device):
key = (min_deg, max_deg, dtype, device)
if key not in _POSENC_SCALES_CACHE:
_POSENC_SCALES_CACHE[key] = torch.tensor(
[2**i for i in range(min_deg, max_deg)], dtype=dtype, device=device
)
return _POSENC_SCALES_CACHE[key]
def posenc(x, *, min_deg, max_deg):
"""Cat x with a positional encoding of x with scales 2^[min_deg, max_deg-1].
Instead of computing [sin(x), cos(x)], we use the trig identity
cos(x) = sin(x + pi/2) and do one vectorized call to sin([x, x+pi/2]).
Args:
x: torch.Tensor, variables to be encoded. Note that x should be in [-pi, pi].
min_deg: int, the minimum (inclusive) degree of the encoding.
max_deg: int, the maximum (exclusive) degree of the encoding.
Returns:
encoded: torch.Tensor, encoded variables.
"""
if min_deg == max_deg:
return x
scales = _get_posenc_scales(min_deg, max_deg, x.dtype, x.device)
xb = (x[..., None, :] * scales[:, None]).reshape(list(x.shape[:-1]) + [-1])
four_feat = torch.sin(torch.cat([xb, xb + 0.5 * torch.pi], dim=-1))
return torch.cat([x] + [four_feat], dim=-1)
def bilinear_sampler(input, coords, mode="bilinear", align_corners=True, padding_mode="border"):
r"""Sample a tensor using bilinear interpolation
`bilinear_sampler(input, coords)` samples a tensor :attr:`input` at
coordinates :attr:`coords` using bilinear interpolation. It is the same
as `torch.nn.functional.grid_sample()` but with a different coordinate
convention.
The input tensor is assumed to be of shape :math:`(B, C, H, W)`, where
:math:`B` is the batch size, :math:`C` is the number of channels,
:math:`H` is the height of the image, and :math:`W` is the width of the
image. The tensor :attr:`coords` of shape :math:`(B, H_o, W_o, 2)` is
interpreted as an array of 2D point coordinates :math:`(x_i,y_i)`.
Alternatively, the input tensor can be of size :math:`(B, C, T, H, W)`,
in which case sample points are triplets :math:`(t_i,x_i,y_i)`. Note
that in this case the order of the components is slightly different
from `grid_sample()`, which would expect :math:`(x_i,y_i,t_i)`.
If `align_corners` is `True`, the coordinate :math:`x` is assumed to be
in the range :math:`[0,W-1]`, with 0 corresponding to the center of the
left-most image pixel :math:`W-1` to the center of the right-most
pixel.
If `align_corners` is `False`, the coordinate :math:`x` is assumed to
be in the range :math:`[0,W]`, with 0 corresponding to the left edge of
the left-most pixel :math:`W` to the right edge of the right-most
pixel.
Similar conventions apply to the :math:`y` for the range
:math:`[0,H-1]` and :math:`[0,H]` and to :math:`t` for the range
:math:`[0,T-1]` and :math:`[0,T]`.
Args:
input (Tensor): batch of input images.
coords (Tensor): batch of coordinates.
align_corners (bool, optional): Coordinate convention. Defaults to `True`.
padding_mode (str, optional): Padding mode. Defaults to `"border"`.
Returns:
Tensor: sampled points.
"""
sizes = input.shape[2:]
assert len(sizes) in [2, 3]
if len(sizes) == 3:
# t x y -> x y t to match dimensions T H W in grid_sample
coords = torch.stack([coords[..., 1], coords[..., 2], coords[..., 0]], dim=-1)
coords = coords * _grid_sample_scales(sizes, align_corners, coords.device)
coords -= 1
return F.grid_sample(
input, coords, align_corners=align_corners, padding_mode=padding_mode, mode=mode
)
def get_1d_sincos_pos_embed_from_grid(
embed_dim: int, pos: torch.Tensor
) -> torch.Tensor:
"""
This function generates a 1D positional embedding from a given grid using sine and cosine functions.
Args:
- embed_dim: The embedding dimension.
- pos: The position to generate the embedding from.
Returns:
- emb: The generated 1D positional embedding.
"""
if embed_dim % 2 != 0:
result = get_1d_sincos_pos_embed_from_grid(embed_dim - 1, pos)
return torch.cat([result, result.new_zeros(result.shape[:-1] + (1,))], dim=-1)
omega = torch.arange(embed_dim // 2, dtype=torch.double)
omega /= embed_dim / 2.0
omega = 1.0 / 10000**omega # (D/2,)
pos = pos.reshape(-1) # (M,)
out = torch.einsum("m,d->md", pos, omega) # (M, D/2), outer product
emb_sin = torch.sin(out) # (M, D/2)
emb_cos = torch.cos(out) # (M, D/2)
emb = torch.cat([emb_sin, emb_cos], dim=1) # (M, D)
return emb[None].float()