Diffusers documentation

Controlling image quality

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v0.27.2).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Controlling image quality

The components of a diffusion model, like the UNet and scheduler, can be optimized to improve the quality of generated images leading to better image lighting and details. These techniques are especially useful if you don’t have the resources to simply use a larger model for inference. You can enable these techniques during inference without any additional training.

This guide will show you how to turn these techniques on in your pipeline and how to configure them to improve the quality of your generated images.

Lighting

The Stable Diffusion models aren’t very good at generating images that are very bright or dark because the scheduler doesn’t start sampling from the last timestep and it doesn’t enforce a zero signal-to-noise ratio (SNR). The Common Diffusion Noise Schedules and Sample Steps are Flawed paper fixes these issues which are now available in some Diffusers schedulers.

For inference, you need a model that has been trained with v_prediction. To train your own model with v_prediction, add the following flag to the train_text_to_image.py or train_text_to_image_lora.py scripts.

--prediction_type="v_prediction"

For example, load the ptx0/pseudo-journey-v2 checkpoint which was trained with v_prediction and the DDIMScheduler. Now you should configure the following parameters in the DDIMScheduler.

  • rescale_betas_zero_snr=True to rescale the noise schedule to zero SNR
  • timestep_spacing="trailing" to start sampling from the last timestep

Set guidance_rescale in the pipeline to prevent over-exposure. A lower value increases brightness but some of the details may appear washed out.

from diffusers import DiffusionPipeline, DDIMScheduler

pipeline = DiffusionPipeline.from_pretrained("ptx0/pseudo-journey-v2", use_safetensors=True)

pipeline.scheduler = DDIMScheduler.from_config(
    pipeline.scheduler.config, rescale_betas_zero_snr=True, timestep_spacing="trailing"
)
pipeline.to("cuda")
prompt = "cinematic photo of a snowy mountain at night with the northern lights aurora borealis overhead, 35mm photograph, film, professional, 4k, highly detailed"
generator = torch.Generator(device="cpu").manual_seed(23)
image = pipeline(prompt, guidance_rescale=0.7, generator=generator).images[0]
image
default Stable Diffusion v2-1 image
image with zero SNR and trailing timestep spacing enabled

Details

FreeU improves image details by rebalancing the UNet’s backbone and skip connection weights. The skip connections can cause the model to overlook some of the backbone semantics which may lead to unnatural image details in the generated image. This technique does not require any additional training and can be applied on the fly during inference for tasks like image-to-image and text-to-video.

Use the enable_freeu() method on your pipeline and configure the scaling factors for the backbone (b1 and b2) and skip connections (s1 and s2). The number after each scaling factor corresponds to the stage in the UNet where the factor is applied. Take a look at the FreeU repository for reference hyperparameters for different models.

Stable Diffusion v1-5
Stable Diffusion v2-1
Stable Diffusion XL
Zeroscope
import torch
from diffusers import DiffusionPipeline

pipeline = DiffusionPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16, safety_checker=None
).to("cuda")
pipeline.enable_freeu(s1=0.9, s2=0.2, b1=1.5, b2=1.6)
generator = torch.Generator(device="cpu").manual_seed(33)
prompt = ""
image = pipeline(prompt, generator=generator).images[0]
image
FreeU disabled
FreeU enabled

Call the pipelines.StableDiffusionMixin.disable_freeu() method to disable FreeU.

pipeline.disable_freeu()
< > Update on GitHub