-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtask3_sound_event_detection_in_real_life_audio.m
987 lines (882 loc) · 41.8 KB
/
task3_sound_event_detection_in_real_life_audio.m
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
280
281
282
283
284
285
286
287
288
289
290
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
432
433
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
482
483
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
512
513
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
function task3_real_life_audio_sound_event_detection(varargin)
% DCASE 2016
% Task 3: Sound Event Detection in Real-life Audio
% Baseline System
% ---------------------------------------------
% Tampere University of Technology / Audio Research Group
% Author: Toni Heittola ( toni.heittola@tut.fi )
%
% System description
% This is an baseline implementation for the D-CASE 2016, task 3 - Sound event detection in real-life audio.
% The system has binary classifier for each included sound event class. The GMM classifier is trained with
% the positive and negative examples from the mixture signals, and classification is done between these
% two models as likelihood ratio. Acoustic features are MFCC+Delta+Acceleration (MFCC0 omitted).
%
%
download_external_libraries % Download external libraries
add_paths; % Add file paths
rng(123456); % let's make randomization predictable
parser = inputParser;
parser.addOptional('mode','development',@isstr);
parse(parser,varargin{:});
params = load_parameters('task3_sound_event_detection_in_real_life_audio.yaml');
params = process_parameters(params);
title('DCASE 2016::Sound Event Detection in Real-life Audio / Baseline System');
% Check if mode is defined
if(strcmp(parser.Results.mode,'development')),
args.development = true;
args.challenge = false;
elseif(strcmp(parser.Results.mode,'challenge')),
args.development = false;
args.challenge = true;
end
dataset_evaluation_mode = 'folds';
if(args.development && ~args.challenge),
disp('Running system in development mode');
dataset_evaluation_mode = 'folds';
elseif(~args.development && args.challenge),
disp('Running system in challenge mode');
dataset_evaluation_mode = 'full';
end
% Get dataset container class
if strcmp(params.general.development_dataset,'TUTSoundEvents_2016_DevelopmentSet')
dataset = TUTSoundEvents_2016_DevelopmentSet(params.path.data);
else
error(['Unknown development dataset [', params.general.development_dataset, ']']);
end
% Fetch data over internet and setup the data
% ==================================================
if params.flow.initialize
dataset.fetch();
end
% Extract features for all audio files in the dataset
% ==================================================
if params.flow.extract_features
section_header('Feature extraction');
% Collect files from evalaution sets
files = [];
for fold=dataset.folds(dataset_evaluation_mode)
train_items = dataset.train(fold);
for item_id=1:length(train_items)
item = train_items(item_id);
if sum(strcmp(item.file,files)) == 0
files = [files, {item.file}];
end
end
test_items = dataset.test(fold);
for item_id=1:length(test_items)
item = test_items(item_id);
if sum(strcmp(item.file,files)) == 0
files = [files, {item.file}];
end
end
end
files = sort(files);
% Go through files and make sure all features are extracted
do_feature_extraction(files, ...
dataset, ...
params.path.features, ...
params.features, ...
params.general.overwrite);
foot();
end
% Prepare feature normalizers
% ==================================================
if params.flow.feature_normalizer
section_header('Feature normalizer');
do_feature_normalization(dataset,...
params.path.feature_normalizers,...
params.path.features,...
dataset_evaluation_mode,...
params.general.overwrite);
foot();
end
% System training
% ==================================================
if params.flow.train_system
section_header('System training');
do_system_training(dataset,...
params.path.models,...
params.path.feature_normalizers,...
params.path.features,...
params.features.hop_length_seconds,...
params.classifier.parameters,...
dataset_evaluation_mode,...
params.classifier.method,...
params.general.overwrite);
foot();
end
% System evaluation in development mode
if(args.development && ~args.challenge)
% System testing
% ==================================================
if params.flow.test_system
section_header('System testing [Development data]');
do_system_testing(dataset,...
params.path.features,...
params.path.results,...
params.path.models,...
params.features,...
params.detector,...
dataset_evaluation_mode,...
params.classifier.method,...
params.general.overwrite);
foot();
end
% System evaluation
% ==================================================
if params.flow.evaluate_system
section_header('System evaluation');
do_system_evaluation(dataset,...
dataset_evaluation_mode,...
params.path.results);
foot();
end
% System evaluation with challenge data
elseif(~args.development && args.challenge)
% Get dataset container class
if strcmp(params.general.challenge_dataset, 'TUTSoundEvents_2016_EvaluationSet')
challenge_dataset = TUTSoundEvents_2016_EvaluationSet(params.path.data);
else
error(['Unknown development dataset [', params.general.evaluation_dataset, ']']);
end
if params.flow.initialize
challenge_dataset.fetch();
end
% System testing
if params.flow.test_system
section_header('System testing [Challenge data]');
do_system_testing(challenge_dataset,...
params.path.features,...
params.path.challenge_results,...
params.path.models,...
params.features,...
params.detector,...
dataset_evaluation_mode,...
params.classifier.method,...
1);
foot();
disp(' ');
disp(['Your results for the challenge data are stored at [',params.path.challenge_results,']']);
disp(' ');
end
end
end
function params = process_parameters(params)
% Parameter post-processing.
%
% Parameters
% ----------
% params : struct
% parameters in struct
%
% Returns
% -------
% params : struct
% processed parameters
%
params.features.mfcc.win_length_seconds = params.features.win_length_seconds;
params.features.mfcc.hop_length_seconds = params.features.hop_length_seconds;
params.features.mfcc.win_length = round(params.features.win_length_seconds * params.features.fs);
params.features.mfcc.hop_length = round(params.features.hop_length_seconds * params.features.fs);
params.classifier.parameters = getfield(params.classifier_parameters,params.classifier.method);
params.features.hash = get_parameter_hash(params.features);
params.classifier.hash = get_parameter_hash(params.classifier);
params.detector.hash = get_parameter_hash(params.detector);
params.path.features = fullfile(params.path.base, params.path.features,params.features.hash);
params.path.feature_normalizers = fullfile(params.path.base, params.path.feature_normalizers,params.features.hash);
params.path.models = fullfile(params.path.base, params.path.models,params.features.hash, params.classifier.hash);
params.path.results = fullfile(params.path.base, params.path.results, params.features.hash, params.classifier.hash, params.detector.hash);
end
function filename = get_feature_filename(audio_file, path, extension)
% Get feature filename
%
% Parameters
% ----------
% audio_file : str
% audio file name from which the features are extracted
%
% path : str
% feature path
%
% extension : str
% file extension
% (Default value='mat')
%
% Returns
% -------
% feature_filename : str
% full feature filename
%
%
if nargin < 3
extension = 'mat';
end
[~, raw_filename, ext] = fileparts(audio_file);
filename = fullfile(path, ['sequence_',raw_filename,'.',extension]);
end
function filename = get_feature_normalizer_filename(fold, scene_label, path, extension)
% Get normalizer filename
%
% Parameters
% ----------
% fold : int >= 0
% evaluation fold number
%
% path : str
% normalizer path
%
% extension : str
% file extension
% (Default value='mat')
%
% Returns
% -------
% normalizer_filename : str
% full normalizer filename
%
%
if nargin < 4
extension = 'mat';
end
filename = fullfile(path, ['scale_fold',num2str(fold),'_',scene_label,'.',extension]);
end
function filename = get_model_filename(fold, scene_label, path, extension)
% Get model filename
%
% Parameters
% ----------
% fold : int >= 0
% evaluation fold number
%
% path : str
% model path
%
% extension : str
% file extension
% (Default value='mat')
%
% Returns
% -------
% model_filename : str
% full model filename
%
%
if nargin < 4
extension = 'mat';
end
filename = fullfile(path, ['model_fold',num2str(fold),'_',scene_label,'.',extension]);
end
function filename = get_result_filename(fold, scene_label, path, extension)
% Get result filename
%
% Parameters
% ----------
% fold : int >= 0
% evaluation fold number
%
% path : str
% result path
%
% extension : str
% file extension
% (Default value='mat')
%
% Returns
% -------
% result_filename : str
% full result filename
%
if nargin < 4
extension = 'txt';
end
if(fold == 0)
filename = fullfile(path, ['results_',scene_label,'.',extension]);
else
filename = fullfile(path, ['results_fold',num2str(fold),'_',scene_label,'.',extension]);
end
end
function do_feature_extraction(files, dataset, feature_path, params, overwrite)
% Feature extraction
%
% Parameters
% ----------
% files : cell array
% file list
%
% dataset : class
% dataset class
%
% feature_path : str
% path where the features are saved
%
% params : struct
% parameter dict
%
% overwrite : bool
% overwrite existing feature files
%
% Returns
% -------
% nothing
%
% Raises
% -------
% error
% Audio file not found.
%
%
% Check that target path exists, create if not
check_path(feature_path);
progress(1,'Extracting [sequences]',(0 / length(files)),'');
for file_id = 1:length(files)
audio_filename = files{file_id};
[raw_path, raw_filename, ext] = fileparts(audio_filename);
current_feature_file = get_feature_filename(audio_filename,feature_path);
progress(0,'Extracting [sequences]', (file_id / length(files)), raw_filename)
if or(~exist(current_feature_file,'file'),overwrite)
% Load audio data
if exist(dataset.relative_to_absolute_path(audio_filename),'file')
[y, fs] = load_audio(dataset.relative_to_absolute_path(audio_filename), 'mono', true, 'fs', params.fs);
else
error(['Audio file not found [',audio_filename,']']);
end
% Extract features
feature_data = feature_extraction(y,fs,...
'statistics',true,...
'include_mfcc0',params.include_mfcc0,...
'include_delta',params.include_delta,...
'include_acceleration',params.include_acceleration,...
'mfcc_params',params.mfcc,...
'delta_params',params.mfcc_delta,...
'acceleration_params', params.mfcc_acceleration);
% Save
save_data(current_feature_file, feature_data)
end
end
disp(' ');
end
function do_feature_normalization(dataset, feature_normalizer_path, feature_path, dataset_evaluation_mode, overwrite)
% Feature normalization
%
% Calculated normalization factors for each evaluation fold based on the training material available.
%
% Parameters
% ----------
% dataset : class
% dataset class
%
% feature_normalizer_path : str
% path where the feature normalizers are saved.
%
% feature_path : str
% path where the features are saved.
%
% dataset_evaluation_mode : str ['folds', 'full']
% evaluation mode, 'full' all material available is considered to belong to one fold.
%
% overwrite : bool
% overwrite existing normalizers
%
% Returns
% -------
% nothing
%
% Raises
% -------
% error
% Features not found.
%
% Check that target path exists, create if not
check_path(feature_normalizer_path);
progress(1,'Collecting data',0,'');
for fold=dataset.folds(dataset_evaluation_mode)
scene_labels = dataset.scene_labels();
for scene_id=1:length(scene_labels)
scene_label = scene_labels{scene_id};
current_normalizer_file = get_feature_normalizer_filename(fold, scene_label, feature_normalizer_path);
if or(~exist(current_normalizer_file,'file'),overwrite)
% Collect sequence files from scene class
files = [];
train_items = dataset.train(fold, 'scene_label',scene_label);
for item_id=1:length(train_items)
files = [files; {train_items(item_id).file}];
end
files = unique(files);
file_count = length(files);
% Initialize statistics
normalizer = FeatureNormalizer();
for file_id=1:length(files)
audio_filename = files{file_id};
progress(0,'Collecting data', (file_id / length(files)), audio_filename, fold);
% Load features
feature_filename = get_feature_filename(audio_filename, feature_path);
if exist(feature_filename,'file')
feature_data = load_data(feature_filename);
feature_data = feature_data.stat;
else
error(['Features not found [', item.file, ']']);
end
% Accumulate statistics
normalizer.accumulate(feature_data);
end
% Calculate normalization factors
normalizer.finalize();
% Save
save_data(current_normalizer_file, normalizer);
end
end
end
disp(' ');
end
function do_system_training(dataset, model_path, feature_normalizer_path, feature_path, hop_length_seconds, classifier_params, dataset_evaluation_mode, classifier_method, overwrite)
% System training
%
% model container format (struct):
% model.normalizer = normalizer_class;
% model.models = containers.Map();
% model.models(scene_label) = [model_struct (positive model), model_struct (negative model)];
%
% Parameters
% ----------
% dataset : class
% dataset class
%
% model_path : str
% path where the models are saved.
%
% feature_normalizer_path : str
% path where the feature normalizers are saved.
%
% feature_path : str
% path where the features are saved.
%
% hop_length_seconds : float > 0
% feature frame hop length in seconds
%
% classifier_params : struct
% parameter struct
%
% dataset_evaluation_mode : str ['folds', 'full']
% evaluation mode, 'full' all material available is considered to belong to one fold.
%
% classifier_method : str ['gmm']
% classifier method, currently only GMM supported
%
% overwrite : bool
% overwrite existing models
%
% Returns
% -------
% nothing
%
% Raises
% -------
% error
% classifier_method is unknown.
% Feature normalizer not found.
% Feature file not found.
%
if ~strcmp(classifier_method, 'gmm')
disp(['Unknown classifier method [', classifier_method, ']']);
end
% Check that target path exists, create if not
check_path(model_path);
progress(1, 'Collecting data', 0, '');
for fold=dataset.folds(dataset_evaluation_mode)
scene_labels = dataset.scene_labels();
for scene_id=1:length(scene_labels)
scene_label = scene_labels{scene_id};
current_model_file = get_model_filename(fold, scene_label, model_path);
if or(~exist(current_model_file, 'file'),overwrite)
% Load normalizer
feature_normalizer_filename = get_feature_normalizer_filename(fold, scene_label, feature_normalizer_path);
if exist(feature_normalizer_filename, 'file')
normalizer = load_data(feature_normalizer_filename);
else
error(['Feature normalizer not found [', feature_normalizer_filename, ']']);
end
% Initialize model container
model_container = struct('normalizer', normalizer, 'models',containers.Map());
train_items = dataset.train(fold, 'scene_label', scene_label);
% Restructure training data in to structure[files][events]
ann = containers.Map();
for item_id=1:length(train_items)
item = train_items(item_id);
[~, name, ext] = fileparts(item.file);
key = name;
if ~ann.isKey(key)
ann(key) = [];
end
ann(key) = [ann(key); {item.event_label, item.file, item.event_onset, item.event_offset}];
end
% Collect training examples
data_positive = containers.Map();
data_negative = containers.Map();
keys = ann.keys;
for item_id=1:length(ann)
list = ann(keys{item_id});
events = unique(list(:,1));
file = list{1,2};
progress(0,'Collecting data',(item_id / length(ann)),[scene_label,' / ',file],fold);
% Load features
feature_filename = get_feature_filename(file, feature_path);
if exist(feature_filename,'file')
feature_data = load_data(feature_filename);
feature_data = feature_data.feat;
else
error(['Features not found [', file, ']']);
end
% Normalize features
feature_data = model_container.normalizer.normalize(feature_data);
for event_id = 1:length(events)
event_label = events{event_id};
positive_mask = false(size(feature_data,2),1);
for i=1:size(list,1)
event = list(i,:);
if(strcmp(event{1},event_label))
start_frame = floor(event{3} / hop_length_seconds)+1;
stop_frame = ceil(event{4} / hop_length_seconds)+1;
if stop_frame > size(feature_data,2)
stop_frame = size(feature_data,2);
end
positive_mask(start_frame:stop_frame) = 1;
end
end
% Store positive examples
if ~data_positive.isKey(event_label)
data_positive(event_label) = feature_data(:,positive_mask);
else
data_positive(event_label) = [data_positive(event_label), feature_data(:,positive_mask)];
end
% Store negative examples
if ~data_negative.isKey(event_label)
data_negative(event_label) = feature_data(:,~positive_mask);
else
data_negative(event_label) = [data_negative(event_label), feature_data(:,~positive_mask)];
end
end
end
% Train models for each class
label_id = 1;
for event_label=data_positive.keys
progress(0,'Train models',(label_id / length(data_positive.keys)),[scene_label,' / ',char(event_label)],fold);
if strcmp(classifier_method,'gmm')
[positive_gmm.mu,...
positive_gmm.Sigma,...
positive_gmm.w,...
positive_gmm.avglogl,...
positive_gmm.f,...
positive_gmm.normlogl,...
positive_gmm.avglogl_iter]=gaussmix(data_positive(char(event_label))',...
[],...
classifier_params.n_iter+classifier_params.min_covar,...
classifier_params.n_components,...
'hf');
[negative_gmm.mu,...
negative_gmm.Sigma,...
negative_gmm.w,...
negative_gmm.avglogl,...
negative_gmm.f,...
negative_gmm.normlogl,...
negative_gmm.avglogl_iter]=gaussmix(data_negative(char(event_label))',...
[],...
classifier_params.n_iter+classifier_params.min_covar,...
classifier_params.n_components,...
'hf');
model_container.models(char(event_label)) = [positive_gmm, negative_gmm];
end
label_id = label_id + 1;
end
% Save models
save_data(current_model_file, model_container);
end
end
end
disp(' ');
end
function do_system_testing(dataset, feature_path, result_path, model_path, feature_params, detector_params, dataset_evaluation_mode, classifier_method, overwrite)
% System testing.
%
% If extracted features are not found from disk, they are extracted but not saved.
%
% Parameters
% ----------
% dataset : class
% dataset class
%
% result_path : str
% path where the results are saved.
%
% feature_path : str
% path where the features are saved.
%
% model_path : str
% path where the models are saved.
%
% feature_params : struct
% parameter struct
%
% dataset_evaluation_mode : str ['folds', 'full']
% evaluation mode, 'full' all material available is considered to belong to one fold.
%
% classifier_method : str ['gmm']
% classifier method, currently only GMM supported
%
% overwrite : bool
% overwrite existing models
%
% Returns
% -------
% nothing
%
% Raises
% -------
% error
% classifier_method is unknown.
% Model file not found.
% Audio file not found.
%
if ~strcmp(classifier_method, 'gmm')
error(['Unknown classifier method [', classifier_method, ']']);
end
% Check that target path exists, create if not
check_path(result_path);
progress(1, 'Testing', 0, '');
for fold=dataset.folds(dataset_evaluation_mode)
scene_labels = dataset.scene_labels();
for scene_id=1:length(scene_labels)
scene_label = scene_labels{scene_id};
current_result_file = get_result_filename(fold, scene_label, result_path);
if or(~exist(current_result_file,'file'),overwrite)
results = [];
% Load class model container
model_filename = get_model_filename(fold, scene_label, model_path);
if exist(model_filename,'file')
model_container = load_data(model_filename);
else
error(['Model file not found [',model_filename,']']);
end
test_items = dataset.test(fold, 'scene_label', scene_label);
for item_id=1:length(test_items)
item = test_items(item_id);
progress(0, 'Testing', (item_id / length(test_items)), [scene_label,' / ',item.file], fold);
% Load features
feature_filename = get_feature_filename(item.file, feature_path);
if exist(feature_filename, 'file')
feature_data = load_data(feature_filename);
feature_data = feature_data.feat;
else
% Load audio
if exist(dataset.relative_to_absolute_path(item.file),'file')
[y, fs] = load_audio(dataset.relative_to_absolute_path(item.file), 'mono', true, 'target_fs', feature_params.fs);
else
error(['Audio file not found [', item.file, ']']);
end
feature_data = feature_extraction(y,...
fs,...
'statistics',false,...
'include_mfcc0',feature_params.include_mfcc0,...
'include_delta',feature_params.include_delta,...
'include_acceleration',feature_params.include_acceleration,...
'mfcc_params',feature_params.mfcc,...
'delta_params',feature_params.mfcc_delta,...
'acceleration_params', feature_params.mfcc_acceleration);
feature_data = feature_data.feat;
end
% Normalize features
feature_data = model_container.normalizer.normalize(feature_data);
current_results = event_detection(feature_data,...
model_container,...
'hop_length_seconds',feature_params.hop_length_seconds,...
'smoothing_window_length_seconds',detector_params.smoothing_window_length,...
'decision_threshold',detector_params.decision_threshold,...
'minimum_event_length',detector_params.minimum_event_length,...
'minimum_event_gap',detector_params.minimum_event_gap);
% Store the result
for event_id=1:size(current_results,1)
results = [results; {dataset.absolute_to_relative(item.file), current_results{event_id,1},current_results{event_id,2},current_results{event_id,3}}];
end
end
% Save testing results
fid = fopen(current_result_file, 'wt');
for result_id=1:size(results,1)
result_item = results(result_id,:);
fprintf(fid,'%s\t%5.2f\t%5.2f\t%s\n',result_item{1},result_item{2},result_item{3},result_item{4});
end
fclose(fid);
end
end
end
disp(' ');
end
function do_system_evaluation(dataset, dataset_evaluation_mode, result_path)
% System evaluation. Testing outputs are collected and evaluated. Evaluation results are printed.
%
% Parameters
% ----------
% dataset : class
% dataset class
%
% result_path : str
% path where the results are saved.
%
% dataset_evaluation_mode : str ['folds', 'full']
% evaluation mode, 'full' all material available is considered to belong to one fold.
%
% Returns
% -------
% nothing
%
% Raises
% -------
% error
% Result file not found
%
scene_labels = dataset.scene_labels();
overall_metrics_per_scene = containers.Map();
progress(1, 'Collecting results', 0, '');
for scene_id=1:length(scene_labels)
scene_label = scene_labels{scene_id};
dcase2016_segment_based_metric = DCASE2016_EventDetection_SegmentBasedMetrics(dataset.event_labels('scene_label',scene_label));
dcase2016_event_based_metric = DCASE2016_EventDetection_EventBasedMetrics(dataset.event_labels('scene_label',scene_label),'use_onset_condition',1,'use_offset_condition',0);
for fold=dataset.folds(dataset_evaluation_mode)
result_filename = get_result_filename(fold, scene_label, result_path);
if exist(result_filename,'file')
[fid,error_message] = fopen(result_filename,'r');
if isempty(error_message)
C = textscan(fid, '%s%f%f%s', 'delimiter','\t');
else
error(['Error while opening file [',result_filename,'], error [',error_message,']']);
end
fclose(fid);
else
error(['Result file not found [',result_filename,']']);
end
results = [];
for i=1:length(C{1})
results = [results; {strtrim(C{1}{i}) C{2}(i) C{3}(i) strtrim(C{4}{i})}];
end
test_items = dataset.test(fold, 'scene_label', scene_label);
for file_id=1:length(test_items)
progress(0, 'Collecting results', (file_id / length(test_items)), scene_label, fold);
item = test_items(file_id);
current_file_results = [];
for result_id=1:size(results,1)
result_line = results(result_id,:);
if strcmp(result_line{1}, item.file)
current_file_results = [current_file_results; struct('file', result_line{1},...
'event_onset',result_line{2},...
'event_offset',result_line{3},...
'event_label',result_line{4})];
end
end
meta = dataset.file_meta(dataset.absolute_to_relative(item.file));
dcase2016_segment_based_metric.evaluate(current_file_results, meta);
dcase2016_event_based_metric.evaluate(current_file_results, meta);
end
end
overall_metrics_per_scene(scene_label) = struct('segment_based_metrics',dcase2016_segment_based_metric.results(),...
'event_based_metrics',dcase2016_event_based_metric.results());
end
fprintf(' Evaluation over %d folds\n',dataset.fold_count());
fprintf(' \n');
fprintf(' Results per scene\n');
fprintf(' %-18s | %-5s | | %-39s \n','','Main','Secondary metrics');
fprintf(' %-18s | %-5s | | %-38s | %-14s | %-14s | %-14s |\n','','','Seg/Overall', 'Seg/class', 'Event/Overall', 'Event/Class');
fprintf(' %-18s | %-5s | | %-6s : %-5s : %-5s : %-5s : %-5s | %-6s : %-5s | %-6s : %-5s | %-6s : %-5s |\n','Scene','ER','F1', 'ER', 'ER/S', 'ER/D', 'ER/I', 'F1', 'ER', 'F1', 'ER', 'F1', 'ER');
fprintf(' ----------------------+-------+ +--------+-------+-------+-------+-------+--------+-------+--------+-------+--------+-------+\n');
averages = struct('segment_based_metrics', struct('overall', struct ('ER', [],'F', []),...
'class_wise_average', struct('ER', [],'F', [])),...
'event_based_metrics', struct('overall', struct('ER', [],'F', []),...
'class_wise_average', struct('ER', [],'F', [])));
for scene_id=1:length(scene_labels)
scene_label = scene_labels{scene_id};
fprintf(' %-18s | %5.2f | | %4.1f %% : %5.2f : %5.2f : %5.2f : %5.2f | %4.1f %% : %5.2f | %4.1f %% : %5.2f | %4.1f %% : %5.2f |\n',...
scene_label,...
overall_metrics_per_scene(scene_label).segment_based_metrics.overall.ER,...
overall_metrics_per_scene(scene_label).segment_based_metrics.overall.F * 100,...
overall_metrics_per_scene(scene_label).segment_based_metrics.overall.ER,...
overall_metrics_per_scene(scene_label).segment_based_metrics.overall.S,...
overall_metrics_per_scene(scene_label).segment_based_metrics.overall.D,...
overall_metrics_per_scene(scene_label).segment_based_metrics.overall.I,...
overall_metrics_per_scene(scene_label).segment_based_metrics.class_wise_average.F * 100,...
overall_metrics_per_scene(scene_label).segment_based_metrics.class_wise_average.ER,...
overall_metrics_per_scene(scene_label).event_based_metrics.overall.F * 100,...
overall_metrics_per_scene(scene_label).event_based_metrics.overall.ER,...
overall_metrics_per_scene(scene_label).event_based_metrics.class_wise_average.F * 100,...
overall_metrics_per_scene(scene_label).event_based_metrics.class_wise_average.ER);
averages.segment_based_metrics.overall.ER = [averages.segment_based_metrics.overall.ER; overall_metrics_per_scene(scene_label).segment_based_metrics.overall.ER];
averages.segment_based_metrics.overall.F = [averages.segment_based_metrics.overall.F; overall_metrics_per_scene(scene_label).segment_based_metrics.overall.F];
averages.segment_based_metrics.class_wise_average.ER = [averages.segment_based_metrics.class_wise_average.ER; overall_metrics_per_scene(scene_label).segment_based_metrics.class_wise_average.ER];
averages.segment_based_metrics.class_wise_average.F = [averages.segment_based_metrics.class_wise_average.F; overall_metrics_per_scene(scene_label).segment_based_metrics.class_wise_average.F];
averages.event_based_metrics.overall.ER = [averages.event_based_metrics.overall.ER; overall_metrics_per_scene(scene_label).event_based_metrics.overall.ER];
averages.event_based_metrics.overall.F = [averages.event_based_metrics.overall.F; overall_metrics_per_scene(scene_label).event_based_metrics.overall.F];
averages.event_based_metrics.class_wise_average.ER =[averages.event_based_metrics.class_wise_average.ER; overall_metrics_per_scene(scene_label).event_based_metrics.class_wise_average.ER];
averages.event_based_metrics.class_wise_average.F = [averages.event_based_metrics.class_wise_average.F; overall_metrics_per_scene(scene_label).event_based_metrics.class_wise_average.F];
end
fprintf(' ----------------------+-------+ +--------+-------+-------+-------+-------+--------+-------+--------+-------+--------+-------+\n');
fprintf(' %-18s | %5.2f | | %4.1f %% : %5.2f : %-21s | %4.1f %% : %5.2f | %4.1f %% : %5.2f | %4.1f %% : %5.2f |\n',...
'Average',...
mean(averages.segment_based_metrics.overall.ER),...
mean(averages.segment_based_metrics.overall.F) * 100,...
mean(averages.segment_based_metrics.overall.ER),...
'',...
mean(averages.segment_based_metrics.class_wise_average.F) * 100,...
mean(averages.segment_based_metrics.class_wise_average.ER),...
mean(averages.event_based_metrics.overall.F) * 100,...
mean(averages.event_based_metrics.overall.ER),...
mean(averages.event_based_metrics.class_wise_average.F) * 100,...
mean(averages.event_based_metrics.class_wise_average.ER));
fprintf(' \n');
fprintf(' Results per events \n');
for scene_id=1:length(scene_labels)
scene_label = scene_labels{scene_id};
fprintf(' \n');
fprintf(' %-21s \n',upper(scene_label));
fprintf(' %-20s | %-30s | | %-15s \n','', 'Segment-based', 'Event-based');
fprintf(' %-20s | %-5s : %-5s : %-6s : %-5s | | %-5s : %-5s : %-6s : %-5s |\n', 'Event', 'Nref', 'Nsys', 'F1', 'ER', 'Nref', 'Nsys', 'F1', 'ER');
fprintf(' ---------------------+-------+-------+--------+-------+ +-------+-------+--------+-------+\n');
seg_Nref = 0;
seg_Nsys = 0;
event_Nref = 0;
event_Nsys = 0;
event_labels = dataset.event_labels('scene_label',scene_label);
for event_label_id=1:length(event_labels),
event_label = event_labels{event_label_id};
segment_based_metrics = overall_metrics_per_scene(scene_label).segment_based_metrics.class_wise;
event_based_metrics = overall_metrics_per_scene(scene_label).event_based_metrics.class_wise;
fprintf(' %-20s | %5d : %5d : %4.1f %% : %5.2f | | %5d : %5d : %4.1f %% : %5.2f |\n',event_label,...
segment_based_metrics(event_label).Nref,...
segment_based_metrics(event_label).Nsys,...
segment_based_metrics(event_label).F*100,...
segment_based_metrics(event_label).ER,...
event_based_metrics(event_label).Nref,...
event_based_metrics(event_label).Nsys,...
event_based_metrics(event_label).F*100,...
event_based_metrics(event_label).ER);
seg_Nref = seg_Nref + segment_based_metrics(event_label).Nref;
seg_Nsys = seg_Nsys + segment_based_metrics(event_label).Nsys;
event_Nref = event_Nref + event_based_metrics(event_label).Nref;
event_Nsys = event_Nsys + event_based_metrics(event_label).Nsys;
end
fprintf(' ---------------------+-------+-------+--------+-------+ +-------+-------+--------+-------+\n');
fprintf(' %-20s | %5d : %5d : %-14s | | %5d : %5d : %-14s |\n',...
'Sum',...
seg_Nref,...
seg_Nsys,...
'',...
event_Nref,...
event_Nsys,...
'');
fprintf(' %-20s | %-5s %-5s : %4.1f %% : %5.2f | | %-5s %-5s : %4.1f %% : %5.2f |\n',...
'Average',...
'', '',...
overall_metrics_per_scene(scene_label).segment_based_metrics.class_wise_average.F*100,...
overall_metrics_per_scene(scene_label).segment_based_metrics.class_wise_average.ER,...
'', '',...
overall_metrics_per_scene(scene_label).event_based_metrics.class_wise_average.F*100,...
overall_metrics_per_scene(scene_label).event_based_metrics.class_wise_average.ER);
fprintf(' \n');
end
end