Training-free Object Counting with Prompts

Zenglin Shi, Ying Sun, Mengmi Zhang

Abstract

Traditional object counting models often require extensive, labor-intensive point annotations and task-specific training. This paper presents a novel training-free object counter that utilizes foundation models like the Segment Anything Model (SAM) and CLIP. By incorporating similarity, segment, and semantic priors into the mask generation process, the model can be prompted with points, boxes, or text to count objects in a class-agnostic manner. Experimental results on benchmarks like FSC-147 demonstrate that this training-free approach achieves competitive performance against supervised, learning-based methods.

Introduction

Object counting is a fundamental task in computer vision with applications ranging from agriculture to urban planning. However, the field faces a significant data bottleneck: high-performing models typically require massive datasets with manual point annotations for every object. While class-agnostic models exist, they usually still require a training phase to learn how to map similarity maps to density maps. This research explores whether a counter can be built that requires zero training and can be "prompted" to count anything using simple inputs like points, boxes, or text.

Overview of promptable counting

Methods

The authors frame counting as an instance segmentation problem: if every instance of a target object can be perfectly segmented, the count is simply the number of masks generated. While SAM serves as a powerful "generalist" backbone, it lacks the class-specific knowledge required to distinguish target objects from background clutter in a counting context. To address this, the authors introduce Prior-guided Mask Generation.

Vanilla vs. Prior-guided pipelines

As shown in the comparison above, the proposed pipeline incorporates three specific priors to refine the segmentation process:

  1. Similarity Prior: The model calculates the cosine similarity between a reference object (derived from the prompt) and the rest of the image. This creates a "label map" that guides SAM by identifying likely target areas (positive points) and background (negative points).
  2. Segment Prior: To improve efficiency and prevent redundant counting, the model maintains an overall segment map. This ensures that SAM does not waste computational cycles re-segmenting regions that have already been counted.
  3. Semantic Prior: Visual features of the reference object ($F_R$) are injected directly into SAM’s mask decoder, helping the model focus specifically on the visual characteristics of the target class.

For text-specified counting, the system employs a two-stage approach using CLIP-Surgery to find a reference object in the image based on the text prompt, which is then used to initialize the prior-guided pipeline.

Key Results and Insights

The model was evaluated on the FSC-147 and CARPK datasets. On FSC-147, using box prompts, it achieved a Mean Absolute Error (MAE) of 19.95, outperforming several established supervised models like GMN and FamNet+. In zero-shot text counting, it reached an MAE of 24.79, nearing the performance of state-of-the-art models specifically trained for text-to-count tasks.

Beyond accuracy, the method is notably efficient. By utilizing the Segment Prior to manage redundancy, the pipeline processes images in approximately 2.1 seconds, compared to 3.4 seconds for a standard vanilla SAM implementation.

Limitations and Future Work

Despite its strong performance, the model struggles in extremely dense scenes where objects are only a few pixels wide or heavily occluded. It can also suffer from semantic ambiguity, occasionally "double counting" parts of a single object as separate entities. Future research will investigate adaptive thresholding to handle varying object scales and explore potential few-shot fine-tuning to further bridge the gap with fully supervised models.

Summary updated

BibTeX
@inproceedings{shi2024training,
  author = {Zenglin Shi and Ying Sun and Mengmi Zhang},
  title = {{Training-free Object Counting with Prompts}},
  booktitle = {IEEE/CVF Winter Conference on Applications of Computer Vision},
  year = {2024},
  doi = {10.1109/wacv57701.2024.00039},
}
Figure