| 96 | |
| 97 | |
| 98 | class Downsample(nn.Module): |
| 99 | def __init__(self, in_channels, with_conv): |
| 100 | super().__init__() |
| 101 | self.with_conv = with_conv |
| 102 | if self.with_conv: |
| 103 | # no asymmetric padding in torch conv, must do it ourselves |
| 104 | self.conv = torch.nn.Conv2d(in_channels, |
| 105 | in_channels, |
| 106 | kernel_size=3, |
| 107 | stride=2, |
| 108 | padding=0) |
| 109 | |
| 110 | def forward(self, x): |
| 111 | if self.with_conv: |
| 112 | pad = (0,1,0,1) |
| 113 | x = torch.nn.functional.pad(x, pad, mode="constant", value=0) |
| 114 | x = self.conv(x) |
| 115 | else: |
| 116 | x = torch.nn.functional.avg_pool2d(x, kernel_size=2, stride=2) |
| 117 | return x |
| 118 | |
| 119 | |
| 120 | class ResnetBlock(nn.Module): |