-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.py
More file actions
510 lines (423 loc) · 20 KB
/
test.py
File metadata and controls
510 lines (423 loc) · 20 KB
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
import argparse
from torch.autograd import Variable
from torch.utils.data import DataLoader
from tqdm import tqdm
import threading
from dataset import *
import time
from collections import OrderedDict
from model.SDSNet import SDSNet as SDSNet
# from loss import *
import model.Config as config
import numpy as np
import torch
from skimage import measure
def cal_tp_pos_fp_neg(output, target, nclass, score_thresh):
predict = (output > score_thresh).float()
if len(target.shape) == 3:
print('????') # Add a dimension to make the size of target consistent with output
target = target.unsqueeze(dim=0)
# target = np.expand_dims(target.float(), axis=1)
target.to('cuda', torch.float)
elif len(target.shape) == 4:
target = target.float()
else:
raise ValueError("Unknown target dimension")
# Now the part of predict higher than the threshold is a matrix of all 1s, target is GT
intersection = predict * ((predict == target).float())
tp = intersection.sum() # True positive: correct prediction is positive
fp = (predict * ((predict != target).float())).sum() # False positive: incorrect prediction is positive, number of false alarm pixels
tn = ((1 - predict) * ((predict == target).float())).sum() # True negative: incorrect prediction is negative
fn = (((predict != target).float()) * (1 - predict)).sum() # False negative: correct prediction is negative
pos = tp + fn # Number of positives in the label
neg = fp + tn # Number of negatives in the label
class_pos = tp + fp # Number of detected positives
return tp, pos, fp, neg, class_pos
class SamplewiseSigmoidMetric(object):
"""Computes pixAcc and mIoU metric scores
"""
def __init__(self, nclass, score_thresh=0.5):
self.nclass = nclass
self.score_thresh = score_thresh
self.lock = threading.Lock()
self.reset()
def update(self, preds, labels):
"""Updates the internal evaluation result.
Parameters
----------
labels : 'NDArray' or list of `NDArray`
The labels of the data.
preds : 'NDArray' or list of `NDArray`
Predicted values.
"""
def evaluate_worker(self, label, pred):
inter_arr, union_arr = batch_intersection_union_n(
pred, label, self.nclass, self.score_thresh)
with self.lock:
self.total_inter = np.append(self.total_inter, inter_arr)
self.total_union = np.append(self.total_union, union_arr)
if isinstance(preds, torch.Tensor):
evaluate_worker(self, labels, preds)
elif isinstance(preds, (list, tuple)):
threads = [threading.Thread(target=evaluate_worker,
args=(self, label, pred),
)
for (label, pred) in zip(labels, preds)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
def get(self):
"""Gets the current evaluation result.
Returns
-------
metrics : tuple of float
pixAcc and mIoU
"""
IoU = 1.0 * self.total_inter / (np.spacing(1) + self.total_union)
nIoU = IoU.mean()
return nIoU
def reset(self):
"""Resets the internal evaluation result to initial state."""
self.total_inter = np.array([])
self.total_union = np.array([])
self.total_correct = np.array([])
self.total_label = np.array([])
def batch_intersection_union_n(output, target, nclass, score_thresh):
"""nIoU"""
mini = 1
maxi = 1 # nclass
nbins = 1 # nclass
outputnp = output.detach().cpu().numpy()
# outputsig = F.sigmoid(output).detach().cpu().numpy()
# outputsig = nd.sigmoid(output).asnumpy()
predict = (outputnp > 0.5).astype('int64')
# predict = predict.detach().cpu().numpy()
# predict = (output.asnumpy() > 0).astype('int64') # P
if len(target.shape) == 3:
target = np.expand_dims(target, axis=1).astype('int64') # T
elif len(target.shape) == 4:
target = target.cpu().numpy().astype('int64') # T
else:
raise ValueError("Unknown target dimension")
intersection = predict * (predict == target) # TP intersection
num_sample = intersection.shape[0]
area_inter_arr = np.zeros(num_sample)
area_pred_arr = np.zeros(num_sample)
area_lab_arr = np.zeros(num_sample)
area_union_arr = np.zeros(num_sample)
for b in range(num_sample):
# areas of intersection and union
area_inter, _ = np.histogram(intersection[b], bins=nbins, range=(mini, maxi))
area_inter_arr[b] = area_inter
area_pred, _ = np.histogram(predict[b], bins=nbins, range=(mini, maxi))
area_pred_arr[b] = area_pred
area_lab, _ = np.histogram(target[b], bins=nbins, range=(mini, maxi))
area_lab_arr[b] = area_lab
area_union = area_pred + area_lab - area_inter
area_union_arr[b] = area_union
assert (area_inter <= area_union).all(), \
"Intersection area should be smaller than Union area"
return area_inter_arr, area_union_arr
class ROCMetric05():
"""Computes pixAcc and mIoU metric scores
"""
def __init__(self, nclass, bins):
# The meaning of bin is actually to determine how many discrete values the threshold on the ROC curve takes
# nclass: how many classes, infrared small target detection has only one class
super(ROCMetric05, self).__init__()
self.nclass = nclass
self.bins = bins
self.tp_arr = np.zeros(self.bins + 1)
self.pos_arr = np.zeros(self.bins + 1)
self.fp_arr = np.zeros(self.bins + 1)
self.neg_arr = np.zeros(self.bins + 1)
self.class_pos = np.zeros(self.bins + 1)
# self.reset()
# The results and labels of the network input, calculate the metrics between them
def update(self, preds, labels):
for iBin in range(self.bins + 1):
# score_thresh = (iBin + 0.0) / self.bins
score_thresh = (0.0 + iBin) / self.bins
# print(iBin, "-th, score_thresh: ", score_thresh)
i_tp, i_pos, i_fp, i_neg, i_class_pos = cal_tp_pos_fp_neg(preds, labels, self.nclass, score_thresh)
self.tp_arr[iBin] += i_tp
self.pos_arr[iBin] += i_pos
self.fp_arr[iBin] += i_fp # Number of false alarm pixels
self.neg_arr[iBin] += i_neg
self.class_pos[iBin] += i_class_pos
def get(self):
tp_rates = self.tp_arr / (self.pos_arr + 0.001) # tp_rates = recall = TP/(TP+FN)
fp_rates = self.fp_arr / (self.neg_arr + 0.001) # fp_rates = FP/(FP+TN)
FP = self.fp_arr / (self.neg_arr + self.pos_arr)
recall = self.tp_arr / (self.pos_arr + 0.001) # recall = TP/(TP+FN)
precision = self.tp_arr / (self.class_pos + 0.001) # precision = TP/(TP+FP)
f1_score = (2.0 * recall[5] * precision[5]) / (recall[5] + precision[5] + 0.00001)
return tp_rates, fp_rates, recall, precision, FP, f1_score
def reset(self):
self.tp_arr = np.zeros([11])
self.pos_arr = np.zeros([11])
self.fp_arr = np.zeros([11])
self.neg_arr = np.zeros([11])
self.class_pos = np.zeros([11])
class mIoU():
def __init__(self):
super(mIoU, self).__init__()
self.reset()
def update(self, preds, labels):
correct, labeled = batch_pix_accuracy(preds, labels) # labeled: number of target pixels in GT, correct: number of correctly predicted pixels
inter, union = batch_intersection_union(preds, labels)
self.total_correct += correct
self.total_label += labeled
self.total_inter += inter
self.total_union += union
def get(self):
pixAcc = 1.0 * self.total_correct / (np.spacing(1) + self.total_label)
IoU = 1.0 * self.total_inter / (np.spacing(1) + self.total_union)
mIoU = IoU.mean()
return float(pixAcc), mIoU
def reset(self):
self.total_inter = 0
self.total_union = 0
self.total_correct = 0
self.total_label = 0
class PDFA():
def __init__(self, ):
super(PDFA, self).__init__()
self.image_area_total = []
self.image_area_match = []
self.dismatch_pixel = 0
self.all_pixel = 0
self.PD = 0
self.target = 0
def update(self, preds, labels, size):
predits = np.array((preds).cpu()).astype('int64')
labelss = np.array((labels).cpu()).astype('int64')
image = measure.label(predits, connectivity=2)
coord_image = measure.regionprops(image)
label = measure.label(labelss, connectivity=2)
coord_label = measure.regionprops(label)
self.target += len(coord_label)
self.image_area_total = []
self.image_area_match = []
self.distance_match = []
self.dismatch = []
for K in range(len(coord_image)):
area_image = np.array(coord_image[K].area)
self.image_area_total.append(area_image)
for i in range(len(coord_label)):
centroid_label = np.array(list(coord_label[i].centroid))
for m in range(len(coord_image)):
centroid_image = np.array(list(coord_image[m].centroid))
distance = np.linalg.norm(centroid_image - centroid_label)
area_image = np.array(coord_image[m].area)
if distance < 3:
self.distance_match.append(distance)
self.image_area_match.append(area_image)
del coord_image[m]
break
self.dismatch = [x for x in self.image_area_total if x not in self.image_area_match]
self.dismatch_pixel += np.sum(self.dismatch)
self.all_pixel += size[0] * size[1]
self.PD += len(self.distance_match)
def get(self):
Final_FA = self.dismatch_pixel / self.all_pixel
Final_PD = self.PD / self.target
return Final_PD, float(Final_FA.cpu().detach().numpy())
def reset(self):
self.FA = np.zeros([self.bins + 1])
self.PD = np.zeros([self.bins + 1])
def batch_pix_accuracy(output, target):
if len(target.shape) == 3:
target = np.expand_dims(target.float(), axis=1)
elif len(target.shape) == 4:
target = target.float()
else:
raise ValueError("Unknown target dimension")
assert output.shape == target.shape, "Predict and Label Shape Don't Match"
predict = (output > 0).float()
pixel_labeled = (target > 0).float().sum()
pixel_correct = (((predict == target).float()) * ((target > 0)).float()).sum()
assert pixel_correct <= pixel_labeled, "Correct area should be smaller than Labeled"
return pixel_correct, pixel_labeled
def batch_intersection_union(output, target):
mini = 1
maxi = 1
nbins = 1
predict = (output > 0).float()
if len(target.shape) == 3:
target = np.expand_dims(target.float(), axis=1)
elif len(target.shape) == 4:
target = target.float()
else:
raise ValueError("Unknown target dimension")
intersection = predict * ((predict == target).float())
area_inter, _ = np.histogram(intersection.cpu(), bins=nbins, range=(mini, maxi))
area_pred, _ = np.histogram(predict.cpu(), bins=nbins, range=(mini, maxi))
area_lab, _ = np.histogram(target.cpu(), bins=nbins, range=(mini, maxi))
area_union = area_pred + area_lab - area_inter
assert (area_inter <= area_union).all(), \
"Error: Intersection area should be smaller than Union area"
return area_inter, area_union
class PD_FA():
def __init__(self, ):
super(PD_FA, self).__init__()
self.image_area_total = []
self.image_area_match = []
self.dismatch_pixel = 0
self.all_pixel = 0
self.PD = 0
self.target = 0
def update(self, preds, labels, size):
predits = np.array((preds).cpu()).astype('int64')
labelss = np.array((labels).cpu()).astype('int64')
image = measure.label(predits, connectivity=2)
coord_image = measure.regionprops(image)
label = measure.label(labelss, connectivity=2)
coord_label = measure.regionprops(label)
self.target += len(coord_label) # Total number of targets, directly use the number of connected components in GT
self.image_area_total = [] # List of predicted regions in the image
self.image_area_match = []
self.distance_match = []
self.dismatch = []
for K in range(len(coord_image)):
area_image = np.array(coord_image[K].area)
self.image_area_total.append(area_image)
for i in range(len(coord_label)): # Determine connected components between image and label based on centroids
centroid_label = np.array(list(coord_label[i].centroid))
for m in range(len(coord_image)):
centroid_image = np.array(list(coord_image[m].centroid))
distance = np.linalg.norm(centroid_image - centroid_label)
area_image = np.array(coord_image[m].area)
if distance < 3:
self.distance_match.append(distance)
self.image_area_match.append(area_image)
del coord_image[m] # Remove one after a match
break
self.dismatch = [x for x in self.image_area_total if x not in self.image_area_match] # In image but not in label
self.dismatch_pixel += np.sum(self.dismatch) # Fa: number of false alarm pixels
# print(self.dismatch_pixel)
self.all_pixel += size[0] * size[1]
self.PD += len(self.distance_match) # If the distance between centroids is less than 3, it's a Pd. So Pd is the number of matched targets.
def get(self):
Final_FA = self.dismatch_pixel / self.all_pixel
Final_PD = self.PD / self.target
return Final_PD, float(Final_FA.cpu().detach().numpy())
def reset(self):
self.FA = np.zeros([self.bins + 1])
self.PD = np.zeros([self.bins + 1])
os.environ['KMP_DUPLICATE_LIB_OK'] = 'TRUE'
parser = argparse.ArgumentParser(description="PyTorch BasicIRSTD test")
parser.add_argument('--ROC_thr', type=int, default=10, help='num')
parser.add_argument("--model_names", default=['SDSNet'], type=list,
help="model_name: 'ACM', 'Ours01', 'DNANet', 'ISNet', 'ACMNet', 'Ours01', 'ISTDU-Net', 'U-Net', 'RISTDnet'")
parser.add_argument("--pth_dirs", default=['/NUDT-SIRST/SDSNet_628_best.pth.tar'], type=list)
parser.add_argument("--dataset_dir", default=r'/home/boss/syh/SDSNet-main/datasets', type=str, help="train_dataset_dir")
parser.add_argument("--dataset_names", default=['NUDT-SIRST'], type=list,
help="dataset_name: 'NUAA-SIRST', 'NUDT-SIRST', 'IRSTD-1K', 'SIRST3', 'NUDT-SIRST-Sea'")
parser.add_argument("--img_norm_cfg", default=None, type=dict,
help="specific a img_norm_cfg, default=None (using img_norm_cfg values of each dataset)")
parser.add_argument("--save_img", default=False, type=bool, help="save image of or not")
parser.add_argument("--save_img_dir", type=str, default=r'/home/boss/syh/SDSNet-main/Result',
help="path of saved image")
parser.add_argument("--save_log", type=str, default=r'/home/boss/syh/SDSNet-main/log', help="path of saved .pth")
parser.add_argument("--threshold", type=float, default=0.5)
global opt
opt = parser.parse_args()
def test():
test_set = TestSetLoader(opt.dataset_dir, opt.train_dataset_name, opt.test_dataset_name, opt.img_norm_cfg)
test_loader = DataLoader(dataset=test_set, num_workers=1, batch_size=1, shuffle=False)
# *************************Fixed threshold**********************
# Calculate mIOU, completely OK
IOU = mIoU()
# Calculate nIOU, completely OK
nIoU_metric = SamplewiseSigmoidMetric(nclass=1, score_thresh=0)
# Calculate PD_FA, completely OK
eval_05 = PD_FA()
ROC_05 = ROCMetric05(nclass=1, bins=10)
config_vit = config.get_config()
# CPU
net = SDSNet(config_vit, mode='test', deepsuper=True)
state_dict = torch.load(opt.pth_dir, map_location='cpu')
# # CUDA
# net = SDSNet(config_vit, mode='test', deepsuper=True).cuda()
# state_dict = torch.load(opt.pth_dir)
new_state_dict = OrderedDict()
#
for k, v in state_dict['state_dict'].items():
name = k[6:] # remove `module.`, which means taking characters from the 7th to the end, effectively removing 'module.'
new_state_dict[name] = v # The value corresponding to the key in the new dictionary is the one-to-one value.
net.load_state_dict(new_state_dict)
net.eval()
tbar = tqdm(test_loader)
with torch.no_grad():
for idx_iter, (img, gt_mask, size, img_dir) in enumerate(tbar):
# img = Variable(img)
# CPU
pred = net.forward(img)
pred = pred[:, :, :size[0], :size[1]]
gt_mask = gt_mask[:, :, :size[0], :size[1]]
# # CUDA:
# pred = net.forward(img).cuda()
# pred = pred[:, :, :size[0], :size[1]].cuda()
# gt_mask = gt_mask[:, :, :size[0], :size[1]].cuda()
# Fix threshold ##########################################################
# IOU
IOU.update((pred > 0.5), gt_mask) # pixel
# nIOU
nIoU_metric.update(pred, gt_mask) # pixel
eval_05.update((pred[0, 0, :, :] > opt.threshold).cpu(), gt_mask[0, 0, :, :], size) # target
ROC_05.update(pred, gt_mask)
# save img
if opt.save_img == True:
img_save = transforms.ToPILImage()((pred[0, 0, :, :]).cpu())
if not os.path.exists(opt.save_img_dir + opt.test_dataset_name + '/' + opt.model_name):
os.makedirs(opt.save_img_dir + opt.test_dataset_name + '/' + opt.model_name)
img_save.save(opt.save_img_dir + opt.test_dataset_name + '/' + opt.model_name + '/' + img_dir[0] + '.png')
# 0.5
# IOU OK Good!
pixAcc, mIOU = IOU.get()
# # nIOU OK Good!
nIoU = nIoU_metric.get()
# # Pd Fa
results2 = eval_05.get()
#
# # FP
ture_positive_rate, false_positive_rate, recall, precision, FP, F1_score = ROC_05.get()
print('pixAcc: %.4f| mIoU: %.4f | nIoU: %.4f | Pd: %.4f| Fa: %.4f |F1: %.4f'
% (pixAcc * 100, mIOU * 100, nIoU * 100, results2[0] * 100, results2[1] * 1e+6, F1_score * 100))
if __name__ == '__main__':
opt.f = open(opt.save_log + 'test_' + (time.ctime()).replace(' ', '_').replace(':', '_') + '.txt', 'w')
if opt.pth_dirs == None:
for i in range(len(opt.model_names)):
opt.model_name = opt.model_names[i]
print(opt.model_name)
opt.f.write(opt.model_name + '_400.pth.tar' + '\n')
for dataset_name in opt.dataset_names:
opt.dataset_name = dataset_name
opt.train_dataset_name = opt.dataset_name
opt.test_dataset_name = opt.dataset_name
print(dataset_name)
opt.f.write(opt.dataset_name + '\n')
opt.pth_dir = opt.save_log + opt.dataset_name + '/' + opt.model_name + '_400.pth.tar'
test()
print('\n')
opt.f.write('\n')
opt.f.close()
else:
for model_name in opt.model_names:
for dataset_name in opt.dataset_names:
for pth_dir in opt.pth_dirs:
# if dataset_name in pth_dir and model_name in pth_dir:
opt.test_dataset_name = dataset_name
opt.model_name = model_name
opt.train_dataset_name = pth_dir.split('/')[0]
print(pth_dir)
opt.f.write(pth_dir)
print(opt.test_dataset_name)
opt.f.write(opt.test_dataset_name + '\n')
opt.pth_dir = opt.save_log + pth_dir
test()
print('\n')
opt.f.write('\n')
opt.f.close()