forked from zorzi-s/PolyWorldPretrainedNetwork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoco_IoU_cIoU.py
87 lines (67 loc) · 2.68 KB
/
coco_IoU_cIoU.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
from pycocotools.coco import COCO
from pycocotools import mask as cocomask
import numpy as np
import json
from tqdm import tqdm
def calc_IoU(a, b):
i = np.logical_and(a, b)
u = np.logical_or(a, b)
I = np.sum(i)
U = np.sum(u)
iou = I/(U + 1e-9)
is_void = U == 0
if is_void:
return 1.0
else:
return iou
def compute_IoU_cIoU(input_json, gti_annotations):
# Ground truth annotations
coco_gti = COCO(gti_annotations)
# Predictions annotations
submission_file = json.loads(open(input_json).read())
coco = COCO(gti_annotations)
coco = coco.loadRes(submission_file)
image_ids = coco.getImgIds(catIds=coco.getCatIds())
bar = tqdm(image_ids)
list_iou = []
list_ciou = []
for image_id in bar:
img = coco.loadImgs(image_id)[0]
annotation_ids = coco.getAnnIds(imgIds=img['id'])
annotations = coco.loadAnns(annotation_ids)
N = 0
for _idx, annotation in enumerate(annotations):
rle = cocomask.frPyObjects(annotation['segmentation'], img['height'], img['width'])
m = cocomask.decode(rle)
if _idx == 0:
mask = m.reshape((img['height'], img['width']))
N = len(annotation['segmentation'][0]) // 2
else:
mask = mask + m.reshape((img['height'], img['width']))
N = N + len(annotation['segmentation'][0]) // 2
mask = mask != 0
annotation_ids = coco_gti.getAnnIds(imgIds=img['id'])
annotations = coco_gti.loadAnns(annotation_ids)
N_GT = 0
for _idx, annotation in enumerate(annotations):
rle = cocomask.frPyObjects(annotation['segmentation'], img['height'], img['width'])
m = cocomask.decode(rle)
if _idx == 0:
mask_gti = m.reshape((img['height'], img['width']))
N_GT = len(annotation['segmentation'][0]) // 2
else:
mask_gti = mask_gti + m.reshape((img['height'], img['width']))
N_GT = N_GT + len(annotation['segmentation'][0]) // 2
mask_gti = mask_gti != 0
ps = 1 - np.abs(N - N_GT) / (N + N_GT + 1e-9)
iou = calc_IoU(mask, mask_gti)
list_iou.append(iou)
list_ciou.append(iou * ps)
bar.set_description("iou: %2.4f, c-iou: %2.4f" % (np.mean(list_iou), np.mean(list_ciou)))
bar.refresh()
print("Done!")
print("Mean IoU: ", np.mean(list_iou))
print("Mean C-IoU: ", np.mean(list_ciou))
if __name__ == "__main__":
compute_IoU_cIoU(input_json="./predictions.json",
gti_annotations="/home/stefano/Workspace/data/mapping_challenge_dataset/raw/val/annotation.json")