-
Notifications
You must be signed in to change notification settings - Fork 1
/
infer_determine_thre.py
352 lines (305 loc) · 12 KB
/
infer_determine_thre.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
"""
Assess the checkpoint performance on the validation set to determine the optimal threshold.
Utilize this identified threshold to evaluate on the test set.
"""
from dataset.dataload import func_getdataloader, func_getdataloader_16
from model.choose_net import func_getnetwork
from utils.data import get_coordinate_list
from utils.data import get_coordinates, get_probabilities, get_fullheatmap_from_fold
from creterion.f1 import compute_metrics_once
import torch
import torch.nn as nn
import numpy as np
import os
import cv2
import time
import scipy
import pandas as pd
from dataset.dataprocess import func_normlize
import argparse
import shutil
__author__ = "Yudong Zhang"
def save_args_to_file(args, path):
with open(path, "a+") as file:
for arg, value in vars(args).items():
if isinstance(value, list):
value = ", ".join(map(str, value))
file.write(f"{arg}: {value}\n")
file.write("--------------------------\n")
def parse_args_():
parser = argparse.ArgumentParser(description="Train keypoints network")
# model
parser.add_argument(
"--model_mode",
choices=["deepBlink", "DetNet", "superpoint", "PointDet"],
default="deepBlink",
)
# dataset
parser.add_argument(
"--test_datapath",
type=str,
default="/data/ldap_shared/synology_shared/zyd/data/20220611_detparticle/testdataset/test_VESICLE/SNR4/",
)
parser.add_argument(
"--val_datapath",
type=str,
default="/data/ldap_shared/synology_shared/zyd/data/20220611_detparticle/val_VESICLE/SNR4/",
)
parser.add_argument("--datatype", choices=["8bit", "16bit"], default="8bit")
# optimizer
parser.add_argument("--gpu_list", nargs="+", default=[1])
# If resume
parser.add_argument("--ckpt_path", type=str, required=True)
# Only for DetNet
parser.add_argument("--alpha", type=float, default=0.1)
# Only for PointDet
parser.add_argument("--cfg", type=str, default="./config/inference_demo_coco.yaml")
parser.add_argument(
"opts",
help="Modify config options using the command-line",
default=None,
nargs=argparse.REMAINDER,
)
# Log and save
parser.add_argument("--log_root", type=str, default="./Log/")
parser.add_argument("--exp_name", type=str, default="VESICEL_SNR4_deepBlink")
parser.add_argument("--use_visdom", type=bool, default=False)
parser.add_argument("--port", type=int, default=4006)
args = parser.parse_args()
return args
if __name__ == "__main__":
opt = parse_args_()
# model
model_mode = opt.model_mode
# dataset
test_datapath = opt.test_datapath
val_datapath = opt.val_datapath
datatype = opt.datatype
# optimizer
gpu_list = opt.gpu_list
# if resume
ckp_path = opt.ckpt_path
# log and save
Log_path = opt.log_root
now = int(round(time.time() * 1000))
nowname = time.strftime("%Y%m%d_%H_%M_%S", time.localtime(now / 1000))
expname = nowname + "_" + opt.exp_name + "_eval"
# Makedirs and Save files
if not os.path.exists(Log_path + expname):
os.makedirs(Log_path + expname)
# save this file
thisfilepath = os.path.abspath(__file__)
shutil.copy(thisfilepath, Log_path + expname + "/eval_determinethre_code.py")
# record log
logtxt_path = Log_path + expname + "/log.txt"
logtxt = open(logtxt_path, "a+")
logtxt.write("\n\n")
logtxt.write("===============Eval determine thre===============\n")
logtxt.write("==============={}===============\n".format(expname))
logtxt.close()
save_args_to_file(opt, logtxt_path)
# load data model
if datatype == "16bit":
dataloader_ins_val = func_getdataloader_16(
model_mode,
val_datapath,
batch_size=1,
shuffle=False,
num_workers=16,
training=False,
)
dataloader_ins_test = func_getdataloader_16(
model_mode,
test_datapath,
batch_size=1,
shuffle=False,
num_workers=16,
training=False,
)
else:
dataloader_ins_val = func_getdataloader(
model_mode,
val_datapath,
batch_size=1,
shuffle=False,
num_workers=16,
training=False,
)
dataloader_ins_test = func_getdataloader(
model_mode,
test_datapath,
batch_size=1,
shuffle=False,
num_workers=16,
training=False,
)
model_ins = func_getnetwork(model_mode, opt)
# if use GPU
device = torch.device(
"cuda:{}".format(gpu_list[0]) if torch.cuda.is_available() else "cpu"
)
model_ins.to(device)
if torch.cuda.device_count() > 1 and len(gpu_list) > 1:
model_ins = nn.DataParallel(model_ins, device_ids=gpu_list)
# load checkpoint
c_checkpoint = torch.load(ckp_path, map_location="cuda:{}".format(gpu_list[0]))
model_ins.load_state_dict(c_checkpoint["model_state_dict"])
print("==> Loaded pretrianed model checkpoint '{}'.".format(ckp_path))
thre = None
# start inferance
print("====>>>Choose Threshold")
model_ins.eval()
f1_max = 0
thre_max = 0.1
for thre in range(1, 10):
loss_ = 0
f1_list = []
precis_list = []
recall_list = []
abs_euclideans_list = []
since = time.time()
for data in dataloader_ins_val:
inp = data[0].to(device)
if model_mode == "superpoint":
lab = data[1]
lab_heatmap = get_fullheatmap_from_fold(lab)[0]
lab_coords = get_coordinates(lab_heatmap, thre=0.5)
pred = model_ins(inp)
pred_heatmap = get_fullheatmap_from_fold(pred)[0].detach().cpu().numpy()
pred_coords = get_coordinates(pred_heatmap, thre=thre * 0.1)
elif model_mode == "DetNet":
lab_coords = data[1][0].numpy()[:, ::-1]
# lab_coords = get_coordinates(lab, thre=0.5)
pred = model_ins(inp)[0].permute(1, 2, 0).detach().cpu().numpy()
pred_coords = get_coordinates(pred, thre=thre * 0.1)
elif model_mode == "deepBlink":
if datatype == "16bit":
lab = data[1][0]
lab_coords, _ = get_coordinate_list(
lab, image_size=max(inp.shape), probability=0.5
)
else:
lab_coords = data[1][0].numpy()[:, ::-1]
pred = model_ins(inp)[0].permute(1, 2, 0).detach().cpu().numpy()
pred_coords, scores = get_coordinate_list(
pred, image_size=max(inp.shape), probability=thre * 0.1
)
elif model_mode == "PointDet":
lab_coords = data[1][0].numpy()
pheatmap, poffset, psegment = model_ins(inp)
psegment = psegment[0, 0, :, :].detach().cpu().numpy()
pred_coords = get_coordinates(psegment, thre * 0.1)
if pred_coords.shape[0] == 0 or lab_coords.shape == 0:
f1_, precis_, recall_, abs_euclideans = 0, 0, 0, 1e10
else:
f1_, precis_, recall_, abs_euclideans = compute_metrics_once(
pred=pred_coords, true=lab_coords, mdist=3.0
)
f1_list.append(f1_)
precis_list.append(precis_)
recall_list.append(recall_)
abs_euclideans_list.append(abs_euclideans)
if np.array(f1_list).mean() > f1_max:
f1_max = np.array(f1_list).mean()
thre_max = thre * 0.1
# claculate time
time_elapsed = time.time() - since
# record loss time
message = "threthold:{:.1f} f1:{:.3f} precision{:.3f} recall{:.3f} rmse{:.3f} elapse:{:.0f}m {:.0f}s".format(
thre * 0.1,
np.array(f1_list).mean(),
np.array(precis_list).mean(),
np.array(recall_list).mean(),
np.array(abs_euclideans_list).mean(),
time_elapsed // 60,
time_elapsed % 60,
)
print(message)
logtxt = open(logtxt_path, "a+")
logtxt.write(message + "\n")
logtxt.close()
print("best f1:{:.3f},with threshold:{:.1f}".format(f1_max, thre_max))
print("===>Start prediction")
# make save folder
inf_dir = Log_path + expname + "/prediction_" + str(thre_max)
if not os.path.exists(inf_dir):
os.makedirs(inf_dir)
loss_ = 0
f1_list = []
precis_list = []
recall_list = []
abs_euclideans_list = []
for data in dataloader_ins_test:
inp = data[0].to(device)
name = data[2][0]
inputimage = data[3][0].numpy()
if model_mode == "superpoint":
lab = data[1]
lab_heatmap = get_fullheatmap_from_fold(lab)[0]
lab_coords = get_coordinates(lab_heatmap, thre=0.5)
pred = model_ins(inp)
pred_heatmap = get_fullheatmap_from_fold(pred)[0].detach().cpu().numpy()
pred_coords = get_coordinates(pred_heatmap, thre=thre_max)
elif model_mode == "DetNet":
lab_coords = data[1][0].numpy()[:, ::-1]
# lab_coords = get_coordinates(lab,thre=0.5)
pred = model_ins(inp)[0].permute(1, 2, 0).detach().cpu().numpy()
pred_coords = get_coordinates(pred, thre=thre_max)
elif model_mode == "deepBlink":
if datatype == "16bit":
lab = data[1][0]
lab_coords, _ = get_coordinate_list(
lab, image_size=max(inp.shape), probability=0.5
)
else:
lab_coords = data[1][0].numpy()[:, ::-1]
pred = model_ins(inp)[0].permute(1, 2, 0).detach().cpu().numpy()
pred_coords, scores = get_coordinate_list(
pred, image_size=max(inp.shape), probability=thre_max
)
elif model_mode == "PointDet":
lab_coords = data[1][0].numpy()
pheatmap, poffset, psegment = model_ins(inp)
psegment = psegment[0, 0, :, :].detach().cpu().numpy()
pred_coords = get_coordinates(psegment, thre * 0.1)
f1_, precis_, recall_, abs_euclideans = compute_metrics_once(
pred=pred_coords, true=lab_coords, mdist=3.0
)
f1_list.append(f1_)
precis_list.append(precis_)
recall_list.append(recall_)
abs_euclideans_list.append(abs_euclideans)
pred_coords_pd = pd.DataFrame(pred_coords, columns=["pos_y", "pos_x"])
pred_coords_pd.to_csv(inf_dir + "/" + name + ".csv", index=None)
inputimage = func_normlize(inputimage, mode="maxmin_norm")
inputimage = np.clip(np.round(inputimage * 255), 0, 255).astype(np.uint8)
# inputimage[:,:,0] = 0
# inputimage[:,:,2] = 0
# cv2.imwrite(inf_dir+'/'+name+'.png',inputimage)
if model_mode == "PointDet":
for y, x in pred_coords:
cv2.circle(inputimage, (int(y), int(x)), 5, (0, 255, 255), 1)
for y, x in lab_coords:
cv2.circle(inputimage, (int(y), int(x)), 1, (0, 0, 255), 1)
else:
for x, y in pred_coords:
cv2.circle(inputimage, (int(y), int(x)), 5, (0, 255, 255), 1)
for x, y in lab_coords:
cv2.circle(inputimage, (int(y), int(x)), 1, (0, 0, 255), 1)
# print('save:'+name)
cv2.imwrite(inf_dir + "/" + name + "_f1{:.3f}.png".format(f1_), inputimage)
message = "==>>[TEST]threthold:{:.1f} f1:{:.3f} precision:{:.3f} recall:{:.3f} rmse:{:.3f}".format(
thre_max,
np.array(f1_list).mean(),
np.array(precis_list).mean(),
np.array(recall_list).mean(),
np.array(abs_euclideans_list).mean(),
)
print(message)
logtxt_ = open(logtxt_path, "a+")
logtxt_.write(message)
logtxt_.close()
# print('=====>>>>'+nowname)
# print(test_datapath.split('/')[-3])
# print(test_datapath.split('/')[-2])
# print(test_datapath.split('/')[-1])