Spatially conditioned normalization as defined in https://arxiv.org/abs/2209.09002. Args: f_channels (`int`): The number of channels for input to group normalization layer, and output of the spatial norm layer. zq_channels (`int`): The number
| 1669 | |
| 1670 | |
| 1671 | class SpatialNorm(nn.Module): |
| 1672 | """ |
| 1673 | Spatially conditioned normalization as defined in https://arxiv.org/abs/2209.09002. |
| 1674 | |
| 1675 | Args: |
| 1676 | f_channels (`int`): |
| 1677 | The number of channels for input to group normalization layer, and output of the spatial norm layer. |
| 1678 | zq_channels (`int`): |
| 1679 | The number of channels for the quantized vector as described in the paper. |
| 1680 | """ |
| 1681 | |
| 1682 | def __init__( |
| 1683 | self, |
| 1684 | f_channels: int, |
| 1685 | zq_channels: int, |
| 1686 | ): |
| 1687 | super().__init__() |
| 1688 | self.norm_layer = nn.GroupNorm(num_channels=f_channels, num_groups=32, eps=1e-6, affine=True) |
| 1689 | self.conv_y = nn.Conv2d(zq_channels, f_channels, kernel_size=1, stride=1, padding=0) |
| 1690 | self.conv_b = nn.Conv2d(zq_channels, f_channels, kernel_size=1, stride=1, padding=0) |
| 1691 | |
| 1692 | def forward(self, f: torch.FloatTensor, zq: torch.FloatTensor) -> torch.FloatTensor: |
| 1693 | f_size = f.shape[-2:] |
| 1694 | zq = F.interpolate(zq, size=f_size, mode="nearest") |
| 1695 | norm_f = self.norm_layer(f) |
| 1696 | new_f = norm_f * self.conv_y(zq) + self.conv_b(zq) |
| 1697 | return new_f |
| 1698 | |
| 1699 | |
| 1700 | ## Deprecated |