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 inner
| 117 | |
| 118 | |
| 119 | class Upsample(nn.Module): |
| 120 | """ |
| 121 | An upsampling layer with an optional convolution. |
| 122 | :param channels: channels in the inputs and outputs. |
| 123 | :param use_conv: a bool determining if a convolution is applied. |
| 124 | :param dims: determines if the signal is 1D, 2D, or 3D. If 3D, then |
| 125 | upsampling occurs in the inner-two dimensions. |
| 126 | """ |
| 127 | |
| 128 | def __init__(self, channels, use_conv, dims=2, out_channels=None, padding=1, third_up=False): |
| 129 | super().__init__() |
| 130 | self.channels = channels |
| 131 | self.out_channels = out_channels or channels |
| 132 | self.use_conv = use_conv |
| 133 | self.dims = dims |
| 134 | self.third_up = third_up |
| 135 | if use_conv: |
| 136 | self.conv = conv_nd(dims, self.channels, self.out_channels, 3, padding=padding) |
| 137 | |
| 138 | def forward(self, x): |
| 139 | assert x.shape[1] == self.channels |
| 140 | if self.dims == 3: |
| 141 | t_factor = 1 if not self.third_up else 2 |
| 142 | x = F.interpolate( |
| 143 | x, |
| 144 | (t_factor * x.shape[2], x.shape[3] * 2, x.shape[4] * 2), |
| 145 | mode="nearest", |
| 146 | ) |
| 147 | else: |
| 148 | x = F.interpolate(x, scale_factor=2, mode="nearest") |
| 149 | if self.use_conv: |
| 150 | x = self.conv(x) |
| 151 | return x |
| 152 | |
| 153 | |
| 154 | class TransposedUpsample(nn.Module): |