Forward function.
(self, x)
| 408 | self.norm = None |
| 409 | |
| 410 | def forward(self, x): |
| 411 | """Forward function.""" |
| 412 | B, C, H, W = x.size() |
| 413 | if W % self.patch_size[1] != 0: |
| 414 | x = F.pad(x, (0, self.patch_size[1] - W % self.patch_size[1])) |
| 415 | if H % self.patch_size[0] != 0: |
| 416 | x = F.pad(x, (0, 0, 0, self.patch_size[0] - H % self.patch_size[0])) |
| 417 | |
| 418 | if self.use_pre_norm: |
| 419 | if self.norm is not None: |
| 420 | x = x.flatten(2).transpose(1, 2) # B Ph*Pw C |
| 421 | x = self.norm(x).transpose(1, 2).view(B, C, H, W) |
| 422 | x = self.proj(x) |
| 423 | else: |
| 424 | x = self.proj(x) # B C Wh Ww |
| 425 | if self.norm is not None: |
| 426 | Wh, Ww = x.size(2), x.size(3) |
| 427 | x = x.flatten(2).transpose(1, 2) |
| 428 | x = self.norm(x) |
| 429 | x = x.transpose(1, 2).view(-1, self.embed_dim, Wh, Ww) |
| 430 | |
| 431 | return x |
| 432 | |
| 433 | |
| 434 | class FocalNet(nn.Module): |
nothing calls this directly
no outgoing calls
no test coverage detected