Papers
Topics
Authors
Recent
Search
2000 character limit reached

Count Anything: A Unified Counting Framework

Updated 5 July 2026
  • Count Anything is a family of problems where the target set is dynamically specified using exemplars, text, literals, or constraints instead of fixed classes.
  • The literature showcases methodologies ranging from exemplar matching to weakly supervised repetition recognition, employing density maps, sparse localization, and combinatorial techniques.
  • Approaches emphasize overcoming challenges in instance separation, semantic grounding, and computational complexity to accurately count diverse objects in visual, textual, and symbolic domains.

In the literature surveyed here, “Count Anything” denotes a family of counting problems in which the counted set is not fixed by a single pre-specified class. Instead, the target is specified by exemplars, text, literal assumptions, stream definitions, or combinatorial constraints, and the system returns either a scalar count or a set whose cardinality is the count. The phrase is most visible in class-agnostic and open-world visual counting, but closely related formulations also appear in zero-shot numerosity, semantic-invariant list counting, answer set counting, streaming frequency moments, machine-learning counting engines, and generalized combinatorial counting functions (Lu et al., 2018, Lei et al., 29 May 2026, Fichte et al., 2023, 0802.2305, Janjic et al., 2013).

1. Formal problem formulations

A common thread across these works is that counting is defined by an explicit specification of the target set. In few-shot object counting, the support set is given as S={(Isj,bj)}j=1kS=\{(I_s^j,b^j)\}_{j=1}^k, where each bjb^j is a bounding box around one instance of the target category in support image IsjI_s^j; for a query image IqI_q containing an unknown number yy of instances, the goal is to estimate y^\hat y (Ma et al., 2023). In multi-grained visual counting, the input is an image II, visual exemplars EE, text TT, and an intended granularity gg, and the counting function is written as bjb^j0 (Liu et al., 11 May 2026). In text-guided cross-domain counting, the output is an instance-grounded point set bjb^j1, with final count bjb^j2 (Lei et al., 29 May 2026). Outside vision, ASP navigation asks for bjb^j3 under assumptions bjb^j4 on literals (Fichte et al., 2023), stream counting estimates the bjb^j5th frequency moment bjb^j6 (0802.2305), and database-style counting engines answer queries of the form bjb^j7 (Karan et al., 2018).

Setting Target specification Count output
Few-shot object counting Support set bjb^j8 and query image bjb^j9 Estimate IsjI_s^j0
Multi-grained/open-world visual counting Image IsjI_s^j1, exemplars IsjI_s^j2, text IsjI_s^j3, granularity IsjI_s^j4 IsjI_s^j5 or IsjI_s^j6
ASP counting under assumptions Ground ASP program IsjI_s^j7 and assumptions IsjI_s^j8 IsjI_s^j9
Stream moment counting Turnstile updates to IqI_q0 IqI_q1
ML contingency counting Dataset IqI_q2 and equality predicate IqI_q3 IqI_q4

The output representation differs substantially by domain. Some systems infer counts by summing a density map, some by thresholding localized proposals, some by counting masks or points, and some by exact symbolic enumeration. This suggests that “count anything” is not a single algorithmic template, but a broader design objective: decouple the specification of the target from a fixed, domain-specific detector or class label.

2. Exemplar-based class-agnostic visual counting

A foundational visual formulation is class-agnostic counting as matching. “Class-Agnostic Counting” introduced the Generic Matching Network (GMN), which reformulated counting as exemplar matching via image self-similarity rather than class-specific detection (Lu et al., 2018). An exemplar patch IqI_q5 and image IqI_q6 are embedded as IqI_q7 and IqI_q8, and a learned relation module predicts a similarity map,

IqI_q9

When this similarity map is trained as a density map, the count is obtained by summation; when objects are well separated, local maxima can be counted as detections. GMN also introduced residual adapters for few-shot customization, training only 178K parameters out of 6.0M total.

“Represent, Compare, and Learn: A Similarity-Aware Framework for Class-Agnostic Counting” argued that a standard exemplar pipeline depends critically on both representation and similarity metric, and that naive inner-product matching produces noisy similarity maps (Shi et al., 2022). BMNet defined a learnable bilinear similarity

yy0

while BMNet+ added self-similarity representation enhancement, dynamic exemplar-conditioned channel attention, and a similarity-aware loss. These models remained density-based, with the final count computed as

yy1

On FSC147, BMNet+ reported Test MAE 14.62 and MSE 91.83, improving over FamNet and BMNet under the reported protocol (Shi et al., 2022).

“SQLNet: Scale-Modulated Query and Localization Network for Few-Shot Class-Agnostic Counting” replaced density regression with localization-based counting and made scale a first-class supervisory signal (Wu et al., 2023). SQLNet comprises the Hierarchical Exemplars Collaborative Enhancement (HECE) module, the Exemplars-Unified Query Correlation (EUQC) module, and the Scale-aware Multi-head Localization (SAML) module. It predicts per-instance confidence, center, and size, and counts by thresholding confidences:

yy2

Its scale-aware localization loss combines weighted BCE for confidence, yy3 for center localization, and yy4 for exemplar-driven size regression. On FSC-147, SQLNet reported Val MAE 12.40 and RMSE 42.30, and Test MAE 12.49 and RMSE 80.85, while also producing approximate bounding boxes (Wu et al., 2023).

These exemplar-based systems differ primarily in the representation of similarity. GMN uses learned matching over dense features, BMNet/BMNet+ jointly learn feature space and metric, and SQLNet explicitly localizes points and sizes. A plausible implication is that the central difficulty in class-agnostic counting is not only target specification but also the geometry of the matching space: whether the model produces a smooth density, a sparse localization set, or a similarity field strongly affects failure modes and downstream utility.

3. Reference-less and text-guided open-world counting

“Learning to Count Anything: Reference-less Class-agnostic Counting with Weak Supervision” removed the test-time reference image entirely and recast counting as repetition recognition (Hobley et al., 2022). The method uses a ViT-Small backbone initialized with DINO, flattens globally contextual token features yy5, and predicts the count with a linear regressor,

yy6

Training uses only image-level count labels with the Absolute Percentage Error loss,

yy7

On FSC-147, the method reported Test MAE 17.12 and RMSE 104.53; on the cleaned FSC-133 benchmark it reported Test MAE 14.23 and RMSE 43.83 (Hobley et al., 2022). The paper’s central claim is that globally contextual ViT features are sufficient for weakly supervised reference-less enumeration.

“Can SAM Count Anything? An Empirical Study on SAM Counting” examined whether the frozen Segment Anything Model can perform few-shot counting without additional classifiers or detectors (Ma et al., 2023). The pipeline uses SAM’s ViT-H dense features, segments support exemplars from bounding-box prompts, generates query masks from a yy8 grid of point prompts, converts each mask to a masked-average feature vector, and counts the masks whose cosine similarity to the support prototype exceeds a threshold:

yy9

No fine-tuning, NMS, clustering, or IoU-based de-duplication is used. On COCO val2017, SAM reported average MAE 3.87 and RMSE 8.03; on FSC-147 (3-shot), it reported Test MAE 27.97 and RMSE 131.24, with the gap widening on small and crowded scenes (Ma et al., 2023).

A more explicit open-world formulation appears in “Count Anything at Any Granularity” (Liu et al., 11 May 2026). This work argues that open-world counting is brittle because counting granularity is usually left implicit. It formalizes multi-grained counting with five explicit levels: identity-level, attribute-level, category-level, instance-level, and concept-level. The proposed KubriCount dataset contains 110,507 images, about 7.3M instances, 157 categories, and five granularity levels, while HieraCount combines text and visual exemplars as complementary target specifications. HieraCount reported overall MAE 4.67 and RMSE 11.07 on KubriCount, with L4 remaining the hardest level and reporting MAE 8.37 and RMSE 17.14 (Liu et al., 11 May 2026).

A closely related but distinct text-guided formulation is given in “Count Anything” (Lei et al., 29 May 2026). This work defines text-guided object counting over CLOC, a Cross-domain Large-scale Object Counting dataset with about 220K images, 619 categories, and 15.356M instances across General Scene, Remote Sensing, Histopathology, Cellular Microscopy, Agriculture, and Microbiology. The model outputs discrete instance points rather than density maps, using a Region-level Sparse Counter, a Pixel-level Dense Counter, and Complementary Count Fusion. On the full CLOC test set it reported 9.34 MAE, 33.34 RMSE, and 0.75 NAE (Lei et al., 29 May 2026).

Taken together, these works show a progression from exemplar-conditioned matching, to weakly supervised repetition recognition, to text-guided and multi-grained prompt following. They also show that removing or broadening the reference mechanism does not remove the need for precise target specification; rather, the specification shifts from a cropped exemplar to a combination of global context, text, and explicit granularity labels.

4. Zero-shot numerosity and semantic invariance

A distinct line of work studies whether counting itself generalizes independently of object identity. “Zero-shot counting with a dual-stream neural network model” defines zero-shot counting as correctly reporting how many target items are present when those items were never seen during training (Thompson et al., 2024). The model uses 12 saliency-based, log-polar glimpses, a ventral pathway for glimpse contents, a dorsal pathway for fixation coordinates, a recurrent PPC-like module, a 36-unit sigmoid map layer, and a 5-way numerosity readout for counts 1–5. In simple counting, the dual-stream RNN reported validation accuracy 99.0% ± 0.1, OOD shape 98.1% ± 0.2, OOD luminance 98.4% ± 0.2, and OOD both 97.1% ± 0.3; in the ignore-distracters setting it reported OOD both 95.5% ± 0.6 (Thompson et al., 2024). The recurrent units developed bell-shaped numerosity tuning better fit by Gaussians in y^\hat y0 than in y^\hat y1, consistent with a lognormal code:

y^\hat y2

At a different level of abstraction, “Semantic Content Determines Algorithmic Performance” argues that counting should be invariant to what is being counted (Ríos-García et al., 29 Jan 2026). It defines a semantic invariance desideratum for a counting operator y^\hat y3 over delimited lists:

y^\hat y4

and measures semantic dependence by

y^\hat y5

On the WhatCounts benchmark, frontier LLMs exhibited over 40 percentage points of accuracy variation depending solely on semantic category, even when structure and length were held constant (Ríos-García et al., 29 Jan 2026). Explicit separator prompts, XML wrapping, and token-count controls did not eliminate the semantic gap, and agentic tool use increased accuracy while widening the gap.

These two strands use different experimental objects—small visual arrays in one case, delimited text lists in the other—but they converge on a common theoretical issue. Counting generalizes when the system separates content from structure, as in the dorsal/ventral factorization, and fails when semantic content contaminates aggregation, as in WhatCounts. This suggests that “count anything” depends not only on supervision scale but on whether the architecture explicitly isolates the operation of aggregation from the semantics of the operands.

5. Exact counting in answer set programming

In symbolic reasoning, “count anything” takes the form of counting solutions of declarative programs under changing assumptions. “IASCAR: Incremental Answer Set Counting by Anytime Refinement” addresses repeated counting of stable models of a ground ASP program under literal assumptions (Fichte et al., 2023). The method compiles Clark’s completion of the program into an sd-DNNF, counts supported models under assumptions by conditioning, and then corrects the supported-model overcount through inclusion–exclusion over unsupported cycle constraints:

y^\hat y6

At full depth y^\hat y7, y^\hat y8 equals the exact answer set count y^\hat y9. The method is incremental and anytime: after the offline compilation phase, conditioning and recounting are very fast, often milliseconds, while depth-2 inclusion–exclusion often gives useful bounds (Fichte et al., 2023).

“Exact ASP Counting with Compact Encodings” takes a different route to exactness (Kabir et al., 2023). sharpASP constructs Clark’s completion II0 together with a compact copy layer II1 only for loop atoms, and proves the equivalence

II2

This encoding lets standard #SAT machinery—component decomposition, caching, unit propagation, and knowledge compilation—be reused with minimal changes. On 1470 benchmarks, the hybrid sharpASP solver solved 1062 instances with PAR2 score 3082, compared with 895 instances and PAR2 score 4205 for the previous best hybrid under the same conditions (Kabir et al., 2023).

The contrast between IASCAR and sharpASP is instructive. IASCAR compiles an overapproximation and corrects it online by inclusion–exclusion; sharpASP builds a compact exact encoding whose residual copy structure captures the stability condition. Both rely on the separation between a tractable compiled representation and a domain-specific correction or semantic test. In this literature, “count anything” therefore means count arbitrary solution sets of the same declarative theory under many queries, not detect arbitrary visual categories.

6. Streaming, query-engine, and combinatorial counting

In streaming algorithms, “Count Anything” corresponds to maintaining generic aggregate statistics of a stream rather than enumerating objects. “Compressed Counting” estimates the II3th frequency moment II4 for II5 in the Turnstile model under the condition that II6 at evaluation time (0802.2305). It uses maximally skewed stable random projections and provides estimators for II7 from projected samples. Near II8, the method achieves sample complexity II9 instead of EE0:

EE1

with EE2 and EE3 as EE4. The same sketch supports Rényi and Tsallis entropy estimation, logarithmic norms, logarithmic distances, and method-of-moments statistics (0802.2305).

“Fast Counting in Machine Learning Applications” abstracts counting queries over discrete datasets into a streaming accumulator interface (Karan et al., 2018). The basic query is

EE5

with applications to BN sufficient statistics and association rule mining. The paper proposes Bitmap and Radix strategies that stream counts to user-defined accumulators rather than materializing heavy-weight indexes. In empirical evaluation, Radix was about 20× faster than hash tables on average in random queries, 5–25× faster than hash tables in Bayesian network learning, and up to 100× faster than ADtrees on high-arity datasets (Karan et al., 2018).

“A Counting Function” provides a combinatorial unification of many counting problems through the number of EE6-insets of a set system with EE7 main blocks of sizes EE8 and an additional block of size EE9 (Janjic et al., 2013). The counting function

TT0

admits the inclusion–exclusion formula

TT1

The paper shows that this same function counts, among other objects, TT2-matrices with no zero rows or columns, Delannoy numbers, Catalan-related quantities, crystal ball sequences of the cubic lattice, spanning subgraphs of complete bipartite graphs, squares in a square, and the number of rook, bishop, and queen moves on a chessboard (Janjic et al., 2013).

These works are methodologically distant from visual counting, but they share a structural aim: define a count operator that can be reused across many targets by changing only the specification of the set being aggregated. In streaming this specification is a moment or sketch; in tabular ML it is a predicate context; in combinatorics it is a block system and inset constraint.

7. Recurrent failure modes and open problems

Across modalities, the main obstacle to “count anything” is not the arithmetic of counting but the fidelity of target specification and instance separation. In frozen SAM counting, the two core issues are mask fragmentation or merging in crowded scenes and class-agnostic ambiguity, producing undercounting for tightly packed small objects and confusions among visually similar categories (Ma et al., 2023). SQLNet explicitly notes that very heavy occlusions and extreme scale variations can still challenge size estimation, and that optional IoU-based NMS may be needed in extremely crowded scenes (Wu et al., 2023). HieraCount reports that L4, the instance-level distinction within a category, remains the hardest granularity (Liu et al., 11 May 2026). Count Anything on CLOC identifies fusion edge cases in extremely crowded or heavily occluded scenes and notes residual annotation noise and domain imbalance (Lei et al., 29 May 2026). RCC reports that very dense images with more than 1000 instances are a failure mode because the 28×28 token grid under-resolves tiny objects (Hobley et al., 2022).

A second recurrent problem is semantic grounding. Fine-grained prompt following fails systematically in multi-grained open-world counting, especially when positive and negative sets differ by attributes or near-neighbor instance types (Liu et al., 11 May 2026). In language-only list counting, WhatCounts shows that aggregation itself can depend strongly on semantic category, even when item boundaries are unambiguous and explicit (Ríos-García et al., 29 Jan 2026). The failure is therefore not reducible to low-level segmentation alone; it can also arise from argument-dependent aggregation.

A third problem is computational complexity. IASCAR amortizes expensive compilation over repeated queries, but its online inclusion–exclusion stage remains worst-case exponential in the number of positive cycles (Fichte et al., 2023). sharpASP inherits the #P-completeness of exact ASP counting and can lose efficiency on hard-to-decompose instances (Kabir et al., 2023). Compressed Counting achieves strong guarantees only under the evaluation-time nonnegativity restriction and for TT3 (0802.2305). Fast counting engines for ML workloads assume discrete features and equality predicates, not arbitrary relational or continuous queries (Karan et al., 2018).

Taken together, these results suggest that robust “count anything” systems require three aligned components. The target set must be specified explicitly, whether by exemplars, text, assumptions, or predicates; the output representation must preserve enough instance structure to support aggregation; and the gap between tractable approximation and target semantics must be corrected, whether by supervised localization, granularity-aware prompting, inclusion–exclusion, compact encodings, deterministic counting tools, or sketching guarantees. That synthesis is a plausible interpretation of the surveyed literature rather than a single stated theorem, but it captures the most consistent pattern across these otherwise disparate lines of work.

Topic to Video (Beta)

No one has generated a video about this topic yet.

Whiteboard

No one has generated a whiteboard explanation for this topic yet.

Follow Topic

Get notified by email when new papers are published related to Count Anything.