Forward function. Args: x: Input feature, tensor size (B, H*W, C). H, W: Spatial resolution of the input feature.
(self, x, H, W, l, l_mask)
| 562 | nn.init.zeros_(self.pwam_gate[2].weight) |
| 563 | |
| 564 | def forward(self, x, H, W, l, l_mask): |
| 565 | """ Forward function. |
| 566 | |
| 567 | Args: |
| 568 | x: Input feature, tensor size (B, H*W, C). |
| 569 | H, W: Spatial resolution of the input feature. |
| 570 | """ |
| 571 | |
| 572 | # calculate attention mask for SW-MSA |
| 573 | Hp = int(np.ceil(H / self.window_size)) * self.window_size |
| 574 | Wp = int(np.ceil(W / self.window_size)) * self.window_size |
| 575 | img_mask = torch.zeros((1, Hp, Wp, 1), device=x.device) # 1 Hp Wp 1 |
| 576 | h_slices = (slice(0, -self.window_size), |
| 577 | slice(-self.window_size, -self.shift_size), |
| 578 | slice(-self.shift_size, None)) |
| 579 | w_slices = (slice(0, -self.window_size), |
| 580 | slice(-self.window_size, -self.shift_size), |
| 581 | slice(-self.shift_size, None)) |
| 582 | cnt = 0 |
| 583 | for h in h_slices: |
| 584 | for w in w_slices: |
| 585 | img_mask[:, h, w, :] = cnt |
| 586 | cnt += 1 |
| 587 | |
| 588 | mask_windows = window_partition(img_mask, self.window_size) # nW, window_size, window_size, 1 |
| 589 | mask_windows = mask_windows.view(-1, self.window_size * self.window_size) |
| 590 | attn_mask = mask_windows.unsqueeze(1) - mask_windows.unsqueeze(2) |
| 591 | attn_mask = attn_mask.masked_fill(attn_mask != 0, float(-100.0)).masked_fill(attn_mask == 0, float(0.0)) |
| 592 | |
| 593 | for blk in self.blocks: |
| 594 | blk.H, blk.W = H, W |
| 595 | if self.use_checkpoint: |
| 596 | x = checkpoint.checkpoint(blk, x, attn_mask) |
| 597 | else: |
| 598 | x = blk(x, attn_mask) # output of a Block has shape (B, H*W, dim) |
| 599 | |
| 600 | # PWAM fusion |
| 601 | x_residual = self.pwam_fusion(x, l, l_mask) |
| 602 | # apply a gate on the residual |
| 603 | x = x + (self.pwam_gate(x_residual) * x_residual) |
| 604 | |
| 605 | if self.downsample is not None: |
| 606 | x_down = self.downsample(x, H, W) |
| 607 | Wh, Ww = (H + 1) // 2, (W + 1) // 2 |
| 608 | return x_residual, H, W, x_down, Wh, Ww |
| 609 | else: |
| 610 | return x_residual, H, W, x, H, W |
| 611 | |
| 612 | class DynamicAdapter(nn.Module): |
| 613 | def __init__(self, in_dim=512, out_dim=512, kernel_size=1): |
nothing calls this directly
no test coverage detected