-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcocoevals.py
101 lines (82 loc) · 3.41 KB
/
cocoevals.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
from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval
from pictText_utils import convertMinMaxToCenteroid
import numpy as np
import sys
from io import StringIO
class PycocoMetric:
def __init__(self, iou_threshold, confidence_threshold, top_k, num_classes=2, show_summary=False):
self.iou_threshold = iou_threshold
self.confidence_threshold = confidence_threshold
self.top_k = top_k
self.num_classes = num_classes
self.show_summary = show_summary
def __call__(self, y_true, y_pred):
'''
y_true, y_pred must be in decoded format
format: # boxes, quad, rboxes, confs, labels
# 4,8,5,1,1
# boxes format: xmin, ymin, xmax, ymax
'''
print(f"y_true.shape: {y_true.shape}, {y_true[0].shape}")
print(f"y_pred.shape: {y_pred.shape}")
dataset = {
'images': [],
'annotations': [],
'categories': []
}
counter= 0
annotation_id = 1
image_id = 1
for boxes in y_true:
if (len(boxes) == 0): print(image_id)
for box in boxes:
counter+=1
print(box, image_id)
area = abs((box[2] - box[0]) * (box[3] - box[1]))
dataset['annotations'].append({
'id': annotation_id,
'image_id': image_id,
'category_id': int(box[-1]),
'bbox': convertMinMaxToCenteroid(box[:4]).tolist(),
'area': area,
'iscrowd': 0
})
annotation_id+=1
dataset['images'].append({'id': int(image_id)})
image_id+=1
category_ids = [i for i in range(self.num_classes)]
dataset['categories'] = [
{'id': int(category_id)} for category_id in category_ids
]
detections = []
pred_img_id = 1
for boxes in y_pred:
for box in boxes:
x,y,w,h = convertMinMaxToCenteroid(box[:4])
final_box = [pred_img_id, x, y, w, h, box[-2], box[-1]] # format imageID, x1, y1, w, h, score, class
detections.append(final_box)
pred_img_id+=1
detections = np.array(detections)
detections = np.reshape(detections, (-1, 7))
print(f"Number of annotations in dataset: {len(dataset['annotations'])}")
print(f"len of detections: {len(detections)}")
coco_gt = COCO()
coco_gt.dataset = dataset
coco_gt.createIndex()
coco_dt = coco_gt.loadRes(detections)
coco_eval = COCOeval(coco_gt, coco_dt, iouType='bbox')
image_ids = list(set(detections[:, 0]))
coco_eval.params.imgIds = image_ids
coco_eval.evaluate()
coco_eval.accumulate()
old_stdout = sys.stdout
sys.stdout = mystdout = StringIO()
coco_eval.summarize()
sys.stdout = old_stdout
if self.show_summary:
coco_eval.summarize()
names = ['='.join(item.split("=")[:-1]) for item in mystdout.getvalue().split("\n")]
coco_metrics = list(zip(names, coco_eval.stats))
print(coco_metrics)
return coco_metrics