-
Notifications
You must be signed in to change notification settings - Fork 39
/
test.py
180 lines (128 loc) · 5.26 KB
/
test.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from matplotlib import pyplot as plt
import os
import random
import torch
from torch.autograd import Variable
import torchvision.transforms as standard_transforms
import misc.transforms as own_transforms
import pandas as pd
from models.CC import CrowdCounter
from config import cfg
from misc.utils import *
import scipy.io as sio
from PIL import Image, ImageOps
torch.cuda.set_device(0)
torch.backends.cudnn.benchmark = True
exp_name = './vis'
if not os.path.exists(exp_name):
os.mkdir(exp_name)
mean_std = cfg.DATA.MEAN_STD
img_transform = standard_transforms.Compose([
standard_transforms.ToTensor(),
standard_transforms.Normalize(*mean_std)
])
restore = standard_transforms.Compose([
own_transforms.DeNormalize(*mean_std),
standard_transforms.ToPILImage()
])
pil_to_tensor = standard_transforms.ToTensor()
dataRoot = '/media/D/DataSet/CC/UCF-qnrf/768x1024_1221/test'
model_path = './pre/XXX.pth'
def main():
# file_list = [filename for filename in os.listdir(dataRoot+'/img/') if os.path.isfile(os.path.join(dataRoot+'/img/',filename))]
file_list = [filename for root,dirs,filename in os.walk(dataRoot+'/img/')]
# pdb.set_trace()
ht_img = cfg.TRAIN.INPUT_SIZE[0]
wd_img = cfg.TRAIN.INPUT_SIZE[1]
test(file_list[0], model_path)
def test(file_list, model_path):
net = CrowdCounter()
net.load_state_dict(torch.load(model_path))
# net = tr_net.CNN()
# net.load_state_dict(torch.load(model_path))
net.cuda()
net.eval()
maes = []
mses = []
for filename in file_list:
print filename
imgname = dataRoot + '/img/' + filename
filename_no_ext = filename.split('.')[0]
denname = dataRoot + '/den/' + filename_no_ext + '.csv'
den = pd.read_csv(denname, sep=',',header=None).values
den = den.astype(np.float32, copy=False)
img = Image.open(imgname)
if img.mode == 'L':
img = img.convert('RGB')
# prepare
wd_1, ht_1 = img.size
# pdb.set_trace()
if wd_1 < cfg.DATA.STD_SIZE[1]:
dif = cfg.DATA.STD_SIZE[1] - wd_1
img = ImageOps.expand(img, border=(0,0,dif,0), fill=0)
pad = np.zeros([ht_1,dif])
den = np.array(den)
den = np.hstack((den,pad))
if ht_1 < cfg.DATA.STD_SIZE[0]:
dif = cfg.DATA.STD_SIZE[0] - ht_1
img = ImageOps.expand(img, border=(0,0,0,dif), fill=0)
pad = np.zeros([dif,wd_1])
den = np.array(den)
den = np.vstack((den,pad))
img = img_transform(img)
gt = np.sum(den)
img = Variable(img[None,:,:,:],volatile=True).cuda()
#forward
pred_map = net.test_forward(img)
pred_map = pred_map.cpu().data.numpy()[0,0,:,:]
pred = np.sum(pred_map)/100.0
maes.append(abs(pred-gt))
mses.append((pred-gt)*(pred-gt))
# vis
pred_map = pred_map/np.max(pred_map+1e-20)
pred_map = pred_map[0:ht_1,0:wd_1]
den = den/np.max(den+1e-20)
den = den[0:ht_1,0:wd_1]
den_frame = plt.gca()
plt.imshow(den, 'jet')
den_frame.axes.get_yaxis().set_visible(False)
den_frame.axes.get_xaxis().set_visible(False)
den_frame.spines['top'].set_visible(False)
den_frame.spines['bottom'].set_visible(False)
den_frame.spines['left'].set_visible(False)
den_frame.spines['right'].set_visible(False)
plt.savefig(exp_name+'/'+filename_no_ext+'_gt_'+str(int(gt))+'.png',\
bbox_inches='tight',pad_inches=0,dpi=150)
plt.close()
# sio.savemat(exp_name+'/'+filename_no_ext+'_gt_'+str(int(gt))+'.mat',{'data':den})
pred_frame = plt.gca()
plt.imshow(pred_map, 'jet')
pred_frame.axes.get_yaxis().set_visible(False)
pred_frame.axes.get_xaxis().set_visible(False)
pred_frame.spines['top'].set_visible(False)
pred_frame.spines['bottom'].set_visible(False)
pred_frame.spines['left'].set_visible(False)
pred_frame.spines['right'].set_visible(False)
plt.savefig(exp_name+'/'+filename_no_ext+'_pred_'+str(float(pred))+'.png',\
bbox_inches='tight',pad_inches=0,dpi=150)
plt.close()
# sio.savemat(exp_name+'/'+filename_no_ext+'_pred_'+str(float(pred))+'.mat',{'data':pred_map})
diff = den-pred_map
diff_frame = plt.gca()
plt.imshow(diff, 'jet')
plt.colorbar()
diff_frame.axes.get_yaxis().set_visible(False)
diff_frame.axes.get_xaxis().set_visible(False)
diff_frame.spines['top'].set_visible(False)
diff_frame.spines['bottom'].set_visible(False)
diff_frame.spines['left'].set_visible(False)
diff_frame.spines['right'].set_visible(False)
plt.savefig(exp_name+'/'+filename_no_ext+'_diff.png',\
bbox_inches='tight',pad_inches=0,dpi=150)
plt.close()
# sio.savemat(exp_name+'/'+filename_no_ext+'_diff.mat',{'data':diff})
print '[file %s]: [pred %.2f], [gt %.2f]' % (filename, pred, gt)
print np.average(np.array(maes))
print np.sqrt(np.average(np.array(mses)))
if __name__ == '__main__':
main()