A downsampling layer with an optional convolution. :param channels: channels in the inputs and outputs. :param use_conv: a bool determining if a convolution is applied. :param dims: determines if the signal is 1D, 2D, or 3D. If 3D, then downsampling occurs in the in
| 133 | |
| 134 | |
| 135 | class Downsample(nn.Module): |
| 136 | """ |
| 137 | A downsampling layer with an optional convolution. |
| 138 | :param channels: channels in the inputs and outputs. |
| 139 | :param use_conv: a bool determining if a convolution is applied. |
| 140 | :param dims: determines if the signal is 1D, 2D, or 3D. If 3D, then |
| 141 | downsampling occurs in the inner-two dimensions. |
| 142 | """ |
| 143 | |
| 144 | def __init__(self, channels, use_conv, dims=2, out_channels=None,padding=1): |
| 145 | super().__init__() |
| 146 | self.channels = channels |
| 147 | self.out_channels = out_channels or channels |
| 148 | self.use_conv = use_conv |
| 149 | self.dims = dims |
| 150 | stride = 2 if dims != 3 else (1, 2, 2) |
| 151 | if use_conv: |
| 152 | self.op = conv_nd( |
| 153 | dims, self.channels, self.out_channels, 3, stride=stride, padding=padding |
| 154 | ) |
| 155 | else: |
| 156 | assert self.channels == self.out_channels |
| 157 | self.op = avg_pool_nd(dims, kernel_size=stride, stride=stride) |
| 158 | |
| 159 | def forward(self, x): |
| 160 | assert x.shape[1] == self.channels |
| 161 | return self.op(x) |
| 162 | |
| 163 | |
| 164 | class ResBlock(TimestepBlock): |