| 33 | |
| 34 | |
| 35 | class Downsample(nn.Module): |
| 36 | def __init__(self, in_channels, with_conv): |
| 37 | super().__init__() |
| 38 | self.with_conv = with_conv |
| 39 | if self.with_conv: |
| 40 | # no asymmetric padding in torch conv, must do it ourselves |
| 41 | self.conv = torch.nn.Conv2d( |
| 42 | in_channels, in_channels, kernel_size=3, stride=2, padding=0 |
| 43 | ) |
| 44 | |
| 45 | def forward(self, x): |
| 46 | if self.with_conv: |
| 47 | pad = (0, 1, 0, 1) |
| 48 | x = torch.nn.functional.pad(x, pad, mode="constant", value=0) |
| 49 | x = self.conv(x) |
| 50 | else: |
| 51 | x = torch.nn.functional.avg_pool2d(x, kernel_size=2, stride=2) |
| 52 | return x |
| 53 | |
| 54 | |
| 55 | class ResnetBlock(nn.Module): |