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
| 187 | |
| 188 | |
| 189 | class Downsample(nn.Module): |
| 190 | """ |
| 191 | A downsampling layer with an optional convolution. |
| 192 | :param channels: channels in the inputs and outputs. |
| 193 | :param use_conv: a bool determining if a convolution is applied. |
| 194 | :param dims: determines if the signal is 1D, 2D, or 3D. If 3D, then |
| 195 | downsampling occurs in the inner-two dimensions. |
| 196 | """ |
| 197 | |
| 198 | def __init__(self, channels, use_conv, dims=2, out_channels=None,padding=1): |
| 199 | super().__init__() |
| 200 | self.channels = channels |
| 201 | self.out_channels = out_channels or channels |
| 202 | self.use_conv = use_conv |
| 203 | self.dims = dims |
| 204 | stride = 2 if dims != 3 else (1, 2, 2) |
| 205 | if use_conv: |
| 206 | self.op = conv_nd( |
| 207 | dims, self.channels, self.out_channels, 3, stride=stride, padding=padding |
| 208 | ) |
| 209 | else: |
| 210 | assert self.channels == self.out_channels |
| 211 | self.op = avg_pool_nd(dims, kernel_size=stride, stride=stride) |
| 212 | |
| 213 | def forward(self, x): |
| 214 | assert x.shape[1] == self.channels |
| 215 | return self.op(x) |
| 216 | |
| 217 | |
| 218 | class ResBlock(TimestepBlock): |