-
Notifications
You must be signed in to change notification settings - Fork 1
/
dsplab_task1a_2.py
1172 lines (957 loc) · 40.9 KB
/
dsplab_task1a_2.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
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
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# DCASE 2018
# Task 1A: Acoustic Scene Classification
# Baseline system
# ---------------------------------------------
# Author: Toni Heittola ( [email protected] ), Tampere University of Technology / Audio Research Group
# License: MIT
import dcase_util
import sys
import numpy
import os
import sed_eval
from utils import *
__version_info__ = ('1', '0', '0')
__version__ = '.'.join(__version_info__)
def main(argv):
# Read application default parameter file
parameters = dcase_util.containers.DictContainer().load(
filename='dsplab_task1a_2.yaml'
)
# Initialize application parameters
param = dcase_util.containers.DCASEAppParameterContainer(
parameters,
path_structure={
'FEATURE_EXTRACTOR': ['FEATURE_EXTRACTOR'],
'FEATURE_NORMALIZER': ['FEATURE_EXTRACTOR'],
'LEARNER': ['DATA_PROCESSING_CHAIN', 'LEARNER'],
'RECOGNIZER': ['DATA_PROCESSING_CHAIN', 'LEARNER', 'RECOGNIZER'],
}
)
# Handle application arguments
args = handle_application_arguments(
param=param,
application_title='Task 1A: Acoustic Scene Classification',
version=__version__
)
# Process parameters, this is done only after application argument handling in case
# parameters where injected from command line.
param.process()
if args.dataset_path:
# Download only dataset if requested
# Make sure given path exists
dcase_util.utils.Path().create(
paths=args.dataset_path
)
# Get dataset and initialize
dcase_util.datasets.dataset_factory(
dataset_class_name=param.get_path('dataset.parameters.dataset'),
data_path=args.dataset_path,
).initialize().log()
sys.exit(0)
if args.parameter_set:
# Check parameter set ids given as program arguments
parameters_sets = args.parameter_set.split(',')
# Check parameter_sets
for set_id in parameters_sets:
if not param.set_id_exists(set_id=set_id):
raise ValueError('Parameter set id [{set_id}] not found.'.format(set_id=set_id))
else:
parameters_sets = [param.active_set()]
# Get application mode
if args.mode:
application_mode = args.mode
else:
application_mode = 'dev'
# Get overwrite flag
overwrite = param.get_path('general.overwrite')
# Make sure all system paths exists
dcase_util.utils.Path().create(
paths=list(param['path'].values())
)
# Setup logging
dcase_util.utils.setup_logging(
logging_file=os.path.join(param.get_path('path.log'), 'task1a.log')
)
# Get logging interface
log = dcase_util.ui.ui.FancyLogger()
# Log title
log.title('DCASE2018 / Task1A -- Acoustic scene classification')
log.line()
if args.show_results:
# Show evaluated systems
show_results(param=param, log=log)
sys.exit(0)
if args.show_set_list:
show_parameter_sets(param=param, log=log)
sys.exit(0)
# Create timer instance
timer = dcase_util.utils.Timer()
for parameter_set in parameters_sets:
# Set parameter set
param['active_set'] = parameter_set
param.update_parameter_set(parameter_set)
# Get dataset and initialize
db = dcase_util.datasets.dataset_factory(
dataset_class_name=param.get_path('dataset.parameters.dataset'),
data_path=param.get_path('path.dataset'),
).initialize()
if application_mode == 'eval' or application_mode == 'leaderboard':
# Application is set to work in 'eval' or 'leaderboard' mode. In these modes, training is done with
# all data from development dataset, and testing with all data from evaluation dataset.
# Make sure we are using all data
active_folds = db.folds(
mode='full'
)
else:
# Application working in normal mode aka 'dev' mode
# Get active folds from dataset
active_folds = db.folds(
mode=param.get_path('dataset.parameters.evaluation_mode')
)
# Get active fold list from parameters
active_fold_list = param.get_path('general.active_fold_list')
if active_fold_list and len(set(active_folds).intersection(active_fold_list)) > 0:
# Active fold list is set and it intersects with active_folds given by dataset class
active_folds = list(set(active_folds).intersection(active_fold_list))
# Print some general information
show_general_information(
parameter_set=parameter_set,
active_folds=active_folds,
param=param,
db=db,
log=log
)
if param.get_path('flow.feature_extraction'):
# Feature extraction stage
log.section_header('Feature Extraction')
timer.start()
processed_items = do_feature_extraction(
db=db,
param=param,
log=log,
overwrite=overwrite
)
timer.stop()
log.foot(
time=timer.elapsed(),
item_count=len(processed_items)
)
if param.get_path('flow.feature_normalization'):
# Feature extraction stage
log.section_header('Feature Normalization')
timer.start()
processed_items = do_feature_normalization(
db=db,
folds=active_folds,
param=param,
log=log,
overwrite=overwrite
)
timer.stop()
log.foot(
time=timer.elapsed(),
item_count=len(processed_items)
)
if param.get_path('flow.learning'):
# Learning stage
log.section_header('Learning')
timer.start()
processed_items = do_learning(
db=db,
folds=active_folds,
param=param,
log=log,
overwrite=overwrite
)
timer.stop()
log.foot(
time=timer.elapsed(),
item_count=len(processed_items)
)
if application_mode == 'dev':
# System evaluation in 'dev' mode
if param.get_path('flow.testing'):
# Testing stage
log.section_header('Testing')
timer.start()
processed_items = do_testing(
db=db,
folds=active_folds,
param=param,
log=log,
overwrite=overwrite
)
timer.stop()
log.foot(
time=timer.elapsed(),
item_count=len(processed_items)
)
if args.output_file:
save_system_output(
db=db,
folds=active_folds,
param=param,
log=log,
output_file=args.output_file
)
if param.get_path('flow.evaluation'):
# Evaluation stage
log.section_header('Evaluation')
timer.start()
do_evaluation(
db=db,
folds=active_folds,
param=param,
log=log,
application_mode=application_mode
)
timer.stop()
log.foot(
time=timer.elapsed(),
)
elif application_mode == 'eval' or application_mode == 'leaderboard':
# System evaluation in eval/leaderboard mode
if application_mode == 'eval':
# Get set id for eval parameters, test if current set id with eval post fix exists
eval_parameter_set_id = param.active_set() + '_eval'
if not param.set_id_exists(eval_parameter_set_id):
raise ValueError(
'Parameter set id [{set_id}] not found for eval mode.'.format(
set_id=eval_parameter_set_id
)
)
elif application_mode == 'leaderboard':
# Get set id for eval parameters, test if current set id with eval post fix exists
eval_parameter_set_id = param.active_set() + '_leaderboard'
if not param.set_id_exists(eval_parameter_set_id):
raise ValueError(
'Parameter set id [{set_id}] not found for leaderboard mode.'.format(
set_id=eval_parameter_set_id
)
)
# Change active parameter set
param.update_parameter_set(eval_parameter_set_id)
# Get eval dataset and initialize
db_eval = dcase_util.datasets.dataset_factory(
dataset_class_name=param.get_path('dataset.parameters.dataset'),
data_path=param.get_path('path.dataset'),
).initialize()
# Get active folds
active_folds = db_eval.folds(
mode='full'
)
if param.get_path('flow.feature_extraction'):
# Feature extraction for eval
log.section_header('Feature Extraction')
timer.start()
processed_items = do_feature_extraction(
db=db_eval,
param=param,
log=log,
overwrite=overwrite
)
timer.stop()
log.foot(
time=timer.elapsed(),
item_count=len(processed_items)
)
if param.get_path('flow.testing'):
# Testing stage for eval
log.section_header('Testing')
timer.start()
processed_items = do_testing(
db=db_eval,
folds=active_folds,
param=param,
log=log,
overwrite=overwrite
)
timer.stop()
log.foot(
time=timer.elapsed(),
item_count=len(processed_items)
)
if args.output_file:
save_system_output(
db=db_eval,
folds=active_folds,
param=param,
log=log,
output_file=args.output_file,
mode='leaderboard' if application_mode == 'leaderboard' else 'dcase'
)
if db_eval.reference_data_present and param.get_path('flow.evaluation'):
if application_mode == 'eval':
# Evaluation stage for eval
log.section_header('Evaluation')
timer.start()
do_evaluation(
db=db_eval,
folds=active_folds,
param=param,
log=log,
application_mode=application_mode
)
timer.stop()
log.foot(
time=timer.elapsed(),
)
elif application_mode == 'leaderboard':
# Evaluation stage for eval
log.section_header('Evaluation')
timer.start()
do_evaluation_task1a_leaderboard(
db=db_eval,
folds=active_folds,
param=param,
log=log,
application_mode=application_mode
)
timer.stop()
log.foot(
time=timer.elapsed(),
)
return 0
def do_feature_extraction(db, param, log, overwrite=False):
"""Feature extraction stage
Parameters
----------
db : dcase_util.dataset.Dataset
Dataset
param : dcase_util.containers.DCASEAppParameterContainer
Application parameters
log : dcase_util.ui.FancyLogger
Logging interface
overwrite : bool
Overwrite data always
Default value False
Returns
-------
list of str
"""
# Prepare feature extractor
mel_extractor = dcase_util.features.MelExtractor(
**param.get_path('feature_extractor.parameters', {})
)
# Loop over all audio files in the current dataset and extract acoustic features for each of them.
processed_files = []
for item_id, audio_filename in enumerate(db.audio_files):
# Get filename for feature data from audio filename
feature_filename = dcase_util.utils.Path(
path=audio_filename
).modify(
path_base=param.get_path('path.application.feature_extractor'),
filename_extension='.cpickle'
)
if not os.path.isfile(feature_filename) or overwrite:
log.line(
data='[{item: >5} / {total}] [{filename}]'.format(
item=item_id,
total=len(db.audio_files),
filename=os.path.split(audio_filename)[1]
),
indent=2
)
# Load audio data
audio = dcase_util.containers.AudioContainer().load(
filename=audio_filename,
mono=True,
fs=param.get_path('feature_extractor.fs')
)
# Extract features and store them into FeatureContainer, and save it to the disk
dcase_util.containers.FeatureContainer(
data=mel_extractor.extract(audio.data),
time_resolution=param.get_path('feature_extractor.hop_length_seconds')
).save(
filename=feature_filename
)
processed_files.append(feature_filename)
# # Plotting
# print("plotting features")
# dcase_util.containers.DataMatrix2DContainer(
# data=mel_extractor.extract(audio.data),
# time_resolution=param.get_path('feature_extractor.hop_length_seconds')
# ).plot()
return processed_files
def do_feature_normalization(db, folds, param, log, overwrite=False):
"""Feature normalization stage
Parameters
----------
db : dcase_util.dataset.Dataset
Dataset
folds : list
List of active folds
param : dcase_util.containers.DCASEAppParameterContainer
Application parameters
log : dcase_util.ui.FancyLogger
Logging interface
overwrite : bool
Overwrite data always
Default value False
Returns
-------
list of str
"""
# Loop over all active cross-validation folds and calculate mean and std for the training data
processed_files = []
for fold in folds:
log.line(
data='Fold [{fold}]'.format(fold=fold),
indent=2
)
# Get filename for the normalization factors
fold_stats_filename = os.path.join(
param.get_path('path.application.feature_normalizer'),
'norm_fold_{fold}.cpickle'.format(fold=fold)
)
if not os.path.isfile(fold_stats_filename) or overwrite:
normalizer = dcase_util.data.Normalizer(
filename=fold_stats_filename
)
# Loop through all training data
for item in db.train(fold=fold):
# Get feature filename
feature_filename = dcase_util.utils.Path(
path=item.filename
).modify(
path_base=param.get_path('path.application.feature_extractor'),
filename_extension='.cpickle'
)
# Load feature matrix
features = dcase_util.containers.FeatureContainer().load(
filename=feature_filename
)
# Accumulate statistics
normalizer.accumulate(
data=features
)
# Finalize and save
normalizer.finalize().save()
processed_files.append(fold_stats_filename)
return processed_files
def do_learning(db, folds, param, log, overwrite=False):
"""Learning stage
Parameters
----------
db : dcase_util.dataset.Dataset
Dataset
folds : list
List of active folds
param : dcase_util.containers.DCASEAppParameterContainer
Application parameters
log : dcase_util.ui.FancyLogger
Logging interface
overwrite : bool
Overwrite data always
Default value False
Returns
-------
nothing
"""
# Loop over all cross-validation folds and learn acoustic models
processed_files = []
for fold in folds:
log.line(
data='Fold [{fold}]'.format(fold=fold),
indent=2
)
# Get model filename
fold_model_filename = os.path.join(
param.get_path('path.application.learner'),
'model_fold_{fold}.h5'.format(fold=fold)
)
if not os.path.isfile(fold_model_filename) or overwrite:
log.line()
# Get normalization factor filename
fold_stats_filename = os.path.join(
param.get_path('path.application.feature_normalizer'),
'norm_fold_{fold}.cpickle'.format(fold=fold)
)
# Create data processing chain for features
data_processing_chain = dcase_util.processors.ProcessingChain()
for chain in param.get_path('data_processing_chain.parameters.chain'):
processor_name = chain.get('processor_name')
init_parameters = chain.get('init_parameters', {})
# Inject parameters
if processor_name == 'dcase_util.processors.NormalizationProcessor':
init_parameters['filename'] = fold_stats_filename
data_processing_chain.push_processor(
processor_name=processor_name,
init_parameters=init_parameters,
)
# Create meta processing chain for reference data
meta_processing_chain = dcase_util.processors.ProcessingChain()
for chain in param.get_path('meta_processing_chain.parameters.chain'):
processor_name = chain.get('processor_name')
init_parameters = chain.get('init_parameters', {})
# Inject parameters
if processor_name == 'dcase_util.processors.OneHotEncodingProcessor':
init_parameters['label_list'] = db.scene_labels()
meta_processing_chain.push_processor(
processor_name=processor_name,
init_parameters=init_parameters,
)
if param.get_path('learner.parameters.validation_set') and param.get_path('learner.parameters.validation_set.enable', True):
# Get validation files
training_files, validation_files = db.validation_split(
fold=fold,
split_type='balanced',
validation_amount=param.get_path('learner.parameters.validation_set.validation_amount'),
balancing_mode=param.get_path('learner.parameters.validation_set.balancing_mode'),
seed=param.get_path('learner.parameters.validation_set.seed', 0),
verbose=True
)
else:
# No validation set used
training_files = db.train(fold=fold).unique_files
validation_files = dcase_util.containers.MetaDataContainer()
# Create item_list_train and item_list_validation
item_list_train = []
item_list_validation = []
for item in db.train(fold=fold):
# Get feature filename
feature_filename = dcase_util.utils.Path(
path=item.filename
).modify(
path_base=param.get_path('path.application.feature_extractor'),
filename_extension='.cpickle'
)
item_ = {
'data': {
'filename': feature_filename
},
'meta': {
'label': item.scene_label
}
}
if item.filename in validation_files:
item_list_validation.append(item_)
elif item.filename in training_files:
item_list_train.append(item_)
# Setup keras, run only once
dcase_util.keras.setup_keras(
seed=param.get_path('learner.parameters.random_seed'),
profile=param.get_path('learner.parameters.keras_profile'),
backend=param.get_path('learner.parameters.backend', 'tensorflow'),
print_indent=2
)
if param.get_path('learner.parameters.generator.enable'):
# Create data generators for training and validation
# Get generator class, class is inherited from keras.utils.Sequence class.
KerasDataSequence = dcase_util.keras.get_keras_data_sequence_class()
# Training data generator
train_data_sequence = KerasDataSequence(
item_list=item_list_train,
data_processing_chain=data_processing_chain,
meta_processing_chain=meta_processing_chain,
batch_size=param.get_path('learner.parameters.fit.batch_size'),
data_format=param.get_path('learner.parameters.data.data_format'),
target_format=param.get_path('learner.parameters.data.target_format'),
**param.get_path('learner.parameters.generator', default={})
)
# Show data properties
train_data_sequence.log()
if item_list_validation:
# Validation data generator
validation_data_sequence = KerasDataSequence(
item_list=item_list_validation,
data_processing_chain=data_processing_chain,
meta_processing_chain=meta_processing_chain,
batch_size=param.get_path('learner.parameters.fit.batch_size'),
data_format=param.get_path('learner.parameters.data.data_format'),
target_format=param.get_path('learner.parameters.data.target_format')
)
else:
validation_data_sequence = None
# Get data item size
data_size = train_data_sequence.data_size
else:
# Collect training data and corresponding targets to matrices
log.line('Collecting training data', indent=2)
X_train, Y_train, data_size = dcase_util.keras.data_collector(
item_list=item_list_train,
data_processing_chain=data_processing_chain,
meta_processing_chain=meta_processing_chain,
target_format=param.get_path('learner.parameters.data.target_format', 'single_target_per_sequence'),
channel_dimension=param.get_path('learner.parameters.data.data_format', 'channels_first'),
verbose=True,
print_indent=4
)
log.foot(indent=2)
if item_list_validation:
log.line('Collecting validation data', indent=2)
X_validation, Y_validation, data_size = dcase_util.keras.data_collector(
item_list=item_list_validation,
data_processing_chain=data_processing_chain,
meta_processing_chain=meta_processing_chain,
target_format=param.get_path('learner.parameters.data.target_format', 'single_target_per_sequence'),
channel_dimension=param.get_path('learner.parameters.data.data_format', 'channels_first'),
verbose=True,
print_indent=4
)
log.foot(indent=2)
validation_data = (X_validation, Y_validation)
else:
validation_data = None
# Collect constants for the model generation, add class count and feature matrix size
model_parameter_constants = {
'CLASS_COUNT': int(db.scene_label_count()),
'FEATURE_VECTOR_LENGTH': int(data_size['data']),
'INPUT_SEQUENCE_LENGTH': int(data_size['time']),
}
# Read constants from parameters
model_parameter_constants.update(
param.get_path('learner.parameters.model.constants', {})
)
# Create sequential model
keras_model = dcase_util.keras.create_sequential_model(
model_parameter_list=param.get_path('learner.parameters.model.config'),
constants=model_parameter_constants
)
# Create optimizer object
param.set_path(
path='learner.parameters.compile.optimizer',
new_value=dcase_util.keras.create_optimizer(
class_name=param.get_path('learner.parameters.optimizer.class_name'),
config=param.get_path('learner.parameters.optimizer.config')
)
)
# Compile model
keras_model.compile(
**param.get_path('learner.parameters.compile', {})
)
# Show model topology
log.line(
dcase_util.keras.model_summary_string(keras_model)
)
# Create callback list
callback_list = [
dcase_util.keras.ProgressLoggerCallback(
epochs=param.get_path('learner.parameters.fit.epochs'),
metric=param.get_path('learner.parameters.compile.metrics')[0],
loss=param.get_path('learner.parameters.compile.loss'),
output_type='logging'
)
]
if param.get_path('learner.parameters.callbacks.StopperCallback'):
# StopperCallback
callback_list.append(
dcase_util.keras.StopperCallback(
epochs=param.get_path('learner.parameters.fit.epochs'),
**param.get_path('learner.parameters.callbacks.StopperCallback', {})
)
)
if param.get_path('learner.parameters.callbacks.ProgressPlotterCallback'):
# ProgressPlotterCallback
callback_list.append(
dcase_util.keras.ProgressPlotterCallback(
epochs=param.get_path('learner.parameters.fit.epochs'),
**param.get_path('learner.parameters.callbacks.ProgressPlotterCallback', {})
)
)
if param.get_path('learner.parameters.callbacks.StasherCallback'):
# StasherCallback
callback_list.append(
dcase_util.keras.StasherCallback(
epochs=param.get_path('learner.parameters.fit.epochs'),
**param.get_path('learner.parameters.callbacks.StasherCallback', {})
)
)
# Train model
if param.get_path('learner.parameters.generator.enable'):
keras_model.fit_generator(
generator=train_data_sequence,
validation_data=validation_data_sequence,
callbacks=callback_list,
verbose=0,
epochs=param.get_path('learner.parameters.fit.epochs'),
shuffle=param.get_path('learner.parameters.fit.shuffle')
)
else:
keras_model.fit(
x=X_train,
y=Y_train,
validation_data=validation_data,
callbacks=callback_list,
verbose=0,
epochs=param.get_path('learner.parameters.fit.epochs'),
batch_size=param.get_path('learner.parameters.fit.batch_size'),
shuffle=param.get_path('learner.parameters.fit.shuffle')
)
for callback in callback_list:
if isinstance(callback, dcase_util.keras.StasherCallback):
# Fetch the best performing model
callback.log()
best_weights = callback.get_best()['weights']
if best_weights:
keras_model.set_weights(best_weights)
break
# Save model
keras_model.save(fold_model_filename)
processed_files.append(fold_model_filename)
return processed_files
def do_testing(db, folds, param, log, overwrite=False):
"""Testing stage
Parameters
----------
db : dcase_util.dataset.Dataset
Dataset
folds : list
List of active folds
param : dcase_util.containers.DCASEAppParameterContainer
Application parameters
log : dcase_util.ui.FancyLogger
Logging interface
overwrite : bool
Overwrite data always
Default value False
Returns
-------
list
"""
processed_files = []
# Loop over all cross-validation folds and test
for fold in folds:
log.line(
data='Fold [{fold}]'.format(fold=fold),
indent=2
)
# Get model filename
fold_model_filename = os.path.join(
param.get_path('path.application.learner'),
'model_fold_{fold}.h5'.format(fold=fold)
)
# Initialize model to None, load when first non-tested file encountered.
keras_model = None
# Get normalization factor filename
fold_stats_filename = os.path.join(
param.get_path('path.application.feature_normalizer'),
'norm_fold_{fold}.cpickle'.format(fold=fold)
)
# Create processing chain for features
data_processing_chain = dcase_util.processors.ProcessingChain()
for chain in param.get_path('data_processing_chain.parameters.chain'):
processor_name = chain.get('processor_name')
init_parameters = chain.get('init_parameters', {})
# Inject parameters
if processor_name == 'dcase_util.processors.NormalizationProcessor':
init_parameters['filename'] = fold_stats_filename
data_processing_chain.push_processor(
processor_name=processor_name,
init_parameters=init_parameters,
)
# Get results filename
fold_results_filename = os.path.join(
param.get_path('path.application.recognizer'),
'res_fold_{fold}.txt'.format(fold=fold)
)
if not os.path.isfile(fold_results_filename) or overwrite:
# Load model if not yet loaded
if not keras_model:
dcase_util.keras.setup_keras(
seed=param.get_path('learner.parameters.random_seed'),
profile=param.get_path('learner.parameters.keras_profile'),
backend=param.get_path('learner.parameters.backend', 'tensorflow'),
print_indent=2
)
import keras
keras_model = keras.models.load_model(fold_model_filename)
# Initialize results container
res = dcase_util.containers.MetaDataContainer(
filename=fold_results_filename
)
# Loop through all test files from the current cross-validation fold
for item in db.test(fold=fold):
# Get feature filename
feature_filename = dcase_util.utils.Path(
path=item.filename
).modify(
path_base=param.get_path('path.application.feature_extractor'),
filename_extension='.cpickle'
)
features = data_processing_chain.process(
filename=feature_filename
)
input_data = features.data
if len(keras_model.input_shape) == 4:
# Add channel
if keras_model.get_config()[0]['config']['data_format'] == 'channels_first':
input_data = numpy.expand_dims(input_data, 0)
elif keras_model.get_config()[0]['config']['data_format'] == 'channels_last':
input_data = numpy.expand_dims(input_data, 3)
# Get network output
probabilities = keras_model.predict(x=input_data).T
# Binarization of the network output
frame_decisions = dcase_util.data.ProbabilityEncoder().binarization(
probabilities=probabilities,
binarization_type=param.get_path('recognizer.frame_binarization.type', 'global_threshold'),
threshold=param.get_path('recognizer.frame_binarization.threshold', 0.5)
)
estimated_scene_label = dcase_util.data.DecisionEncoder(
label_list=db.scene_labels()
).majority_vote(
frame_decisions=frame_decisions
)
# Store result into results container
res.append(
{
'filename': item.filename,
'scene_label': estimated_scene_label
}
)
processed_files.append(item.filename)
# Save results container
res.save()
return processed_files
def do_evaluation(db, folds, param, log, application_mode='default'):
"""Evaluation stage
Parameters
----------
db : dcase_util.dataset.Dataset
Dataset
folds : list
List of active folds
param : dcase_util.containers.DCASEAppParameterContainer
Application parameters