Counting Guidance for High Fidelity Text-to-Image Synthesis

Wonjun Kang, Kevin Galim, Hyung Il Koo · OpenReview

Abstract

Recently, the quality and performance of text-to-image generation significantly advanced due to the impressive results of diffusion models. However, text-to-image diffusion models still fail to generate high fidelity content with respect to the input prompt. One problem where text-to-diffusion models struggle is generating the exact number of objects specified in the text prompt. E.g. given a prompt "five apples and ten lemons on a table", diffusion-generated images usually contain the wrong number of objects. In this paper, we propose a method to improve diffusion models to focus on producing the correct object count given 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 handle multiple types of objects in the prompt, we use novel attention map guidance to obtain high-fidelity masks for each object. Finally, we guide the denoising process by the calculated gradients for each object. Through extensive experiments and evaluation, we demonstrate that our proposed guidance method greatly improves the fidelity of diffusion models to object count.

Introduction

While diffusion models like Stable Diffusion produce stunning visuals, they are notoriously bad at basic math—ask for "five apples," and you might get three or seven. This happens because models learn the "vibe" of a prompt rather than discrete logic; they learn what a cluster of apples looks like, but not necessarily how to render exactly five of them. Furthermore, when a prompt contains multiple object types, models often suffer from semantic mixing, where attributes like color or shape bleed between different classes.

This paper introduces Counting Guidance, a method that plugs a pre-trained counting network into the diffusion denoising loop to "steer" the model toward the correct number of objects without requiring any retraining or fine-tuning.

Examples of high-fidelity counting for single and multiple object types, including bananas, apples, and donuts.

Methodology

The authors propose a two-pronged approach to fix counting and composition issues:

1. Counting Guidance via Universal Guidance

The researchers use a pre-trained, reference-less counting network called RCC. Unlike many counters that produce heatmaps, RCC uses regression to output a single number, making it easier to calculate gradients. By using the Universal Guidance framework, they take the "predicted clean image" ($\hat{x}_0$) at any denoising step, run it through the counting network, and calculate a "counting loss." The gradient of this loss is then used to shift the noise prediction, nudging the model to add or remove objects until the count matches the prompt.

2. Attention Map Guidance for Multiple Objects

To handle prompts with more than one type of object, the team leverages the model's internal cross-attention maps to create high-quality binary masks for each object class. By applying the counting guidance only to these masked regions, they ensure the "apple counter" only looks at apples and the "donut counter" only looks at donuts. This separation effectively eliminates the semantic mixing problem.

A multi-object generation result showing two apples and four donuts, demonstrating the effectiveness of masked attention guidance.

Evaluation and Key Results

The authors created two new evaluation datasets: a Single-Object Dataset (680 prompts) and a Multi-Object Dataset (1,122 prompts). They used Grounding DINO, a state-of-the-art object detection model, to objectively count objects for quantitative metrics.

Key findings include:

  • Improved Accuracy: On the single-object dataset, the Mean Absolute Error (MAE) dropped from 0.599 (Vanilla) to 0.567.
  • High Counts: The improvement is most visible in difficult cases. For "fifty apples," vanilla Stable Diffusion generated only 18, while the proposed method generated 46.
  • Human Preference: In a user study, the proposed method was preferred 1.9 times more than vanilla Stable Diffusion for its ability to follow numerical instructions.
  • Dynamic Scaling: Performance was optimized by scaling the guidance strength linearly with the requested number of objects ($N$).

Conclusion

While Counting Guidance significantly improves fidelity, it comes with a computational trade-off, roughly doubling generation time due to extra gradient calculations. Future work will focus on automating hyperparameter selection and optimizing the framework to be more robust across a wider variety of prompts without manual intervention.

Summary updated

Abstract

Recently, the quality and performance of text-to-image generation significantly advanced due to the impressive results of diffusion models. However, text-to-image diffusion models still fail to generate high fidelity content with respect to the input prompt. One problem where text-to-diffusion models struggle is generating the exact number of objects specified in the text prompt. E.g. given a prompt "five apples and ten lemons on a table", diffusion-generated images usually contain the wrong number of objects. In this paper, we propose a method to improve diffusion models to focus on producing the correct object count given 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 handle multiple types of objects in the prompt, we use novel attention map guidance to obtain high-fidelity masks for each object. Finally, we guide the denoising process by the calculated gradients for each object. Through extensive experiments and evaluation, we demonstrate that our proposed guidance method greatly improves the fidelity of diffusion models to object count.
BibTeX
@inproceedings{kang2025counting,
  author = {Wonjun Kang and Kevin Galim and Hyung Il Koo},
  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