(self, image: torch.Tensor, token_rows: int, token_cols: int, return_class_token: bool = False)
| 109 | wrap_dinov2_attention_with_sdpa(self.backbone.blocks[i].attn) |
| 110 | |
| 111 | def forward(self, image: torch.Tensor, token_rows: int, token_cols: int, return_class_token: bool = False) -> Tuple[torch.Tensor, torch.Tensor]: |
| 112 | image_14 = F.interpolate(image, (token_rows * 14, token_cols * 14), mode="bilinear", align_corners=False, antialias=True) |
| 113 | image_14 = (image_14 - self.image_mean) / self.image_std |
| 114 | |
| 115 | # Get intermediate layers from the backbone |
| 116 | features = self.backbone.get_intermediate_layers(image_14, n=self.intermediate_layers, return_class_token=True) |
| 117 | |
| 118 | # Project features to the desired dimensionality |
| 119 | x = torch.stack([ |
| 120 | proj(feat.permute(0, 2, 1).unflatten(2, (token_rows, token_cols)).contiguous()) |
| 121 | for proj, (feat, clstoken) in zip(self.output_projections, features) |
| 122 | ], dim=1).sum(dim=1) |
| 123 | |
| 124 | if return_class_token: |
| 125 | return x, features[-1][1] |
| 126 | else: |
| 127 | return x |
| 128 | |
| 129 | |
| 130 | class Resampler(nn.Sequential): |
nothing calls this directly
no test coverage detected