| 50 | |
| 51 | |
| 52 | class Downsample(nn.Module): |
| 53 | def __init__(self, in_channels, with_conv): |
| 54 | super().__init__() |
| 55 | self.with_conv = with_conv |
| 56 | if self.with_conv: |
| 57 | # no asymmetric padding in torch conv, must do it ourselves |
| 58 | self.conv = torch.nn.Conv2d(in_channels, in_channels, kernel_size=3, stride=2, padding=0) |
| 59 | |
| 60 | def forward(self, x): |
| 61 | if self.with_conv: |
| 62 | pad = (0, 1, 0, 1) |
| 63 | x = torch.nn.functional.pad(x, pad, mode="constant", value=0) |
| 64 | x = self.conv(x) |
| 65 | else: |
| 66 | x = torch.nn.functional.avg_pool2d(x, kernel_size=2, stride=2) |
| 67 | return x |
| 68 | |
| 69 | |
| 70 | class ResnetBlock(nn.Module): |