Metrics#
Utilities for evaluating a Norfair pipeline against the
MOTChallenge format. These let you parse
ground-truth annotations, record per-frame predictions, and compute standard
multi-object-tracking metrics (MOTA, IDF1, ID switches, …) via
py-motmetrics.
This module is optional. Its MOTChallenge-specific helpers require the
metrics extra:
1 | |
Overview#
DetectionFileParser— reads MOTChallengedet.txt/gt.txtfiles and yields per-frameDetectionlists ready to feed into aTracker.PredictionsTextFile— writes your tracker output to a MOTChallenge-format text file, one row per tracked object per frame.InformationFile— tiny parser for theseqinfo.inisidecar files that ship with MOTChallenge sequences.Accumulators— a thin wrapper aroundmotmetricsaccumulators that lets you evaluate a whole dataset and print a summary table.
Typical workflow#
- For each sequence in the dataset, use
DetectionFileParserto replay the provided detections through yourTracker, and write the output withPredictionsTextFile. - Once all sequences have been processed, compare predictions against
ground truth with
Accumulatorsto get the standard MOT metrics.
See the MOTChallenge demo for a complete script that exercises all of the above.
API#
MOTChallenge-style I/O helpers and accumulators for the evaluation suite.
InformationFile
#
Tiny reader for MOTChallenge seqinfo.ini style metadata files.
Loads the file once at construction time and exposes a simple
:meth:search method to look up key=value pairs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
Path to the |
required |
Source code in norfair/metrics.py
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 | |
path = file_path
instance-attribute
#
lines = file.splitlines()
instance-attribute
#
__init__(file_path)
#
Read file_path into memory and split it into lines.
Source code in norfair/metrics.py
38 39 40 41 42 43 | |
search(variable_name)
#
Return the value of variable_name in the loaded file.
Integer-looking values are returned as int; everything
else is returned as str.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
variable_name
|
str
|
Key to look up, matched as a line prefix followed by an
|
required |
Returns:
| Type | Description |
|---|---|
int or str
|
The parsed value. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in norfair/metrics.py
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 | |
PredictionsTextFile
#
Write tracked objects to a MOTChallenge-format predictions file.
Each call to :meth:update appends one row per tracked object
for the current frame; the file is closed automatically once the
number of frames hits the sequence length, or explicitly via
:meth:close.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_path
|
str
|
Path to the sequence being processed. |
required |
save_path
|
str
|
Directory under which a |
'.'
|
information_file
|
InformationFile
|
Pre-parsed |
None
|
Source code in norfair/metrics.py
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 | |
length = int(seq_length) if isinstance(seq_length, str) else seq_length
instance-attribute
#
text_file = open(out_file_name, 'w+')
instance-attribute
#
frame_number = 1
instance-attribute
#
__init__(input_path, save_path='.', information_file=None)
#
Create the output file and record the sequence length.
Source code in norfair/metrics.py
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 | |
update(predictions, frame_number=None)
#
Write predictions for the current frame to the output file.
The output line format is::
1 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predictions
|
iterable of TrackedObject
|
Objects tracked for the current frame. |
required |
frame_number
|
int
|
Override for the frame index. When |
None
|
Source code in norfair/metrics.py
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 | |
close()
#
Close the underlying file handle.
Source code in norfair/metrics.py
177 178 179 180 | |
__enter__()
#
Enter the runtime context and return self.
Source code in norfair/metrics.py
182 183 184 | |
__exit__(exc_type, exc_val, exc_tb)
#
Exit the runtime context, ensuring the file handle is closed.
Source code in norfair/metrics.py
186 187 188 189 | |
__del__()
#
Ensure the underlying file is closed on garbage collection.
Source code in norfair/metrics.py
191 192 193 | |
DetectionFileParser
#
Parse MOTChallenge det/det.txt files into Norfair detections.
Pre-sorts detections by frame so that iterating the parser yields
a list of :class:Detection for each frame in order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_path
|
str
|
Path to the MOTChallenge sequence directory. |
required |
information_file
|
InformationFile
|
Pre-parsed |
None
|
Source code in norfair/metrics.py
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 | |
frame_number = 1
instance-attribute
#
matrix_detections = self.matrix_detections[row_order]
instance-attribute
#
length = int(seq_length) if isinstance(seq_length, str) else seq_length
instance-attribute
#
sorted_by_frame = []
instance-attribute
#
__init__(input_path, information_file=None)
#
Load and pre-sort the detections matrix.
Source code in norfair/metrics.py
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 | |
get_dets_from_frame(frame_number)
#
Return the list of Norfair Detection for frame_number.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame_number
|
int
|
1-based frame index to retrieve detections for. |
required |
Returns:
| Type | Description |
|---|---|
list of Detection
|
Norfair |
Source code in norfair/metrics.py
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 | |
__iter__()
#
Reset the frame counter and return self as an iterator.
Source code in norfair/metrics.py
275 276 277 278 | |
__next__()
#
Return the detection list for the next frame in the sequence.
Source code in norfair/metrics.py
280 281 282 283 284 285 286 287 288 | |
Accumulators
#
Collect tracker outputs across sequences for MOT metrics evaluation.
Each sequence is opened with :meth:create_accumulator, fed
frame-by-frame via :meth:update, and finally evaluated with
:meth:compute_metrics to produce a dataframe of metrics.
Source code in norfair/metrics.py
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 431 | |
matrixes_predictions = []
instance-attribute
#
paths = []
instance-attribute
#
__init__()
#
Initialize the per-sequence prediction buffers.
Source code in norfair/metrics.py
299 300 301 302 303 | |
create_accumulator(input_path, information_file=None)
#
Start collecting predictions for the sequence at input_path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_path
|
str
|
Path to the MOTChallenge sequence directory. |
required |
information_file
|
InformationFile
|
Pre-parsed |
None
|
Source code in norfair/metrics.py
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 | |
update(predictions=None)
#
Append predictions for the current frame and advance the bar.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predictions
|
iterable of TrackedObject
|
Objects tracked for the current frame. When |
None
|
Source code in norfair/metrics.py
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 | |
compute_metrics(metrics=None, generate_overall=True)
#
Compute MOTChallenge metrics over all collected sequences.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metrics
|
list of str
|
Subset of |
None
|
generate_overall
|
bool
|
If |
True
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Dataframe of per-sequence metrics (and overall if
requested). Also stored on |
Source code in norfair/metrics.py
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 | |
save_metrics(save_path='.', file_name='metrics.txt')
#
Write the rendered summary_text to save_path/file_name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
save_path
|
str
|
Directory where the metrics file is written. Created if it
does not exist. Defaults to |
'.'
|
file_name
|
str
|
Name of the output file. Defaults to |
'metrics.txt'
|
Source code in norfair/metrics.py
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 | |
print_metrics()
#
Print the rendered summary_text to stdout.
Source code in norfair/metrics.py
429 430 431 | |
load_motchallenge(matrix_data, min_confidence=-1)
#
Load MOTChallenge-formatted predictions from a numpy array.
Adapted from motmetrics.io.loadtxt but reading from an
in-memory array instead of a text file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix_data
|
ndarray
|
Float array whose rows contain
|
required |
min_confidence
|
float
|
Rows with |
-1
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Dataframe with columns |
Source code in norfair/metrics.py
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 | |
compare_dataframes(gts, ts)
#
Build a motmetrics accumulator per sequence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gts
|
dict of str to pandas.DataFrame
|
Mapping of sequence name to ground-truth dataframe. |
required |
ts
|
dict of str to pandas.DataFrame
|
Mapping of sequence name to tracker-output dataframe. |
required |
Returns:
| Type | Description |
|---|---|
tuple of (list, list)
|
|
Source code in norfair/metrics.py
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 | |
eval_motChallenge(matrixes_predictions, paths, metrics=None, generate_overall=True)
#
Evaluate tracker predictions against MOTChallenge ground truth.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrixes_predictions
|
list of np.ndarray
|
Per-sequence prediction arrays in the format accepted by
:func: |
required |
paths
|
sequence of str
|
Paths to the corresponding MOTChallenge sequence directories;
ground truth is read from |
required |
metrics
|
list of str
|
Metric names to compute. Defaults to the full MOTChallenge list. |
None
|
generate_overall
|
bool
|
If |
True
|
Returns:
| Type | Description |
|---|---|
tuple of (str, pandas.DataFrame)
|
|
Source code in norfair/metrics.py
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 | |