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
| 166 | |
| 167 | |
| 168 | class Downsample(nn.Module): |
| 169 | """ |
| 170 | A downsampling layer with an optional convolution. |
| 171 | :param channels: channels in the inputs and outputs. |
| 172 | :param use_conv: a bool determining if a convolution is applied. |
| 173 | :param dims: determines if the signal is 1D, 2D, or 3D. If 3D, then |
| 174 | downsampling occurs in the inner-two dimensions. |
| 175 | """ |
| 176 | |
| 177 | def __init__(self, channels, use_conv, dims=2, out_channels=None, padding=1, third_down=False): |
| 178 | super().__init__() |
| 179 | self.channels = channels |
| 180 | self.out_channels = out_channels or channels |
| 181 | self.use_conv = use_conv |
| 182 | self.dims = dims |
| 183 | stride = 2 if dims != 3 else ((1, 2, 2) if not third_down else (2, 2, 2)) |
| 184 | if use_conv: |
| 185 | print(f"Building a Downsample layer with {dims} dims.") |
| 186 | print( |
| 187 | f" --> settings are: \n in-chn: {self.channels}, out-chn: {self.out_channels}, " |
| 188 | f"kernel-size: 3, stride: {stride}, padding: {padding}" |
| 189 | ) |
| 190 | if dims == 3: |
| 191 | print(f" --> Downsampling third axis (time): {third_down}") |
| 192 | self.op = conv_nd( |
| 193 | dims, |
| 194 | self.channels, |
| 195 | self.out_channels, |
| 196 | 3, |
| 197 | stride=stride, |
| 198 | padding=padding, |
| 199 | ) |
| 200 | else: |
| 201 | assert self.channels == self.out_channels |
| 202 | self.op = avg_pool_nd(dims, kernel_size=stride, stride=stride) |
| 203 | |
| 204 | def forward(self, x): |
| 205 | assert x.shape[1] == self.channels |
| 206 | return self.op(x) |
| 207 | |
| 208 | |
| 209 | class ResBlock(TimestepBlock): |