Spaces:
Running on Zero
Running on Zero
Download models/point_updaters/efficient_updateformer.py from Charlie839242/StreamTAP3D: direct link, hf CLI and curl.
- Browser
- Download file 6.65 kB
-
https://huggingface.co/spaces/Charlie839242/StreamTAP3D/resolve/main/models/point_updaters/efficient_updateformer.py
- Command line
-
hf download hf://spaces/Charlie839242/StreamTAP3D/models/point_updaters/efficient_updateformer.py
-
curl -L -o efficient_updateformer.py https://huggingface.co/spaces/Charlie839242/StreamTAP3D/resolve/main/models/point_updaters/efficient_updateformer.py
6.65 kB
| from typing import List | |
| import torch | |
| import torch.nn as nn | |
| from einops import rearrange | |
| from ..utils.cotracker_blocks import ( | |
| AttnBlock, | |
| CrossAttnBlock, | |
| Attention, | |
| ) | |
| from .ttt_memory import TTTState, build_ttt_memory | |
| class EfficientUpdateFormer(nn.Module): | |
| """ | |
| Transformer model that updates track estimates. | |
| """ | |
| def __init__( | |
| self, | |
| space_depth=6, | |
| time_depth=6, | |
| input_dim=320, | |
| hidden_size=384, | |
| num_heads=8, | |
| output_dim=130, | |
| mlp_ratio=4.0, | |
| num_virtual_tracks=64, | |
| add_space_attn=True, | |
| linear_layer_for_vis_conf=True, | |
| ttt_layers=(), | |
| ttt_type="linear", | |
| ttt_write_dim=None, | |
| ttt_head_dim=64, | |
| ttt_base_lr=0.1, | |
| ): | |
| super().__init__() | |
| assert add_space_attn and linear_layer_for_vis_conf | |
| self.num_heads = num_heads | |
| self.hidden_size = hidden_size | |
| self.input_dim = input_dim | |
| self.input_transform = torch.nn.Linear(input_dim, hidden_size, bias=True) | |
| self.flow_head = torch.nn.Linear(hidden_size, output_dim - 2, bias=True) | |
| self.vis_conf_head = torch.nn.Linear(hidden_size, 2, bias=True) | |
| self.num_virtual_tracks = num_virtual_tracks | |
| self.virual_tracks = nn.Parameter( | |
| torch.randn(1, num_virtual_tracks, 1, hidden_size) | |
| ) | |
| self.time_blocks = nn.ModuleList( | |
| [ | |
| AttnBlock( | |
| hidden_size, | |
| num_heads, | |
| mlp_ratio=mlp_ratio, | |
| attn_class=Attention, | |
| ) | |
| for _ in range(time_depth) | |
| ] | |
| ) | |
| self.space_virtual_blocks = nn.ModuleList( | |
| [ | |
| AttnBlock( | |
| hidden_size, | |
| num_heads, | |
| mlp_ratio=mlp_ratio, | |
| attn_class=Attention, | |
| ) | |
| for _ in range(space_depth) | |
| ] | |
| ) | |
| self.space_point2virtual_blocks = nn.ModuleList( | |
| [ | |
| CrossAttnBlock( | |
| hidden_size, hidden_size, num_heads, mlp_ratio=mlp_ratio | |
| ) | |
| for _ in range(space_depth) | |
| ] | |
| ) | |
| self.space_virtual2point_blocks = nn.ModuleList( | |
| [ | |
| CrossAttnBlock( | |
| hidden_size, hidden_size, num_heads, mlp_ratio=mlp_ratio | |
| ) | |
| for _ in range(space_depth) | |
| ] | |
| ) | |
| assert len(self.time_blocks) >= len(self.space_virtual2point_blocks) | |
| # Per-track TTT memory read after the selected time blocks | |
| self.ttt_layers = [int(i) for i in ttt_layers] | |
| assert len(set(self.ttt_layers)) == len(self.ttt_layers) and all(0 <= i < time_depth for i in self.ttt_layers) | |
| assert self.ttt_layers and ttt_write_dim is not None | |
| self.ttt_norms = nn.ModuleList([nn.LayerNorm(hidden_size, elementwise_affine=False, eps=1e-6) for _ in self.ttt_layers]) | |
| self.ttt_memories = nn.ModuleList([ | |
| build_ttt_memory(ttt_type, hidden_size, ttt_write_dim, head_dim=ttt_head_dim, base_lr=ttt_base_lr) for _ in self.ttt_layers | |
| ]) | |
| self.ttt_gammas = nn.ParameterList([nn.Parameter(torch.zeros(hidden_size)) for _ in self.ttt_layers]) | |
| def ttt_init_states(self, num_tracks: int) -> List[TTTState]: | |
| return [memory.init_state(num_tracks) for memory in self.ttt_memories] | |
| def ttt_write(self, write_token: torch.Tensor, states: List[TTTState]) -> List[TTTState]: | |
| """ | |
| write_token: (M, ttt_write_dim), states: the per-layer states of the M tracks | |
| """ | |
| return [memory.write(write_token, state) for memory, state in zip(self.ttt_memories, states)] | |
| def forward(self, input_tensor, mask, ttt_states: List[TTTState]): | |
| """ | |
| ttt_states holds the per-layer TTT states of the B * N real tracks, read by every token of each track. | |
| """ | |
| tokens = self.input_transform(input_tensor) | |
| tokens = rearrange(tokens, 'b t n c -> b n t c') | |
| B, _, T, _ = tokens.shape | |
| # At the moment we don't want to deal with T larger than 16. | |
| assert T <= 16, f"The shape of input_tensor should be (b, t, n, c), but you passed tokens with shape {tokens.shape}." | |
| virtual_tokens = self.virual_tracks.repeat(B, 1, T, 1) | |
| tokens = torch.cat([tokens, virtual_tokens], dim=1) | |
| _, N, _, _ = tokens.shape | |
| N_real = N - self.num_virtual_tracks | |
| assert len(ttt_states) == len(self.ttt_layers) | |
| j = 0 | |
| for i in range(len(self.time_blocks)): | |
| time_tokens = tokens.contiguous().view(B * N, T, -1) # B N T C -> (B N) T C | |
| time_tokens = self.time_blocks[i](time_tokens) | |
| tokens = time_tokens.view(B, N, T, -1) # (B N) T C -> B N T C | |
| if i in self.ttt_layers: | |
| l = self.ttt_layers.index(i) | |
| point_tokens = tokens[:, :N_real].reshape(B * N_real, T, -1) | |
| ttt_out = self.ttt_memories[l].read(self.ttt_norms[l](point_tokens), ttt_states[l]) | |
| point_tokens = point_tokens + (self.ttt_gammas[l] * ttt_out).to(point_tokens.dtype) | |
| tokens = torch.cat([point_tokens.view(B, N_real, T, -1), tokens[:, N_real:]], dim=1) | |
| if i % (len(self.time_blocks) // len(self.space_virtual_blocks)) == 0: | |
| space_tokens = ( | |
| tokens.permute(0, 2, 1, 3).contiguous().view(B * T, N, -1) | |
| ) # B N T C -> (B T) N C | |
| point_tokens = space_tokens[:, : N - self.num_virtual_tracks] | |
| virtual_tokens = space_tokens[:, N - self.num_virtual_tracks :] | |
| virtual_tokens = self.space_virtual2point_blocks[j]( | |
| virtual_tokens, point_tokens, mask=mask | |
| ) | |
| virtual_tokens = self.space_virtual_blocks[j](virtual_tokens) | |
| point_tokens = self.space_point2virtual_blocks[j]( | |
| point_tokens, virtual_tokens, mask=mask | |
| ) | |
| space_tokens = torch.cat([point_tokens, virtual_tokens], dim=1) | |
| tokens = space_tokens.view(B, T, N, -1).permute( | |
| 0, 2, 1, 3 | |
| ) # (B T) N C -> B N T C | |
| j += 1 | |
| tokens = tokens[:, : N - self.num_virtual_tracks] | |
| flow = self.flow_head(tokens) | |
| vis_conf = self.vis_conf_head(tokens) | |
| flow = torch.cat([flow, vis_conf], dim=-1) | |
| flow = rearrange(flow, 'b n t c -> b t n c') | |
| return flow | |