Spaces:
Running on Zero
Running on Zero
Download models/motion_net/cotracker.py from Charlie839242/StreamTAP3D: direct link, hf CLI and curl.
- Browser
- Download file 7.4 kB
-
https://huggingface.co/spaces/Charlie839242/StreamTAP3D/resolve/main/models/motion_net/cotracker.py
- Command line
-
hf download hf://spaces/Charlie839242/StreamTAP3D/models/motion_net/cotracker.py
-
curl -L -o cotracker.py https://huggingface.co/spaces/Charlie839242/StreamTAP3D/resolve/main/models/motion_net/cotracker.py
7.4 kB
| import torch | |
| import torch.nn as nn | |
| from .blocks import ( | |
| Mlp, | |
| AttnBlock, | |
| Attention, | |
| ) | |
| from .ttt_memory import 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, | |
| ttt_layers=(), | |
| ttt_type="linear", | |
| ttt_head_dim=32, | |
| ttt_base_lr=0.1, | |
| ): | |
| super().__init__() | |
| self.num_heads = num_heads | |
| self.hidden_size = hidden_size | |
| 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) | |
| self.ttt_layers = [int(layer) for layer in ttt_layers] | |
| if len(self.ttt_layers) != len(set(self.ttt_layers)): | |
| raise ValueError("ttt_layers must be unique, got %s" % (self.ttt_layers,)) | |
| if any(layer < 0 or layer >= time_depth for layer in self.ttt_layers): | |
| raise ValueError( | |
| "ttt_layers %s are outside time_depth %d" % (self.ttt_layers, time_depth) | |
| ) | |
| if self.ttt_layers: | |
| 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, hidden_size, ttt_head_dim, 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): | |
| return [memory.init_state(num_tracks) for memory in self.ttt_memories] | |
| def ttt_write(self, write_token, states): | |
| return [ | |
| memory.write(write_token, state) | |
| for memory, state in zip(self.ttt_memories, states) | |
| ] | |
| def forward(self, input_tensor, ttt_states): | |
| tokens = self.input_transform(input_tensor) | |
| B, _, T, _ = 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 | |
| if len(ttt_states) != len(self.ttt_layers): | |
| raise ValueError( | |
| "expected %d ttt states, got %d" % (len(self.ttt_layers), len(ttt_states)) | |
| ) | |
| 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: | |
| layer = self.ttt_layers.index(i) | |
| point_tokens = tokens[:, :n_real].reshape(B * n_real, T, -1) | |
| ttt_out = self.ttt_memories[layer].read( | |
| self.ttt_norms[layer](point_tokens), ttt_states[layer] | |
| ) | |
| point_tokens = point_tokens + (self.ttt_gammas[layer] * 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 | |
| ) | |
| virtual_tokens = self.space_virtual_blocks[j](virtual_tokens) | |
| point_tokens = self.space_point2virtual_blocks[j]( | |
| point_tokens, virtual_tokens | |
| ) | |
| 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) | |
| return flow | |
| class CrossAttnBlock(nn.Module): | |
| def __init__( | |
| self, hidden_size, context_dim, num_heads=1, mlp_ratio=4.0, **block_kwargs | |
| ): | |
| super().__init__() | |
| self.norm1 = nn.LayerNorm(hidden_size, elementwise_affine=False, eps=1e-6) | |
| self.norm_context = nn.LayerNorm(hidden_size) | |
| self.cross_attn = Attention( | |
| hidden_size, | |
| context_dim=context_dim, | |
| num_heads=num_heads, | |
| qkv_bias=True, | |
| **block_kwargs | |
| ) | |
| self.norm2 = nn.LayerNorm(hidden_size, elementwise_affine=False, eps=1e-6) | |
| mlp_hidden_dim = int(hidden_size * mlp_ratio) | |
| approx_gelu = lambda: nn.GELU(approximate="tanh") | |
| self.mlp = Mlp( | |
| in_features=hidden_size, | |
| hidden_features=mlp_hidden_dim, | |
| act_layer=approx_gelu, | |
| drop=0, | |
| ) | |
| def forward(self, x, context): | |
| x = x + self.cross_attn(self.norm1(x), context=self.norm_context(context)) | |
| x = x + self.mlp(self.norm2(x)) | |
| return x | |