-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathutils.py
163 lines (130 loc) · 5.64 KB
/
utils.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
# py-motmetrics - Metrics for multiple object tracker (MOT) benchmarking.
# https://github.com/cheind/py-motmetrics/
#
# MIT License
# Copyright (c) 2017-2020 Christoph Heindl, Jack Valmadre and others.
# See LICENSE file for terms.
"""Functions for populating event accumulators."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
from motmetrics.distances import iou_matrix, norm2squared_matrix
from motmetrics.mot import MOTAccumulator
from motmetrics.preprocess import preprocessResult
def compare_to_groundtruth(gt, dt, dist='iou', distfields=None, distth=0.5, vflag=''):
"""Compare groundtruth and detector results.
This method assumes both results are given in terms of DataFrames with at least the following fields
- `FrameId` First level index used for matching ground-truth and test frames.
- `Id` Secondary level index marking available object / hypothesis ids
Depending on the distance to be used relevant distfields need to be specified.
Params
------
gt : pd.DataFrame
Dataframe for ground-truth
test : pd.DataFrame
Dataframe for detector results
Kwargs
------
dist : str, optional
String identifying distance to be used. Defaults to intersection over union.
distfields: array, optional
Fields relevant for extracting distance information. Defaults to ['X', 'Y', 'Width', 'Height']
distth: float, optional
Maximum tolerable distance. Pairs exceeding this threshold are marked 'do-not-pair'.
"""
# pylint: disable=too-many-locals
if distfields is None:
distfields = ['X', 'Y', 'Width', 'Height']
def compute_iou(a, b):
return iou_matrix(a, b, max_iou=distth)
def compute_euc(a, b):
return norm2squared_matrix(a, b, max_d2=distth)
compute_dist = compute_iou if dist.upper() == 'IOU' else compute_euc
acc = MOTAccumulator()
# We need to account for all frames reported either by ground truth or
# detector. In case a frame is missing in GT this will lead to FPs, in
# case a frame is missing in detector results this will lead to FNs.
allframeids = gt.index.union(dt.index).levels[0]
gt = gt[distfields]
dt = dt[distfields]
fid_to_fgt = dict(iter(gt.groupby('FrameId')))
fid_to_fdt = dict(iter(dt.groupby('FrameId')))
for fid in allframeids:
oids = np.empty(0)
hids = np.empty(0)
dists = np.empty((0, 0))
if fid in fid_to_fgt:
fgt = fid_to_fgt[fid]
oids = fgt.index.get_level_values('Id')
if fid in fid_to_fdt:
fdt = fid_to_fdt[fid]
hids = fdt.index.get_level_values('Id')
if len(oids) > 0 and len(hids) > 0:
dists = compute_dist(fgt.values, fdt.values)
acc.update(oids, hids, dists, frameid=fid, vf=vflag)
return acc
def CLEAR_MOT_M(gt, dt, inifile, dist='iou', distfields=None, distth=0.5, include_all=False, vflag=''):
"""Compare groundtruth and detector results.
This method assumes both results are given in terms of DataFrames with at least the following fields
- `FrameId` First level index used for matching ground-truth and test frames.
- `Id` Secondary level index marking available object / hypothesis ids
Depending on the distance to be used relevant distfields need to be specified.
Params
------
gt : pd.DataFrame
Dataframe for ground-truth
test : pd.DataFrame
Dataframe for detector results
Kwargs
------
dist : str, optional
String identifying distance to be used. Defaults to intersection over union.
distfields: array, optional
Fields relevant for extracting distance information. Defaults to ['X', 'Y', 'Width', 'Height']
distth: float, optional
Maximum tolerable distance. Pairs exceeding this threshold are marked 'do-not-pair'.
"""
# pylint: disable=too-many-locals
if distfields is None:
distfields = ['X', 'Y', 'Width', 'Height']
def compute_iou(a, b):
return iou_matrix(a, b, max_iou=distth)
def compute_euc(a, b):
return norm2squared_matrix(a, b, max_d2=distth)
compute_dist = compute_iou if dist.upper() == 'IOU' else compute_euc
acc = MOTAccumulator()
dt = preprocessResult(dt, gt, inifile)
if include_all:
gt = gt[gt['Confidence'] >= 0.99]
else:
gt = gt[(gt['Confidence'] >= 0.99) & (gt['ClassId'] == 1)]
# We need to account for all frames reported either by ground truth or
# detector. In case a frame is missing in GT this will lead to FPs, in
# case a frame is missing in detector results this will lead to FNs.
allframeids = gt.index.union(dt.index).levels[0]
analysis = {'hyp': {}, 'obj': {}}
for fid in allframeids:
oids = np.empty(0)
hids = np.empty(0)
dists = np.empty((0, 0))
if fid in gt.index:
fgt = gt.loc[fid]
oids = fgt.index.values
for oid in oids:
oid = int(oid)
if oid not in analysis['obj']:
analysis['obj'][oid] = 0
analysis['obj'][oid] += 1
if fid in dt.index:
fdt = dt.loc[fid]
hids = fdt.index.values
for hid in hids:
hid = int(hid)
if hid not in analysis['hyp']:
analysis['hyp'][hid] = 0
analysis['hyp'][hid] += 1
if oids.shape[0] > 0 and hids.shape[0] > 0:
dists = compute_dist(fgt[distfields].values, fdt[distfields].values)
acc.update(oids, hids, dists, frameid=fid, vf=vflag)
return acc, analysis