diffusion_models.models.SmallAttentionUnet

Module Contents

class ResidualBlock(in_channels, out_channels, time_emb_dim, num_groups, dropout)[source]

A GroupNorm residual block conditioned on the timestep embedding.

norm1[source]
conv1[source]
time_proj[source]
norm2[source]
dropout[source]
conv2[source]
skip[source]
forward(x, t)[source]
class SelfAttention2d(channels, num_groups, num_heads=4)[source]

Multi-head self-attention over the spatial positions of a feature map.

num_heads = 4[source]
norm[source]
qkv[source]
proj[source]
forward(x)[source]
class Downsample(channels)[source]

Initialize internal Module state, shared by both nn.Module and ScriptModule.

conv[source]
forward(x)[source]
class Upsample(channels)[source]

Initialize internal Module state, shared by both nn.Module and ScriptModule.

conv[source]
forward(x)[source]
class SmallAttentionUnet(diffuser, image_channels, base_channels=64, channel_multipliers=(1, 2, 4, 4), num_res_blocks=2, attention_levels=2, num_groups=32, dropout=0.0)[source]

A small DDPM-style Unet with GroupNorm and low-resolution attention.

Compared to SimpleUnet, this model uses GroupNorm (so training and evaluation behave identically, which matters for sampling and EMA), residual blocks, and self-attention only at the lowest attention_levels resolutions. With the defaults it has roughly 25M parameters and fits comfortably on a small GPU at 64x64.

Parameters:
  • diffuser (BaseDiffuser) – A gaussian diffuser.

  • image_channels (int) – The number of image channels.

  • base_channels (int) – The channel count at the highest resolution.

  • channel_multipliers (Sequence[int]) – Per level channel multiplier. Every level but the first halves the resolution.

  • num_res_blocks (int) – The number of residual blocks per level (down path).

  • attention_levels (int) – The number of lowest-resolution levels (and the bottleneck) which get self-attention.

  • num_groups (int) – The number of GroupNorm groups.

  • dropout (float) – The dropout probability inside residual blocks.

time_mlp[source]
conv_in[source]
down_stages[source]
downsamples[source]
mid_block1[source]
mid_block2[source]
up_stages[source]
upsamples[source]
num_res_blocks = 2[source]
norm_out[source]
conv_out[source]
forward(x, timestep, labels=None)[source]

Forward pass of the diffusion model.

The forward pass of the diffusion model, predicting the noise at a single step.

Parameters:
  • x (Tensor) – A batch of noisy images.

  • timestep (Tensor) – The timesteps of each image in the batch.

  • labels (Optional[Tensor]) – Optional labels to be passed when conditioning the diffusion model.

Returns:

A tensor representing the noise predicted for each image.

Return type:

Tensor