File size: 6,734 Bytes
47af768
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import pytest

import trackeval


def no_confusion():
    num_timesteps = 5
    num_gt_ids = 2
    num_tracker_ids = 2

    # No overlap between pairs (0, 0) and (1, 1).
    similarity = np.zeros([num_timesteps, num_gt_ids, num_tracker_ids])
    similarity[:, 0, 1] = [0, 0, 0, 1, 1]
    similarity[:, 1, 0] = [1, 1, 0, 0, 0]
    gt_present = np.zeros([num_timesteps, num_gt_ids])
    gt_present[:, 0] = [1, 1, 1, 1, 1]
    gt_present[:, 1] = [1, 1, 1, 0, 0]
    tracker_present = np.zeros([num_timesteps, num_tracker_ids])
    tracker_present[:, 0] = [1, 1, 1, 1, 0]
    tracker_present[:, 1] = [1, 1, 1, 1, 1]

    expected = {
            'clear': {
                    'CLR_TP': 4,
                    'CLR_FN': 4,
                    'CLR_FP': 5,
                    'IDSW': 0,
                    'MOTA': 1 - 9 / 8,
            },
            'identity': {
                    'IDTP': 4,
                    'IDFN': 4,
                    'IDFP': 5,
                    'IDR': 4 / 8,
                    'IDP': 4 / 9,
                    'IDF1': 2 * 4 / 17,
            },
            'vace': {
                    'STDA': 2 / 5 + 2 / 4,
                    'ATA': (2 / 5 + 2 / 4) / 2,
            },
    }

    data = _from_dense(
            num_timesteps=num_timesteps,
            num_gt_ids=num_gt_ids,
            num_tracker_ids=num_tracker_ids,
            gt_present=gt_present,
            tracker_present=tracker_present,
            similarity=similarity,
    )
    return data, expected


def with_confusion():
    num_timesteps = 5
    num_gt_ids = 2
    num_tracker_ids = 2

    similarity = np.zeros([num_timesteps, num_gt_ids, num_tracker_ids])
    similarity[:, 0, 1] = [0, 0, 0, 1, 1]
    similarity[:, 1, 0] = [1, 1, 0, 0, 0]
    # Add some overlap between (0, 0) and (1, 1).
    similarity[:, 0, 0] = [0, 0, 1, 0, 0]
    similarity[:, 1, 1] = [0, 1, 0, 0, 0]
    gt_present = np.zeros([num_timesteps, num_gt_ids])
    gt_present[:, 0] = [1, 1, 1, 1, 1]
    gt_present[:, 1] = [1, 1, 1, 0, 0]
    tracker_present = np.zeros([num_timesteps, num_tracker_ids])
    tracker_present[:, 0] = [1, 1, 1, 1, 0]
    tracker_present[:, 1] = [1, 1, 1, 1, 1]

    expected = {
            'clear': {
                    'CLR_TP': 5,
                    'CLR_FN': 3,  # 8 - 5
                    'CLR_FP': 4,  # 9 - 5
                    'IDSW': 1,
                    'MOTA': 1 - 8 / 8,
            },
            'identity': {
                    'IDTP': 4,
                    'IDFN': 4,
                    'IDFP': 5,
                    'IDR': 4 / 8,
                    'IDP': 4 / 9,
                    'IDF1': 2 * 4 / 17,
            },
            'vace': {
                    'STDA': 2 / 5 + 2 / 4,
                    'ATA': (2 / 5 + 2 / 4) / 2,
            },
    }

    data = _from_dense(
            num_timesteps=num_timesteps,
            num_gt_ids=num_gt_ids,
            num_tracker_ids=num_tracker_ids,
            gt_present=gt_present,
            tracker_present=tracker_present,
            similarity=similarity,
    )
    return data, expected


def split_tracks():
    num_timesteps = 5
    num_gt_ids = 2
    num_tracker_ids = 5

    similarity = np.zeros([num_timesteps, num_gt_ids, num_tracker_ids])
    # Split ground-truth 0 between tracks 0, 3.
    similarity[:, 0, 0] = [1, 1, 0, 0, 0]
    similarity[:, 0, 3] = [0, 0, 0, 1, 1]
    # Split ground-truth 1 between tracks 1, 2, 4.
    similarity[:, 1, 1] = [0, 0, 1, 1, 0]
    similarity[:, 1, 2] = [0, 0, 0, 0, 1]
    similarity[:, 1, 4] = [1, 1, 0, 0, 0]
    gt_present = np.zeros([num_timesteps, num_gt_ids])
    gt_present[:, 0] = [1, 1, 0, 1, 1]
    gt_present[:, 1] = [1, 1, 1, 1, 1]
    tracker_present = np.zeros([num_timesteps, num_tracker_ids])
    tracker_present[:, 0] = [1, 1, 0, 0, 0]
    tracker_present[:, 1] = [0, 0, 1, 1, 1]
    tracker_present[:, 2] = [0, 0, 0, 0, 1]
    tracker_present[:, 3] = [0, 0, 1, 1, 1]
    tracker_present[:, 4] = [1, 1, 0, 0, 0]

    expected = {
            'clear': {
                    'CLR_TP': 9,
                    'CLR_FN': 0,  # 9 - 9
                    'CLR_FP': 2,  # 11 - 9
                    'IDSW': 3,
                    'MOTA': 1 - 5 / 9,
            },
            'identity': {
                    'IDTP': 4,
                    'IDFN': 5,  # 9 - 4
                    'IDFP': 7,  # 11 - 4
                    'IDR': 4 / 9,
                    'IDP': 4 / 11,
                    'IDF1': 2 * 4 / 20,
            },
            'vace': {
                    'STDA': 2 / 4 + 2 / 5,
                    'ATA': (2 / 4 + 2 / 5) / (0.5 * (2 + 5)),
            },
    }

    data = _from_dense(
            num_timesteps=num_timesteps,
            num_gt_ids=num_gt_ids,
            num_tracker_ids=num_tracker_ids,
            gt_present=gt_present,
            tracker_present=tracker_present,
            similarity=similarity,
    )
    return data, expected


def _from_dense(num_timesteps, num_gt_ids, num_tracker_ids, gt_present, tracker_present, similarity):
    gt_subset = [np.flatnonzero(gt_present[t, :]) for t in range(num_timesteps)]
    tracker_subset = [np.flatnonzero(tracker_present[t, :]) for t in range(num_timesteps)]
    similarity_subset = [
            similarity[t][gt_subset[t], :][:, tracker_subset[t]]
            for t in range(num_timesteps)
    ]
    data = {
            'num_timesteps': num_timesteps,
            'num_gt_ids': num_gt_ids,
            'num_tracker_ids': num_tracker_ids,
            'num_gt_dets': np.sum(gt_present),
            'num_tracker_dets': np.sum(tracker_present),
            'gt_ids': gt_subset,
            'tracker_ids': tracker_subset,
            'similarity_scores': similarity_subset,
    }
    return data


METRICS_BY_NAME = {
        'clear': trackeval.metrics.CLEAR(),
        'identity': trackeval.metrics.Identity(),
        'vace': trackeval.metrics.VACE(),
}

SEQUENCE_BY_NAME = {
        'no_confusion': no_confusion(),
        'with_confusion': with_confusion(),
        'split_tracks': split_tracks(),
}


@pytest.mark.parametrize('sequence_name,metric_name', [
        ('no_confusion', 'clear'),
        ('no_confusion', 'identity'),
        ('no_confusion', 'vace'),
        ('with_confusion', 'clear'),
        ('with_confusion', 'identity'),
        ('with_confusion', 'vace'),
        ('split_tracks', 'clear'),
        ('split_tracks', 'identity'),
        ('split_tracks', 'vace'),
])
def test_metric(sequence_name, metric_name):
    data, expected = SEQUENCE_BY_NAME[sequence_name]
    metric = METRICS_BY_NAME[metric_name]
    result = metric.eval_sequence(data)
    for key, value in expected[metric_name].items():
        assert result[key] == pytest.approx(value), key