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