An upsampling 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 upsampling occurs in the inne
| 150 | |
| 151 | |
| 152 | class Upsample(nn.Module): |
| 153 | """ |
| 154 | An upsampling layer with an optional convolution. |
| 155 | |
| 156 | :param channels: channels in the inputs and outputs. |
| 157 | :param use_conv: a bool determining if a convolution is applied. |
| 158 | :param dims: determines if the signal is 1D, 2D, or 3D. If 3D, then |
| 159 | upsampling occurs in the inner-two dimensions. |
| 160 | """ |
| 161 | |
| 162 | def __init__(self, channels, use_conv, dims=2, out_channels=None): |
| 163 | super().__init__() |
| 164 | self.channels = channels |
| 165 | self.out_channels = out_channels or channels |
| 166 | self.use_conv = use_conv |
| 167 | self.dims = dims |
| 168 | if use_conv: |
| 169 | self.conv = conv_nd(dims, self.channels, self.out_channels, 3, padding=1) |
| 170 | |
| 171 | def forward(self, x): |
| 172 | assert x.shape[1] == self.channels |
| 173 | if self.dims == 3: |
| 174 | x = F.interpolate( |
| 175 | x, (x.shape[2], x.shape[3] * 2, x.shape[4] * 2), mode="nearest" |
| 176 | ) |
| 177 | else: |
| 178 | x = F.interpolate(x, scale_factor=2, mode="nearest") |
| 179 | if self.use_conv: |
| 180 | x = self.conv(x) |
| 181 | return x |
| 182 | |
| 183 | |
| 184 | class Downsample(nn.Module): |