Evaluating Diffusion Models

Evaluating Diffusion Models

Open In Colab

Evaluation of generative models like Stable Diffusion is subjective in nature. But as practitioners and researchers, we often have to make careful choices amongst many different possibilities. So, when working with different generative models (like GANs, Diffusion, etc.), how do we choose one over the other?

Qualitative evaluation of such models can be error-prone and might incorrectly influence a decision. However, quantitative metrics don’t necessarily correspond to image quality. So, usually, a combination of both qualitative and quantitative evaluations provides a stronger signal when choosing one model over the other.

In this document, we provide a non-exhaustive overview of qualitative and quantitative methods to evaluate Diffusion models. For quantitative methods, we specifically focus on how to implement them alongside diffusers.

The methods shown in this document can also be used to evaluate different noise schedulers keeping the underlying generation model fixed.

Scenarios

We cover Diffusion models with the following pipelines:

Qualitative Evaluation

Qualitative evaluation typically involves human assessment of generated images. Quality is measured across aspects such as compositionality, image-text alignment, and spatial relations. Common prompts provide a degree of uniformity for subjective metrics. DrawBench and PartiPrompts are prompt datasets used for qualitative benchmarking. DrawBench and PartiPrompts were introduced by Imagen and Parti respectively.

From the official Parti website:

PartiPrompts (P2) is a rich set of over 1600 prompts in English that we release as part of this work. P2 can be used to measure model capabilities across various categories and challenge aspects.

parti-prompts

PartiPrompts has the following columns:

  • Prompt

  • Category of the prompt (such as β€œAbstract”, β€œWorld Knowledge”, etc.)

  • Challenge reflecting the difficulty (such as β€œBasic”, β€œComplex”, β€œWriting & Symbols”, etc.)

These benchmarks allow for side-by-side human evaluation of different image generation models.

For this, the 🧨 Diffusers team has built Open Parti Prompts, which is a community-driven qualitative benchmark based on Parti Prompts to compare state-of-the-art open-source diffusion models:

To manually compare images, let’s see how we can use diffusers on a couple of PartiPrompts.

Below we show some prompts sampled across different challenges: Basic, Complex, Linguistic Structures, Imagination, and Writing & Symbols. Here we are using PartiPrompts as a dataset.

Copied

Now we can use these prompts to generate some images using Stable Diffusion (v1-4 checkpoint):

Copied

parti-prompts-14

We can also set num_images_per_prompt accordingly to compare different images for the same prompt. Running the same pipeline but with a different checkpoint (v1-5), yields:

parti-prompts-15

Once several images are generated from all the prompts using multiple models (under evaluation), these results are presented to human evaluators for scoring. For more details on the DrawBench and PartiPrompts benchmarks, refer to their respective papers.

It is useful to look at some inference samples while a model is training to measure the training progress. In our training scripts, we support this utility with additional support for logging to TensorBoard and Weights & Biases.

Quantitative Evaluation

In this section, we will walk you through how to evaluate three different diffusion pipelines using:

  • CLIP score

  • CLIP directional similarity

  • FID

Text-guided image generation

CLIP score measures the compatibility of image-caption pairs. Higher CLIP scores imply higher compatibility πŸ”Ό. The CLIP score is a quantitative measurement of the qualitative concept β€œcompatibility”. Image-caption pair compatibility can also be thought of as the semantic similarity between the image and the caption. CLIP score was found to have high correlation with human judgement.

Let’s first load a StableDiffusionPipeline:

Copied

Generate some images with multiple prompts:

Copied

And then, we calculate the CLIP score.

Copied

In the above example, we generated one image per prompt. If we generated multiple images per prompt, we would have to take the average score from the generated images per prompt.

Now, if we wanted to compare two checkpoints compatible with the StableDiffusionPipeline we should pass a generator while calling the pipeline. First, we generate images with a fixed seed with the v1-4 Stable Diffusion checkpoint:

Copied

Then we load the v1-5 checkpoint to generate images:

Copied

And finally, we compare their CLIP scores:

Copied

It seems like the v1-5 checkpoint performs better than its predecessor. Note, however, that the number of prompts we used to compute the CLIP scores is quite low. For a more practical evaluation, this number should be way higher, and the prompts should be diverse.

By construction, there are some limitations in this score. The captions in the training dataset were crawled from the web and extracted from alt and similar tags associated an image on the internet. They are not necessarily representative of what a human being would use to describe an image. Hence we had to β€œengineer” some prompts here.

Image-conditioned text-to-image generation

In this case, we condition the generation pipeline with an input image as well as a text prompt. Let’s take the StableDiffusionInstructPix2PixPipeline, as an example. It takes an edit instruction as an input prompt and an input image to be edited.

Here is one example:

edit-instruction

One strategy to evaluate such a model is to measure the consistency of the change between the two images (in CLIP space) with the change between the two image captions (as shown in CLIP-Guided Domain Adaptation of Image Generators). This is referred to as the ”CLIP directional similarityβ€œ.

  • Caption 1 corresponds to the input image (image 1) that is to be edited.

  • Caption 2 corresponds to the edited image (image 2). It should reflect the edit instruction.

Following is a pictorial overview:

edit-consistency

We have prepared a mini dataset to implement this metric. Let’s first load the dataset.

Copied

Copied

Here we have:

  • input is a caption corresponding to the image.

  • edit denotes the edit instruction.

  • output denotes the modified caption reflecting the edit instruction.

Let’s take a look at a sample.

Copied

Copied

And here is the image:

Copied

edit-dataset

We will first edit the images of our dataset with the edit instruction and compute the directional similarity.

Let’s first load the StableDiffusionInstructPix2PixPipeline:

Copied

Now, we perform the edits:

Copied

To measure the directional similarity, we first load CLIP’s image and text encoders:

Copied

Notice that we are using a particular CLIP checkpoint, i.e., openai/clip-vit-large-patch14. This is because the Stable Diffusion pre-training was performed with this CLIP variant. For more details, refer to the documentation.

Next, we prepare a PyTorch nn.Module to compute directional similarity:

Copied

Let’s put DirectionalSimilarity to use now.

Copied

Like the CLIP Score, the higher the CLIP directional similarity, the better it is.

It should be noted that the StableDiffusionInstructPix2PixPipeline exposes two arguments, namely, image_guidance_scale and guidance_scale that let you control the quality of the final edited image. We encourage you to experiment with these two arguments and see the impact of that on the directional similarity.

We can extend the idea of this metric to measure how similar the original image and edited version are. To do that, we can just do F.cosine_similarity(img_feat_two, img_feat_one). For these kinds of edits, we would still want the primary semantics of the images to be preserved as much as possible, i.e., a high similarity score.

We can use these metrics for similar pipelines such as the StableDiffusionPix2PixZeroPipeline.

Both CLIP score and CLIP direction similarity rely on the CLIP model, which can make the evaluations biased.

Extending metrics like IS, FID (discussed later), or KID can be difficult when the model under evaluation was pre-trained on a large image-captioning dataset (such as the LAION-5B dataset). This is because underlying these metrics is an InceptionNet (pre-trained on the ImageNet-1k dataset) used for extracting intermediate image features. The pre-training dataset of Stable Diffusion may have limited overlap with the pre-training dataset of InceptionNet, so it is not a good candidate here for feature extraction.

Using the above metrics helps evaluate models that are class-conditioned. For example, DiT. It was pre-trained being conditioned on the ImageNet-1k classes.

Class-conditioned image generation

Class-conditioned generative models are usually pre-trained on a class-labeled dataset such as ImageNet-1k. Popular metrics for evaluating these models include FrΓ©chet Inception Distance (FID), Kernel Inception Distance (KID), and Inception Score (IS). In this document, we focus on FID (Heusel et al.). We show how to compute it with the DiTPipeline, which uses the DiT model under the hood.

FID aims to measure how similar are two datasets of images. As per this resource:

FrΓ©chet Inception Distance is a measure of similarity between two datasets of images. It was shown to correlate well with the human judgment of visual quality and is most often used to evaluate the quality of samples of Generative Adversarial Networks. FID is calculated by computing the FrΓ©chet distance between two Gaussians fitted to feature representations of the Inception network.

These two datasets are essentially the dataset of real images and the dataset of fake images (generated images in our case). FID is usually calculated with two large datasets. However, for this document, we will work with two mini datasets.

Let’s first download a few images from the ImageNet-1k training set:

Copied

Copied

These are 10 images from the following Imagenet-1k classes: β€œcassette_player”, β€œchain_saw” (x2), β€œchurch”, β€œgas_pump” (x3), β€œparachute” (x2), and β€œtench”.

Real images.

Now that the images are loaded, let’s apply some lightweight pre-processing on them to use them for FID calculation.

Copied

We now load the DiTPipeline to generate images conditioned on the above-mentioned classes.

Copied

Now, we can compute the FID using torchmetrics.

Copied

The lower the FID, the better it is. Several things can influence FID here:

  • Number of images (both real and fake)

  • Randomness induced in the diffusion process

  • Number of inference steps in the diffusion process

  • The scheduler being used in the diffusion process

For the last two points, it is, therefore, a good practice to run the evaluation across different seeds and inference steps, and then report an average result.

FID results tend to be fragile as they depend on a lot of factors:

  • The specific Inception model used during computation.

  • The implementation accuracy of the computation.

  • The image format (not the same if we start from PNGs vs JPGs).

Keeping that in mind, FID is often most useful when comparing similar runs, but it is hard to reproduce paper results unless the authors carefully disclose the FID measurement code.

These points apply to other related metrics too, such as KID and IS.

As a final step, let’s visually inspect the fake_images.

Fake images.

Last updated