File eval_context_aware_network.py changed (mode: 100644) (index 760e6bd..6c8d057) |
... |
... |
from torchvision import transforms |
10 |
10 |
from models.context_aware_network import CANNet |
from models.context_aware_network import CANNet |
11 |
11 |
from data_util import ShanghaiTechDataPath |
from data_util import ShanghaiTechDataPath |
12 |
12 |
from hard_code_variable import HardCodeVariable |
from hard_code_variable import HardCodeVariable |
|
13 |
|
from visualize_util import save_img, save_density_map |
13 |
14 |
|
|
14 |
15 |
_description=""" |
_description=""" |
15 |
16 |
This file run predict |
This file run predict |
|
... |
... |
Data path = /home/tt/project/ShanghaiTechCAN/part_B/test_data/images |
17 |
18 |
model path = /home/tt/project/MODEL/Context-aware/part_B_pre.pth.tar |
model path = /home/tt/project/MODEL/Context-aware/part_B_pre.pth.tar |
18 |
19 |
""" |
""" |
19 |
20 |
|
|
|
21 |
|
# if true, render every density map and its image |
|
22 |
|
IS_VISUAL = True |
|
23 |
|
saved_folder = "visualize/eval_context_aware_network_part_b" |
|
24 |
|
|
20 |
25 |
transform=transforms.Compose([ |
transform=transforms.Compose([ |
21 |
26 |
transforms.ToTensor(),transforms.Normalize(mean=[0.485, 0.456, 0.406], |
transforms.ToTensor(),transforms.Normalize(mean=[0.485, 0.456, 0.406], |
22 |
27 |
std=[0.229, 0.224, 0.225]), |
std=[0.229, 0.224, 0.225]), |
23 |
28 |
]) |
]) |
24 |
29 |
|
|
|
30 |
|
transform_no_normalize=transforms.Compose([ |
|
31 |
|
transforms.ToTensor() |
|
32 |
|
]) |
|
33 |
|
|
25 |
34 |
# the folder contains all the test images |
# the folder contains all the test images |
26 |
35 |
hard_code = HardCodeVariable() |
hard_code = HardCodeVariable() |
27 |
36 |
shanghaitech_data = ShanghaiTechDataPath(root=hard_code.SHANGHAITECH_PATH) |
shanghaitech_data = ShanghaiTechDataPath(root=hard_code.SHANGHAITECH_PATH) |
|
... |
... |
img_paths=[] |
33 |
42 |
|
|
34 |
43 |
for img_path in glob.glob(os.path.join(img_folder, '*.jpg')): |
for img_path in glob.glob(os.path.join(img_folder, '*.jpg')): |
35 |
44 |
img_paths.append(img_path) |
img_paths.append(img_path) |
36 |
|
# img_paths = img_paths[:10] |
|
|
45 |
|
img_paths = img_paths[:10] |
37 |
46 |
|
|
38 |
47 |
|
|
39 |
48 |
model = CANNet() |
model = CANNet() |
|
... |
... |
model.eval() |
49 |
58 |
pred= [] |
pred= [] |
50 |
59 |
gt = [] |
gt = [] |
51 |
60 |
|
|
|
61 |
|
if IS_VISUAL: |
|
62 |
|
os.makedirs(saved_folder, exist_ok=True) |
|
63 |
|
|
52 |
64 |
for i in range(len(img_paths)): |
for i in range(len(img_paths)): |
|
65 |
|
img_original = transform_no_normalize(Image.open(img_paths[i]).convert('RGB')).unsqueeze(0) |
53 |
66 |
img = transform(Image.open(img_paths[i]).convert('RGB')).cuda() |
img = transform(Image.open(img_paths[i]).convert('RGB')).cuda() |
54 |
67 |
img = img.unsqueeze(0) |
img = img.unsqueeze(0) |
55 |
68 |
h,w = img.shape[2:4] |
h,w = img.shape[2:4] |
56 |
69 |
h_d = int(h/2) |
h_d = int(h/2) |
57 |
70 |
w_d = int(w/2) |
w_d = int(w/2) |
58 |
71 |
img_1 = Variable(img[:,:,:h_d,:w_d].cuda()) |
img_1 = Variable(img[:,:,:h_d,:w_d].cuda()) |
|
72 |
|
img_original_1 = img_original[:,:,:h_d,:w_d] |
|
73 |
|
|
59 |
74 |
img_2 = Variable(img[:,:,:h_d,w_d:].cuda()) |
img_2 = Variable(img[:,:,:h_d,w_d:].cuda()) |
|
75 |
|
img_original_2 = img_original[:,:,:h_d,w_d:] |
|
76 |
|
|
60 |
77 |
img_3 = Variable(img[:,:,h_d:,:w_d].cuda()) |
img_3 = Variable(img[:,:,h_d:,:w_d].cuda()) |
|
78 |
|
img_original_3 = img_original[:,:,h_d:,:w_d] |
|
79 |
|
|
61 |
80 |
img_4 = Variable(img[:,:,h_d:,w_d:].cuda()) |
img_4 = Variable(img[:,:,h_d:,w_d:].cuda()) |
|
81 |
|
img_original_4 = img_original[:,:,h_d:,w_d:] |
|
82 |
|
|
62 |
83 |
density_1 = model(img_1).data.cpu().numpy() |
density_1 = model(img_1).data.cpu().numpy() |
63 |
84 |
density_2 = model(img_2).data.cpu().numpy() |
density_2 = model(img_2).data.cpu().numpy() |
64 |
85 |
density_3 = model(img_3).data.cpu().numpy() |
density_3 = model(img_3).data.cpu().numpy() |
|
... |
... |
for i in range(len(img_paths)): |
71 |
92 |
pred.append(pred_sum) |
pred.append(pred_sum) |
72 |
93 |
gt.append(np.sum(groundtruth)) |
gt.append(np.sum(groundtruth)) |
73 |
94 |
print("done ", i, "pred ",pred_sum, " gt ", np.sum(groundtruth)) |
print("done ", i, "pred ",pred_sum, " gt ", np.sum(groundtruth)) |
|
95 |
|
## print out visual |
|
96 |
|
name_prefix = os.path.join(saved_folder, "sample_"+str(i)) |
|
97 |
|
save_img(img_original_1, name_prefix+"_img1.png") |
|
98 |
|
save_img(img_original_2, name_prefix + "_img2.png") |
|
99 |
|
save_img(img_original_3, name_prefix + "_img3.png") |
|
100 |
|
save_img(img_original_4, name_prefix + "_img4.png") |
|
101 |
|
|
|
102 |
|
save_density_map(density_1.squeeze(), name_prefix + "_pred1.png") |
|
103 |
|
save_density_map(density_2.squeeze(), name_prefix + "_pred2.png") |
|
104 |
|
save_density_map(density_3.squeeze(), name_prefix + "_pred3.png") |
|
105 |
|
save_density_map(density_4.squeeze(), name_prefix + "_pred4.png") |
|
106 |
|
## |
74 |
107 |
|
|
75 |
108 |
print(len(pred)) |
print(len(pred)) |
76 |
109 |
print(len(gt)) |
print(len(gt)) |