Package: lwcc
LWCC: A LightWeight Crowd Counting library for PythonLWCC 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, andSFANet. The library is based on PyTorch.InstallationThe easiest way to install library LWCC and its prerequisites is to use the package managerpip.pipinstalllwccUsageYou can import the library and use its functionalities by:fromlwccimportLWCCCount estimationMost 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 imagesNote: 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 theresize_imgtoFalse. The call should look like this:count=LWCC.get_count(img,model_name="DM-Count",model_weights="SHB",resize_img=True)Multiple imagesLibrary allows prediction of count for multiple images with a single call ofget_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 pairsimage_name : image_count:Density mapYou can also request a density map by setting flagreturn_density = True. The result is then a tuple(count, density_map), wheredensity_mapis a 2d array with predicted densities. The array is smaller than the input image and its size depends on the model.importmatplotlib.pyplotaspltcount,density=LWCC.get_count(img,return_density=True)plt.imshow(density)plt.show()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 ofimage_name : image_count) and the second dictionary contains pairs ofimage_name : density_map.Loading the modelYou can also directly access the PyTorch models by loading them first with theload_modelmethod.model=LWCC.load_model(model_name="DM-Count",model_weights="SHA")The loadedmodelis 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)ModelsLWCC currently offers 4 models (CSRNet, Bayesian crowd counting, DM-Count, SFANet) pretrained onShanghai A,Shanghai B, andUCF-QNRFdatasets. The following table shows the model name and MAE / MSE result of the available pretrained models on the test sets.Model nameSHASHBQNRFCSRNet75.44 / 113.5511.27 / 19.32Not availableBay66.92 / 112.078.27 / 13.5690.43 / 161.41DM-Count61.39 / 98.567.68 / 12.6688.97 / 154.11SFANetNot available7.05 / 12.18Not availableValid options formodel_nameare written in the first column and thus include:CSRNet,Bay,DM-Count, andSFANet.
Valid options formodel_weightsare written in the first row and thus include:SHA,SHB,  andQNRF.Note: Not allmodel_weightsare supported with allmodel_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 questionsCan I see some more examples of LWCC in action?Yes, you can find some examples inExamples.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 sectionmodels. 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. UsingSHAweights on relatively sparse crowds might also give very wrong results. On the other hand,SHBmight 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 useSHBif you are planning on estimating the number of people in images with sparse crowds, andSHAorQNRFfor 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.SupportIf 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 (matijatersek@protonmail.comormasika.kljun@gmail.com).StargazersCitationThis 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.LicenseThis library is licensed under MIT license (seeLICENSE). Licenses of the models wrapped in the library will be inherited, depending on the model you use (CSRNet,Bayesian crowd counting,DM-Count, andSFANet).