-
Notifications
You must be signed in to change notification settings - Fork 22
/
cls_compute_seld_results.py
228 lines (186 loc) · 12.3 KB
/
cls_compute_seld_results.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
import os
import SELD_evaluation_metrics
import cls_feature_class
import parameters
import numpy as np
from scipy import stats
from IPython import embed
def jackknife_estimation(global_value, partial_estimates, significance_level=0.05):
"""
Compute jackknife statistics from a global value and partial estimates.
Original function by Nicolas Turpault
:param global_value: Value calculated using all (N) examples
:param partial_estimates: Partial estimates using N-1 examples at a time
:param significance_level: Significance value used for t-test
:return:
estimate: estimated value using partial estimates
bias: Bias computed between global value and the partial estimates
std_err: Standard deviation of partial estimates
conf_interval: Confidence interval obtained after t-test
"""
mean_jack_stat = np.mean(partial_estimates)
n = len(partial_estimates)
bias = (n - 1) * (mean_jack_stat - global_value)
std_err = np.sqrt(
(n - 1) * np.mean((partial_estimates - mean_jack_stat) * (partial_estimates - mean_jack_stat), axis=0)
)
# bias-corrected "jackknifed estimate"
estimate = global_value - bias
# jackknife confidence interval
if not (0 < significance_level < 1):
raise ValueError("confidence level must be in (0, 1).")
t_value = stats.t.ppf(1 - significance_level / 2, n - 1)
# t-test
conf_interval = estimate + t_value * np.array((-std_err, std_err))
return estimate, bias, std_err, conf_interval
class ComputeSELDResults(object):
def __init__(
self, params, ref_files_folder=None, use_polar_format=True
):
self._use_polar_format = use_polar_format
self._desc_dir = ref_files_folder if ref_files_folder is not None else os.path.join(params['dataset_dir'], 'metadata_dev')
self._doa_thresh = params['lad_doa_thresh']
# Load feature class
self._feat_cls = cls_feature_class.FeatureClass(params)
# collect reference files
self._ref_labels = {}
for split in os.listdir(self._desc_dir):
for ref_file in os.listdir(os.path.join(self._desc_dir, split)):
# Load reference description file
gt_dict = self._feat_cls.load_output_format_file(os.path.join(self._desc_dir, split, ref_file))
if not self._use_polar_format:
gt_dict = self._feat_cls.convert_output_format_polar_to_cartesian(gt_dict)
nb_ref_frames = max(list(gt_dict.keys()))
self._ref_labels[ref_file] = [self._feat_cls.segment_labels(gt_dict, nb_ref_frames), nb_ref_frames]
self._nb_ref_files = len(self._ref_labels)
self._average = params['average']
@staticmethod
def get_nb_files(file_list, tag='all'):
'''
Given the file_list, this function returns a subset of files corresponding to the tag.
Tags supported
'all' -
'ir'
:param file_list: complete list of predicted files
:param tag: Supports two tags 'all', 'ir'
:return: Subset of files according to chosen tag
'''
_group_ind = {'room': 10}
_cnt_dict = {}
for _filename in file_list:
if tag == 'all':
_ind = 0
else:
_ind = int(_filename[_group_ind[tag]])
if _ind not in _cnt_dict:
_cnt_dict[_ind] = []
_cnt_dict[_ind].append(_filename)
return _cnt_dict
def get_SELD_Results(self, pred_files_path, is_jackknife=False):
# collect predicted files info
pred_files = os.listdir(pred_files_path)
pred_labels_dict = {}
eval = SELD_evaluation_metrics.SELDMetrics(nb_classes=self._feat_cls.get_nb_classes(), doa_threshold=self._doa_thresh, average=self._average)
for pred_cnt, pred_file in enumerate(pred_files):
# Load predicted output format file
pred_dict = self._feat_cls.load_output_format_file(os.path.join(pred_files_path, pred_file))
if self._use_polar_format:
pred_dict = self._feat_cls.convert_output_format_cartesian_to_polar(pred_dict)
pred_labels = self._feat_cls.segment_labels(pred_dict, self._ref_labels[pred_file][1])
# Calculated scores
eval.update_seld_scores(pred_labels, self._ref_labels[pred_file][0])
if is_jackknife:
pred_labels_dict[pred_file] = pred_labels
# Overall SED and DOA scores
ER, F, LE, LR, seld_scr, classwise_results = eval.compute_seld_scores()
if is_jackknife:
global_values = [ER, F, LE, LR, seld_scr]
if len(classwise_results):
global_values.extend(classwise_results.reshape(-1).tolist())
partial_estimates = []
# Calculate partial estimates by leave-one-out method
for leave_file in pred_files:
leave_one_out_list = pred_files[:]
leave_one_out_list.remove(leave_file)
eval = SELD_evaluation_metrics.SELDMetrics(nb_classes=self._feat_cls.get_nb_classes(), doa_threshold=self._doa_thresh, average=self._average)
for pred_cnt, pred_file in enumerate(leave_one_out_list):
# Calculated scores
eval.update_seld_scores(pred_labels_dict[pred_file], self._ref_labels[pred_file][0])
ER, F, LE, LR, seld_scr, classwise_results = eval.compute_seld_scores()
leave_one_out_est = [ER, F, LE, LR, seld_scr]
if len(classwise_results):
leave_one_out_est.extend(classwise_results.reshape(-1).tolist())
# Overall SED and DOA scores
partial_estimates.append(leave_one_out_est)
partial_estimates = np.array(partial_estimates)
estimate, bias, std_err, conf_interval = [-1]*len(global_values), [-1]*len(global_values), [-1]*len(global_values), [-1]*len(global_values)
for i in range(len(global_values)):
estimate[i], bias[i], std_err[i], conf_interval[i] = jackknife_estimation(
global_value=global_values[i],
partial_estimates=partial_estimates[:, i],
significance_level=0.05
)
return [ER, conf_interval[0]], [F, conf_interval[1]], [LE, conf_interval[2]], [LR, conf_interval[3]], [seld_scr, conf_interval[4]], [classwise_results, np.array(conf_interval)[5:].reshape(5,13,2) if len(classwise_results) else []]
else:
return ER, F, LE, LR, seld_scr, classwise_results
def get_consolidated_SELD_results(self, pred_files_path, score_type_list=['all', 'room']):
'''
Get all categories of results.
;score_type_list: Supported
'all' - all the predicted files
'room' - for individual rooms
'''
# collect predicted files info
pred_files = os.listdir(pred_files_path)
nb_pred_files = len(pred_files)
# Calculate scores for different splits, overlapping sound events, and impulse responses (reverberant scenes)
print('Number of predicted files: {}\nNumber of reference files: {}'.format(nb_pred_files, self._nb_ref_files))
print('\nCalculating {} scores for {}'.format(score_type_list, os.path.basename(pred_output_format_files)))
for score_type in score_type_list:
print('\n\n---------------------------------------------------------------------------------------------------')
print('------------------------------------ {} ---------------------------------------------'.format('Total score' if score_type=='all' else 'score per {}'.format(score_type)))
print('---------------------------------------------------------------------------------------------------')
split_cnt_dict = self.get_nb_files(pred_files, tag=score_type) # collect files corresponding to score_type
# Calculate scores across files for a given score_type
for split_key in np.sort(list(split_cnt_dict)):
# Load evaluation metric class
eval = SELD_evaluation_metrics.SELDMetrics(nb_classes=self._feat_cls.get_nb_classes(), doa_threshold=self._doa_thresh, average=self._average)
for pred_cnt, pred_file in enumerate(split_cnt_dict[split_key]):
# Load predicted output format file
pred_dict = self._feat_cls.load_output_format_file(os.path.join(pred_output_format_files, pred_file))
if self._use_polar_format:
pred_dict = self._feat_cls.convert_output_format_cartesian_to_polar(pred_dict)
pred_labels = self._feat_cls.segment_labels(pred_dict, self._ref_labels[pred_file][1])
# Calculated scores
eval.update_seld_scores(pred_labels, self._ref_labels[pred_file][0])
# Overall SED and DOA scores
ER, F, LE, LR, seld_scr, classwise_results = eval.compute_seld_scores()
print('\nAverage score for {} {} data using {} coordinates'.format(score_type, 'fold' if score_type=='all' else split_key, 'Polar' if self._use_polar_format else 'Cartesian' ))
print('SELD score (early stopping metric): {:0.2f}'.format(seld_scr))
print('SED metrics: Error rate: {:0.2f}, F-score:{:0.1f}'.format(ER, 100*F))
print('DOA metrics: Localization error: {:0.1f}, Localization Recall: {:0.1f}'.format(LE, 100*LR))
def reshape_3Dto2D(A):
return A.reshape(A.shape[0] * A.shape[1], A.shape[2])
if __name__ == "__main__":
pred_output_format_files = 'results/3_11553814_dev_split0_multiaccdoa_foa_20220429142557_test' # Path of the DCASEoutput format files
params = parameters.get_params()
# Compute just the DCASE final results
score_obj = ComputeSELDResults(params)
use_jackknife=False
ER, F, LE, LR, seld_scr, classwise_test_scr = score_obj.get_SELD_Results(pred_output_format_files,is_jackknife=use_jackknife )
print('SELD score (early stopping metric): {:0.2f} {}'.format(seld_scr[0] if use_jackknife else seld_scr, '[{:0.2f}, {:0.2f}]'.format(seld_scr[1][0], seld_scr[1][1]) if use_jackknife else ''))
print('SED metrics: Error rate: {:0.2f} {}, F-score: {:0.1f} {}'.format(ER[0] if use_jackknife else ER, '[{:0.2f}, {:0.2f}]'.format(ER[1][0], ER[1][1]) if use_jackknife else '', 100*F[0] if use_jackknife else 100*F, '[{:0.2f}, {:0.2f}]'.format(100*F[1][0], 100*F[1][1]) if use_jackknife else ''))
print('DOA metrics: Localization error: {:0.1f} {}, Localization Recall: {:0.1f} {}'.format(LE[0] if use_jackknife else LE, '[{:0.2f}, {:0.2f}]'.format(LE[1][0], LE[1][1]) if use_jackknife else '', 100*LR[0] if use_jackknife else 100*LR,'[{:0.2f}, {:0.2f}]'.format(100*LR[1][0], 100*LR[1][1]) if use_jackknife else ''))
if params['average']=='macro':
print('Classwise results on unseen test data')
print('Class\tER\tF\tLE\tLR\tSELD_score')
for cls_cnt in range(params['unique_classes']):
print('{}\t{:0.2f} {}\t{:0.2f} {}\t{:0.2f} {}\t{:0.2f} {}\t{:0.2f} {}'.format(
cls_cnt,
classwise_test_scr[0][0][cls_cnt] if use_jackknife else classwise_test_scr[0][cls_cnt], '[{:0.2f}, {:0.2f}]'.format(classwise_test_scr[1][0][cls_cnt][0], classwise_test_scr[1][0][cls_cnt][1]) if use_jackknife else '',
classwise_test_scr[0][1][cls_cnt] if use_jackknife else classwise_test_scr[1][cls_cnt], '[{:0.2f}, {:0.2f}]'.format(classwise_test_scr[1][1][cls_cnt][0], classwise_test_scr[1][1][cls_cnt][1]) if use_jackknife else '',
classwise_test_scr[0][2][cls_cnt] if use_jackknife else classwise_test_scr[2][cls_cnt], '[{:0.2f}, {:0.2f}]'.format(classwise_test_scr[1][2][cls_cnt][0], classwise_test_scr[1][2][cls_cnt][1]) if use_jackknife else '',
classwise_test_scr[0][3][cls_cnt] if use_jackknife else classwise_test_scr[3][cls_cnt], '[{:0.2f}, {:0.2f}]'.format(classwise_test_scr[1][3][cls_cnt][0], classwise_test_scr[1][3][cls_cnt][1]) if use_jackknife else '',
classwise_test_scr[0][4][cls_cnt] if use_jackknife else classwise_test_scr[4][cls_cnt], '[{:0.2f}, {:0.2f}]'.format(classwise_test_scr[1][4][cls_cnt][0], classwise_test_scr[1][4][cls_cnt][1]) if use_jackknife else ''))
# UNCOMMENT to Compute DCASE results along with room-wise performance
# score_obj.get_consolidated_SELD_results(pred_output_format_files)