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.
- 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.
- 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.
- 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.
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."
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.
Summary updated