# chapter5-learning_CSRNet **Repository Path**: alfa__cv__ai/chapter5-learning_CSRNet ## Basic Information - **Project Name**: chapter5-learning_CSRNet - **Description**: code-visualization-pytorch - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2021-03-29 - **Last Updated**: 2021-03-29 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # chapter5-learning_CSRNet What **CSRNet** is doing: counting the number of people in the picture ------ I encountered some problems when building the environment according to the author's github(https://github.com/leeyeehoo/CSRNet-pytorch). I debugged it and Baidu took a long time to solve it. The purpose of writing this chapter is to help everyone learn better and take less detours. In this article I will lead everyone to debug the code and visualize it. ![1553123470473](img/1553123470473.png) ------ ## step1. install For the specific installation process, you can refer to the author's github. Here I simply show the command line of my operation. ``` conda create -n CSRNet python=3.6 source activate CSRNet unzip CSRNet-pytorch-master.zip pip install -i https://pypi.tuna.tsinghua.edu.cn/simple torch torchvision pip install decorator cloudpickle>=0.2.1 dask[array]>=1.0.0 matplotlib>=2.0.0 networkx>=1.8 scipy>=0.17.0 bleach python-dateutil>=2.1 decorator unzip ShanghaiTech_Crowd_Counting_Dataset.zip jupyter nbconvert --to script make_dataset.ipynb #Convert .ipynb file to .py file ``` ## step2. make_dataset.py I just run the command to convert the **make_dataset.ipynb** file to a **make_dataset.py** file.Now you need to modify the contents of the **make_dataset.py** file. Find the location where **root** is, add **def main()** in the above line ![1553080510253](img/1553080510253.png) Add these two lines at the end of the **make_dataset.py**, adjust the format of the code ![1553080672186](img/1553080672186.png) There is an error in the author's source code, you need to change the code Replace **pts = np.array(zip(np.nonzero(gt)[1], np.nonzero(gt)[0]))** with **pts = np.array(list(zip(np.nonzero(gt)[1], np.nonzero(gt)[0])))** ![1553081148694](img/1553081148694.png) Then run the **make_dataset.py** file ![1553081269333](img/1553081269333.png) ------ **The above is just a general summary, then we will run and visualize the line-by-line code.** I will use this image as an example. ![1553083826276](img/1553083826276.png) ``` # coding: utf-8 import h5py import scipy.io as io import PIL.Image as Image import numpy as np import os import glob from matplotlib import pyplot as plt from scipy.ndimage.filters import gaussian_filter import scipy import json from matplotlib import cm as CM from image import * from model import CSRNet import torch img_path='D:\\paper\\CSRNet\\CSRNet\\dataset\\Shanghai\\part_A_final\\train_data\\images\\IMG_21.jpg' mat='D:\\paper\\CSRNet\\CSRNet\\dataset\\Shanghai\\part_A_final\\train_data\\ground_truth\\GT_IMG_21.mat' mat = io.loadmat(mat) img= plt.imread(img_path) k = np.zeros((img.shape[0],img.shape[1])) ``` The following is the information of **k** ![1553084203041](img/1553084203041.png) ``` gt = mat["image_info"][0,0][0,0][0] ``` ![1553084581750](img/1553084581750.png) ``` for i in range(0,len(gt)): if int(gt[i][1]) 1: sigma = (distances[i][1]+distances[i][2]+distances[i][3])*0.1 else: sigma = np.average(np.array(gt.shape))/2./2. #case: 1 point density += scipy.ndimage.filters.gaussian_filter(pt2d, sigma, mode='constant') print('done.') ``` ![1553086270822](img/1553086270822.png) ``` k = density with h5py.File(img_path.replace('.jpg','.h5').replace('images','ground_truth'), 'w') as hf: hf['density'] = k ``` ![1553086450949](img/1553086450949.png) So far, we have generated true values for the image. At this point I will sort the above code as follows ``` # coding: utf-8 import h5py import scipy.io as io import PIL.Image as Image import numpy as np import os import glob from matplotlib import pyplot as plt from scipy.ndimage.filters import gaussian_filter import scipy import json from matplotlib import cm as CM from image import * from model import CSRNet import torch def gaussian_filter_density(gt): print(gt.shape) density = np.zeros(gt.shape, dtype=np.float32) gt_count = np.count_nonzero(gt) if gt_count == 0: return density # pts = np.array(zip(np.nonzero(gt)[1], np.nonzero(gt)[0])) pts = np.array(list(zip(np.nonzero(gt)[1], np.nonzero(gt)[0]))) leafsize = 2048 # build kdtree tree = scipy.spatial.KDTree(pts.copy(), leafsize=leafsize) # query kdtree distances, locations = tree.query(pts, k=4) print('generate density...') for i, pt in enumerate(pts): pt2d = np.zeros(gt.shape, dtype=np.float32) pt2d[pt[1],pt[0]] = 1. if gt_count > 1: sigma = (distances[i][1]+distances[i][2]+distances[i][3])*0.1 else: sigma = np.average(np.array(gt.shape))/2./2. #case: 1 point density += scipy.ndimage.filters.gaussian_filter(pt2d, sigma, mode='constant') print('done.') return density img_path='D:\\paper\\CSRNet\\CSRNet\\dataset\\Shanghai\\part_A_final\\train_data\\images\\IMG_21.jpg' mat='D:\\paper\\CSRNet\\CSRNet\\dataset\\Shanghai\\part_A_final\\train_data\\ground_truth\\GT_IMG_21.mat' img_paths = [] img_paths.append(img_path) for img_path in img_paths: print(img_path) mat = io.loadmat(mat) img= plt.imread(img_path) k = np.zeros((img.shape[0],img.shape[1])) gt = mat["image_info"][0,0][0,0][0] for i in range(0,len(gt)): if int(gt[i][1])