LWCC: A LightWeight Crowd Counting library for Python that includes several pretrained state-of-the-art models.

Overview

LWCC: A LightWeight Crowd Counting library for Python

LWCC is a lightweight crowd counting framework for Python. It wraps four state-of-the-art models all based on convolutional neural networks: CSRNet, Bayesian crowd counting, DM-Count, and SFANet. The library is based on PyTorch.

Installation

The easiest way to install library LWCC and its prerequisites is to use the package manager pip.

pip install lwcc

Usage

You can import the library and use its functionalities by:

from lwcc import LWCC

Count estimation

Most straightforward way to use the library:

img = "path/to/image"
count = LWCC.get_count(img)

This uses CSRNet pretrained on SHA (default). You can choose a different model pretrained on different data set using:

count = LWCC.get_count(img, model_name = "DM-Count", model_weights = "SHB")

The result is a float with predicted count.

Large images

Note: By default all images are resized such that the longest side is less than 1000px, preserving the aspect ratio. Otherwise models might perform worse for large images with sparse crowds (counting patterns on shirts, dresses). If you are estimating dense crowds, we recommend you to set the resize_img to False. The call should look like this:

count = LWCC.get_count(img, model_name = "DM-Count", model_weights = "SHB", resize_img = True)

Multiple images

Library allows prediction of count for multiple images with a single call of get_count. You can simply pass a list of image paths:

img1 = "path/to/image1"
img2 = "path/to/image2"
count = LWCC.get_count([img1, img2])

Result is then a dictionary of pairs image_name : image_count: result

Density map

You can also request a density map by setting flag return_density = True. The result is then a tuple (count, density_map), where density_map is a 2d array with predicted densities. The array is smaller than the input image and its size depends on the model.

import matplotlib.pyplot as plt

count, density = LWCC.get_count(img, return_density = True)

plt.imshow(density)
plt.show()

result_density

This also works for multiple images (list of image paths as input). Result is then a tuple of two dictionaries, where the first dictionary is the same as above (pairs of image_name : image_count) and the second dictionary contains pairs of image_name : density_map.

Loading the model

You can also directly access the PyTorch models by loading them first with the load_model method.

model = LWCC.load_model(model_name = "DM-Count", model_weights = "SHA")

The loaded model is a PyTorch model and you can access its weights as with any other PyTorch model.

You can use it for inference as:

 count = LWCC.get_count(img, model = model)

Models

LWCC currently offers 4 models (CSRNet, Bayesian crowd counting, DM-Count, SFANet) pretrained on Shanghai A, Shanghai B, and UCF-QNRF datasets. The following table shows the model name and MAE / MSE result of the available pretrained models on the test sets.

Model name SHA SHB QNRF
CSRNet 75.44 / 113.55 11.27 / 19.32 Not available
Bay 66.92 / 112.07 8.27 / 13.56 90.43 / 161.41
DM-Count 61.39 / 98.56 7.68 / 12.66 88.97 / 154.11
SFANet Not available 7.05 / 12.18 Not available

Valid options for model_name are written in the first column and thus include: CSRNet, Bay, DM-Count, and SFANet. Valid options for model_weights are written in the first row and thus include: SHA, SHB, and QNRF.

Note: Not all model_weights are supported with all model_names. See the above table for possible combinations.

How does it work?

The goal of crowd counting methods is to determine the number of people present in a particular area. There exist many approaches (detection, regression, density-based approaches), however, since 2015 many convolutional neural network (CNN) based approaches have been proposed. The basic idea behind CNN based approaches is that they normally try to predict the density map from the input image and infer the count from it. These models differ in the use of different backbones, loss functions, additional maps, etc. If you are interested in a particular algorithm, you are welcome to read the paper belonging to the specific model.

FAQ - Frequently asked questions

Can I see some more examples of LWCC in action?

Yes, you can find some examples in Examples.ipynb!

How accurate are the models?

You can see the mean absolute error (MAE) and mean squared error (MSE) of the pretrained models on test sets in section models. We recommend models pretrained on SHA or QNRF for dense crowds, and SHB for sparse crowds.

Is GPU support available?

No, GPU support is currently not supported yet, but is planned for the future version.

Can I load custom weights?

Full support of loading custom pretrained weights is not supported, but is planned in the future version.

Can I train the models myself?

The library does not support training, only inference.

Why are my results bad?

This might depend on the model you use, image size, density or type of the crowd, or the weights that you use. For example, models might often make mistakes for images with a group portrait, as they are trained on images containing crowds on streets, concerts, etc. Using SHAweights on relatively sparse crowds might also give very wrong results. On the other hand, SHB might perform better as the weights were trained on Shanghai B data set, which containts images with relatively sparse crowds. Using high quality images with sparse crowds might also yield bad results, as the algorithms might mistake some textures of clothings for a crowd.

As a rule of thumb, you should use SHB if you are planning on estimating the number of people in images with sparse crowds, and SHA or QNRF for images with dense crowds. Keep in mind that current algorithms predict the density, and there still might be some mistakes. You are welcome to try out different combinations of models and weights and see which one works the best for your problem.

Support

If you like the library please show us your support by ⭐️ starring the project!

If you wish to include your own crowd counting model, please contact us ([email protected] or [email protected]).

Stargazers

Stargazers repo roster for @tersekmatija/lwcc

Citation

This library is a result of a research of CNN Crowd Counting models by Matija Teršek and Maša Kljun. Although the paper has not been published yet, please provide the link to this GitHub repository if you use LWCC in your research.

License

This library is licensed under MIT license (see LICENSE). Licenses of the models wrapped in the library will be inherited, depending on the model you use ( CSRNet, Bayesian crowd counting, DM-Count, and SFANet).

You might also like...
This repository contains several image-to-image translation models, whcih were tested for RGB to NIR image generation. The models are Pix2Pix, Pix2PixHD, CycleGAN and PointWise.

RGB2NIR_Experimental This repository contains several image-to-image translation models, whcih were tested for RGB to NIR image generation. The models

Deepparse is a state-of-the-art library for parsing multinational street addresses using deep learning
Deepparse is a state-of-the-art library for parsing multinational street addresses using deep learning

Here is deepparse. Deepparse is a state-of-the-art library for parsing multinational street addresses using deep learning. Use deepparse to Use the pr

🔪 Elimination based Lightweight Neural Net with Pretrained Weights

ELimNet ELimNet: Eliminating Layers in a Neural Network Pretrained with Large Dataset for Downstream Task Removed top layers from pretrained Efficient

PyTorch implementation and pretrained models for XCiT models. See XCiT: Cross-Covariance Image Transformer
State of the Art Neural Networks for Deep Learning

pyradox This python library helps you with implementing various state of the art neural networks in a totally customizable fashion using Tensorflow 2

Code for paper "A Critical Assessment of State-of-the-Art in Entity Alignment" (https://arxiv.org/abs/2010.16314)

A Critical Assessment of State-of-the-Art in Entity Alignment This repository contains the source code for the paper A Critical Assessment of State-of

State of the art Semantic Sentence Embeddings

Contrastive Tension State of the art Semantic Sentence Embeddings Published Paper · Huggingface Models · Report Bug Overview This is the official code

tsai is an open-source deep learning package built on top of Pytorch & fastai focused on state-of-the-art techniques for time series classification, regression and forecasting.
tsai is an open-source deep learning package built on top of Pytorch & fastai focused on state-of-the-art techniques for time series classification, regression and forecasting.

Time series Timeseries Deep Learning Pytorch fastai - State-of-the-art Deep Learning with Time Series and Sequences in Pytorch / fastai

Deep Text Search is an AI-powered multilingual text search and recommendation engine with state-of-the-art transformer-based multilingual text embedding (50+ languages).
Deep Text Search is an AI-powered multilingual text search and recommendation engine with state-of-the-art transformer-based multilingual text embedding (50+ languages).

Deep Text Search - AI Based Text Search & Recommendation System Deep Text Search is an AI-powered multilingual text search and recommendation engine w

Comments
  • PermissionError during creating the folders for downloaded weights data

    PermissionError during creating the folders for downloaded weights data

    Hi,

    when I first tried out the package, I got always following exception

    Traceback (most recent call last):
      File "/usr/lib/python3.8/pathlib.py", line 1288, in mkdir
        self._accessor.mkdir(self, mode)
    FileNotFoundError: [Errno 2] No such file or directory: '/.lwcc/weights'
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "./crowd_counting.py", line 53, in <module>
        results = count(args.img_path)
      File "./crowd_counting.py", line 38, in count
        count, density = LWCC.get_count(
      File "/home/jmkrieger/.local/lib/python3.8/site-packages/lwcc/LWCC.py", line 65, in get_count
        model = load_model(model_name, model_weights)
      File "/home/jmkrieger/.local/lib/python3.8/site-packages/lwcc/LWCC.py", line 31, in load_model
        model = model.make_model(model_weights)
      File "/home/jmkrieger/.local/lib/python3.8/site-packages/lwcc/models/CSRNet.py", line 13, in make_model
        output = weights_check("CSRNet", model_weights)
      File "/home/jmkrieger/.local/lib/python3.8/site-packages/lwcc/util/functions.py", line 17, in weights_check
        Path("/.lwcc/weights").mkdir(parents=True, exist_ok=True)
      File "/usr/lib/python3.8/pathlib.py", line 1292, in mkdir
        self.parent.mkdir(parents=True, exist_ok=True)
      File "/usr/lib/python3.8/pathlib.py", line 1288, in mkdir
        self._accessor.mkdir(self, mode)
    PermissionError: [Errno 13] Permission denied: '/.lwcc'
    

    I traced the error and found that the function weights_check in lwcc/util/functions.py tried to create the folder designated for the downloaded weights in the root folder, and not the home directory.

    I could easily fixed it for me by changing the function to

    def weights_check(model_name, model_weights):
        home = str(Path.home())
    
        # create dir if does not exists
        Path(os.path.join(home, ".lwcc/weights")).mkdir(parents=True, exist_ok=True)
    
        # download weights if not available
        file_name = "{}_{}.pth".format(model_name, model_weights)
        url = build_url(file_name)
        output = os.path.join(home, ".lwcc/weights/", file_name)
        # [...]
    

    Thank you for creating this package - it's quite mindblowing what today's neural networks can achieve!

    opened by jakobkrieger 1
Owner
Matija Teršek
Data Science Master's student
Matija Teršek
Weighing Counts: Sequential Crowd Counting by Reinforcement Learning

LibraNet This repository includes the official implementation of LibraNet for crowd counting, presented in our paper: Weighing Counts: Sequential Crow

Hao Lu 18 Nov 5, 2022
Variational Attention: Propagating Domain-Specific Knowledge for Multi-Domain Learning in Crowd Counting (ICCV, 2021)

DKPNet ICCV 2021 Variational Attention: Propagating Domain-Specific Knowledge for Multi-Domain Learning in Crowd Counting Baseline of DKPNet is availa

null 19 Oct 14, 2022
DCSL - Generalizable Crowd Counting via Diverse Context Style Learning

DCSL Generalizable Crowd Counting via Diverse Context Style Learning Requirement

null 3 Jun 13, 2022
A state of the art of new lightweight YOLO model implemented by TensorFlow 2.

CSL-YOLO: A New Lightweight Object Detection System for Edge Computing This project provides a SOTA level lightweight YOLO called "Cross-Stage Lightwe

Miles Zhang 54 Dec 21, 2022
Quickly comparing your image classification models with the state-of-the-art models (such as DenseNet, ResNet, ...)

Image Classification Project Killer in PyTorch This repo is designed for those who want to start their experiments two days before the deadline and ki

null 349 Dec 8, 2022
QuickAI is a Python library that makes it extremely easy to experiment with state-of-the-art Machine Learning models.

QuickAI is a Python library that makes it extremely easy to experiment with state-of-the-art Machine Learning models.

null 152 Jan 2, 2023
TorchMultimodal is a PyTorch library for training state-of-the-art multimodal multi-task models at scale.

TorchMultimodal (Alpha Release) Introduction TorchMultimodal is a PyTorch library for training state-of-the-art multimodal multi-task models at scale.

Meta Research 663 Jan 6, 2023
LaneDet is an open source lane detection toolbox based on PyTorch that aims to pull together a wide variety of state-of-the-art lane detection models

LaneDet is an open source lane detection toolbox based on PyTorch that aims to pull together a wide variety of state-of-the-art lane detection models. Developers can reproduce these SOTA methods and build their own methods.

TuZheng 405 Jan 4, 2023
PaddleViT: State-of-the-art Visual Transformer and MLP Models for PaddlePaddle 2.0+

PaddlePaddle Vision Transformers State-of-the-art Visual Transformer and MLP Models for PaddlePaddle ?? PaddlePaddle Visual Transformers (PaddleViT or

null 1k Dec 28, 2022
PySlowFast: video understanding codebase from FAIR for reproducing state-of-the-art video models.

PySlowFast PySlowFast is an open source video understanding codebase from FAIR that provides state-of-the-art video classification models with efficie

Meta Research 5.3k Jan 3, 2023