Niki Amini-Naieni & Andrew Zisserman
[NOTE]: Full training and inference code will be released by June 3rd, 2026 (CVPR 2026 conference start date)
Official PyTorch implementation for CountGD++. Details can be found in the paper, [Paper] [Project page].
If you find this repository useful, please give it a star ⭐.
New capabilities of CountGD++. (a) Counting with Positive & Negative Prompts: The negative visual exemplar enables CountGD++ to differentiate between cells that have the same round shape as the object to count but are of a different appearance; (b) Pseudo-Exemplars: Pseudo-exemplars are automatically detected from text-only input and fed back to the model, improving the accuracy of the final count for objects, like unfamiliar fruits, that are challenging to identify given text alone.A Gradio graphical user interface demo has been created to allow users to test the model. The demo can be run on a remote GPU and accessed via a public link generated at runtime. A short video illustrating the demo workflow is included here. Note that pseudo-exemplars and adaptive cropping are not implemented in the demo. Please see the FSCD-147, PrACo, and ShanghaiTech test scripts to see how the pseudo-exemplars are implemented. Please see the FSCD-147 test script to see how adaptive cropping is implemented.
git clone git@github.com:niki-amini-naieni/CountGDPlusPlus.git
Install GCC. In this project, GCC 11.3 and 11.4 were tested. The following command installs GCC and other development libraries and tools required for compiling software in Ubuntu.
sudo apt update
sudo apt install build-essential
sudo apt install gcc-11 g++-11
NOTE: In order to install detectron2 in step 4, you need to install the CUDA Toolkit. Refer to: https://developer.nvidia.com/cuda-downloads. If multiple CUDA versions are installed, make sure you are using the right one. This repository is quite useful for switching between CUDA versions.
The following commands will create a suitable Anaconda environment for running and training CountGD++. To produce the results in the paper, we used Anaconda version 2024.02-1.
conda create -n countgdplusplus python=3.10
conda activate countgdplusplus
conda install -c conda-forge gxx_linux-64 compilers libstdcxx-ng # ensure to install required compilers
cd CountGDPlusPlus
pip install -r requirements.txt
export CC=/usr/bin/gcc-11 # this ensures that gcc 11 is being used for compilation
cd models/GroundingDINO/ops
python setup.py build install
python test.py # should result in 6 lines of * True
cd ../../../
pip install --no-build-isolation 'git+https://github.com/facebookresearch/detectron2.git'
-
Make the
checkpointsdirectory inside theCountGDPlusPlusrepository.mkdir checkpoints -
Execute the following command.
python download_bert.py -
Download the pretrained CountGD++ model available here (1.25 GB), and place it in the
checkpointsdirectory Or usegdownto download the weights.pip install gdown gdown --id 1j6N22TtKu2NVcKpgfrf-sJHGeLDqs9hs
Run the command below to launch the demo. A video illustrating the demo workflow is provided here.
python app.py
Download FSCD-147 from here, and update datasets_fscd147_val.json, and datasets_fscd147_test.json to point to the image folder you have downloaded.
-
To test the setting with positive text and 1 "positive internal exemplar" (a visual exemplar of the object to count from inside the input image), run the following commands:
python test_dataset.py --dataset_folder data/blood_cell_detection --pretrain_model_path checkpoints/countgd_plusplus.pth --pos_text --num_pos_exemp 1 --out_dir blood_cell_detection_output_pos_text_pos_int_exemppython evaluate_coco_metrics.py --gt data/blood_cell_detection/_annotations.coco.json --pred blood_cell_detection_output_pos_text_pos_int_exemp/coco_predictions.json -
To test the setting with positive text and 1 "positive external exemplar" (a visual exemplar of the object to count from one image applied across the dataset), run the following commands:
python test_dataset.py --dataset_folder data/blood_cell_detection --pretrain_model_path checkpoints/countgd_plusplus.pth --pos_text --num_pos_exemp 1 --use_ext_pos_exemp --out_dir blood_cell_detection_output_pos_text_pos_ext_exemppython evaluate_coco_metrics.py --gt data/blood_cell_detection/_annotations.coco.json --pred blood_cell_detection_output_pos_text_pos_ext_exemp/coco_predictions.json -
To test the setting with positive text, 1 "positive internal exemplar" (a visual exemplar of the object to count from inside the input image), negative text, and 1 "negative internal exemplar" (a visual exemplar of the object to not count from inside the input image), run the following command:
python test_dataset.py --dataset_folder data/blood_cell_detection --pretrain_model_path checkpoints/countgd_plusplus.pth --pos_text --num_pos_exemp 1 --neg_text --num_neg_exemp 1 --out_dir blood_cell_detection_output_pos_text_pos_int_exemp_neg_text_neg_int_exemppython evaluate_coco_metrics.py --gt data/blood_cell_detection/_annotations.coco.json --pred blood_cell_detection_output_pos_text_pos_int_exemp_neg_text_neg_int_exemp/coco_predictions.json -
To test the setting with positive text, 1 "positive external exemplar" (a visual exemplar of the object to count from one image applied across the dataset), negative text and 1 "negative external exemplar" (a visual exemplar of the object to not count from one image applied across the dataset), run the following command:
python test_dataset.py --dataset_folder data/blood_cell_detection --pretrain_model_path checkpoints/countgd_plusplus.pth --pos_text --num_pos_exemp 1 --use_ext_pos_exemp --neg_text --num_neg_exemp 1 --use_ext_neg_exemp --out_dir blood_cell_detection_output_pos_text_pos_ext_exemp_neg_text_neg_ext_exemppython evaluate_coco_metrics.py --gt data/blood_cell_detection/_annotations.coco.json --pred blood_cell_detection_output_pos_text_pos_ext_exemp_neg_text_neg_ext_exemp/coco_predictions.json
-
To test the setting with positive text and 1 "positive internal exemplar" (a visual exemplar of the object to count from inside the input image), run the following commands:
python test_dataset.py --dataset_folder data/omnicount_fruits_test --pretrain_model_path checkpoints/countgd_plusplus.pth --pos_text --num_pos_exemp 1 --out_dir omnicount_fruits_test_output_pos_text_pos_int_exemppython evaluate_coco_metrics.py --gt data/omnicount_fruits_test/_annotations.coco.json --pred omnicount_fruits_test_output_pos_text_pos_int_exemp/coco_predictions.json -
To test the setting with positive text and 1 "positive external exemplar" (a visual exemplar of the object to count from one image applied across the dataset), run the following commands:
python test_dataset.py --dataset_folder data/omnicount_fruits_test --pretrain_model_path checkpoints/countgd_plusplus.pth --pos_text --num_pos_exemp 1 --use_ext_pos_exemp --out_dir omnicount_fruits_test_output_pos_text_pos_ext_exemppython evaluate_coco_metrics.py --gt data/omnicount_fruits_test/_annotations.coco.json --pred omnicount_fruits_test_output_pos_text_pos_ext_exemp/coco_predictions.json -
To test the setting with positive text, 1 "positive internal exemplar" (a visual exemplar of the object to count from inside the input image), negative text, and 1 "negative internal exemplar" (a visual exemplar of the object to not count from inside the input image) for each negative class, run the following command:
python test_dataset.py --dataset_folder data/omnicount_fruits_test --pretrain_model_path checkpoints/countgd_plusplus.pth --pos_text --num_pos_exemp 1 --neg_text --num_neg_exemp 1 --out_dir omnicount_fruits_test_output_pos_text_pos_int_exemp_neg_text_neg_int_exemppython evaluate_coco_metrics.py --gt data/omnicount_fruits_test/_annotations.coco.json --pred omnicount_fruits_test_output_pos_text_pos_int_exemp_neg_text_neg_int_exemp/coco_predictions.json -
To test the setting with positive text, 1 "positive external exemplar" (a visual exemplar of the object to count from one image applied across the dataset), negative text and 1 "negative external exemplar" (a visual exemplar of the object to not count from one image applied across the dataset) for each negative class, run the following command:
python test_dataset.py --dataset_folder data/omnicount_fruits_test --pretrain_model_path checkpoints/countgd_plusplus.pth --pos_text --num_pos_exemp 1 --use_ext_pos_exemp --neg_text --num_neg_exemp 1 --use_ext_neg_exemp --out_dir omnicount_fruits_test_output_pos_text_pos_ext_exemp_neg_text_neg_ext_exemppython evaluate_coco_metrics.py --gt data/omnicount_fruits_test/_annotations.coco.json --pred omnicount_fruits_test_output_pos_text_pos_ext_exemp_neg_text_neg_ext_exemp/coco_predictions.json
- To test CountGD++ on ShanghaiTech Part A Test given both text and pseudo-exemplars (exemplars automatically generated by CountGD++ using text only), run the following command:
python test_shanghai_tech.py --pretrain_model_path checkpoints/countgd_plusplus.pth --image_folder data/ShanghaiTech/part_A/test_data/images/ --gt_folder data/ShanghaiTech/part_A/test_data/ground-truth/ - To test CountGD++ on ShanghaiTech Part B Test given both text and pseudo-exemplars (exemplars automatically generated by CountGD++ using text only), run the following command:
python test_shanghai_tech.py --pretrain_model_path checkpoints/countgd_plusplus.pth --image_folder data/ShanghaiTech/part_B/test_data/images/ --gt_folder data/ShanghaiTech/part_B/test_data/ground-truth/
Please cite our related papers if you build off of our work.
@InProceedings{AminiNaieni26,
title={CountGD++: Generalized Prompting for Open-World Counting},
author={Amini-Naieni, N. and Zisserman, A.},
booktitle = {Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition (CVPR)},
year={2026}
}
@InProceedings{AminiNaieni24,
title = {CountGD: Multi-Modal Open-World Counting},
author = {Amini-Naieni, N. and Han, T. and Zisserman, A.},
booktitle = {Advances in Neural Information Processing Systems (NeurIPS)},
year = {2024},
}
The authors would like to thank Dr Christian Schroeder de Witt (Oxford Witt Lab, OWL) for his helpful feedback and insights on the paper figures and Gia Khanh Nguyen, Yifeng Huang, and Professor Minh Hoai for their help with the PairTally Benchmark. This research is funded by an AWS Studentship, the Reuben Foundation, a Qualcomm Innovation Fellowship (mentors: Dr Farhad Zanjani and Dr Davide Abati), the AIMS CDT program at the University of Oxford, EPSRC Programme Grant VisualAI EP/T028572/1, and a Royal Society Research Professorship RSRP\R\241003.

