-
Notifications
You must be signed in to change notification settings - Fork 0
/
ensemble_COCO_kvasir.py
165 lines (127 loc) · 5.79 KB
/
ensemble_COCO_kvasir.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
# Created by Gorkem Polat at 14.03.2021
# contact: [email protected]
import glob
import os
import json
import shutil
from ensemble_boxes import *
import argparse
import time
# parser = argparse.ArgumentParser(description='EndoCV2021: inference on test set, by Ece Isik Polat')
# parser.add_argument("-it", "--iou_threshold", type=float, default=0.3)
# args = parser.parse_args()
weights = [1, 1, 1, 1]
# iou_thr = args.iou_threshold
iou_thr = 0.5 # Be careful on here! normal = 0.4
skip_box_thr = 0.0001
# predicted_path_list = ["test_kvasir_bbox_results_0.json",
# "test_kvasir_bbox_results_1.json",
# "test_kvasir_bbox_results_2.json",
# "test_kvasir_bbox_results_3.json"]
# ground_truth_path = '/home/ws2080/Desktop/data/EndoCV2021/edited_files/paper/Kvasir-SEG/kvasir_seg_COCO.json'
# target_file = "ensemble_kvasir.json"
# predicted_path_list = ["test_kvasir_mini_test_bbox_results_0.json",
# "test_kvasir_mini_test_bbox_results_1.json",
# "test_kvasir_mini_test_bbox_results_2.json",
# "test_kvasir_mini_test_bbox_results_3.json"]
# ground_truth_path = '/home/ws2080/Desktop/data/EndoCV2021/edited_files/paper/Kvasir-SEG/kvasir_seg_test_COCO.json'
# target_file = "ensemble_kvasir_test.json"
predicted_path_list = ["test_kvasir_model_on_endocv_center_bbox_results_0.json",
"test_kvasir_model_on_endocv_center_bbox_results_1.json",
"test_kvasir_model_on_endocv_center_bbox_results_2.json",
"test_kvasir_model_on_endocv_center_bbox_results_3.json"]
ground_truth_path = 'datasets/polyps_all_centers/annotations/instances_test.json'
target_file = "ensemble_kvasir_on_endocv_center.json"
def calculate_normalized_voc_given_json_path(predicted_path, ground_truth_path):
f1 = open(predicted_path)
json_dict = json.load(f1)
f2 = open(ground_truth_path)
originals = json.load(f2)
organized_json_dict = []
organized_counter = 0
for i in range(len(json_dict)):
image_id = json_dict[i]["image_id"]
image_width = originals["images"][image_id]["width"]
image_height = originals["images"][image_id]["height"]
x1 = json_dict[i]["bbox"][0]
y1 = json_dict[i]["bbox"][1]
w = json_dict[i]["bbox"][2]
h = json_dict[i]["bbox"][3]
x2 = x1 + w
y2 = y1 + h
if x2 > image_width:
x2 = image_width
if y2 > image_height:
y2 = image_height
voc = [x1, y1, x2, y2]
normalized = [x1 / image_width, y1 / image_height, x2 / image_width, y2 / image_height]
json_dict[i].update({"voc": voc})
json_dict[i].update({"normalized": normalized})
if ((x1 < image_width) & (y1 < image_height) & (y2 > y1) & (x2 > x1)):
organized_json_dict.append(json_dict[i])
organized_counter = organized_counter + 1
return organized_json_dict
def get_original_images_id_list(ground_truth_path):
f = open(ground_truth_path)
json_dict = json.load(f)
original_images_ids = []
for org_img in json_dict["images"]:
original_images_ids.append(org_img["id"])
return original_images_ids
original_images_ids = get_original_images_id_list(ground_truth_path)
total_elapsed = 0
def get_enseble_results(predicted_path_list, ground_truth_path):
global total_elapsed
f_gt = open(ground_truth_path)
gt_dict = json.load(f_gt)
original_images_id_list = get_original_images_id_list(ground_truth_path)
fusion_dict = []
for image_id in original_images_id_list:
boxes_list = []
scores_list = []
labels_list = []
for json_path in predicted_path_list:
start = time.time()
json_dict = calculate_normalized_voc_given_json_path(json_path, ground_truth_path)
image_annotations = [x for x in json_dict if x["image_id"] == image_id]
bb = []
scr = []
lbl = []
for ann in image_annotations:
for j in range(4):
if (ann["normalized"][j] < 0):
print(json_path, ann["id"], image_id, ann["normalized"][j])
if (ann["normalized"][j] > 1):
print(json_path, ann["id"], image_id, ann["normalized"][j])
bb.append(ann["normalized"])
scr.append(ann["score"])
lbl.append(1)
boxes_list.append(bb)
scores_list.append(scr)
labels_list.append(lbl)
boxes, scores, labels = weighted_boxes_fusion(boxes_list, scores_list, labels_list, weights=weights,
iou_thr=iou_thr, skip_box_thr=skip_box_thr)
end = time.time()
total_elapsed += (end - start)
image_width = gt_dict["images"][image_id]["width"]
image_height = gt_dict["images"][image_id]["height"]
annotation_counter = 0
for i in range(len(scores)):
x1 = int(boxes[i, 0] * image_width)
y1 = int(boxes[i, 1] * image_height)
x2 = int(boxes[i, 2] * image_width)
y2 = int(boxes[i, 3] * image_height)
object_width = x2 - x1
object_height = y2 - y1
annotation_dict = {}
annotation_dict["image_id"] = image_id
annotation_dict["category_id"] = 1
annotation_dict["score"] = scores[i].astype(float)
annotation_dict["bbox"] = [x1, y1, object_width, object_height]
fusion_dict.append(annotation_dict)
annotation_counter += 1
with open(target_file, "w") as outfile:
json.dump(fusion_dict, outfile)
get_enseble_results(predicted_path_list, ground_truth_path)
print(total_elapsed)
print(total_elapsed / 1000)