Diffusers BOINC AI docs
  • 🌍GET STARTED
    • Diffusers
    • Quicktour
    • Effective and efficient diffusion
    • Installation
  • 🌍TUTORIALS
    • Overview
    • Understanding models and schedulers
    • AutoPipeline
    • Train a diffusion model
  • 🌍USING DIFFUSERS
    • 🌍LOADING & HUB
      • Overview
      • Load pipelines, models, and schedulers
      • Load and compare different schedulers
      • Load community pipelines
      • Load safetensors
      • Load different Stable Diffusion formats
      • Push files to the Hub
    • 🌍TASKS
      • Unconditional image generation
      • Text-to-image
      • Image-to-image
      • Inpainting
      • Depth-to-image
    • 🌍TECHNIQUES
      • Textual inversion
      • Distributed inference with multiple GPUs
      • Improve image quality with deterministic generation
      • Control image brightness
      • Prompt weighting
    • 🌍PIPELINES FOR INFERENCE
      • Overview
      • Stable Diffusion XL
      • ControlNet
      • Shap-E
      • DiffEdit
      • Distilled Stable Diffusion inference
      • Create reproducible pipelines
      • Community pipelines
      • How to contribute a community pipeline
    • 🌍TRAINING
      • Overview
      • Create a dataset for training
      • Adapt a model to a new task
      • Unconditional image generation
      • Textual Inversion
      • DreamBooth
      • Text-to-image
      • Low-Rank Adaptation of Large Language Models (LoRA)
      • ControlNet
      • InstructPix2Pix Training
      • Custom Diffusion
      • T2I-Adapters
    • 🌍TAKING DIFFUSERS BEYOND IMAGES
      • Other Modalities
  • 🌍OPTIMIZATION/SPECIAL HARDWARE
    • Overview
    • Memory and Speed
    • Torch2.0 support
    • Stable Diffusion in JAX/Flax
    • xFormers
    • ONNX
    • OpenVINO
    • Core ML
    • MPS
    • Habana Gaudi
    • Token Merging
  • 🌍CONCEPTUAL GUIDES
    • Philosophy
    • Controlled generation
    • How to contribute?
    • Diffusers' Ethical Guidelines
    • Evaluating Diffusion Models
  • 🌍API
    • 🌍MAIN CLASSES
      • Attention Processor
      • Diffusion Pipeline
      • Logging
      • Configuration
      • Outputs
      • Loaders
      • Utilities
      • VAE Image Processor
    • 🌍MODELS
      • Overview
      • UNet1DModel
      • UNet2DModel
      • UNet2DConditionModel
      • UNet3DConditionModel
      • VQModel
      • AutoencoderKL
      • AsymmetricAutoencoderKL
      • Tiny AutoEncoder
      • Transformer2D
      • Transformer Temporal
      • Prior Transformer
      • ControlNet
    • 🌍PIPELINES
      • Overview
      • AltDiffusion
      • Attend-and-Excite
      • Audio Diffusion
      • AudioLDM
      • AudioLDM 2
      • AutoPipeline
      • Consistency Models
      • ControlNet
      • ControlNet with Stable Diffusion XL
      • Cycle Diffusion
      • Dance Diffusion
      • DDIM
      • DDPM
      • DeepFloyd IF
      • DiffEdit
      • DiT
      • IF
      • PaInstructPix2Pix
      • Kandinsky
      • Kandinsky 2.2
      • Latent Diffusionge
      • MultiDiffusion
      • MusicLDM
      • PaintByExample
      • Parallel Sampling of Diffusion Models
      • Pix2Pix Zero
      • PNDM
      • RePaint
      • Score SDE VE
      • Self-Attention Guidance
      • Semantic Guidance
      • Shap-E
      • Spectrogram Diffusion
      • 🌍STABLE DIFFUSION
        • Overview
        • Text-to-image
        • Image-to-image
        • Inpainting
        • Depth-to-image
        • Image variation
        • Safe Stable Diffusion
        • Stable Diffusion 2
        • Stable Diffusion XL
        • Latent upscaler
        • Super-resolution
        • LDM3D Text-to-(RGB, Depth)
        • Stable Diffusion T2I-adapter
        • GLIGEN (Grounded Language-to-Image Generation)
      • Stable unCLIP
      • Stochastic Karras VE
      • Text-to-image model editing
      • Text-to-video
      • Text2Video-Zero
      • UnCLIP
      • Unconditional Latent Diffusion
      • UniDiffuser
      • Value-guided sampling
      • Versatile Diffusion
      • VQ Diffusion
      • Wuerstchen
    • 🌍SCHEDULERS
      • Overview
      • CMStochasticIterativeScheduler
      • DDIMInverseScheduler
      • DDIMScheduler
      • DDPMScheduler
      • DEISMultistepScheduler
      • DPMSolverMultistepInverse
      • DPMSolverMultistepScheduler
      • DPMSolverSDEScheduler
      • DPMSolverSinglestepScheduler
      • EulerAncestralDiscreteScheduler
      • EulerDiscreteScheduler
      • HeunDiscreteScheduler
      • IPNDMScheduler
      • KarrasVeScheduler
      • KDPM2AncestralDiscreteScheduler
      • KDPM2DiscreteScheduler
      • LMSDiscreteScheduler
      • PNDMScheduler
      • RePaintScheduler
      • ScoreSdeVeScheduler
      • ScoreSdeVpScheduler
      • UniPCMultistepScheduler
      • VQDiffusionScheduler
Powered by GitBook
On this page
  • UNet2DModel
  • UNet2DModel
  • UNet2DOutput
  1. API
  2. MODELS

UNet2DModel

PreviousUNet1DModelNextUNet2DConditionModel

Last updated 1 year ago

UNet2DModel

The model was originally introduced by Ronneberger et al for biomedical image segmentation, but it is also commonly used in 🌍 Diffusers because it outputs images that are the same size as the input. It is one of the most important components of a diffusion system because it facilitates the actual diffusion process. There are several variants of the UNet model in 🌍 Diffusers, depending on it’s number of dimensions and whether it is a conditional model or not. This is a 2D UNet model.

The abstract from the paper is:

There is large consent that successful training of deep networks requires many thousand annotated training samples. In this paper, we present a network and training strategy that relies on the strong use of data augmentation to use the available annotated samples more efficiently. The architecture consists of a contracting path to capture context and a symmetric expanding path that enables precise localization. We show that such a network can be trained end-to-end from very few images and outperforms the prior best method (a sliding-window convolutional network) on the ISBI challenge for segmentation of neuronal structures in electron microscopic stacks. Using the same network trained on transmitted light microscopy images (phase contrast and DIC) we won the ISBI cell tracking challenge 2015 in these categories by a large margin. Moreover, the network is fast. Segmentation of a 512x512 image takes less than a second on a recent GPU. The full implementation (based on Caffe) and the trained networks are available at .

UNet2DModel

class diffusers.UNet2DModel

( sample_size: typing.Union[int, typing.Tuple[int, int], NoneType] = Nonein_channels: int = 3out_channels: int = 3center_input_sample: bool = Falsetime_embedding_type: str = 'positional'freq_shift: int = 0flip_sin_to_cos: bool = Truedown_block_types: typing.Tuple[str] = ('DownBlock2D', 'AttnDownBlock2D', 'AttnDownBlock2D', 'AttnDownBlock2D')up_block_types: typing.Tuple[str] = ('AttnUpBlock2D', 'AttnUpBlock2D', 'AttnUpBlock2D', 'UpBlock2D')block_out_channels: typing.Tuple[int] = (224, 448, 672, 896)layers_per_block: int = 2mid_block_scale_factor: float = 1downsample_padding: int = 1downsample_type: str = 'conv'upsample_type: str = 'conv'dropout: float = 0.0act_fn: str = 'silu'attention_head_dim: typing.Optional[int] = 8norm_num_groups: int = 32norm_eps: float = 1e-05resnet_time_scale_shift: str = 'default'add_attention: bool = Trueclass_embed_type: typing.Optional[str] = Nonenum_class_embeds: typing.Optional[int] = None )

Parameters

  • sample_size (int or Tuple[int, int], optional, defaults to None) β€” Height and width of input/output sample. Dimensions must be a multiple of 2 ** (len(block_out_channels) - 1).

  • in_channels (int, optional, defaults to 3) β€” Number of channels in the input sample.

  • out_channels (int, optional, defaults to 3) β€” Number of channels in the output.

  • center_input_sample (bool, optional, defaults to False) β€” Whether to center the input sample.

  • time_embedding_type (str, optional, defaults to "positional") β€” Type of time embedding to use.

  • freq_shift (int, optional, defaults to 0) β€” Frequency shift for Fourier time embedding.

  • flip_sin_to_cos (bool, optional, defaults to True) β€” Whether to flip sin to cos for Fourier time embedding.

  • down_block_types (Tuple[str], optional, defaults to ("DownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D")) β€” Tuple of downsample block types.

  • mid_block_type (str, optional, defaults to "UNetMidBlock2D") β€” Block type for middle of UNet, it can be either UNetMidBlock2D or UnCLIPUNetMidBlock2D.

  • up_block_types (Tuple[str], optional, defaults to ("AttnUpBlock2D", "AttnUpBlock2D", "AttnUpBlock2D", "UpBlock2D")) β€” Tuple of upsample block types.

  • block_out_channels (Tuple[int], optional, defaults to (224, 448, 672, 896)) β€” Tuple of block output channels.

  • layers_per_block (int, optional, defaults to 2) β€” The number of layers per block.

  • mid_block_scale_factor (float, optional, defaults to 1) β€” The scale factor for the mid block.

  • downsample_padding (int, optional, defaults to 1) β€” The padding for the downsample convolution.

  • downsample_type (str, optional, defaults to conv) β€” The downsample type for downsampling layers. Choose between β€œconv” and β€œresnet”

  • upsample_type (str, optional, defaults to conv) β€” The upsample type for upsampling layers. Choose between β€œconv” and β€œresnet”

  • dropout (float, optional, defaults to 0.0) β€” The dropout probability to use.

  • act_fn (str, optional, defaults to "silu") β€” The activation function to use.

  • attention_head_dim (int, optional, defaults to 8) β€” The attention head dimension.

  • norm_num_groups (int, optional, defaults to 32) β€” The number of groups for normalization.

  • norm_eps (float, optional, defaults to 1e-5) β€” The epsilon for normalization.

  • resnet_time_scale_shift (str, optional, defaults to "default") β€” Time scale shift config for ResNet blocks (see ResnetBlock2D). Choose from default or scale_shift.

  • class_embed_type (str, optional, defaults to None) β€” The type of class embedding to use which is ultimately summed with the time embeddings. Choose from None, "timestep", or "identity".

  • num_class_embeds (int, optional, defaults to None) β€” Input dimension of the learnable embedding matrix to be projected to time_embed_dim when performing class conditioning with class_embed_type equal to None.

A 2D UNet model that takes a noisy sample and a timestep and returns a sample shaped output.

forward

Parameters

  • sample (torch.FloatTensor) β€” The noisy input tensor with the following shape (batch, channel, height, width).

  • timestep (torch.FloatTensor or float or int) β€” The number of timesteps to denoise an input.

  • class_labels (torch.FloatTensor, optional, defaults to None) β€” Optional class labels for conditioning. Their embeddings will be summed with the timestep embeddings.

Returns

UNet2DOutput

class diffusers.models.unet_2d.UNet2DOutput

( sample: FloatTensor )

Parameters

  • sample (torch.FloatTensor of shape (batch_size, num_channels, height, width)) β€” The hidden states output from the last layer of the model.

This model inherits from . Check the superclass documentation for it’s generic methods implemented for all models (such as downloading or saving).

( sample: FloatTensortimestep: typing.Union[torch.Tensor, float, int]class_labels: typing.Optional[torch.Tensor] = Nonereturn_dict: bool = True ) β†’ or tuple

return_dict (bool, optional, defaults to True) β€” Whether or not to return a instead of a plain tuple.

or tuple

If return_dict is True, an is returned, otherwise a tuple is returned where the first element is the sample tensor.

The forward method.

The output of .

🌍
🌍
UNet
http://lmb.informatik.uni-freiburg.de/people/ronneber/u-net
<source>
ModelMixin
<source>
UNet2DOutput
UNet2DOutput
UNet2DOutput
UNet2DOutput
UNet2DModel
<source>
UNet2DModel