forked from WongKinYiu/yolov7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate_yolov7_pnp_model.py
executable file
·341 lines (292 loc) · 10.8 KB
/
evaluate_yolov7_pnp_model.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
#!/usr/bin/env python3
import json
from abc import ABC, abstractmethod
from copy import copy
from dataclasses import dataclass
from pathlib import Path
import time
from typing import List
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import torch
from keypoint_detection.utils.heatmap import get_keypoints_from_heatmap
from keypoint_detection.utils.load_checkpoints import load_from_checkpoint
from sklearn import metrics
from torchvision.io import read_image
from tqdm import tqdm
import click
import cv2
import torchvision.transforms as T
from cone_detector.metrics import *
from cone_detector.utils import *
from yolov7_detect import Yolov7Detector
import wandb
print_green = lambda x: print("\033[32m" + x + "\033[0m")
# Colours in BGR
CAT_TO_COLOUR = {
0: (255, 0, 0),
1: (0, 250, 250),
2: (0, 100, 220),
3: (0, 130, 220),
}
# Parameters
MIN_CONE_HEIGHT = 10
METRIC_THRESHOLDS = [0.1, 0.5, 1, 3]
LATENCY_TEST_REPETITIONS = 1000
CONE_IMAGE_MARGIN = 3
IMAGE_SIZE = (128, 96)
image_encoding = T.Resize((int(IMAGE_SIZE[0]), int(IMAGE_SIZE[1])))
device = torch.device("cuda")
# Camera information
W, H = 1920, 1200
Sw, Sh = 9.21, 5.76
focal_length = 8
m = np.load("../keypoint-detection/model_evaluation/camera_calibration_baumer.npz")
camera_matrix = m["camera_matrix"]
distortion_matrix = m["distortion_matrix"]
cone_models = np.load("../keypoint-detection/model_evaluation/cones.npz")
cone_model_small = cone_models["cone_0"]
cone_model_big = cone_models["cone_2"]
cv2rep = lambda x: np.array([x[2], -x[0], -x[1]])
def get_position_estimates(
keypoints_filtered, valid_kpt_mask, category, camera_matrix, distortion_matrix
):
points_3d = cone_model_big if category == 2 else cone_model_small
points_3d_filtered = points_3d[valid_kpt_mask, :]
_, _, tvec = cv2.solvePnP(
points_3d_filtered, keypoints_filtered, camera_matrix, distortion_matrix
)
return cv2rep(tvec).T
@click.command()
@click.argument(
"yolo_model_path",
type=click.Path(exists=True, file_okay=True, dir_okay=False),
)
@click.argument(
"artifact_id",
type=str,
)
@click.option(
'--new-run',
default=False,
help='Create a new W&B run instead of overwriting the old one.'
)
def main(yolo_model_path, artifact_id, new_run):
# Connect to wandb run
print_green("Retrieving wandb run")
run = wandb.init(
project="thesis-monocular-depth-estimation",
id=artifact_id[49:57],
resume="must",
)
artifact = run.use_artifact(artifact_id, type="model")
artifact_dir = artifact.download()
checkpoint_path = Path(artifact_dir) / "model.ckpt"
kp_model = load_from_checkpoint(checkpoint_path)
detector = Yolov7Detector(yolo_model_path)
if new_run:
run.finish()
run = wandb.init(
project="thesis-monocular-depth-estimation",\
)
# Initialise the dataset
dataset_path = Path("../test_set_real/dataset.json")
# Open the dataset and map image ids to their annotations
with open(dataset_path, "r") as f:
dataset = json.load(f)
ann_by_image_id = {}
for annot in dataset["annotations"]:
if annot["image_id"] not in ann_by_image_id:
ann_by_image_id[annot["image_id"]] = [annot]
else:
ann_by_image_id[annot["image_id"]].append(annot)
# Predict the data
global_predictions, global_gt = [], []
prediction_times = []
image_ids = []
print_green("Predicting test set cones")
# Form predictions for the entire dataset
for image_obj in tqdm(dataset["images"], desc="Predicting cone locations"):
# Load in the image
original_image = (
read_image(str(dataset_path.parent / image_obj["file_name"])).float() / 255
)
vis_img = cv2.imread(str(dataset_path.parent / image_obj["file_name"]))
image_ids.append(image_obj['id'])
# Predict keypoints
yolo_time, detections = detector.detect(vis_img)
prediction_times.append(
yolo_time
) # Firstly append the YOLO time, the cone extraction will be added in-place later
predictions = np.empty((0, 4))
for detection in detections:
category = int(detection[-1])
bbox_kpts = [round(x) for x in detection[:4]]
bbox_kpts[0] = max(0, bbox_kpts[0] - CONE_IMAGE_MARGIN)
bbox_kpts[1] = max(0, bbox_kpts[1] - CONE_IMAGE_MARGIN)
vis_img = cv2.rectangle(vis_img, bbox_kpts[:2], bbox_kpts[2:], CAT_TO_COLOUR[category], 2)
cone_img = original_image[
:, bbox_kpts[1] : bbox_kpts[3], bbox_kpts[0] : bbox_kpts[2]
]
original_size = cone_img.shape[1:]
with torch.no_grad():
cone_img = image_encoding(
cone_img
) # Don't forget to rescale the image before inference
cone_img = cone_img[np.newaxis, ...]
result = kp_model(cone_img)
tic = time.perf_counter()
kpts = []
valid_kpt_mask = np.zeros(7, dtype=bool)
for i in range(7):
tens = torch.Tensor(get_keypoints_from_heatmap(result[0, i], 2))
# Don't forget to undo any image resizing afterwards
if len(tens) != 0:
tens = tens[0]
tens[0] = tens[0] * original_size[1] / IMAGE_SIZE[1] + bbox_kpts[0]
tens[1] = tens[1] * original_size[0] / IMAGE_SIZE[0] + bbox_kpts[1]
valid_kpt_mask[i] = True
kpts.append(tens.detach().cpu().numpy())
kpts = np.array(kpts)
if np.sum(valid_kpt_mask) < 4:
print("Incomplete detection")
print(kpts)
continue
for pred in kpts:
pred_int = (round(pred[0]), round(pred[1]))
vis_img = cv2.circle(
vis_img, pred_int, 3, CAT_TO_COLOUR[category], -1
)
vis_img = cv2.circle(vis_img, pred_int, 2, (0, 0, 0), -1)
vis_img = cv2.circle(vis_img, pred_int, 1, (255, 255, 255), -1)
cone_pos = get_position_estimates(
kpts, valid_kpt_mask, category, camera_matrix, distortion_matrix
)[0]
cone = np.array([category, *cone_pos])
predictions = np.vstack((predictions, cone))
toc = time.perf_counter()
prediction_times[-1] += toc - tic
# If there is GT data available, extract the GT positions
if image_obj["id"] not in ann_by_image_id:
gt = None
else:
gt = extract_positions(ann_by_image_id[image_obj["id"]], MIN_CONE_HEIGHT)
if len(predictions) == 0:
predictions = None
pred_count = 0 if predictions is None else predictions.shape[0]
gt_count = 0 if gt is None else gt.shape[0]
print(f"#detected/#gt -> {pred_count}/{gt_count}")
global_predictions.append(predictions)
global_gt.append(gt)
vis_img = cv2.cvtColor(vis_img, cv2.COLOR_BGR2RGB)
wandb.log({f"[{image_obj['id']}] Detections": wandb.Image(vis_img)})
print_green("Calculate metrics")
columns = []
for threshold in METRIC_THRESHOLDS:
columns += [
f"lucas_{threshold}",
f"colour_{threshold}",
f"matching_{threshold}",
f"pos_2d_{threshold}",
f"pos_3d_{threshold}",
]
df = pd.DataFrame(columns=columns)
for i in range(len(global_predictions)):
entry = []
for threshold in METRIC_THRESHOLDS:
pred_to_gt, gt_fn = match_cones(
global_gt[i], global_predictions[i], threshold
)
l_score = lucas_score(
global_gt[i],
global_predictions[i],
pred_to_gt,
gt_fn,
threshold,
)
l2_score = lucas_score2(
global_gt[i],
global_predictions[i],
pred_to_gt,
gt_fn,
threshold,
)
c_score = colour_score(
global_gt[i],
global_predictions[i],
pred_to_gt,
gt_fn,
)
m_score = matching_score(
global_gt[i],
global_predictions[i],
pred_to_gt,
gt_fn,
)
score_2d, score_3d = pos_score(
global_gt[i], global_predictions[i], pred_to_gt, gt_fn
)
if threshold == 0.5:
fig = generate_plot(
global_predictions[i],
global_gt[i],
f"[{image_ids[i]}] Lucas score 0.5: {round(l_score)}/{l2_score:.2f} ({c_score:.2f}, {m_score:.2f}, {score_2d:.2f})",
)
wandb.log({f"lucas_score_0.5_{i}": wandb.Image(fig)})
entry += [
l_score,
colour_score(
global_gt[i],
global_predictions[i],
pred_to_gt,
gt_fn,
),
matching_score(
global_gt[i],
global_predictions[i],
pred_to_gt,
gt_fn,
),
score_2d,
score_3d,
]
df.loc[len(df)] = entry
print("Median scores:")
print(df.median())
# Log metrics
table = wandb.Table(data=df, columns=columns)
for threshold in METRIC_THRESHOLDS:
wandb.log(
{
f"test/metrics/lucas/{threshold}_histogram": wandb.plot.histogram(
table,
f"lucas_{threshold}",
title=f"Lucas score (threshold {threshold})",
)
}
)
for col_name in columns:
wandb.run.summary[f"test/metrics/median_{col_name}"] = df[col_name].median()
# Finally perform a speed test
print_green("Latency check")
timings = analyse_model_speed(kp_model, IMAGE_SIZE, LATENCY_TEST_REPETITIONS)
data = [[s] for s in timings]
table = wandb.Table(data=data, columns=["timings"])
wandb.log(
{
"latency_histogram": wandb.plot.histogram(
table, "timings", title="Model latency"
)
}
)
wandb.run.summary["median_model_latency"] = np.median(timings)
wandb.run.summary["median_total_latency"] = np.median(timings) + np.median(
prediction_times
)
print(f"Median model latency: {wandb.run.summary['median_model_latency']} ms")
print(f"Median total latency: {wandb.run.summary['median_total_latency']} ms")
run.finish()
if __name__ == "__main__":
main()