-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy patheval_rotate_PR_V8.py
486 lines (403 loc) · 17.5 KB
/
eval_rotate_PR_V8.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
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
# Copyright (c) OpenMMLab. All rights reserved.
from multiprocessing import get_context
import numpy as np
import torch
from mmcv.ops import box_iou_rotated
from mmcv.utils import print_log
from mmdet.core import average_precision
from terminaltables import AsciiTable
import argparse
import os
import sys
from pathlib import Path
import cv2
import torch
import torch.backends.cudnn as cudnn
FILE = Path(__file__).resolve()
ROOT = FILE.parents[0] # YOLOv5 root directory
if str(ROOT) not in sys.path:
sys.path.append(str(ROOT)) # add ROOT to PATH
ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
from models.common import DetectMultiBackend
from utils.datasets import IMG_FORMATS, VID_FORMATS, LoadImages, LoadStreams
from utils.general import (LOGGER, non_max_suppression_obb)
from utils.plots import Annotator, colors, save_one_box
from utils.torch_utils import select_device, time_sync
from utils.rboxs_utils import poly2rbox, rbox2poly
import joblib
import json
def tpfp_default(det_bboxes,
gt_bboxes,
iou_thr=0.3,
area_ranges=None):
"""Check if detected bboxes are true positive or false positive.
Args:
det_bboxes (ndarray): Detected bboxes of this image, of shape (m, 6).
gt_bboxes (ndarray): GT bboxes of this image, of shape (n, 5).
gt_bboxes_ignore (ndarray): Ignored gt bboxes of this image,
of shape (k, 5). Default: None
iou_thr (float): IoU threshold to be considered as matched.
Default: 0.5.
area_ranges (list[tuple] | None): Range of bbox areas to be evaluated,
in the format [(min1, max1), (min2, max2), ...]. Default: None.
Returns:
tuple[np.ndarray]: (tp, fp) whose elements are 0 and 1. The shape of
each array is (num_scales, m).
"""
#一些细长的旋转框的iou可能很微小的扰动就会导致iou急剧下降
iou_thr=0.5
# an indicator of ignored gts
det_bboxes = np.array(det_bboxes)
num_dets = det_bboxes.shape[0]
num_gts = gt_bboxes.shape[0]
if area_ranges is None:
area_ranges = [(None, None)]
num_scales = len(area_ranges)
# tp and fp are of shape (num_scales, num_gts), each row is tp or fp of
# a certain scale
tp = np.zeros((num_scales, num_dets), dtype=np.float32)
fp = np.zeros((num_scales, num_dets), dtype=np.float32)
# if there is no gt bboxes in this image, then all det bboxes
# within area range are false positives
if gt_bboxes.shape[0] == 0:
if area_ranges == [(None, None)]:
fp[...] = 1
else:
raise NotImplementedError
return tp, fp
ious = box_iou_rotated(
torch.from_numpy(det_bboxes).float(),
torch.from_numpy(gt_bboxes).float()).numpy()
# for each det, the max iou with all gts
ious_max = ious.max(axis=1)
# for each det, which gt overlaps most with it
ious_argmax = ious.argmax(axis=1)
# sort all dets in descending order by scores
sort_inds = np.argsort(-det_bboxes[:, -1])
for k, (min_area, max_area) in enumerate(area_ranges):
gt_covered = np.zeros(num_gts, dtype=bool)
for i in sort_inds:
if ious_max[i] >= iou_thr:
matched_gt = ious_argmax[i]
if not gt_covered[matched_gt]:
gt_covered[matched_gt] = True
tp[k, i] = 1
else:
fp[k, i] = 1
# otherwise ignore this detected bbox, tp = 0, fp = 0
elif min_area is None:
fp[k, i] = 1
else:
bbox = det_bboxes[i, :5]
area = bbox[2] * bbox[3]
if area >= min_area and area < max_area:
fp[k, i] = 1
return tp, fp
def get_cls_results(det_results, annotations, class_id):
"""Get det results and gt information of a certain class.
Args:
det_results (list[list]): Same as `eval_map()`.
annotations (list[dict]): Same as `eval_map()`.
class_id (int): ID of a specific class.
Returns:
tuple[list[np.ndarray]]: detected bboxes, gt bboxes, ignored gt bboxes
"""
cls_dets = [img_res[class_id] for img_res in det_results]
cls_gts = []
cls_gts_ignore = []
for ann in annotations:
gt_inds = ann['labels'] == class_id
cls_gts.append(ann['bboxes'][gt_inds, :])
if ann.get('labels_ignore', None) is not None:
ignore_inds = ann['labels_ignore'] == class_id
cls_gts_ignore.append(ann['bboxes_ignore'][ignore_inds, :])
else:
cls_gts_ignore.append(torch.zeros((0, 5), dtype=torch.float64))
return cls_dets, cls_gts, cls_gts_ignore
def eval_rbbox_map(det_results,
annotations,
scale_ranges=None,
iou_thr=0.5,
use_07_metric=True,
dataset=None,
logger=None,
nproc=4):
"""Evaluate mAP of a rotated dataset.
Args:
det_results (list[list]): [[cls1_det, cls2_det, ...], ...].
The outer list indicates images, and the inner list indicates
per-class detected bboxes.
annotations (list[dict]): Ground truth annotations where each item of
the list indicates an image. Keys of annotations are:
- `bboxes`: numpy array of shape (n, 5)
- `labels`: numpy array of shape (n, )
- `bboxes_ignore` (optional): numpy array of shape (k, 5)
- `labels_ignore` (optional): numpy array of shape (k, )
scale_ranges (list[tuple] | None): Range of scales to be evaluated,
in the format [(min1, max1), (min2, max2), ...]. A range of
(32, 64) means the area range between (32**2, 64**2).
Default: None.
iou_thr (float): IoU threshold to be considered as matched.
Default: 0.5.
use_07_metric (bool): Whether to use the voc07 metric.
dataset (list[str] | str | None): Dataset name or dataset classes,
there are minor differences in metrics for different datasets, e.g.
"voc07", "imagenet_det", etc. Default: None.
logger (logging.Logger | str | None): The way to print the mAP
summary. See `mmcv.utils.print_log()` for details. Default: None.
nproc (int): Processes used for computing TP and FP.
Default: 4.
Returns:
tuple: (mAP, [dict, dict, ...])
"""
print('len(det_results)',len(det_results))
print('len(annotations)',len(annotations))
assert len(det_results) == len(annotations)
num_imgs = len(det_results[0])
num_scales = len(scale_ranges) if scale_ranges is not None else 1
num_classes = len(det_results) # positive class num
area_ranges = ([(rg[0]**2, rg[1]**2) for rg in scale_ranges]
if scale_ranges is not None else None)
pool = get_context('spawn').Pool(nproc)
eval_results = []
for i in range(num_classes):
# get gt and det bboxes of this class
# cls_dets, cls_gts, cls_gts_ignore = get_cls_results(
# det_results, annotations, i)
cls_dets=det_results[i]
cls_gts=annotations[i]
# for i in range(len(cls_dets)):
# print('cls_dets',cls_dets[i])
# print('cls_gts',cls_gts[i])
# compute tp and fp for each image with multiple processes
tpfp = pool.starmap(
tpfp_default,
zip(cls_dets, cls_gts,
[iou_thr for _ in range(num_imgs)],
[area_ranges for _ in range(num_imgs)]))
tp, fp = tuple(zip(*tpfp))
# calculate gt number of each scale
# ignored gts or gts beyond the specific scale are not counted
num_gts = np.zeros(num_scales, dtype=int)
for _, bbox in enumerate(cls_gts):
if area_ranges is None:
num_gts[0] += bbox.shape[0]
else:
gt_areas = bbox[:, 2] * bbox[:, 3]
for k, (min_area, max_area) in enumerate(area_ranges):
num_gts[k] += np.sum((gt_areas >= min_area)
& (gt_areas < max_area))
# sort all det bboxes by score, also sort tp and fp
cls_dets = np.vstack(cls_dets)
num_dets = cls_dets.shape[0]
sort_inds = np.argsort(-cls_dets[:, -1])
tp = np.hstack(tp)[:, sort_inds]
fp = np.hstack(fp)[:, sort_inds]
# calculate recall and precision with tp and fp
tp = np.cumsum(tp, axis=1)
fp = np.cumsum(fp, axis=1)
eps = np.finfo(np.float32).eps
recalls = tp / np.maximum(num_gts[:, np.newaxis], eps)
precisions = tp / np.maximum((tp + fp), eps)
# calculate AP
if scale_ranges is None:
recalls = recalls[0, :]
precisions = precisions[0, :]
num_gts = num_gts.item()
mode = 'area' if not use_07_metric else '11points'
ap = average_precision(recalls, precisions, mode)
eval_results.append({
'num_gts': num_gts,
'num_dets': num_dets,
'recall': recalls,
'precision': precisions,
'ap': ap
})
pool.close()
if scale_ranges is not None:
# shape (num_classes, num_scales)
all_ap = np.vstack([cls_result['ap'] for cls_result in eval_results])
all_num_gts = np.vstack(
[cls_result['num_gts'] for cls_result in eval_results])
mean_ap = []
for i in range(num_scales):
if np.any(all_num_gts[:, i] > 0):
mean_ap.append(all_ap[all_num_gts[:, i] > 0, i].mean())
else:
mean_ap.append(0.0)
else:
aps = []
for cls_result in eval_results:
if cls_result['num_gts'] > 0:
aps.append(cls_result['ap'])
mean_ap = np.array(aps).mean().item() if aps else 0.0
print_map_summary(
mean_ap, eval_results, dataset, area_ranges, logger=logger)
return mean_ap, eval_results
def print_map_summary(mean_ap,
results,
dataset=None,
scale_ranges=None,
logger=None):
"""Print mAP and results of each class.
A table will be printed to show the gts/dets/recall/AP of each class and
the mAP.
Args:
mean_ap (float): Calculated from `eval_map()`.
results (list[dict]): Calculated from `eval_map()`.
dataset (list[str] | str | None): Dataset name or dataset classes.
scale_ranges (list[tuple] | None): Range of scales to be evaluated.
logger (logging.Logger | str | None): The way to print the mAP
summary. See `mmcv.utils.print_log()` for details. Default: None.
"""
if logger == 'silent':
return
if isinstance(results[0]['ap'], np.ndarray):
num_scales = len(results[0]['ap'])
else:
num_scales = 1
if scale_ranges is not None:
assert len(scale_ranges) == num_scales
num_classes = len(results)
recalls = np.zeros((num_scales, num_classes), dtype=np.float32)
precision = np.zeros((num_scales, num_classes), dtype=np.float32)
aps = np.zeros((num_scales, num_classes), dtype=np.float32)
num_gts = np.zeros((num_scales, num_classes), dtype=int)
for i, cls_result in enumerate(results):
if cls_result['recall'].size > 0:
recalls[:, i] = np.array(cls_result['recall'], ndmin=2)[:, -1]
if cls_result['precision'].size > 0:
precision[:, i] = np.array(cls_result['precision'], ndmin=2)[:, -1]
aps[:, i] = cls_result['ap']
num_gts[:, i] = cls_result['num_gts']
if dataset is None:
label_names = [str(i) for i in range(num_classes)]
else:
label_names = dataset
if not isinstance(mean_ap, list):
mean_ap = [mean_ap]
header = ['class', 'gts', 'dets', 'recall','precision', 'ap']
for i in range(num_scales):
if scale_ranges is not None:
print_log(f'Scale range {scale_ranges[i]}', logger=logger)
table_data = [header]
for j in range(num_classes):
row_data = [
label_names[j], num_gts[i, j], results[j]['num_dets'],
f'{recalls[i, j]:.3f}', f'{precision[i, j]:.3f}', f'{aps[i, j]:.3f}'
]
table_data.append(row_data)
table_data.append(['mAP', '', '', '', '', f'{mean_ap[i]:.3f}'])
table = AsciiTable(table_data)
table.inner_footing_row_border = True
print_log('\n' + table.table, logger=logger)
def creat_annotions(label_path):
#存到json文件
jdict=[]
annotions=[]
#创建多个类别的list
for i in cls_name_list:
locals()['list_'+str(i)] = list()
for label_name in os.listdir(label_path):
lb_file=label_path + '/' +label_name
with open(lb_file) as f:
labels = [x.split() for x in f.read().strip().splitlines() if len(x)]
l_ = []
for label in labels:
cls_id=cls_name_list.index(label[8])
l_.append(np.concatenate((cls_id, label[:8]), axis=None))
l = np.array(l_, dtype=np.float32)
#存储字典数据到列表用于保存到json文件中
jdict.append(
{
'image_id': label_name.split('.')[0]+'jpg',
'category_id': [int(num) for num in l[:,0].tolist()],
'bbox_score': [poly2rbox(np.array([num]),use_pi=True, use_gaussian=False).tolist()[0] for num in l[:,1:].tolist()]
}
)
nl = len(l)
if nl:
_, i = np.unique(l, axis=0, return_index=True)
if len(i) < nl: # duplicate row check
l = l[i] # remove duplicates
else:
ne = 1 # label empty
l = np.zeros((0, 9), dtype=np.float32)
#创建多个类别的list
for i in cls_name_list:
locals()['list_every_img_'+str(i)] = list()
#创建个空数组
empty_arr=np.empty(shape=(0,5))
for cls_i in range(len(cls_name_list)):
for j in range(nl):
if int(l[j][0])==cls_i:
rboxes = poly2rbox(polys=np.array([l[j, 1:]]),use_pi=True, use_gaussian=False)
locals()['list_every_img_'+str(cls_name_list[cls_i])].append(list(rboxes[0]))
for i in cls_name_list:
if len(locals()['list_every_img_'+str(i)])==0:
locals()['list_'+str(i)].append(empty_arr)
else:
locals()['list_'+str(i)].append(np.array(locals()['list_every_img_'+str(i)]))
for i in cls_name_list:
annotions.append(locals()['list_'+str(i)])
print('jdict',jdict)
with open('json_save/v8_pred.json', 'w') as file:
json.dump(jdict, file)
return annotions
if __name__ == '__main__':
pred_list=[]
device = select_device('0')
weights='/runs/train/exp/weights/best.pt'
img_path='/your_datasets/images/val'
label_path='/your_datasets/labelTxt/val'
cls_name_list=['tading','jyz_r']
#读取gt_labels
annotions=creat_annotions(label_path)
#加载检测模型
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
half='False'
model = DetectMultiBackend(weights, device=device, dnn=False)
model.model.half() if half else model.model.float()
#创建多个类别的list
for i in cls_name_list:
locals()['det_list_'+str(i)] = list()
#存储所有类别的检测结果
det_list=[]
for img_name in os.listdir(img_path):
test_img = img_path + '/' + img_name
print(test_img)
ori_img=cv2.imread(test_img)
font = cv2.FONT_HERSHEY_SIMPLEX
dataset = LoadImages(test_img, img_size=640)
for path, im, im0s, vid_cap, s in dataset:
im = torch.from_numpy(im).to(device)
im = im.half() if half else im.float() # uint8 to fp16/32
im /= 255 # 0 - 255 to 0.0 - 1.0
if len(im.shape) == 3:
im = im[None] # expand for batch dim
# Inference
pred = model(im)
# NMS
pred = non_max_suppression_obb(pred, 0.3, 0.1, multi_label=True, max_det=1000)
pred=pred[0].cpu().numpy()
#创建多个类别的list
for i in cls_name_list:
locals()['det_list_every_img_'+str(i)] = list()
#创建个空数组
empty_arr=np.empty(shape=(0,6))
for cls_i in range(len(cls_name_list)):
#遍历预测结果,将不同类别的输出结果分别保存,如果预测为空,则创建一个空的保持相同维度(0,6)的数组,后面评估vstack需要保持维度一致
for pred_i in range(len(pred)):
if int(pred[pred_i][6])==cls_i:
#由于图像的缩放比例,gt是在原图的大小,det是缩放到640以后的结果
scale=ori_img.shape[1]/640
pred[pred_i,:4]=pred[pred_i,:4]*scale
locals()['det_list_every_img_'+str(cls_name_list[cls_i])].append(list(pred[pred_i,:6]))
for i in cls_name_list:
if len(locals()['det_list_every_img_'+str(i)])==0:
locals()['det_list_'+str(i)].append(empty_arr)
else:
locals()['det_list_'+str(i)].append(np.array(locals()['det_list_every_img_'+str(i)]))
for i in cls_name_list:
det_list.append(locals()['det_list_'+str(i)])
eval_rbbox_map(det_list,annotions)