Args: x (Tensor): Has shape (B, H*W, C_in). input_size (tuple[int]): The spatial shape of x, arrange as (H, W). Default: None. Returns: tuple: Contains merged results and its spatial shape. - x (Tensor): Has shape
(self, x, input_size)
| 272 | self.reduction = nn.Linear(sample_dim, out_channels, bias=bias) |
| 273 | |
| 274 | def forward(self, x, input_size): |
| 275 | """ |
| 276 | Args: |
| 277 | x (Tensor): Has shape (B, H*W, C_in). |
| 278 | input_size (tuple[int]): The spatial shape of x, arrange as (H, W). |
| 279 | Default: None. |
| 280 | |
| 281 | Returns: |
| 282 | tuple: Contains merged results and its spatial shape. |
| 283 | |
| 284 | - x (Tensor): Has shape (B, Merged_H * Merged_W, C_out) |
| 285 | - out_size (tuple[int]): Spatial shape of x, arrange as |
| 286 | (Merged_H, Merged_W). |
| 287 | """ |
| 288 | B, L, C = x.shape |
| 289 | assert isinstance(input_size, Sequence), f"Expect input_size is `Sequence` but get {input_size}" |
| 290 | |
| 291 | H, W = input_size |
| 292 | assert L == H * W, "input feature has wrong size" |
| 293 | |
| 294 | x = x.view(B, H, W, C).permute([0, 3, 1, 2]) # B, C, H, W |
| 295 | # Use nn.Unfold to merge patch. About 25% faster than original method, |
| 296 | # but need to modify pretrained model for compatibility |
| 297 | |
| 298 | if self.adap_padding: |
| 299 | x = self.adap_padding(x) |
| 300 | H, W = x.shape[-2:] |
| 301 | |
| 302 | x = self.sampler(x) |
| 303 | # if kernel_size=2 and stride=2, x should has shape (B, 4*C, H/2*W/2) |
| 304 | |
| 305 | out_h = ( |
| 306 | H + 2 * self.sampler.padding[0] - self.sampler.dilation[0] * (self.sampler.kernel_size[0] - 1) - 1 |
| 307 | ) // self.sampler.stride[0] + 1 |
| 308 | out_w = ( |
| 309 | W + 2 * self.sampler.padding[1] - self.sampler.dilation[1] * (self.sampler.kernel_size[1] - 1) - 1 |
| 310 | ) // self.sampler.stride[1] + 1 |
| 311 | |
| 312 | output_size = (out_h, out_w) |
| 313 | x = x.transpose(1, 2) # B, H/2*W/2, 4*C |
| 314 | x = self.norm(x) if self.norm else x |
| 315 | x = self.reduction(x) |
| 316 | return x, output_size |
nothing calls this directly
no outgoing calls
no test coverage detected