Projects / Faster sampling with DDIM, and LoRA fine-tuning of Stable Diffusion

Faster sampling with DDIM, and LoRA fine-tuning of Stable Diffusion

Jorge Tyrakowski

Image and Video Generation, National Yang Ming Chiao Tung University

Abstract

The second lab builds on the first. I implement DDIM sampling [1], which reuses a trained diffusion model with 50 steps instead of 1,000, on 2D Swiss-roll data and on images, and measure FID across 20 combinations of step count and stochasticity. Then I fine-tune Stable Diffusion v1.4 with LoRA [2], once to learn an artistic style and once, with DreamBooth [3], to learn a character’s identity. Fifty DDIM steps give FID 12.6 against 8.7 with all 1,000, a 20× faster sampler for a moderate loss of quality.

DDIM: the same model in 50 steps

DDIM replaces the Markovian reverse process with one that can skip steps: it samples on an evenly spaced subset of the training timesteps (with 50 steps, 980, 960, …, 0), and a parameter η\eta sets how much fresh noise each step adds (η=0\eta = 0 is deterministic). On the Swiss roll, 50 DDIM steps reach a Chamfer distance of 26.92, under the assignment’s target of 40 (Figure 1).

Scatter plot of DDIM samples over the Swiss-roll data.

DDIM samples with 50 steps (orange) against the Swiss-roll data, Chamfer distance 26.92.

DDIM on images: FID by number of steps and stochasticity η (best value per row in bold).

Stepsη = 0η = 0.2η = 0.5η = 1
1023.9922.4026.6738.76
2016.4517.7117.3423.03
5013.5712.6413.1414.79
10013.7112.0011.5310.58
100011.7011.158.848.69

With few steps a little or no noise works best, and with many steps full stochasticity does. Fifty steps with η=0.2\eta = 0.2 give FID 12.64, a 20× faster sampler for a moderate loss of quality, which is the configuration I would use in practice.

Line chart of FID against the number of DDIM steps on a log scale, one line per eta value; all lines fall as steps increase, and eta = 1 is worst at 10 steps and best at 1,000.

The same results as a chart (drawn from Table 1): with few steps, deterministic or nearly deterministic sampling (η≤0.2\eta \le 0.2) is best; with many steps, full stochasticity (η=1\eta = 1) is.

LoRA and DreamBooth on Stable Diffusion

LoRA trains small low-rank matrices inside Stable Diffusion v1.4 instead of the whole model. I used it twice, on datasets I collected and captioned myself (BLIP-2 captions, corrected by hand), both from the Berserk manga and anime.

A style from 39 images

The first LoRA learns the series’ dark-fantasy castles from 39 images: exteriors, interiors, bridges and night scenes (Figure 3). With the same prompts and seeds, the base model draws generic fantasy castles, while the fine-tuned model picks up the style’s stone architecture, dramatic lighting and atmosphere (Figures 4 and 5).

Six training images of castles from the Berserk anime: a castle by a river, dark mountains, a distant fortress, a walled castle at night, a burning city and a stone tower.

Six of the 39 training images.

Two rows of four generated castles on fire, the top row from the base model and the bottom row after LoRA training.

“Colossal castle on fire, smoke billowing into the night sky, embers flying, chaos and destruction, dark fantasy art”: the base model (top) and after LoRA training, epoch 120 (bottom), with the same seeds.

Two rows of four generated snowy gothic castles, the top row from the base model and the bottom row after LoRA training.

“Gothic forgotten castle covered in snow, intricate architecture, glowing windows, eerie atmosphere, volumetric light, masterpiece”: the base model (top) and after LoRA training, epoch 110 (bottom).

Six castles generated by the LoRA model: an interior with torches, a castle on a cliff under a full moon, a snowy bridge, a mountain fortress, a burning castle and ruins in a forest.

New scenes from the LoRA model (50 steps, guidance 7.5, 512 × 512). Top: “a castle interior, with flickering torches”; “a huge castle on a cliff at night, with a full moon in the sky”; “an ancient castle bridge over a river, surrounded by snow”. Bottom: “epic fantasy castle, perched on a jagged mountain peak, cinematic lighting, dramatic dark clouds”; “a colossal castle on fire …”; “castle ruins in a dark forest”.

A character’s identity with DreamBooth

The second model binds the protagonist, Guts, to a rare token (“sks guts”) from 20 images that vary the angle, expression, pose and background, so that the model learns the identity rather than one picture (Figure 7). Among checkpoints from 1,500 to 4,000 steps, the last kept the identity best without losing flexibility, and 200 sampling steps instead of 50 removed artifacts in the faces. The character stays recognizable in scenes that never appear in the training data (Figure 8).

Six training images of the character Guts from the Berserk anime, in different angles, expressions and poses.

Six of the 20 training images.

Six images generated by the DreamBooth model showing Guts in a portrait, in a library, in a dark forest, eating breakfast, fishing and on a battlefield.

“Portrait of sks guts …” (top: as a portrait, studying in the library, in a dark forest; bottom: eating breakfast, fishing, standing in a battlefield), each followed by “detailed anime character, high quality, sharp focus, clean lines” (200 steps, guidance 7.5).

Findings

  • DDIM trades steps for quality smoothly: 50 steps keep most of the quality of 1,000, and 20 steps are still usable (FID 16.5).
  • The best stochasticity depends on the step count: η=0\eta = 0 or 0.2 with 10 to 50 steps, η=1\eta = 1 with 100 or more.
  • LoRA adapts Stable Diffusion with a few dozen images and a small fraction of its parameters, for a style as well as for a character’s identity.
  • For identity, the data and the sampler mattered: varied training views, the last checkpoint and 200 sampling steps were what kept the character’s face consistent.

References

  1. J. Song, C. Meng and S. Ermon. Denoising diffusion implicit models. ICLR, 2021.
  2. E. J. Hu et al. LoRA: Low-rank adaptation of large language models. ICLR, 2022.
  3. N. Ruiz et al. DreamBooth: Fine tuning text-to-image diffusion models for subject-driven generation. CVPR, 2023.