| 65 | |
| 66 | |
| 67 | class Downsample(nn.Module): |
| 68 | def __init__(self, in_channels, with_conv): |
| 69 | super().__init__() |
| 70 | self.with_conv = with_conv |
| 71 | if self.with_conv: |
| 72 | # no asymmetric padding in torch conv, must do it ourselves |
| 73 | self.conv = torch.nn.Conv2d(in_channels, in_channels, kernel_size=3, stride=2, padding=0) |
| 74 | |
| 75 | def forward(self, x): |
| 76 | if self.with_conv: |
| 77 | pad = (0, 1, 0, 1) |
| 78 | x = torch.nn.functional.pad(x, pad, mode="constant", value=0) |
| 79 | x = self.conv(x) |
| 80 | else: |
| 81 | x = torch.nn.functional.avg_pool2d(x, kernel_size=2, stride=2) |
| 82 | return x |
| 83 | |
| 84 | |
| 85 | class ResnetBlock(nn.Module): |