File eval_context_aware_network.py added (mode: 100644) (index 0000000..760e6bd) |
|
1 |
|
import h5py |
|
2 |
|
import PIL.Image as Image |
|
3 |
|
import numpy as np |
|
4 |
|
import os |
|
5 |
|
import glob |
|
6 |
|
import torch |
|
7 |
|
from torch.autograd import Variable |
|
8 |
|
from sklearn.metrics import mean_squared_error,mean_absolute_error |
|
9 |
|
from torchvision import transforms |
|
10 |
|
from models.context_aware_network import CANNet |
|
11 |
|
from data_util import ShanghaiTechDataPath |
|
12 |
|
from hard_code_variable import HardCodeVariable |
|
13 |
|
|
|
14 |
|
_description=""" |
|
15 |
|
This file run predict |
|
16 |
|
Data path = /home/tt/project/ShanghaiTechCAN/part_B/test_data/images |
|
17 |
|
model path = /home/tt/project/MODEL/Context-aware/part_B_pre.pth.tar |
|
18 |
|
""" |
|
19 |
|
|
|
20 |
|
transform=transforms.Compose([ |
|
21 |
|
transforms.ToTensor(),transforms.Normalize(mean=[0.485, 0.456, 0.406], |
|
22 |
|
std=[0.229, 0.224, 0.225]), |
|
23 |
|
]) |
|
24 |
|
|
|
25 |
|
# the folder contains all the test images |
|
26 |
|
hard_code = HardCodeVariable() |
|
27 |
|
shanghaitech_data = ShanghaiTechDataPath(root=hard_code.SHANGHAITECH_PATH) |
|
28 |
|
# img_folder='/home/tt/project/ShanghaiTechCAN/part_B/test_data/images' |
|
29 |
|
img_folder = shanghaitech_data.get_b().get_test().get_images() |
|
30 |
|
print("image folder = " + str(img_folder)) |
|
31 |
|
|
|
32 |
|
img_paths=[] |
|
33 |
|
|
|
34 |
|
for img_path in glob.glob(os.path.join(img_folder, '*.jpg')): |
|
35 |
|
img_paths.append(img_path) |
|
36 |
|
# img_paths = img_paths[:10] |
|
37 |
|
|
|
38 |
|
|
|
39 |
|
model = CANNet() |
|
40 |
|
|
|
41 |
|
model = model.cuda() |
|
42 |
|
|
|
43 |
|
checkpoint = torch.load('/home/tt/project/MODEL/Context-aware/part_B_pre.pth.tar') |
|
44 |
|
|
|
45 |
|
model.load_state_dict(checkpoint['state_dict']) |
|
46 |
|
|
|
47 |
|
model.eval() |
|
48 |
|
|
|
49 |
|
pred= [] |
|
50 |
|
gt = [] |
|
51 |
|
|
|
52 |
|
for i in range(len(img_paths)): |
|
53 |
|
img = transform(Image.open(img_paths[i]).convert('RGB')).cuda() |
|
54 |
|
img = img.unsqueeze(0) |
|
55 |
|
h,w = img.shape[2:4] |
|
56 |
|
h_d = int(h/2) |
|
57 |
|
w_d = int(w/2) |
|
58 |
|
img_1 = Variable(img[:,:,:h_d,:w_d].cuda()) |
|
59 |
|
img_2 = Variable(img[:,:,:h_d,w_d:].cuda()) |
|
60 |
|
img_3 = Variable(img[:,:,h_d:,:w_d].cuda()) |
|
61 |
|
img_4 = Variable(img[:,:,h_d:,w_d:].cuda()) |
|
62 |
|
density_1 = model(img_1).data.cpu().numpy() |
|
63 |
|
density_2 = model(img_2).data.cpu().numpy() |
|
64 |
|
density_3 = model(img_3).data.cpu().numpy() |
|
65 |
|
density_4 = model(img_4).data.cpu().numpy() |
|
66 |
|
|
|
67 |
|
pure_name = os.path.splitext(os.path.basename(img_paths[i]))[0] |
|
68 |
|
gt_file = h5py.File(img_paths[i].replace('.jpg','.h5').replace('images','ground-truth-h5'),'r') |
|
69 |
|
groundtruth = np.asarray(gt_file['density']) |
|
70 |
|
pred_sum = density_1.sum()+density_2.sum()+density_3.sum()+density_4.sum() |
|
71 |
|
pred.append(pred_sum) |
|
72 |
|
gt.append(np.sum(groundtruth)) |
|
73 |
|
print("done ", i, "pred ",pred_sum, " gt ", np.sum(groundtruth)) |
|
74 |
|
|
|
75 |
|
print(len(pred)) |
|
76 |
|
print(len(gt)) |
|
77 |
|
mae = mean_absolute_error(pred,gt) |
|
78 |
|
rmse = np.sqrt(mean_squared_error(pred,gt)) |
|
79 |
|
|
|
80 |
|
print('MAE: ',mae) |
|
81 |
|
print('RMSE: ',rmse) |