-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDcase21_metrics.py
279 lines (234 loc) · 13.5 KB
/
Dcase21_metrics.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
import numpy as np
eps = np.finfo(np.float).eps
from scipy.optimize import linear_sum_assignment
from IPython import embed
class SELDMetrics(object):
def __init__(self, doa_threshold=20, nb_classes=14):
'''
This class implements both the class-sensitive localization and location-sensitive detection metrics.
Additionally, based on the user input, the corresponding averaging is performed within the segment.
:param nb_classes: Number of sound classes. In the paper, nb_classes = 11
:param doa_thresh: DOA threshold for location sensitive detection.
'''
self._nb_classes = nb_classes
# Variables for Location-senstive detection performance
self._TP = 0
self._FP = 0
self._FN = 0
self._S = 0
self._D = 0
self._I = 0
self._Nref = 0
self._spatial_T = doa_threshold
# Variables for Class-sensitive localization performance
self._total_DE = 0
self._DE_TP = 0
self._DE_FP = 0
self._DE_FN = 0
def compute_seld_scores(self):
'''
Collect the final SELD scores
:return: returns both location-sensitive detection scores and class-sensitive localization scores
'''
# Location-sensitive detection performance
ER = (self._S + self._D + self._I) / float(self._Nref + eps)
F = self._TP / (eps + self._TP + 0.5 * (self._FP + self._FN))
# Class-sensitive localization performance
LE = self._total_DE / float(self._DE_TP + eps) if self._DE_TP else 180 # When the total number of prediction is zero
LR = self._DE_TP / (eps + self._DE_TP + self._DE_FN)
# print('S {}, D {}, I {}, Nref {}, TP {}, FP {}, FN {}, DE_TP {}, DE_FN {}, totalDE {}'.format(self._S, self._D, self._I, self._Nref, self._TP, self._FP, self._FN, self._DE_TP, self._DE_FN, self._total_DE))
return ER, F, LE, LR
def update_seld_scores(self, pred, gt):
'''
Implements the spatial error averaging according to equation 5 in the paper [1] (see papers in the title of the code).
Adds the multitrack extensions proposed in paper [2]
The input pred/gt can either both be Cartesian or Degrees
:param pred: dictionary containing class-wise prediction results for each N-seconds segment block
:param gt: dictionary containing class-wise groundtruth for each N-seconds segment block
'''
for block_cnt in range(len(gt.keys())):
loc_FN, loc_FP = 0, 0
for class_cnt in range(self._nb_classes):
# Counting the number of referece tracks for each class in the segment
nb_gt_doas = max([len(val) for val in gt[block_cnt][class_cnt][0][1]]) if class_cnt in gt[block_cnt] else None
nb_pred_doas = max([len(val) for val in pred[block_cnt][class_cnt][0][1]]) if class_cnt in pred[block_cnt] else None
if nb_gt_doas is not None:
self._Nref += nb_gt_doas
if class_cnt in gt[block_cnt] and class_cnt in pred[block_cnt]:
# True positives or False positive case
# NOTE: For multiple tracks per class, associate the predicted DOAs to corresponding reference
# DOA-tracks using hungarian algorithm and then compute the average spatial distance between
# the associated reference-predicted tracks.
# Reference and predicted track matching
matched_track_dist = {}
matched_track_cnt = {}
gt_ind_list = gt[block_cnt][class_cnt][0][0]
pred_ind_list = pred[block_cnt][class_cnt][0][0]
for gt_cnt, gt_ind in enumerate(gt_ind_list):
if gt_ind in pred_ind_list:
gt_arr = np.array(gt[block_cnt][class_cnt][0][1][gt_cnt])
gt_ids = np.arange(len(gt_arr[:, -1])) #TODO if the reference has track IDS use here - gt_arr[:, -1]
gt_doas = gt_arr[:, :-1]
pred_ind = pred_ind_list.index(gt_ind)
pred_arr = np.array(pred[block_cnt][class_cnt][0][1][pred_ind])
pred_doas = pred_arr[:, :-1]
if gt_doas.shape[-1] == 2: # convert DOAs to radians, if the input is in degrees
gt_doas = gt_doas * np.pi / 180.
pred_doas = pred_doas * np.pi / 180.
dist_list, row_inds, col_inds = least_distance_between_gt_pred(gt_doas, pred_doas)
# Collect the frame-wise distance between matched ref-pred DOA pairs
for dist_cnt, dist_val in enumerate(dist_list):
matched_gt_track = gt_ids[row_inds[dist_cnt]]
if matched_gt_track not in matched_track_dist:
matched_track_dist[matched_gt_track], matched_track_cnt[matched_gt_track] = [], []
matched_track_dist[matched_gt_track].append(dist_val)
matched_track_cnt[matched_gt_track].append(pred_ind)
# Update evaluation metrics based on the distance between ref-pred tracks
if len(matched_track_dist) == 0:
# if no tracks are found. This occurs when the predicted DOAs are not aligned frame-wise to the reference DOAs
loc_FN += nb_pred_doas
self._FN += nb_pred_doas
self._DE_FN += nb_pred_doas
else:
# for the associated ref-pred tracks compute the metrics
for track_id in matched_track_dist:
total_spatial_dist = sum(matched_track_dist[track_id])
total_framewise_matching_doa = len(matched_track_cnt[track_id])
avg_spatial_dist = total_spatial_dist / total_framewise_matching_doa
# Class-sensitive localization performance
self._total_DE += avg_spatial_dist
self._DE_TP += 1
# Location-sensitive detection performance
if avg_spatial_dist <= self._spatial_T:
self._TP += 1
else:
loc_FP += 1
self._FP += 1
# in the multi-instance of same class scenario, if the number of predicted tracks are greater
# than reference tracks count as FP, if it less than reference count as FN
if nb_pred_doas > nb_gt_doas:
# False positive
loc_FP += (nb_pred_doas-nb_gt_doas)
self._FP += (nb_pred_doas-nb_gt_doas)
self._DE_FP += (nb_pred_doas-nb_gt_doas)
elif nb_pred_doas < nb_gt_doas:
# False negative
loc_FN += (nb_gt_doas-nb_pred_doas)
self._FN += (nb_gt_doas-nb_pred_doas)
self._DE_FN += (nb_gt_doas-nb_pred_doas)
elif class_cnt in gt[block_cnt] and class_cnt not in pred[block_cnt]:
# False negative
loc_FN += nb_gt_doas
self._FN += nb_gt_doas
self._DE_FN += nb_gt_doas
elif class_cnt not in gt[block_cnt] and class_cnt in pred[block_cnt]:
# False positive
loc_FP += nb_pred_doas
self._FP += nb_pred_doas
self._DE_FP += nb_pred_doas
self._S += np.minimum(loc_FP, loc_FN)
self._D += np.maximum(0, loc_FN - loc_FP)
self._I += np.maximum(0, loc_FP - loc_FN)
return
def distance_between_spherical_coordinates_rad(az1, ele1, az2, ele2):
"""
Angular distance between two spherical coordinates
MORE: https://en.wikipedia.org/wiki/Great-circle_distance
:return: angular distance in degrees
"""
dist = np.sin(ele1) * np.sin(ele2) + np.cos(ele1) * np.cos(ele2) * np.cos(np.abs(az1 - az2))
# Making sure the dist values are in -1 to 1 range, else np.arccos kills the job
dist = np.clip(dist, -1, 1)
dist = np.arccos(dist) * 180 / np.pi
return dist
def distance_between_cartesian_coordinates(x1, y1, z1, x2, y2, z2):
"""
Angular distance between two cartesian coordinates
MORE: https://en.wikipedia.org/wiki/Great-circle_distance
Check 'From chord length' section
:return: angular distance in degrees
"""
# Normalize the Cartesian vectors
N1 = np.sqrt(x1**2 + y1**2 + z1**2 + 1e-10)
N2 = np.sqrt(x2**2 + y2**2 + z2**2 + 1e-10)
x1, y1, z1, x2, y2, z2 = x1/N1, y1/N1, z1/N1, x2/N2, y2/N2, z2/N2
#Compute the distance
dist = x1*x2 + y1*y2 + z1*z2
dist = np.clip(dist, -1, 1)
dist = np.arccos(dist) * 180 / np.pi
return dist
def least_distance_between_gt_pred(gt_list, pred_list):
"""
Shortest distance between two sets of DOA coordinates. Given a set of groundtruth coordinates,
and its respective predicted coordinates, we calculate the distance between each of the
coordinate pairs resulting in a matrix of distances, where one axis represents the number of groundtruth
coordinates and the other the predicted coordinates. The number of estimated peaks need not be the same as in
groundtruth, thus the distance matrix is not always a square matrix. We use the hungarian algorithm to find the
least cost in this distance matrix.
:param gt_list_xyz: list of ground-truth Cartesian or Polar coordinates in Radians
:param pred_list_xyz: list of predicted Carteisan or Polar coordinates in Radians
:return: cost - distance
:return: less - number of DOA's missed
:return: extra - number of DOA's over-estimated
"""
gt_len, pred_len = gt_list.shape[0], pred_list.shape[0]
ind_pairs = np.array([[x, y] for y in range(pred_len) for x in range(gt_len)])
cost_mat = np.zeros((gt_len, pred_len))
if gt_len and pred_len:
if len(gt_list[0]) == 3: #Cartesian
x1, y1, z1, x2, y2, z2 = gt_list[ind_pairs[:, 0], 0], gt_list[ind_pairs[:, 0], 1], gt_list[ind_pairs[:, 0], 2], pred_list[ind_pairs[:, 1], 0], pred_list[ind_pairs[:, 1], 1], pred_list[ind_pairs[:, 1], 2]
cost_mat[ind_pairs[:, 0], ind_pairs[:, 1]] = distance_between_cartesian_coordinates(x1, y1, z1, x2, y2, z2)
else:
az1, ele1, az2, ele2 = gt_list[ind_pairs[:, 0], 0], gt_list[ind_pairs[:, 0], 1], pred_list[ind_pairs[:, 1], 0], pred_list[ind_pairs[:, 1], 1]
cost_mat[ind_pairs[:, 0], ind_pairs[:, 1]] = distance_between_spherical_coordinates_rad(az1, ele1, az2, ele2)
row_ind, col_ind = linear_sum_assignment(cost_mat)
cost = cost_mat[row_ind, col_ind]
return cost, row_ind, col_ind
def early_stopping_metric(sed_error, doa_error):
"""
Compute early stopping metric from sed and doa errors.
:param sed_error: [error rate (0 to 1 range), f score (0 to 1 range)]
:param doa_error: [doa error (in degrees), frame recall (0 to 1 range)]
:return: early stopping metric result
"""
seld_metric = np.mean([
sed_error[0],
1 - sed_error[1],
doa_error[0]/180,
1 - doa_error[1]]
)
return seld_metric
def segment_labels(_pred_dict, _max_frames,_nb_label_frames_1s=10):
'''
Collects class-wise sound event location information in segments of length 1s from reference dataset
:param _pred_dict: Dictionary containing frame-wise sound event time and location information. Output of SELD method
:param _max_frames: Total number of frames in the recording
:return: Dictionary containing class-wise sound event location information in each segment of audio
dictionary_name[segment-index][class-index] = list(frame-cnt-within-segment, azimuth, elevation)
'''
nb_blocks = int(np.ceil(_max_frames/float(_nb_label_frames_1s)))
output_dict = {x: {} for x in range(nb_blocks)}
for frame_cnt in range(0, _max_frames, _nb_label_frames_1s):
# Collect class-wise information for each block
# [class][frame] = <list of doa values>
# Data structure supports multi-instance occurence of same class
block_cnt = frame_cnt // _nb_label_frames_1s
loc_dict = {}
for audio_frame in range(frame_cnt, frame_cnt+_nb_label_frames_1s):
if audio_frame not in _pred_dict:
continue
for value in _pred_dict[audio_frame]:
if value[0] not in loc_dict:
loc_dict[value[0]] = {}
block_frame = audio_frame - frame_cnt
if block_frame not in loc_dict[value[0]]:
loc_dict[value[0]][block_frame] = []
loc_dict[value[0]][block_frame].append(value[1:])
# Update the block wise details collected above in a global structure
for class_cnt in loc_dict:
if class_cnt not in output_dict[block_cnt]:
output_dict[block_cnt][class_cnt] = []
keys = [k for k in loc_dict[class_cnt]]
values = [loc_dict[class_cnt][k] for k in loc_dict[class_cnt]]
output_dict[block_cnt][class_cnt].append([keys, values])
return output_dict