A residual block that can optionally change the number of channels. :param channels: the number of input channels. :param emb_channels: the number of timestep embedding channels. :param dropout: the rate of dropout. :param out_channels: if specified, the number of out channels.
| 162 | |
| 163 | |
| 164 | class ResBlock(TimestepBlock): |
| 165 | """ |
| 166 | A residual block that can optionally change the number of channels. |
| 167 | :param channels: the number of input channels. |
| 168 | :param emb_channels: the number of timestep embedding channels. |
| 169 | :param dropout: the rate of dropout. |
| 170 | :param out_channels: if specified, the number of out channels. |
| 171 | :param use_conv: if True and out_channels is specified, use a spatial |
| 172 | convolution instead of a smaller 1x1 convolution to change the |
| 173 | channels in the skip connection. |
| 174 | :param dims: determines if the signal is 1D, 2D, or 3D. |
| 175 | :param use_checkpoint: if True, use gradient checkpointing on this module. |
| 176 | :param up: if True, use this block for upsampling. |
| 177 | :param down: if True, use this block for downsampling. |
| 178 | """ |
| 179 | |
| 180 | def __init__( |
| 181 | self, |
| 182 | channels, |
| 183 | emb_channels, |
| 184 | dropout, |
| 185 | out_channels=None, |
| 186 | use_conv=False, |
| 187 | use_scale_shift_norm=False, |
| 188 | dims=2, |
| 189 | use_checkpoint=False, |
| 190 | up=False, |
| 191 | down=False, |
| 192 | ): |
| 193 | super().__init__() |
| 194 | self.channels = channels |
| 195 | self.emb_channels = emb_channels |
| 196 | self.dropout = dropout |
| 197 | self.out_channels = out_channels or channels |
| 198 | self.use_conv = use_conv |
| 199 | self.use_checkpoint = use_checkpoint |
| 200 | self.use_scale_shift_norm = use_scale_shift_norm |
| 201 | |
| 202 | self.in_layers = nn.Sequential( |
| 203 | normalization(channels), |
| 204 | nn.SiLU(), |
| 205 | conv_nd(dims, channels, self.out_channels, 3, padding=1), |
| 206 | ) |
| 207 | |
| 208 | self.updown = up or down |
| 209 | |
| 210 | if up: |
| 211 | self.h_upd = Upsample(channels, False, dims) |
| 212 | self.x_upd = Upsample(channels, False, dims) |
| 213 | elif down: |
| 214 | self.h_upd = Downsample(channels, False, dims) |
| 215 | self.x_upd = Downsample(channels, False, dims) |
| 216 | else: |
| 217 | self.h_upd = self.x_upd = nn.Identity() |
| 218 | |
| 219 | self.emb_layers = nn.Sequential( |
| 220 | nn.SiLU(), |
| 221 | linear( |