diffusion_models.models.SmallAttentionUnet ========================================== .. py:module:: diffusion_models.models.SmallAttentionUnet Module Contents --------------- .. py:class:: ResidualBlock(in_channels, out_channels, time_emb_dim, num_groups, dropout) A GroupNorm residual block conditioned on the timestep embedding. .. py:attribute:: norm1 .. py:attribute:: conv1 .. py:attribute:: time_proj .. py:attribute:: norm2 .. py:attribute:: dropout .. py:attribute:: conv2 .. py:attribute:: skip .. py:method:: forward(x, t) .. py:class:: SelfAttention2d(channels, num_groups, num_heads = 4) Multi-head self-attention over the spatial positions of a feature map. .. py:attribute:: num_heads :value: 4 .. py:attribute:: norm .. py:attribute:: qkv .. py:attribute:: proj .. py:method:: forward(x) .. py:class:: Downsample(channels) Initialize internal Module state, shared by both nn.Module and ScriptModule. .. py:attribute:: conv .. py:method:: forward(x) .. py:class:: Upsample(channels) Initialize internal Module state, shared by both nn.Module and ScriptModule. .. py:attribute:: conv .. py:method:: forward(x) .. py: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) A small DDPM-style Unet with GroupNorm and low-resolution attention. Compared to :class:`~.SimpleUnet.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. :param diffuser: A gaussian diffuser. :param image_channels: The number of image channels. :param base_channels: The channel count at the highest resolution. :param channel_multipliers: Per level channel multiplier. Every level but the first halves the resolution. :param num_res_blocks: The number of residual blocks per level (down path). :param attention_levels: The number of lowest-resolution levels (and the bottleneck) which get self-attention. :param num_groups: The number of GroupNorm groups. :param dropout: The dropout probability inside residual blocks. .. py:attribute:: time_mlp .. py:attribute:: conv_in .. py:attribute:: down_stages .. py:attribute:: downsamples .. py:attribute:: mid_block1 .. py:attribute:: mid_block2 .. py:attribute:: up_stages .. py:attribute:: upsamples .. py:attribute:: num_res_blocks :value: 2 .. py:attribute:: norm_out .. py:attribute:: conv_out .. py:method:: forward(x, timestep, labels = None) Forward pass of the diffusion model. The forward pass of the diffusion model, predicting the noise at a single step. :param x: A batch of noisy images. :param timestep: The timesteps of each image in the batch. :param labels: Optional labels to be passed when conditioning the diffusion model. :returns: A tensor representing the noise predicted for each image.