You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Latent Perceptual Loss (LPL) for Stable Diffusion XL
2
+
3
+
This directory contains an implementation of Latent Perceptual Loss (LPL) for training Stable Diffusion XL models, based on the paper "Boosting Latent Diffusion with Perceptual Objectives" (Berrada et al., 2025). LPL is a perceptual loss that operates in the latent space of a VAE, helping to improve the quality and consistency of generated images by bridging the disconnect between the diffusion model and the autoencoder decoder.
4
+
5
+
## Overview
6
+
7
+
LPL addresses a key limitation in latent diffusion models (LDMs): the disconnect between the diffusion model training and the autoencoder decoder. While LDMs train in the latent space, they don't receive direct feedback about how well their outputs decode into high-quality images. This can lead to:
8
+
9
+
- Loss of fine details in generated images
10
+
- Inconsistent image quality
11
+
- Structural artifacts
12
+
- Reduced sharpness and realism
13
+
14
+
LPL works by comparing intermediate features from the VAE decoder between the predicted and target latents. This helps the model learn better perceptual features and can lead to:
15
+
16
+
- Improved image quality and consistency (6-20% FID improvement)
17
+
- Better preservation of fine details
18
+
- More stable training, especially at high noise levels
19
+
- Better handling of structural information
20
+
- Sharper and more realistic textures
21
+
22
+
## Implementation Details
23
+
24
+
The LPL implementation follows the paper's methodology and includes several key features:
25
+
26
+
1.**Feature Extraction**: Extracts intermediate features from the VAE decoder, including:
27
+
- Middle block features
28
+
- Up block features (configurable number of blocks)
29
+
- Proper gradient checkpointing for memory efficiency
30
+
- Features are extracted only for timesteps below the threshold (high SNR)
31
+
32
+
2.**Feature Normalization**: Multiple normalization options as validated in the paper:
33
+
-`default`: Normalize each feature map independently
34
+
-`shared`: Cross-normalize features using target statistics (recommended)
35
+
-`batch`: Batch-wise normalization
36
+
37
+
3.**Outlier Handling**: Optional removal of outliers in feature maps using:
38
+
- Quantile-based filtering (2% quantiles)
39
+
- Morphological operations (opening/closing)
40
+
- Adaptive thresholding based on standard deviation
41
+
42
+
4.**Loss Types**:
43
+
- MSE loss (default)
44
+
- L1 loss
45
+
- Optional power law weighting (2^(-i) for layer i)
46
+
47
+
## Usage
48
+
49
+
To use LPL in your training, add the following arguments to your training command:
--lpl_pow_law \ # Use power law weighting for layers
59
+
--lpl_num_blocks 4 \ # Number of up blocks to use (1-4)
60
+
--lpl_remove_outliers \ # Remove outliers in feature maps
61
+
--lpl_scale \ # Scale LPL loss by noise level weights
62
+
--lpl_start 0 \ # Step to start applying LPL
63
+
# ... other training arguments ...
64
+
```
65
+
66
+
### Key Parameters
67
+
68
+
-`lpl_weight`: Controls the strength of the LPL loss relative to the main diffusion loss. Higher values (1.0-2.0) improve quality but may slow training.
69
+
-`lpl_t_threshold`: LPL is only applied for timesteps below this threshold (high SNR). Lower values (100-200) focus on more important timesteps.
70
+
-`lpl_loss_type`: Choose between MSE (default) and L1 loss. MSE is recommended for most cases.
71
+
-`lpl_norm_type`: Feature normalization strategy. "shared" is recommended as it showed best results in the paper.
72
+
-`lpl_pow_law`: Whether to use power law weighting (2^(-i) for layer i). Recommended for better feature balance.
73
+
-`lpl_num_blocks`: Number of up blocks to use for feature extraction (1-4). More blocks capture more features but use more memory.
74
+
-`lpl_remove_outliers`: Whether to remove outliers in feature maps. Recommended for stable training.
75
+
-`lpl_scale`: Whether to scale LPL loss by noise level weights. Helps focus on more important timesteps.
76
+
-`lpl_start`: Training step to start applying LPL. Can be used to warm up training.
77
+
78
+
## Recommendations
79
+
80
+
1.**Starting Point** (based on paper results):
81
+
```bash
82
+
--use_lpl \
83
+
--lpl_weight 1.0 \
84
+
--lpl_t_threshold 200 \
85
+
--lpl_loss_type mse \
86
+
--lpl_norm_type shared \
87
+
--lpl_pow_law \
88
+
--lpl_num_blocks 4 \
89
+
--lpl_remove_outliers \
90
+
--lpl_scale
91
+
```
92
+
93
+
2.**Memory Efficiency**:
94
+
- Use `--gradient_checkpointing` for memory efficiency (enabled by default)
95
+
- Reduce `lpl_num_blocks` if memory is constrained (2-3 blocks still give good results)
96
+
- Consider using `--lpl_scale` to focus on more important timesteps
97
+
- Features are extracted only for timesteps below threshold to save memory
98
+
99
+
3.**Quality vs Speed**:
100
+
- Higher `lpl_weight` (1.0-2.0) for better quality
101
+
- Lower `lpl_t_threshold` (100-200) for faster training
102
+
- Use `lpl_remove_outliers` for more stable training
103
+
-`lpl_norm_type shared` provides best quality/speed trade-off
104
+
105
+
## Technical Details
106
+
107
+
### Feature Extraction
108
+
109
+
The LPL implementation extracts features from the VAE decoder in the following order:
110
+
1. Middle block output
111
+
2. Up block outputs (configurable number of blocks)
4. Optional power law weighting (2^(-i) for layer i)
118
+
119
+
### Loss Calculation
120
+
121
+
For each feature map:
122
+
1. Features are normalized according to the chosen strategy
123
+
2. Loss is calculated between normalized features
124
+
3. Outliers are masked out (if enabled)
125
+
4. Loss is weighted by layer depth (if power law enabled)
126
+
5. Final loss is averaged across all layers
127
+
128
+
### Memory Considerations
129
+
130
+
- Gradient checkpointing is used by default
131
+
- Features are extracted only for timesteps below the threshold
132
+
- Outlier removal is done in-place to save memory
133
+
- Feature normalization is done efficiently using vectorized operations
134
+
- Memory usage scales linearly with number of blocks used
135
+
136
+
## Results
137
+
138
+
Based on the paper's findings, LPL provides:
139
+
- 6-20% improvement in FID scores
140
+
- Better preservation of fine details
141
+
- More realistic textures and structures
142
+
- Improved consistency across different resolutions
143
+
- Better performance on both small and large datasets
144
+
145
+
## Citation
146
+
147
+
If you use this implementation in your research, please cite:
148
+
149
+
```bibtex
150
+
@inproceedings{berrada2025boosting,
151
+
title={Boosting Latent Diffusion with Perceptual Objectives},
152
+
author={Tariq Berrada and Pietro Astolfi and Melissa Hall and Marton Havasi and Yohann Benchetrit and Adriana Romero-Soriano and Karteek Alahari and Michal Drozdzal and Jakob Verbeek},
153
+
booktitle={The Thirteenth International Conference on Learning Representations},
0 commit comments