forked from aachenhang/crowdcount-mcnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_mall.py
77 lines (62 loc) · 2.17 KB
/
test_mall.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
import torch
import numpy as np
import sys
sys.path.append('./src/')
from src.crowd_count import CrowdCounter
from src import network
from src.data_loader import ImageDataLoader
from src import utils
import re
import scipy.io
torch.backends.cudnn.enabled = True
torch.backends.cudnn.benchmark = False
vis = False
save_output = True
data_path = './data/original/Mall_origin/test_data/'
gt_csv_path = './data/original/Mall_origin/ground_truth_csv/'
gt_path = './data/original/Mall_origin/ground_truth/'
model_path = './final_models/Mall_MCNN_shtechA_66.h5'
output_dir = './output/'
model_name = os.path.basename(model_path).split('.')[0]
file_results = os.path.join(output_dir,'results_' + model_name + '_.txt')
if not os.path.exists(output_dir):
os.mkdir(output_dir)
output_dir = os.path.join(output_dir, 'density_maps_' + model_name)
if not os.path.exists(output_dir):
os.mkdir(output_dir)
net = CrowdCounter()
trained_model = os.path.join(model_path)
network.load_net(trained_model, net)
net.cuda()
net.eval()
mae = 0.0
mse = 0.0
#load test data
data_loader = ImageDataLoader(data_path, gt_csv_path, shuffle=False, gt_downsample=True, pre_load=True)
#load test data gt
gt_files = os.listdir(gt_path)
gt_files.sort()
for i, blob in enumerate(data_loader):
im_data = blob['data']
gt_data = blob['gt_density']
density_map = net(im_data, gt_data)
density_map = density_map.data.cpu().numpy()
data = scipy.io.loadmat(gt_path+gt_files[i])
gt_count = data['image_info'][0,0][0,0][-1][0,0]
# gt_count = np.sum(gt_data)
et_count = np.sum(density_map)
print(i, gt_count, et_count)
mae += abs(gt_count-et_count)
mse += ((gt_count-et_count)*(gt_count-et_count))
if vis:
utils.display_results(im_data, gt_data, density_map)
if save_output:
utils.save_density_map(density_map, output_dir, 'output_' + blob['fname'].split('.')[0] + '.png')
# input("Press the Enter")
mae = mae/data_loader.get_num_samples()
mse = np.sqrt(mse/data_loader.get_num_samples())
print('\nMAE: %0.2f, MSE: %0.2f' % (mae,mse))
f = open(file_results, 'w')
f.write('MAE: %0.2f, MSE: %0.2f' % (mae,mse))
f.close()