forked from ultralytics/yolov5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calibrate.py
179 lines (154 loc) Β· 6.75 KB
/
calibrate.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
# YOLOv5 π by Ultralytics, GPL-3.0 license
"""
Run YOLOv5 detection inference on images, videos, directories, globs, YouTube, webcam, streams, etc.
Usage - config:
$ python calibrate.py --config 'config/opt/opt_Visdrone_detect_S_Hyp0.yaml'
"""
import sys
from pathlib import Path
import os
from torch.distributed import pickle
import yaml
import pickle
import datetime
from argparse import Namespace, ArgumentParser
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 utils.general import LOGGER, Profile, split_dict, increment_path, print_args
from utils.calibration_utils import (
setup_data_model, get_yolo_predictions, get_calibrator, calibration, calc_mAP, NMS
)
from utils.torch_utils import smart_inference_mode
@smart_inference_mode()
def run(
config=ROOT / 'config/opt/opt_Visdrone_detect_S_Hyp0.yaml',
conf_thres=0.02,
iou_thres_obj=0.60,
iou_thres_class=0.30,
where_apply_calib_obj='before_nms',
where_apply_calib_class='after_nms',
calibrator='isotonic',
size_test=0.6,
speed_info=False,
save_dict=True,
plots=True,
):
with open(config) as f:
config = Namespace(**yaml.safe_load(f))
dataloader, model, device, dt = setup_data_model(config, ROOT)
num_classes = model.model.nc
now = datetime.datetime.now()
save_dir = increment_path(Path(config.project) / config.name / f'calibrate_{str(now.year).zfill(4)}_{str(now.month).zfill(2)}_{str(now.day).zfill(2)}__{str(now.hour).zfill(2)}_{str(now.minute).zfill(2)}', exist_ok=config.exist_ok, mkdir=True) # increment run
d_ = {
"FILE": str(FILE),
"save_dir": str(save_dir),
"nc": model.model.nc,
"names": model.names,
"conf_thres": conf_thres,
"iou_thres_obj": iou_thres_obj,
"iou_thres_class": iou_thres_class,
"where_apply_calib_obj": where_apply_calib_obj,
"where_apply_calib_class": where_apply_calib_class,
"calibrator": calibrator,
"size_test": size_test,
}
with open(os.path.join(save_dir, 'config.yaml'), 'w') as file:
yaml.dump(vars(config), file)
dt = Profile(), Profile(), Profile()
with dt[0]:
data_dict = get_yolo_predictions(dataloader, model, config, device, dt)
calib_dict, test_dict = split_dict(data_dict, size_test)
print("Length calib dict: ", len(calib_dict), " and length test dict: ", len(test_dict))
calibrator = get_calibrator(calibrator)
calib_location = "before_nms"
LOGGER.info(f'\nStarting the calibrations: {calib_location}...')
calib_obj = (where_apply_calib_obj == calib_location)
calib_class = (where_apply_calib_class == calib_location)
if plots:
os.mkdir(os.path.join(save_dir, "images"))
plots = os.path.join(save_dir, "images")
else:
plots = None
with dt[1]:
names_after = calibration(
["bbox_xywh", "obj", "class", "bbox_xyxy_scaled"],
calib_location,
calib_obj,
calib_class,
calib_dict,
test_dict,
calibrator,
conf_thres,
iou_thres_obj,
iou_thres_class,
config,
num_classes,
device,
plots
)
name_preds = names_after.copy()
names_after = [names_after[0]+"_nms", names_after[1]+"_nms", names_after[2]+"_nms", names_after[3]+"_nms"]
NMS(names_after, dataloader, calib_dict, name_preds=name_preds, num_classes=num_classes, opt=config, device=device)
NMS(names_after, dataloader, test_dict, name_preds=name_preds, num_classes=num_classes, opt=config, device=device)
names_after[0] = names_after[3]+"_scaled"
calib_location = "after_nms"
LOGGER.info(f'\nStarting the calibrations: {calib_location}...')
calib_obj = (where_apply_calib_obj == calib_location)
calib_class = (where_apply_calib_class == calib_location)
with dt[2]:
names_after = calibration(
names_after,
calib_location,
calib_obj,
calib_class,
calib_dict,
test_dict,
calibrator,
conf_thres,
iou_thres_obj,
iou_thres_class,
config,
num_classes,
device,
plots,
)
# Print speeds
if speed_info:
t = tuple(x.t / len(data_dict) * 1E3 for x in dt) # speeds per image
LOGGER.info(f'Speed: %.1fms inference, %.1fms before NMS, %.1fms after NMS' % t)
if save_dict:
with open(f'{save_dir}/calib_dict.pickle', 'wb') as f:
pickle.dump(calib_dict, f, protocol=pickle.HIGHEST_PROTOCOL)
with open(f'{save_dir}/test_dict.pickle', 'wb') as f:
pickle.dump(test_dict, f, protocol=pickle.HIGHEST_PROTOCOL)
d_["names_after"] = names_after
with open(os.path.join(save_dir, 'var.yaml'), 'w') as file:
yaml.dump(d_, file)
print("Original Results")
calc_mAP(save_dir, ["bbox_xyxy_scaled_nms", "obj_nms", "class_nms"], device, title="nms", plots=False)
print("Calibrated results --> objectness calibration: ", where_apply_calib_obj, " & class calibration: ", where_apply_calib_class)
calc_mAP(save_dir, names_after, device, title="calib_nms", plots=False)
def parse_opt():
parser = ArgumentParser()
parser.add_argument('--config', type=str, default=ROOT / 'config/opt/opt_Visdrone_detect_S_Hyp0.yaml', help='config_file.yaml path')
parser.add_argument('--conf_thres', type=float, default=0.02, help='confidence threshold')
parser.add_argument('--iou_thres_obj', type=float, default=0.6, help='Objectness IoU threshold')
parser.add_argument('--iou_thres_class', type=float, default=0.3, help='Class IoU threshold')
parser.add_argument('--where_apply_calib_obj', type=str, default='before_nms', help='where to apply objectness calibration')
parser.add_argument('--where_apply_calib_class', type=str, default='after_nms', help='where to apply class calibration')
parser.add_argument('--calibrator', type=str, default='isotonic', help='which calibrator to use')
parser.add_argument('--size_test', type=float, default=0.6, help='the split for the dataset')
parser.add_argument('--speed_info', type=bool, default=False, help='details of the speed')
parser.add_argument('--save_dict', type=bool, default=True, help='should you save the results')
parser.add_argument('--plots', type=bool, default=True, help='save the ECE plots')
opt = parser.parse_args()
print_args(vars(opt))
return opt
def main(opt):
run(**vars(opt))
if __name__ == "__main__":
opt = parse_opt()
main(opt)