Forward function. Args: x: Input feature, tensor size (B, H*W, C). H, W: Spatial resolution of the input feature.
(self, x)
| 166 | self.gamma_2 = nn.Parameter(layerscale_value * torch.ones((dim)), requires_grad=True) |
| 167 | |
| 168 | def forward(self, x): |
| 169 | """ Forward function. |
| 170 | |
| 171 | Args: |
| 172 | x: Input feature, tensor size (B, H*W, C). |
| 173 | H, W: Spatial resolution of the input feature. |
| 174 | """ |
| 175 | B, L, C = x.shape |
| 176 | H, W = self.H, self.W |
| 177 | assert L == H * W, "input feature has wrong size" |
| 178 | |
| 179 | x = x.view(B, H, W, C).permute(0, 3, 1, 2).contiguous() |
| 180 | x = x + self.dw1(x) |
| 181 | x = x.permute(0, 2, 3, 1).contiguous().view(B, L, C) |
| 182 | |
| 183 | shortcut = x |
| 184 | if not self.use_postln: |
| 185 | x = self.norm1(x) |
| 186 | x = x.view(B, H, W, C) |
| 187 | |
| 188 | # FM |
| 189 | x = self.modulation(x).view(B, H * W, C) |
| 190 | x = shortcut + self.drop_path(self.gamma_1 * x) |
| 191 | if self.use_postln: |
| 192 | x = self.norm1(x) |
| 193 | |
| 194 | x = x.view(B, H, W, C).permute(0, 3, 1, 2).contiguous() |
| 195 | x = x + self.dw2(x) |
| 196 | x = x.permute(0, 2, 3, 1).contiguous().view(B, L, C) |
| 197 | |
| 198 | if not self.use_postln: |
| 199 | x = x + self.drop_path(self.gamma_2 * self.mlp(self.norm2(x))) |
| 200 | else: |
| 201 | x = x + self.drop_path(self.gamma_2 * self.mlp(x)) |
| 202 | x = self.norm2(x) |
| 203 | |
| 204 | return x |
| 205 | |
| 206 | class BasicLayer(nn.Module): |
| 207 | """ A basic focal modulation layer for one stage. |
nothing calls this directly
no outgoing calls
no test coverage detected