目录
一、开发环境
二、论文代码+数据集下载
三、导入项目
四、make_dataset.py
五、训练模型
六、测试模型
八、总结
windows 10+Anaconda 3+python 3.7+CUDA 11.1+Pytorch 1.8.0+Pycharm
代码链接.
论文链接.
数据集下载:
链接:https://pan.baidu.com/s/1B0v9eEcx4LnSYqRYEcE-Mg
提取码:06j7
这个项目是jupyter项目,所以在导入Pycharm里面的时候,我们需要将.ipynb文件转换成.py文件。具体操作见博客导入项目.或者直接导入后建立make_dataset.py和val.py文件,将我的代码复制进去就行。
如果是跟着前面博客导入的话,make_dataset.py需要修改:
1、在make_dataset里面import scipy.spatial
2、float() argument must be a string or a number, not ‘zip’
解决:
make_dataset.py文件里面有生成A部分的代码和B部分的代码,代码比较长,但是其实有很多重合部分。下面粘贴出我的代码:
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
import scipy.spatial
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(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
root = './dataset/'
part_A_train = os.path.join(root, 'part_A_final/train_data', 'images')
part_A_test = os.path.join(root, 'part_A_final/test_data', 'images')
#part_B_train = os.path.join(root, 'part_B_final/train_data', 'images')
#part_B_test = os.path.join(root, 'part_B_final/test_data', 'images')
path_sets = [part_A_train, part_A_test]
#path_sets = [part_B_train, part_B_test]
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('images','ground_truth').replace('IMG_', 'GT_IMG_'))
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]) < img.shape[0] and int(gt[i][0]) < img.shape[1]:
k[int(gt[i][1]), int(gt[i][0])] = 1
k = gaussian_filter_density(k)
with h5py.File(img_path.replace('.jpg', '.h5').replace('images', 'ground_truth'), 'w') as hf:
hf['density'] = k
'''plt.imshow(Image.open(img_paths[0]))
gt_file = h5py.File(img_paths[0].replace('.jpg', '.h5').replace('images', 'ground_truth'), 'r')
groundtruth = np.asarray(gt_file['density'])
plt.imshow(groundtruth, cmap=CM.jet)
np.sum(groundtruth)'''
1、先运行make_dataset.py生成A部分的.h5文件(ground_truth里面)
2、将代码修改后运行,生成B部分的.h5文件:
对于SHHA的执行命令:python train.py part_A_train.json part_A_val.json 0 0
对于SHHB的执行命令:python train.py part_B_train.json part_B_val.json 0 0
train.json指代part_A_train.json,part_B_train.json
val.json指代part_A_train_with_val.json,part_A_val.json,part_B_train_with_val.json,part_B_val.json
下面以运行part_A数据集为例来修改代码(修改后,运行B时就不用修改了)
解决1:这是由于python2.7与python3.7版本不兼容的问题,顺便修改下一行的代码。
解决2:我发现是我这里定义少了个y,直接改成如下图所示就行
解决3:将image.py中的文件修改,单斜杠变双斜杠
我还没有跑出来。。。跑完后更新。