Model Architectures

Flemme supports various architectures for classification, segmentation, reconstruction and generation tasks, which are all built from a base encoder-decoder architecture.

Base Architectures

Base Architectures contains a encoder and decoder. The number of input channels of a base model is determined by the encoder, and the number of output channels is determined by the decoder. However, it contains several optional components for context embedding.

There are two main differences between condition embedding \(c\) and time-step embedding \(t\).

  • \(c\) can be encoded for both encoder and decoder

  • \(c\) is combined with \(x\) and \(z\) before encoding and decoding, while \(t\) is an optional independent input of encoder and decoder. Actually, \(t\) is the input of each building block in encoder and decoder.

We formulate context embedding as following equations to help you understand the above rules:

\[\begin{split}\begin{equation} \label{equ:forward} \begin{split} &z = \mathcal{E}\left(x + \mathcal{C}_e (c), \mathcal{T}(t)\right),\\ &\mathcal{M}(x, c, t) = \mathcal{D}\left( z + \mathcal{C}_d (c), \mathcal{T}(t )\right), \end{split} \end{equation}\end{split}\]

Context embeddings can also be defined in the configuration files. In Flemme, \(t\) and \(c\) usually denote time-step (for diffusion models) and conditions. If you want to process \(c\) like \(t\), set merge_timestep_and_condition as True.

Condition embedding

Flemme supports several ways for condition embedding, such as one-hot embedding and sinusoidal positional embedding for category and position/time-step. If the condition is a image or point cloud, any encoders introduced in Encoders can be employed.

 1condition_embedding:
 2  # how to combine condition embedding and input. Can be one of ['cat', 'add']
 3  combine_condition: cat
 4  # merge time-step embedding and context embedding, default: false
 5  # if you set this as true,
 6  # make sure context embedding is a embedding vector and can be merged.
 7  # This means we will process condition embedding in each block.
 8  merge_timestep_and_condition: false
 9  # condition embedding for encoder
10  encoder:
11    ## The condition embedding name should be one of [Identity, OneHot, Time] + supported encoders.
12    ## Usually, you need to specity the output channel,
13    ## unless you are using a supported encoder,
14    ## whose the output channel will be determined automaticlly.
15    name: Identity
16    out_channel: 1
17  # contition embedding for decoder
18  decoder:
19    ## define a cnn encoder for condition embedding
20    name: CNN
21    image_size: [320, 256]
22    in_channel: 3
23    patch_channel: 32
24    patch_size: 2
25    down_channels: [64, 128, 256]
26    middle_channels: [512, 512]
27    dense_channels: [128]
28    building_block: conv
29    normalization: batch

In the above, we simply concat the input and condition for condition embedding of encoder. For decoder, we use an CNN encoder to encode the condition.

Time-step embedding

For time-step, we use sinusoidal positional embedding. You just need to specify the channel of time-step in model configuration.

 1model:
 2  name: Base
 3  # time step embedding
 4  time_channel: 128
 5  # condition embedding
 6  condition_embedding:
 7    combine_condition: cat
 8    merge_timestep_and_condition: false
 9    encoder:
10      name: Identity
11      out_channel: 1
12    ## there is no condition embedding for decoder
13  encoder:
14    ## define a encoder you want ...

Base architecture are not directly trainable. You will need to define the losses for specific tasks. Three main derived architectures are illustrated in the following figure: (a) Classification Model; (b) Segmentation Model (SeM); (c) Auto-Encoder (AE); (d) Denoising Diffusion Probabilistic Model (DDPM)

_images/archis.png

Classification Model

For classification, we provide ClM architecture (Classification Model), whose decoder is just a MLP to project the latent embedding to per-category scores. You need to specify classification_losses for ClM. The following block define a classification model:

1model:
2  # can be AE, VAE, SeM and DDPM
3  name: ClM
4  classification_losses:
5    - name: CE
6  # encoder config
7  encoder:
8    ## define a encoder you want ...

Segmentation Model

For segmentation, we provide SeM architecture (Segmentation Model), which is a simple extension of the base architecture. You need to specify segmentation_losses for SeM. The following block define a segmentation model using a hybrid loss combining Dice and BCE loss. For all supported losses, please refer to Losses.

 1model:
 2## model architecture, SeM indicates Segmentation Model
 3  name: SeM
 4  # loss function of architecture. For SeM, you need to specify the segmentation loss.
 5  segmentation_losses:
 6    - name: Dice
 7      weight: 1.0
 8    - name: BCEL
 9      weight: 1.0
10  encoder:
11    ## define a encoder you want ...

Auto-Encoder

For reconstruction, we provide AE architecture (AutoEncoder). You need to define reconstruction_losses for AE, which can be a list or a single term. AE can be unsupervised or supervised.

1model:
2  # can be AE, VAE and DDPM
3  name: AE
4  ### loss function for reconstruction
5  reconstruction_losses:
6    name: MSE
7  is_supervising: true
8  encoder:
9    ## define a encoder you want ...

Variational Auto-Encoder

AutoEncoder can be regularized by distribution losses to learn a more continuous latent representation and have a certain capability of generation, which is also known as VAE. A VAE can be defined as following configuration:

 1model:
 2  name: VAE
 3  reconstruction_losses:
 4    name: MSE
 5  distribution_loss:
 6    name: KL
 7    ## global weight for loss
 8    weight: 0.01
 9  encoder:
10    ## define a encoder you want ...

Denoising Diffusion Probabilistic Model

In DDPM, base architecture serves as a noise predictor, which are called \(\epsilon\)-model. The loss is named as \(\epsilon\)-loss. We won’t talk much about DDPM that can be refer to this nice blog. To define a DDPM in Flemme, you may need to construct a config file as the following:

 1model:
 2  name: DDPM
 3  ## number of time steps
 4  num_steps: 1000
 5  ## noise scheduler
 6  beta_schedule: consine
 7  ## define a classifier free guidance
 8  ## the eps-model should be conditional
 9  classifier_free_guidance:
10    condition_dropout: 0.2
11    guidance_weight: 2.0
12  ## eps loss
13  eps_loss:
14    name: MSE
15  ## define eps-model, which is the noise predictor
16  eps_model:
17    ### number of channels for time-step embedding
18    ### time-step embedding will be processed in each building block
19    time_channel: 128
20    ### conditional DDPM
21    ### this model take the class label as condition
22    condition_embedding:
23      combine_condition: add
24      merge_timestep_and_condition: true
25      encoder:
26        name: OneHot
27        type: categories
28        out_channel: 128
29        num_classes: 10
30    ### define the encoder of eps-model
31    encoder:
32      ### we always recommend U-shaped networks for DDPM
33      name: UNet
34      #### other parameters related to the encoder

Denoising Diffusion Implicit Model

We support to use DDIM to accelerate sampling process of DDPM. You can directly change DDPM to DDIM in test configuration file without re-training the model. You can also directly train a DDIM model, or train a DDIM model from a DDPM checkpoint.

1model:
2  name: DDIM
3  # number of sampling steps, default is 100
4  num_sample_steps: 100
5  # other parameters stay the same as DDPM configuration

Althrough fully sampled images from DDPM model might have better quality than the images generated by DDIM. We still recommand you to use DDIM instead of DDPM if you want to visualize the generated results during training process. Because a full sampling for DDPM might be very slow. If you set a small number of sample steps for sampler, the generated results could be very unclear (see generated results on MNIST datasets).

Latent Diffusion Model

Latent diffusion models (LDM) perform diffusion and reverse diffusion process on latent space. Therefore, we need a auto-encoder to construct latent space. Similar to DDPM and DDIM, we have LDPM (Latent Diffusion Probabilistic Model) and LDIM (Latent Diffusion Implicit Model).

Latent diffusion contains two sub models: an \(\epsilon\)-model and an auto-encoder (can be AE or VAE). The following configuration files define a point cloud LDM. Latent diffusion model is still in test. Feel free to contact me if you want to know more.

 1model:
 2  name: LDPM
 3  n_steps: 1000
 4  beta_schedule: consine
 5  ## ae model
 6  ## you can choose to freeze the auto-encoder, or update its' weights
 7  freezed_ae: true
 8  ## path of pre-trained ae
 9  ae_path: path/to/auto-encoder.pth
10  ae_model:
11    name: VAE
12    encoder:
13      name: PointNet
14      in_channel: 3
15      point_num: 1024
16      building_block: single
17      conv_channels: [64, 128, 256, 512]
18      dense_channels: [1024]
19      decode_dense_channels: [1024, 512, 256]
20      activation: lrelu
21      pointwise: False
22    reconstruction_losses:
23      - name: EMD
24    distribution_loss:
25      name: KL
26      weight: 0.1
27  ## eps model
28  eps_model:
29    # encoder config
30    time_channel: 128
31    encoder:
32      name: PointWise
33      in_channel: 1024
34      dense_channels: [1024, 2048, 4096]
35      building_block: res_dense
36      activation: silu
37      normalization: layer
38      dropout: 0.1
39      data_form: VEC

Supervised Diffusion Model

Supervised diffusion model can be consider to use generation model for reconstruction (or segmentation). For image restoration, you may input a noisy image \(x\), and want to recover the clear image \(y\). You can easily to train a AE for this task, by use \(x\) as input and \(y\) as target.

If we want to use a diffusion model to do such things, we can use \(x\) as condition to generate \(y\). We wrap these process as SDPM (Supervised Diffusion Probabilistic Model) and SDIM (Supervised Diffusion Implicit Model), so you can use them like an AE or SeM. SDIM uses a accelerated and determined sampling strategy and can be directly test on the trained SDPM.

Because SDM are based on conditional diffusion models, you need to specify the condition embedding. The configuration file of a SDPM looks like the following:

 1model:
 2  # can be AE, VAE and DDPM
 3  name: SDIM
 4  num_steps: 1000
 5  num_ensemble: 1
 6  beta_schedule: consine
 7  eps_loss:
 8    name: MSE
 9  eps_model:
10    time_channel: 128
11    encoder:
12      name: SwinU
13      image_size: [320, 320]
14      in_channel: 1
15      out_channel: 1
16      patch_channel: 32
17      patch_size: 2
18      down_channels: [64, 128, 256]
19      middle_channels: [512, 512]
20      building_block: double_swin
21      abs_pos_embedding: false
22      window_size: 10
23      num_blocks: 1
24    condition_embedding:
25      combine_condition: cat
26      merge_timestep_and_context: false
27      encoder:
28        name: Identity
29        out_channel: 1

Hierarchical Architectures

We extend base architecture and propose a generic hierarchical architecture combining a pyramid loss for vertical feature fusion.

_images/pyramid.png

Details of our hierarchical architecture can refer to our paper. Theoretically, we can build hierarchical versions for all supported architectures. However, we don’t recommend to use this design for diffusion model based methods. Because we predict noise instead of reconstructing the image in the reverse diffusion process. Noise usually doesn’t contain clear global structures, and scaling the noise map may cause severe loss of details. Therefore, we construct hierarchical segmentation model and auto-encoder denoted as HSeM and HAE for image segmentation and reconstruction. Note that HSeM and HAE are not supported for point cloud segmentation and reconstruction.

To summarize, we have the following architectures:

Archi

Applicable tasks

ClM

Classification

SeM, HSeM

Segmentation

AE, HAE

Reconstruction

VAE

Reconstruction, Generation

DDPM, DDIM

Generation

SDPM, SDIM

Reconstruction, Segmentation, Generation

LDPM, LDIM

Generation