AI / Machine Learning
-
February 15, 2024

How to Build a Crowd Counting Model Using CSRNet

Crowd counting refers to the technique used to estimate the number of people in an image or video. It finds applications in various industries, hospitals, crowd gatherings, as well as automated public monitoring like surveillance and traffic control. Unlike object detection, crowd counting focuses on identifying arbitrarily sized targets in different scenarios, including sparse and cluttered scenes simultaneously. If you want more info about crowd counting, you can refer to our extensive and vastly informative survey on the subject.

Today, we will construct a crowd counting model using CSRNet as the model and Python as the programming language. You might be wondering why we chose this particular model among the numerous options available. The answer is simple: CSRNet comes with open-source pre-trained weights, making it an ideal choice. When developing a crowd counting model, one of the major challenges is obtaining suitable datasets, which are often limited and challenging to generate. The most popular datasets for crowd counting include UCSD, Mall, UCF_CC_50, WorldExpo 10, ShanghaiTech, UCF-QNRF, and GCC. Another significant challenge is the computational cost associated with training these models. Training state-of-the-art CNNs for crowd counting requires expensive hardware and considerable time investment. Therefore, having access to pre-trained weights can greatly alleviate these efforts.

Install dependencies

The first thing we have to do is install the required dependencies. To do so, create a file named requirements.txt and copy the following:

Afterwards, open a terminal, go the folder where the requirements.txt file is located and run the following command:

Create the CSRNet using torch

Now that we have installed our dependencies, we can begin constructing our model. To do so, we will create a file named model.py in which we will build a CSRNet neural network with torch and torchvision using the following code:

Load our pre-trained weights

Now we have to load our pre-trained weights. To do so, we will download the following file and move it to the same folder where we have the model.py file.

Generating predictions

The next step involves creating the Python file that will execute the model using the pre-trained weights to predict the crowd size accurately. So, for this we are going to create a file called run.py in the same folder were we have the model.py file and the pretrained weights.

The final folder tree should look like this:

Inside this file we will start by importing the necessary dependencies:

Next, we will add the following code block, which will help us preprocessing the image before passing it to the model:

We then call the model and load the pre-trained weights:

Finally, we add the following snippet to make our predictions. This code will return the original image, the predicted count and the density map:

In conclusion, our full code should like like this:

Final output example: