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
| 79 | |
| 80 | |
| 81 | class Upsample(nn.Module): |
| 82 | """ |
| 83 | An upsampling layer with an optional convolution. |
| 84 | |
| 85 | :param channels: channels in the inputs and outputs. |
| 86 | :param use_conv: a bool determining if a convolution is applied. |
| 87 | :param dims: determines if the signal is 1D, 2D, or 3D. If 3D, then |
| 88 | upsampling occurs in the inner-two dimensions. |
| 89 | """ |
| 90 | |
| 91 | def __init__(self, channels, use_conv, dims=2, out_channels=None): |
| 92 | super().__init__() |
| 93 | self.channels = channels |
| 94 | self.out_channels = out_channels or channels |
| 95 | self.use_conv = use_conv |
| 96 | self.dims = dims |
| 97 | if use_conv: |
| 98 | self.conv = conv_nd(dims, self.channels, self.out_channels, 3, padding=1) |
| 99 | |
| 100 | def forward(self, x): |
| 101 | assert x.shape[1] == self.channels |
| 102 | if self.dims == 3: |
| 103 | x = F.interpolate( |
| 104 | x, (x.shape[2], x.shape[3] * 2, x.shape[4] * 2), mode="nearest" |
| 105 | ) |
| 106 | else: |
| 107 | x = F.interpolate(x, scale_factor=2, mode="nearest") |
| 108 | if self.use_conv: |
| 109 | x = self.conv(x) |
| 110 | return x |
| 111 | |
| 112 | |
| 113 | class Downsample(nn.Module): |