MCPcopy Create free account
hub / github.com/DPS2022/diffusion-posterior-sampling / ResBlock

Class ResBlock

guided_diffusion/unet.py:214–327  ·  view source on GitHub ↗

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.

Source from the content-addressed store, hash-verified

212
213
214class ResBlock(TimestepBlock):
215 """
216 A residual block that can optionally change the number of channels.
217
218 :param channels: the number of input channels.
219 :param emb_channels: the number of timestep embedding channels.
220 :param dropout: the rate of dropout.
221 :param out_channels: if specified, the number of out channels.
222 :param use_conv: if True and out_channels is specified, use a spatial
223 convolution instead of a smaller 1x1 convolution to change the
224 channels in the skip connection.
225 :param dims: determines if the signal is 1D, 2D, or 3D.
226 :param use_checkpoint: if True, use gradient checkpointing on this module.
227 :param up: if True, use this block for upsampling.
228 :param down: if True, use this block for downsampling.
229 """
230
231 def __init__(
232 self,
233 channels,
234 emb_channels,
235 dropout,
236 out_channels=None,
237 use_conv=False,
238 use_scale_shift_norm=False,
239 dims=2,
240 use_checkpoint=False,
241 up=False,
242 down=False,
243 ):
244 super().__init__()
245 self.channels = channels
246 self.emb_channels = emb_channels
247 self.dropout = dropout
248 self.out_channels = out_channels or channels
249 self.use_conv = use_conv
250 self.use_checkpoint = use_checkpoint
251 self.use_scale_shift_norm = use_scale_shift_norm
252
253 self.in_layers = nn.Sequential(
254 normalization(channels),
255 nn.SiLU(),
256 conv_nd(dims, channels, self.out_channels, 3, padding=1),
257 )
258
259 self.updown = up or down
260
261 if up:
262 self.h_upd = Upsample(channels, False, dims)
263 self.x_upd = Upsample(channels, False, dims)
264 elif down:
265 self.h_upd = Downsample(channels, False, dims)
266 self.x_upd = Downsample(channels, False, dims)
267 else:
268 self.h_upd = self.x_upd = nn.Identity()
269
270 self.emb_layers = nn.Sequential(
271 nn.SiLU(),

Callers 2

__init__Method · 0.85
__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected