-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict_ensemble.py
430 lines (348 loc) · 12.9 KB
/
predict_ensemble.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
import os
import argparse
import json
from pathlib import Path
import numpy as np
from PIL import Image
import mmcv
import torch
from mmcv import Config, DictAction
from mmcv.runner import load_checkpoint, wrap_fp16_model
from mmdet.apis import single_gpu_test
from mmdet.datasets import build_dataloader, build_dataset, replace_ImageToTensor
from mmdet.models import build_detector
from mmdet.utils import build_dp, compat_cfg, get_device, setup_multi_processes
from tqdm import tqdm
from ensemble_boxes import nms, soft_nms, non_maximum_weighted, weighted_boxes_fusion
torch.backends.cudnn.benchmark = True
NMS_THRESH = 0.55
BOX_THRESH = 0.55
PP_THRESH = 0.55
CLASSES = (
"Ascaris lumbricoides",
"Capillaria philippinensis",
"Enterobius vermicularis",
"Fasciolopsis buski",
"Hookworm egg",
"Hymenolepis diminuta",
"Hymenolepis nana",
"Opisthorchis viverrine",
"Paragonimus spp",
"Taenia spp. egg",
"Trichuris trichiura",
)
OUTPUT_CATEGORIES = [
{"id": i, "name": class_name, "supercategory": None}
for i, class_name in enumerate(CLASSES)
]
def format_annotations(file_path: str, image_dict: dict):
"""
Formats annotations from a given file path and given dictionary.
"""
try:
annotations = json.load(open(file_path, "r"))["annotations"]
except TypeError as e:
annotations = json.load(open(file_path, "r"))
data = {}
for ann in annotations:
try:
file_name = ann["file_name"]
except KeyError as e:
image_id = ann["image_id"]
file_name = "{}".format(image_id).zfill(4)
file_name = f"{file_name}.jpg"
image_info = image_dict[file_name]
orig_bbox = ann["bbox"]
bbox = norm_coco_bbox(ann["bbox"], image_info["width"], image_info["height"])
score = ann["score"]
category_id = ann["category_id"]
if file_name not in data.keys():
data[file_name] = {
"original_boxes_list": [],
"boxes_list": [],
"labels_list": [],
"scores_list": [],
}
data[file_name]["original_boxes_list"].append(orig_bbox)
data[file_name]["boxes_list"].append(bbox)
data[file_name]["labels_list"].append(category_id)
data[file_name]["scores_list"].append(score)
return data
def norm_coco_bbox(coco_bbox, w, h):
"""Normalize COCO format bounding box to [0, 1] range."""
# COCO bbox format: [x, y, width, height]
# Coordinates for boxes expected to be normalized e.g in range [0; 1]. Order: x1, y1, x2, y2.
x1 = coco_bbox[0] / w
x2 = x1 + (coco_bbox[2] / w)
y1 = coco_bbox[1] / h
y2 = y1 + (coco_bbox[3] / h)
return [x1, y1, x2, y2]
def convert_norm_box_to_coco_bbox(norm_box, w, h):
"""Convert normalized box to COCO format."""
bbox = [
norm_box[0] * w,
norm_box[1] * h,
(norm_box[2] - norm_box[0]) * w,
(norm_box[3] - norm_box[1]) * h,
]
return bbox
def process_fusion(
image_names, iou_thr, skip_box_thr, weight_dict, format_annotation_dict
):
"""
Fusion prediction boxes from different models.
"""
fusions_dict = {}
for path in tqdm(image_names):
boxes_list = []
scores_list = []
labels_list = []
weights = []
for model_name, format_ann in format_annotation_dict.items():
# print(model_name)
info = format_ann.get(path)
if not info:
# no prediction for this model
continue
boxes_list.append(info["boxes_list"])
scores_list.append(info["scores_list"])
labels_list.append(info["labels_list"])
weights.append(weight_dict.get(model_name))
if len(boxes_list) < 4:
print(path, len(boxes_list))
print(labels_list)
print(scores_list)
print("-" * 20)
boxes, scores, labels = weighted_boxes_fusion(
boxes_list,
scores_list,
labels_list,
weights=weights,
iou_thr=iou_thr,
skip_box_thr=skip_box_thr,
)
fusions_dict[path] = {
"boxes": boxes,
"scores": scores,
"labels": labels,
}
return fusions_dict
def create_empty_coco_annotations(full_path_to_images: str):
"""
Create empty COCO annotations for the prediction.
"""
images_info = []
image_dict = {}
file_extensions = ["*.jpg", "*.jpeg", "*.png", "*.tif", "*.gif"]
for extension in file_extensions:
files = list(Path(full_path_to_images).glob(extension))
files.sort()
for i, filename in enumerate(files):
image = Image.open(filename)
width, height = image.size
image_file_name = str(os.path.basename(filename))
images_info.append([image_file_name, int(height), int(width)])
image_dict[image_file_name] = {
"width": width,
"height": height,
"image_id": i,
}
images = [
{
"file_name": image_info[0],
"height": image_info[1],
"width": image_info[2],
"id": i,
}
for i, image_info in enumerate(images_info, start=1)
]
image_id_to_filename = {
f"{image_info['id']}": image_info["file_name"] for image_info in images
}
annotations = {
"categories": OUTPUT_CATEGORIES,
"annotations": [],
"images": images,
}
return annotations, image_id_to_filename, image_dict
def parse_args():
parser = argparse.ArgumentParser(
description="Predicts and ensembles bboxes using pretrained detection models on a given set of images."
)
parser.add_argument("images_dir", help="path to folder containing images")
parser.add_argument(
"models_dir", help="path to folder containing model checkpoints and configs"
)
parser.add_argument(
"--out",
help="output submission file in json format",
default="submission_fusion_20220530_01_thr=0.55.json",
)
parser.add_argument("--gpu-id", type=int, default=0, help="id of gpu to use")
args = parser.parse_args()
return args
def main():
args = parse_args()
if args.out is not None and not args.out.endswith((".json")):
raise ValueError("The output file must be a JSON file.")
# create list of test images in coco format since mmdet inference expects this
annotations, image_id_to_filename, image_dict = create_empty_coco_annotations(
args.images_dir
)
with open("annotations.json", "w") as f:
json.dump(annotations, f)
config_paths = list(Path(args.models_dir).glob("*.py"))
checkpoint_config_paths = [
(config, Path(args.models_dir, config.name.split(".")[0] + ".pth"))
for config in config_paths
]
# list of predictions from each model
result_files = []
################################################## INFERENCE ##################################################
for config_path, checkpoint_path in checkpoint_config_paths:
cfg = Config.fromfile(config_path)
cfg.data.test.ann_file = "annotations.json"
cfg.data.test.img_prefix = args.images_dir
cfg = compat_cfg(cfg)
setup_multi_processes(cfg)
cfg.gpu_ids = [
args.gpu_id,
]
cfg.device = get_device()
test_dataloader_default_args = dict(
samples_per_gpu=1, workers_per_gpu=2, dist=False, shuffle=False
)
cfg.data.test.test_mode = True
if cfg.data.test_dataloader.get("samples_per_gpu", 1) > 1:
# Replace 'ImageToTensor' to 'DefaultFormatBundle'
cfg.data.test.pipeline = replace_ImageToTensor(cfg.data.test.pipeline)
test_loader_cfg = {
**test_dataloader_default_args,
**cfg.data.get("test_dataloader", {}),
}
# build the dataloader
dataset = build_dataset(cfg.data.test)
data_loader = build_dataloader(dataset, **test_loader_cfg)
# build the model and load checkpoint
cfg.model.train_cfg = None
model = build_detector(cfg.model, test_cfg=cfg.get("test_cfg"))
fp16_cfg = cfg.get("fp16", None)
if fp16_cfg is not None:
wrap_fp16_model(model)
checkpoint = load_checkpoint(model, str(checkpoint_path), map_location="cpu")
model = build_dp(model, cfg.device, device_ids=cfg.gpu_ids)
model_name = checkpoint_path.name.split(".")[0]
# run inference with the model on data
outputs = single_gpu_test(model, data_loader, False, None, 0.3)
if not os.path.exists("./predictions"):
os.mkdir("./predictions")
dataset.format_results(outputs, jsonfile_prefix=f"./predictions/{model_name}")
raw_bbox_predictions = json.load(
open(f"./predictions/{model_name}.bbox.json", "r")
)
# prune and format the raw predictions for fusion
annotations_ = []
for r in raw_bbox_predictions:
if r["score"] > 0.1:
anno = {}
anno["id"] = r["image_id"]
anno["bbox"] = r["bbox"]
anno["category_id"] = r["category_id"]
anno["file_name"] = image_id_to_filename[str(r["image_id"])]
anno["score"] = r["score"]
annotations_.append(anno)
annotations = dict(annotations=annotations_)
json.dump(
annotations, open(f"./predictions/{model_name}_threshold=0.1.json", "w")
)
result_files.append(
{
"name": model_name,
"path": f"./predictions/{model_name}_threshold=0.1.json",
"weight": 0.2, # 2022-05-20
}
)
################################################## ENSEMBLE ##################################################
# format result files for fusion
format_annotation_dict = {}
file_paths = []
for rf in result_files:
format_ann = format_annotations(rf["path"], image_dict)
format_annotation_dict[rf["name"]] = format_ann
file_paths.extend(format_ann.keys())
uniq_file_paths = sorted(list(set(file_paths)))
weight_dict = {r["name"]: r["weight"] for r in result_files}
fusions_dict = process_fusion(
uniq_file_paths[:], NMS_THRESH, BOX_THRESH, weight_dict, format_annotation_dict
)
# convert fusion results to submission format
items = []
cnt_id = 0
cnt_lower_threshold = 0
threshold = PP_THRESH
for file_name, info in fusions_dict.items():
# print(file_name)
image_info = image_dict[file_name]
score_indexes = np.argsort(info["scores"])
if len(info["scores"]) == 0:
continue
# filter max score
index = np.argmax(info["scores"])
cnt_annotate = 0
found_category_id = None
for index in score_indexes:
label = info["labels"][index]
score = info["scores"][index]
box = info["boxes"][index]
if score < threshold:
continue
category_id = int(label)
coco_bbox = convert_norm_box_to_coco_bbox(
box, image_info["width"], image_info["height"]
)
item = {
"id": cnt_id,
"bbox": coco_bbox,
"category_id": category_id,
"file_name": file_name,
"score": float(score),
}
items.append(item)
cnt_id += 1
cnt_annotate += 1
found_category_id = category_id
missing_pred_images = set(image_dict.keys()) - set(
list(map(lambda r: r["file_name"], items))
)
# fill missing predictions with the selected model's outputs
selected_model = "tood_r101_dconv_10epoch"
missing_items = []
for missing_im in missing_pred_images:
file_name = missing_im
image_info = image_dict[file_name]
info = format_annotation_dict[selected_model].get(file_name, None)
if info is None:
print("Not found: {}".format(file_name))
continue
# filter max score
index = np.argmax(info["scores_list"])
label = info["labels_list"][index]
score = info["scores_list"][index]
box = info["boxes_list"][index]
category_id = int(label)
coco_bbox = convert_norm_box_to_coco_bbox(
box, image_info["width"], image_info["height"]
)
item = {
"id": cnt_id,
"bbox": coco_bbox,
"category_id": category_id,
"file_name": missing_im,
"score": float(score),
}
items.append(item)
cnt_id += 1
submission_output = {"annotations": items}
json.dump(submission_output, open(args.out, "w"), indent=2, sort_keys=False)
if __name__ == "__main__":
main()