File size: 14,617 Bytes
3446cc1 | 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 | # nuScenes dev-kit.
# Code written by Holger Caesar, Varun Bankiti, and Alex Lang, 2019.
import json
from typing import Any
import numpy as np
from matplotlib import pyplot as plt
from nuscenes import NuScenes
from nuscenes.eval.common.data_classes import EvalBoxes
from nuscenes.eval.common.render import setup_axis
from nuscenes.eval.common.utils import boxes_to_sensor
from nuscenes.eval.detection.constants import TP_METRICS, DETECTION_NAMES, DETECTION_COLORS, TP_METRICS_UNITS, \
PRETTY_DETECTION_NAMES, PRETTY_TP_METRICS
from nuscenes.eval.detection.data_classes import DetectionMetrics, DetectionMetricData, DetectionMetricDataList
from nuscenes.utils.data_classes import LidarPointCloud
from nuscenes.utils.geometry_utils import view_points
Axis = Any
def visualize_sample(nusc: NuScenes,
sample_token: str,
gt_boxes: EvalBoxes,
pred_boxes: EvalBoxes,
nsweeps: int = 1,
conf_th: float = 0.15,
eval_range: float = 50,
verbose: bool = True,
display_legend: bool = False,
savepath: str = None) -> None:
"""
Visualizes a sample from BEV with annotations and detection results.
:param nusc: NuScenes object.
:param sample_token: The nuScenes sample token.
:param gt_boxes: Ground truth boxes grouped by sample.
:param pred_boxes: Prediction grouped by sample.
:param nsweeps: Number of sweeps used for lidar visualization.
:param conf_th: The confidence threshold used to filter negatives.
:param eval_range: Range in meters beyond which boxes are ignored.
:param verbose: Whether to print to stdout.
:param display_legend: Whether to display GT and EST boxes legend on plot.
:param savepath: If given, saves the the rendering here instead of displaying.
"""
# Retrieve sensor & pose records.
sample_rec = nusc.get('sample', sample_token)
sd_record = nusc.get('sample_data', sample_rec['data']['LIDAR_TOP'])
cs_record = nusc.get('calibrated_sensor', sd_record['calibrated_sensor_token'])
pose_record = nusc.get('ego_pose', sd_record['ego_pose_token'])
# Get boxes.
boxes_gt_global = gt_boxes[sample_token]
boxes_est_global = pred_boxes[sample_token]
# Map GT boxes to lidar.
boxes_gt = boxes_to_sensor(boxes_gt_global, pose_record, cs_record)
# Map EST boxes to lidar.
boxes_est = boxes_to_sensor(boxes_est_global, pose_record, cs_record)
# Add scores to EST boxes.
for box_est, box_est_global in zip(boxes_est, boxes_est_global):
box_est.score = box_est_global.detection_score
# Get point cloud in lidar frame.
pc, _ = LidarPointCloud.from_file_multisweep(nusc, sample_rec, 'LIDAR_TOP', 'LIDAR_TOP', nsweeps=nsweeps)
# Init axes.
_, ax = plt.subplots(1, 1, figsize=(9, 9))
# Show point cloud.
points = view_points(pc.points[:3, :], np.eye(4), normalize=False)
dists = np.sqrt(np.sum(pc.points[:2, :] ** 2, axis=0))
colors = np.minimum(1, dists / eval_range)
ax.scatter(points[0, :], points[1, :], c=colors, s=0.2)
# Show ego vehicle.
ax.plot(0, 0, 'x', color='black')
# Show GT boxes.
for box in boxes_gt:
box.render(ax, view=np.eye(4), colors=('g', 'g', 'g'), linewidth=2)
# Show EST boxes.
for box in boxes_est:
# Show only predictions with a high score.
assert not np.isnan(box.score), 'Error: Box score cannot be NaN!'
if box.score >= conf_th:
box.render(ax, view=np.eye(4), colors=('b', 'b', 'b'), linewidth=1)
# Add legend.
if display_legend:
ax.legend(['GT', 'EST'], loc='upper right', labels=['GT', 'EST'],
handles=[mpatches.Patch(color='g'), mpatches.Patch(color='b')])
# Limit visible range.
axes_limit = eval_range + 3 # Slightly bigger to include boxes that extend beyond the range.
ax.set_xlim(-axes_limit, axes_limit)
ax.set_ylim(-axes_limit, axes_limit)
# Show / save plot.
if verbose:
print('Rendering sample token %s' % sample_token)
plt.title(sample_token)
if savepath is not None:
plt.savefig(savepath)
plt.close()
else:
plt.show()
def class_pr_curve(md_list: DetectionMetricDataList,
metrics: DetectionMetrics,
detection_name: str,
min_precision: float,
min_recall: float,
savepath: str = None,
ax: Axis = None) -> None:
"""
Plot a precision recall curve for the specified class.
:param md_list: DetectionMetricDataList instance.
:param metrics: DetectionMetrics instance.
:param detection_name: The detection class.
:param min_precision:
:param min_recall: Minimum recall value.
:param savepath: If given, saves the the rendering here instead of displaying.
:param ax: Axes onto which to render.
"""
# Prepare axis.
if ax is None:
ax = setup_axis(title=PRETTY_DETECTION_NAMES[detection_name], xlabel='Recall', ylabel='Precision', xlim=1,
ylim=1, min_precision=min_precision, min_recall=min_recall)
# Get recall vs precision values of given class for each distance threshold.
data = md_list.get_class_data(detection_name)
# Plot the recall vs. precision curve for each distance threshold.
for md, dist_th in data:
md: DetectionMetricData
ap = metrics.get_label_ap(detection_name, dist_th)
ax.plot(md.recall, md.precision, label='Dist. : {}, AP: {:.1f}'.format(dist_th, ap * 100))
ax.legend(loc='best')
if savepath is not None:
plt.savefig(savepath)
plt.close()
def class_tp_curve(md_list: DetectionMetricDataList,
metrics: DetectionMetrics,
detection_name: str,
min_recall: float,
dist_th_tp: float,
savepath: str = None,
ax: Axis = None) -> None:
"""
Plot the true positive curve for the specified class.
:param md_list: DetectionMetricDataList instance.
:param metrics: DetectionMetrics instance.
:param detection_name:
:param min_recall: Minimum recall value.
:param dist_th_tp: The distance threshold used to determine matches.
:param savepath: If given, saves the the rendering here instead of displaying.
:param ax: Axes onto which to render.
"""
# Get metric data for given detection class with tp distance threshold.
md = md_list[(detection_name, dist_th_tp)]
min_recall_ind = round(100 * min_recall)
if min_recall_ind <= md.max_recall_ind:
# For traffic_cone and barrier only a subset of the metrics are plotted.
rel_metrics = [m for m in TP_METRICS if not np.isnan(metrics.get_label_tp(detection_name, m))]
ylimit = max([max(getattr(md, metric)[min_recall_ind:md.max_recall_ind + 1]) for metric in rel_metrics]) * 1.1
else:
ylimit = 1.0
# Prepare axis.
if ax is None:
ax = setup_axis(title=PRETTY_DETECTION_NAMES[detection_name], xlabel='Recall', ylabel='Error', xlim=1,
min_recall=min_recall)
ax.set_ylim(0, ylimit)
# Plot the recall vs. error curve for each tp metric.
for metric in TP_METRICS:
tp = metrics.get_label_tp(detection_name, metric)
# Plot only if we have valid data.
if tp is not np.nan and min_recall_ind <= md.max_recall_ind:
recall, error = md.recall[:md.max_recall_ind + 1], getattr(md, metric)[:md.max_recall_ind + 1]
else:
recall, error = [], []
# Change legend based on tp value
if tp is np.nan:
label = '{}: n/a'.format(PRETTY_TP_METRICS[metric])
elif min_recall_ind > md.max_recall_ind:
label = '{}: nan'.format(PRETTY_TP_METRICS[metric])
else:
label = '{}: {:.2f} ({})'.format(PRETTY_TP_METRICS[metric], tp, TP_METRICS_UNITS[metric])
ax.plot(recall, error, label=label)
ax.axvline(x=md.max_recall, linestyle='-.', color=(0, 0, 0, 0.3))
ax.legend(loc='best')
if savepath is not None:
plt.savefig(savepath)
plt.close()
def dist_pr_curve(md_list: DetectionMetricDataList,
metrics: DetectionMetrics,
dist_th: float,
min_precision: float,
min_recall: float,
savepath: str = None) -> None:
"""
Plot the PR curves for different distance thresholds.
:param md_list: DetectionMetricDataList instance.
:param metrics: DetectionMetrics instance.
:param dist_th: Distance threshold for matching.
:param min_precision: Minimum precision value.
:param min_recall: Minimum recall value.
:param savepath: If given, saves the the rendering here instead of displaying.
"""
# Prepare axis.
fig, (ax, lax) = plt.subplots(ncols=2, gridspec_kw={"width_ratios": [4, 1]},
figsize=(7.5, 5))
ax = setup_axis(xlabel='Recall', ylabel='Precision',
xlim=1, ylim=1, min_precision=min_precision, min_recall=min_recall, ax=ax)
# Plot the recall vs. precision curve for each detection class.
data = md_list.get_dist_data(dist_th)
for md, detection_name in data:
md = md_list[(detection_name, dist_th)]
ap = metrics.get_label_ap(detection_name, dist_th)
ax.plot(md.recall, md.precision, label='{}: {:.1f}%'.format(PRETTY_DETECTION_NAMES[detection_name], ap * 100),
color=DETECTION_COLORS[detection_name])
hx, lx = ax.get_legend_handles_labels()
lax.legend(hx, lx, borderaxespad=0)
lax.axis("off")
plt.tight_layout()
if savepath is not None:
plt.savefig(savepath)
plt.close()
def summary_plot(md_list: DetectionMetricDataList,
metrics: DetectionMetrics,
min_precision: float,
min_recall: float,
dist_th_tp: float,
savepath: str = None) -> None:
"""
Creates a summary plot with PR and TP curves for each class.
:param md_list: DetectionMetricDataList instance.
:param metrics: DetectionMetrics instance.
:param min_precision: Minimum precision value.
:param min_recall: Minimum recall value.
:param dist_th_tp: The distance threshold used to determine matches.
:param savepath: If given, saves the the rendering here instead of displaying.
"""
n_classes = len(DETECTION_NAMES)
_, axes = plt.subplots(nrows=n_classes, ncols=2, figsize=(15, 5 * n_classes))
for ind, detection_name in enumerate(DETECTION_NAMES):
title1, title2 = ('Recall vs Precision', 'Recall vs Error') if ind == 0 else (None, None)
ax1 = setup_axis(xlim=1, ylim=1, title=title1, min_precision=min_precision,
min_recall=min_recall, ax=axes[ind, 0])
ax1.set_ylabel('{} \n \n Precision'.format(PRETTY_DETECTION_NAMES[detection_name]), size=20)
ax2 = setup_axis(xlim=1, title=title2, min_recall=min_recall, ax=axes[ind, 1])
if ind == n_classes - 1:
ax1.set_xlabel('Recall', size=20)
ax2.set_xlabel('Recall', size=20)
class_pr_curve(md_list, metrics, detection_name, min_precision, min_recall, ax=ax1)
class_tp_curve(md_list, metrics, detection_name, min_recall, dist_th_tp=dist_th_tp, ax=ax2)
plt.tight_layout()
if savepath is not None:
plt.savefig(savepath)
plt.close()
def detailed_results_table_tex(metrics_path: str, output_path: str) -> None:
"""
Renders a detailed results table in tex.
:param metrics_path: path to a serialized DetectionMetrics file.
:param output_path: path to the output file.
"""
with open(metrics_path, 'r') as f:
metrics = json.load(f)
tex = ''
tex += '\\begin{table}[]\n'
tex += '\\small\n'
tex += '\\begin{tabular}{| c | c | c | c | c | c | c |} \\hline\n'
tex += '\\textbf{Class} & \\textbf{AP} & \\textbf{ATE} & \\textbf{ASE} & \\textbf{AOE} & ' \
'\\textbf{AVE} & ' \
'\\textbf{AAE} \\\\ \\hline ' \
'\\hline\n'
for name in DETECTION_NAMES:
ap = np.mean(metrics['label_aps'][name].values()) * 100
ate = metrics['label_tp_errors'][name]['trans_err']
ase = metrics['label_tp_errors'][name]['scale_err']
aoe = metrics['label_tp_errors'][name]['orient_err']
ave = metrics['label_tp_errors'][name]['vel_err']
aae = metrics['label_tp_errors'][name]['attr_err']
tex_name = PRETTY_DETECTION_NAMES[name]
if name == 'traffic_cone':
tex += '{} & {:.1f} & {:.2f} & {:.2f} & N/A & N/A & N/A \\\\ \\hline\n'.format(
tex_name, ap, ate, ase)
elif name == 'barrier':
tex += '{} & {:.1f} & {:.2f} & {:.2f} & {:.2f} & N/A & N/A \\\\ \\hline\n'.format(
tex_name, ap, ate, ase, aoe)
else:
tex += '{} & {:.1f} & {:.2f} & {:.2f} & {:.2f} & {:.2f} & {:.2f} \\\\ ' \
'\\hline\n'.format(tex_name, ap, ate, ase, aoe, ave, aae)
map_ = metrics['mean_ap']
mate = metrics['tp_errors']['trans_err']
mase = metrics['tp_errors']['scale_err']
maoe = metrics['tp_errors']['orient_err']
mave = metrics['tp_errors']['vel_err']
maae = metrics['tp_errors']['attr_err']
tex += '\\hline {} & {:.1f} & {:.2f} & {:.2f} & {:.2f} & {:.2f} & {:.2f} \\\\ ' \
'\\hline\n'.format('\\textbf{Mean}', map_, mate, mase, maoe, mave, maae)
tex += '\\end{tabular}\n'
# All one line
tex += '\\caption{Detailed detection performance on the val set. \n'
tex += 'AP: average precision averaged over distance thresholds (%), \n'
tex += 'ATE: average translation error (${}$), \n'.format(TP_METRICS_UNITS['trans_err'])
tex += 'ASE: average scale error (${}$), \n'.format(TP_METRICS_UNITS['scale_err'])
tex += 'AOE: average orientation error (${}$), \n'.format(TP_METRICS_UNITS['orient_err'])
tex += 'AVE: average velocity error (${}$), \n'.format(TP_METRICS_UNITS['vel_err'])
tex += 'AAE: average attribute error (${}$). \n'.format(TP_METRICS_UNITS['attr_err'])
tex += 'nuScenes Detection Score (NDS) = {:.1f} \n'.format(metrics['nd_score'] * 100)
tex += '}\n'
tex += '\\end{table}\n'
with open(output_path, 'w') as f:
f.write(tex)
|