Spaces:
Sleeping
Sleeping
Download engine/architecture.py from MesserMMP/syntax-video-infer: direct link, hf CLI and curl.
- Browser
- Download file 2.67 kB
-
https://huggingface.co/spaces/MesserMMP/syntax-video-infer/resolve/main/engine/architecture.py
- Command line
-
hf download hf://spaces/MesserMMP/syntax-video-infer/engine/architecture.py
-
curl -L -o architecture.py https://huggingface.co/spaces/MesserMMP/syntax-video-infer/resolve/main/engine/architecture.py
2.67 kB
| """Inference-only architecture with state keys identical to the published checkpoint.""" | |
| import torch | |
| from torch import nn | |
| from torchvision.models.video import r3d_18 | |
| class SyntaxModel(nn.Module): | |
| def __init__(self): | |
| super().__init__() | |
| # Full checkpoints already contain the backbone. No Kinetics download. | |
| self.model = r3d_18(weights=None) | |
| self.model.fc = nn.Identity() | |
| self.lstm = nn.LSTM(512, 128, proj_size=2, batch_first=True) | |
| def aggregate(self, features): | |
| outputs, _ = self.lstm(features) | |
| return outputs.mean(dim=1) | |
| def forward(self, clips): | |
| batch, sequence = clips.shape[:2] | |
| features = self.model(clips.flatten(0, 1)).unflatten(0, (batch, sequence)) | |
| return self.aggregate(features) | |
| def forward_stream(self, clips, device="cpu", progress=None): | |
| features = [] | |
| for index, clip in enumerate(clips): | |
| features.append(self.model(clip.unsqueeze(0).to(device))) | |
| if progress: | |
| progress(index + 1) | |
| return self.aggregate(torch.stack(features, dim=1)) | |
| def artery_model(): | |
| model = r3d_18(weights=None) | |
| model.fc = nn.Linear(512, 1) | |
| return model | |
| def classifier_stream(model, frame_count, get_clip, device="cpu", chunk_outputs=16): | |
| """Exact R3D-18 temporal tiling, with bounded activation memory. | |
| Its final feature map has temporal stride 8 and receptive field 109. A | |
| 56-frame context (rounded up to a multiple of 8) on either side therefore | |
| preserves every output's complete context and global stride alignment. | |
| Only disjoint final feature positions contribute to global average pooling; | |
| overlap is context, never a duplicate acquisition or a repeated prediction. | |
| get_clip(start, stop) returns the normalized C,T,H,W source interval. | |
| """ | |
| total_outputs = (frame_count + 7) // 8 | |
| pooled_sum = None | |
| for output_start in range(0, total_outputs, chunk_outputs): | |
| output_stop = min(output_start + chunk_outputs, total_outputs) | |
| source_start = max(0, output_start * 8 - 56) | |
| source_stop = min(frame_count, output_stop * 8 + 56) | |
| clip = get_clip(source_start, source_stop).unsqueeze(0).to(device) | |
| features = model.layer4(model.layer3(model.layer2(model.layer1(model.stem(clip))))) | |
| first = output_start - source_start // 8 | |
| features = features[:, :, first:first + output_stop - output_start] | |
| contribution = features.mean(dim=(-2, -1)).sum(dim=2) | |
| pooled_sum = contribution if pooled_sum is None else pooled_sum + contribution | |
| del clip, features | |
| return model.fc(pooled_sum / total_outputs) | |