Gaussian_filter fails when executing CSRNet code using SciPy

Asked 1 years ago, Updated 1 years ago, 273 views

Create density map annotations from Person Head Position Annotations in CSRNet for Crowd counting models.

Traceback (most recent call last):
  File "/path/to/directory/make_dataset.py", line 67, in<module>
    k=gaussian_filter_density(k)
  File"/path/to/directory/make_dataset.py", line 38, ingausian_filter_density
    density+=gaussian_filter(pt2d, sigma, mode='constant')
  File"/path/to/directory/env/lib/python 3.10/site-packages/scipy/ndimage/_filters.py", line368, ingausian_filter
    Gaussian_filter1d(input, sigma, axis, order, output,
  File"/path/to/directory/env/lib/python 3.10/site-packages/scipy/ndimage/_filters.py", line 269, ingausian_filter1d
    lw=int(truncate*sd+0.5)
OverflowError:cannot convert float infinity to integer

The first few images work, but the above error occurs in the middle and the execution stops.

make_dataset.py

import h5py
import scipy.io asio
import PIL.Image as Image
import numpy as np
importos
import glob
from matplotlib import pyplot as plt
from scope.ndimage 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.zero(gt.shape, dtype=np.float32)
    gt_count = np.count_nonzero(gt)
    ifgt_count == 0:
        return density

    pts=np.array(list(zip(np.nonzero(gt)[1], np.nonzero(gt)[0])))
    leavesize = 2048
    # build kdtree
    tree=scipy.spatial.KDTree(pts.copy(), leavesize=leafsize)
    # query kdtree
    distances, locations=tree.query(pts,k=4)

    print('generate density...')
    for i, pt in enumerate (pts):
        pt2d=np.zero(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+=gaussian_filter(pt2d, sigma, mode='constant')
    print('done.')
    return density


# set the root to Beijing BRT
root='/path/to/Beijing-BRT-dataset'

# now generate the ground truth
train_path=os.path.join(root, 'train', 'frame')
test_path=os.path.join(root, 'test', 'frame')
path_sets = [train_path, test_path]

img_paths=[]
for path in path_sets:
    for img_path in glob.glob(os.path.join(path, '*.jpg')):
        img_paths.append(img_path)

for img_path in img_paths:
    print(img_path)
    mat = io.loadmat(img_path.replace('.jpg', '.mat') .replace('frame', 'ground_truth')
    img=plt.imread(img_path)
    k=np.zeros((img.shape[0], img.shape[1]))    
    gt = mat ['loc']

    for i in range (len(gt)) :
        if int(gt[i][1])<img.shape[0] and int(gt[i][0])<img.shape[1]:
            k[int(gt[i][1]), int(gt[i][0]]] = 1
            print(k)
    k=gaussian_filter_density(k)
    with h5py.File(img_path.replace('.jpg', '.h5').replace('frame', 'ground_truth'), 'w') ashf:
            hf ['density'] = k

The code is CSRNet developer code for Qiita article and See this site for your reference.Corrected "do" and the original code was written for ShanghaiTech dataset, so the code was changed further for the Being BRT dataset.

  • WSL2, Ubuntu 22.04
  • on Windows 11
  • Python 3.10 with venv
  • SciPy 1.10.0

In addition to the Python code above, I downloaded the Being BRT dataset (such as the image and annotation .mat file) from GitHub, and ran make_dataset.py.

If there are any deficiencies in the question, I will correct them, so please let me know.Thank you for your cooperation.

python machine-learning scipy

2023-02-24 10:24

1 Answers

Line 67 of code

density+=gaussian_filter(pt2d, sigma, mode='constant')

The error was found to be caused by the infinite value of sigma passed in .

You can avoid errors by limiting the sigma value to the appropriate upper limit.
Example:

 sigma=(distances[i][1]+distances[i][2]+distances[i][3])*0.1

res=min(gt.shape)#The resolution here is the number of pixels in vertical or horizontal, which is small

sigma=min((distances[i][1]+distances[i][2]+distances[i][3])*0.1,res*10)#To prevent an overflow error

Change to


2023-02-24 19:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.