Counting Guidance for High Fidelity Text-to-Image Synthesis

Wonjun Kang, Kevin Galim, Hyung Il Koo, Nam Ik Cho

TL;DR

Researchers have developed a "counting guidance" method that allows diffusion models to generate the exact number of objects specified in a prompt. By leveraging the gradients of a pre-trained counting network and using internal attention maps to separate different object types, the method significantly improves numerical accuracy without requiring any retraining or fine-tuning of the base model.

What Problem Does This Paper Address?

Despite the breathtaking quality of modern text-to-image (T2I) models like Stable Diffusion, they remain notoriously bad at basic arithmetic. If you ask for "three apples," you might get two, four, or a cluster of red blobs. This is a failure of compositional synthesis: the model understands the concept of an apple but struggles to ground the quantity specified in the text.

This happens because these models are trained on massive, noisy datasets where text-image alignment for specific counts is often poor. Furthermore, when multiple object types are involved (e.g., "three apples and two donuts"), models often suffer from semantic information mixing, where the features of one object bleed into another, resulting in "apple-colored donuts" or hybrid shapes.

Methods

The authors introduce a plug-and-play guidance mechanism that works during the inference (denoising) stage.

  1. Counting Guidance via Universal Guidance: Instead of retraining a diffusion model to count, the authors use a pre-trained, reference-less counting network called RCC. Because standard counting networks aren't designed to look at noisy, intermediate diffusion latents, the authors adopt the Universal Guidance framework. At each denoising step, the model predicts a "clean" version of the image ($\hat{x}_0$), passes it through the counting network, calculates the error relative to the prompt's target count, and uses the resulting gradient to nudge the denoising process in the right direction.
  2. Attention Map Guidance: To handle prompts with multiple object types, the team uses the model's internal cross-attention maps. These maps indicate which pixels the model "thinks" belong to which word. By optimizing these maps to be distinct and non-overlapping, they prevent semantic mixing.
  3. Masked Counting Guidance: Once they have high-quality attention maps, they binarize them into masks. This allows the counting network to look at specific regions of the image independently—counting apples only where the "apple" mask is active and donuts only where the "donut" mask is active.
Figure 1: Counting guidance applied to Stable Diffusion. The top row shows vanilla failures, while the bottom row shows the proposed method hitting the exact counts.

Datasets

The authors created two new benchmark datasets for evaluation:

  • Single Object Dataset: 680 prompts covering 34 object classes (animals and common objects) with counts ranging from 1 to 20.
  • Multiple Object Dataset: 1,122 prompts featuring pairs of different object classes with randomized counts for each.

For quantitative evaluation, they used Grounding DINO, a state-of-the-art object detection model, to act as an objective "judge" of the final counts.

Key Insights and Ideas

  • Regression is Key: Most counting networks produce heatmaps (density maps). The authors chose RCC specifically because it uses regression to output a single scalar count. This makes it much easier to calculate the direct gradients needed for guidance.
  • The "Convex" Guidance Scale: The authors found that the strength of the guidance ($s_{count}$) follows a convex error curve. Too little guidance fails to fix the count; too much guidance distorts the image structure and destroys fidelity.
  • Dynamic Scheduling: They discovered that a linear schedule for the guidance scale—increasing the strength as the target number $N$ grows—outperforms a constant scale. Larger numbers are harder to generate and thus require a stronger "nudge."
Figure 3: Visualizing how attention map guidance prevents semantic mixing between oranges and eggs.

Key Results

The proposed method outperformed vanilla Stable Diffusion and existing state-of-the-art methods like Attend-and-Excite across several metrics:

  • Counting Accuracy: On the single-object dataset, the method achieved the lowest Mean Absolute Error (MAE) and Root Mean Squared Error (RMSE). For example, their linear guidance method reduced MAE from 0.599 (vanilla) to 0.567.
  • High Counts: The method remains effective even for large numbers. In one test, vanilla Stable Diffusion generated only 18 apples for a "fifty apples" prompt, while the guided version generated 46.
  • Human & AI Preference: In user studies, the proposed method was preferred 1.9 times more than vanilla Stable Diffusion. A GPT-4V-based evaluation also favored their method for count accuracy.

Limitations and Future Work

While highly effective, the method has a few drawbacks:

  • Computational Overhead: Adding counting guidance nearly doubles the generation time (from ~12 seconds to ~27 seconds per image on a V100 GPU).
  • Hyperparameter Sensitivity: As shown in Figure 6, choosing the wrong guidance scale can lead to significant image distortion. The authors currently rely on a linear schedule, but finding a truly universal, robust scale for any prompt remains an open challenge.
  • Structural Integrity: For very high counts or complex backgrounds, the guidance can sometimes recreate the entire image structure to fit the objects, losing the "spirit" of the original unguided seed.
Figure 6: Failure cases showing how an excessive guidance scale ($s_{count}=10$) can distort image quality.

Summary updated

Abstract

Recently, there have been significant improvements in the quality and performance of text-to-image generation, largely due to the impressive results attained by diffusion models. However, text-to-image diffusion models sometimes struggle to create high-fidelity content for the given input prompt. One specific issue is their difficulty in generating the precise number of objects specified in the text prompt. For example, when provided with the prompt "five apples and ten lemons on a table," images generated by diffusion models often contain an incorrect number of objects. In this paper, we present a method to improve diffusion models so that they accurately produce the correct object count based on the input prompt. We adopt a counting network that performs reference-less class-agnostic counting for any given image. We calculate the gradients of the counting network and refine the predicted noise for each step. To address the presence of multiple types of objects in the prompt, we utilize novel attention map guidance to obtain high-quality masks for each object. Finally, we guide the denoising process using the calculated gradients for each object. Through extensive experiments and evaluation, we demonstrate that the proposed method significantly enhances the fidelity of diffusion models with respect to object count. Code is available at https://github.com/furiosa-ai/counting-guidance.
BibTeX
@inproceedings{kang2025counting,
  author = {Wonjun Kang and Kevin Galim and Hyung Il Koo and Nam Ik Cho},
  title = {{Counting Guidance for High Fidelity Text-to-Image Synthesis}},
  booktitle = {IEEE/CVF Winter Conference on Applications of Computer Vision},
  year = {2025},
  doi = {10.1109/wacv61041.2025.00097},
}
Figure