-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
1292 lines (1149 loc) · 75.7 KB
/
main.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
import warnings
import torch
import torch.nn as nn
import torch.optim as optim
import torch.profiler
import numpy as np
import torchaudio
from torch.utils.tensorboard import SummaryWriter
from torch.utils.data import DataLoader
import os, shutil, math
import yaml
import time
import wandb
from easydict import EasyDict
from datetime import datetime
from itertools import islice
from typing import List
from dataset.dcase_dataset import DCASE_SELD_Dataset, InfiniteDataLoader, _get_padders
from evaluation.dcase2022_metrics import cls_compute_seld_results
from evaluation.evaluation_dcase2022 import write_output_format_file, get_accdoa_labels, get_multi_accdoa_labels, determine_similar_location, all_seld_eval
from solver import SolverBasic, SolverDAN
from feature import Feature_StftPlusIV, Feature_MelPlusPhase, Feature_MelPlusIV
import augmentation.spatial_mixup as spm
import torch_audiomentations as t_aug
from parameters import get_parameters
import analysis
import utils
import plots
def get_dataset(config):
dataloader_train = None
datasets_train = []
for dset_root, dset_list in zip(config.dataset_root, config.dataset_list_train):
dataset_tmp = DCASE_SELD_Dataset(directory_root=dset_root,
list_dataset=dset_list,
chunk_size=config.dataset_chunk_size,
chunk_mode=config.dataset_chunk_mode,
trim_wavs=config.dataset_trim_wavs,
multi_track=config.dataset_multi_track,
num_classes=config.unique_classes,
labels_backend=config.dataset_backend,
pad_labels=not config.dataset_ignore_pad_labels,
return_fname=False)
datasets_train.append(dataset_tmp)
dataset_train = torch.utils.data.ConcatDataset(datasets_train)
dataloader_train = InfiniteDataLoader(dataset_train, batch_size=config.batch_size, num_workers=config.num_workers,
shuffle=True, drop_last=True, pin_memory=False)
dataset_valid = DCASE_SELD_Dataset(directory_root=config.dataset_root_valid,
list_dataset=config.dataset_list_valid,
chunk_size=config.dataset_chunk_size,
chunk_mode='full',
trim_wavs=config.dataset_trim_wavs,
multi_track=config.dataset_multi_track,
num_classes=config.unique_classes,
labels_backend=config.dataset_backend,
pad_labels=not config.dataset_ignore_pad_labels,
return_fname=True)
return dataloader_train, dataset_valid
def get_spatial_mixup(device='cpu', p_comp=1.0):
params = {'t_design_degree': 20,
'G_type': 'identity',
'use_slepian': False,
'order_output': 1,
'order_input': 1,
'backend': 'basic',
'w_pattern': 'hypercardioid'}
transform = spm.DirectionalLoudness(t_design_degree=params['t_design_degree'],
G_type=params['G_type'],
use_slepian=params['use_slepian'],
order_output=params['order_output'],
order_input=params['order_input'],
backend=params['backend'],
w_pattern=params['w_pattern'],
device=device,
p_comp=p_comp)
return transform
def get_rotations(device='cpu', p_comp=1.0):
params = {'t_design_degree': 20,
'G_type': 'identity',
'use_slepian': False,
'order_output': 1,
'order_input': 1,
'backend': 'basic',
'w_pattern': 'hypercardioid'}
rotation_params = {'rot_phi': 0.0,
'rot_theta': 0.0,
'rot_psi': 0.0}
rotation_angles = [rotation_params['rot_phi'], rotation_params['rot_theta'], rotation_params['rot_psi']]
rotation = spm.SphericalRotation(rotation_angles_rad=rotation_angles,
t_design_degree=params['t_design_degree'],
order_output=params['order_output'],
order_input=params['order_input'],
device=device,
p_comp=p_comp)
return rotation
def get_rotations_noise(device='cpu', p_comp=1.0):
params = {'t_design_degree': 20,
'G_type': 'identity',
'use_slepian': False,
'order_output': 1,
'order_input': 1,
'backend': 'basic',
'w_pattern': 'hypercardioid'}
rotation = spm.SphericalRotation(rotation_angles_rad=[0,0,0],
t_design_degree=params['t_design_degree'],
order_output=params['order_output'],
order_input=params['order_input'],
ignore_labels=True,
device=device, p_comp=p_comp)
return rotation
def get_audiomentations(p=0.5, fs=24000):
from augmentation.spliceout import SpliceOut
from augmentation.MyBandStopFilter import BandStopFilter
from augmentation.MyBandPassFilter import BandPassFilter
# Initialize augmentation callable
apply_augmentation = t_aug.Compose(
transforms=[
t_aug.Gain(p=p, min_gain_in_db=-15.0, max_gain_in_db=5.0, mode='per_example', p_mode='per_example'),
t_aug.PolarityInversion(p=p, mode='per_example', p_mode='per_example'),
t_aug.PitchShift(p=p, min_transpose_semitones=-1.5, max_transpose_semitones=1.5, sample_rate=fs, mode='per_example', p_mode='per_example'),
t_aug.AddColoredNoise(p=p, min_snr_in_db=6.0, max_snr_in_db=30.0, min_f_decay=-2.0, max_f_decay=2.0, sample_rate=fs, mode='per_example', p_mode='per_example'),
BandStopFilter(p=p, min_center_frequency=400, max_center_frequency=4000, min_bandwidth_fraction=0.25, max_bandwidth_fraction=1.99, sample_rate=fs, p_mode='per_example'),
t_aug.LowPassFilter(p=p, min_cutoff_freq=1000, max_cutoff_freq=7500, sample_rate=fs, p_mode='per_example'),
t_aug.HighPassFilter(p=p, min_cutoff_freq=100, max_cutoff_freq=2000, sample_rate=fs, p_mode='per_example'),
BandPassFilter(p=p, min_center_frequency=400, max_center_frequency=4000, min_bandwidth_fraction=0.5, max_bandwidth_fraction=1.99, sample_rate=fs, p_mode='per_example'),
#SpliceOut(p=p, num_time_intervals=8, max_width=400, sample_rate=fs, p_mode='per_example')
]
)
return apply_augmentation
class RandomAugmentations(nn.Sequential):
def __init__(self, fs=24000, p=1, p_comp=1, n_aug_min=2, n_aug_max=6, threshold_limiter=1):
super().__init__()
self.fs = fs
self.p = p
self.p_comp = p_comp
self.n_aug_min = n_aug_min
self.n_aug_max = n_aug_max
self.threshold_limiter = threshold_limiter
mode = 'per_example' # for speed, we use batch processing
p_mode = 'per_example'
self.augmentations = t_aug.SomeOf((n_aug_min, n_aug_max), p=self.p_comp, output_type='dict',
transforms=[
t_aug.Gain(p=p, min_gain_in_db=-15.0, max_gain_in_db=6.0, mode=mode, p_mode=p_mode),
t_aug.PolarityInversion(p=p, mode=mode, p_mode=p_mode),
#t_aug.PitchShift(p=p, min_transpose_semitones=-1.5, max_transpose_semitones=1.5, sample_rate=fs,
# mode=mode, p_mode=p_mode),
t_aug.AddColoredNoise(p=p, min_snr_in_db=2.0, max_snr_in_db=30.0, min_f_decay=-2.0, max_f_decay=2.0,
sample_rate=fs, mode=mode, p_mode=p_mode),
t_aug.BandStopFilter(p=p, min_center_frequency=400, max_center_frequency=4000,
min_bandwidth_fraction=0.5, max_bandwidth_fraction=1.1, sample_rate=fs,
p_mode=p_mode),
t_aug.LowPassFilter(p=p, min_cutoff_freq=1000, max_cutoff_freq=5000, sample_rate=fs,
p_mode=p_mode),
t_aug.HighPassFilter(p=p, min_cutoff_freq=250, max_cutoff_freq=1500, sample_rate=fs,
p_mode=p_mode),
t_aug.BandPassFilter(p=p, min_center_frequency=400, max_center_frequency=4000,
min_bandwidth_fraction=0.5, max_bandwidth_fraction=1.5, sample_rate=fs,
p_mode=p_mode),
#t_aug.SpliceOut(p=p, num_time_intervals=100, max_width=100, sample_rate=fs, p_mode=p_mode)
]
)
def forward(self, input):
do_reshape = False
if input.shape == 2:
do_reshape = True
input = input[None, ...] # audiomentations expects batches
# Augmentations
output = self.augmentations(input, sample_rate=self.fs) # Returns ObjectDict
output = output['samples']
# Limiter
torch.clamp(output, min=-self.threshold_limiter, max=self.threshold_limiter)
if do_reshape:
output = output.squeeze(0)
return output
class RandomSpecAugmentations(nn.Sequential):
def __init__(self, fs=24000, p=1, p_comp=1, n_aug_min=1, n_aug_max=2):
super().__init__()
self.fs = fs
self.p = p
self.p_comp = p_comp
self.n_aug_min = n_aug_min
self.n_aug_max = n_aug_max
mode = 'per_example' # for speed, we use batch processing
p_mode = 'per_example'
self.augmentations = t_aug.SomeOf((n_aug_min, n_aug_max), p=self.p_comp, output_type='dict',
transforms=[
t_aug.SpecTimeMasking(time_mask_param=24, iid_masks=True, p_proportion=0.3, p=p,
mode=mode, p_mode=p_mode),
t_aug.SpecFreqMasking(freq_mask_param=24, iid_masks=True, p=p,
mode=mode, p_mode=p_mode),
]
)
def forward(self, input):
do_reshape = False
if input.shape == 2:
do_reshape = True
input = input[None, ...] # audiomentations expects batches
# Augmentations
output = self.augmentations(input) # Returns ObjectDict
output = output['samples']
if do_reshape:
output = output.squeeze(0)
return output
class CustomFilter(nn.Sequential):
def __init__(self, fs=24000, p=1, p_comp=1):
super().__init__()
self.fs = fs
self.p = p
self.p_comp = p_comp
mode = 'per_batch' # for speed, we use batch processing
p_mode = 'per_batch'
self.augmentations = t_aug.Compose(output_type='tensor',
transforms=[
t_aug.LowPassFilter(p=p, min_cutoff_freq=5000, max_cutoff_freq=5001, sample_rate=fs,
p_mode=p_mode),
t_aug.HighPassFilter(p=p, min_cutoff_freq=125, max_cutoff_freq=126, sample_rate=fs,
p_mode=p_mode),
]
)
def forward(self, input):
do_reshape = False
if input.shape == 2:
do_reshape = True
input = input[None, ...] # audiomentations expects batches
# Augmentations
output = self.augmentations(input)
if do_reshape:
output = output.squeeze(0)
return output
def main():
# Get config
config = get_parameters()
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Reproducibility
utils.seed_everything(seed=config.seed, mode=config.seed_mode)
# Logging configuration
writer = SummaryWriter(config.logging_dir)
# Data
dataloader_train, dataset_valid = get_dataset(config)
# Solver
if config.solver == 'vanilla':
solver = SolverBasic(config=config, tensorboard_writer=writer)
elif config.solver == 'DAN':
solver = SolverDAN(config=config, tensorboard_writer=writer)
else:
raise ValueError(f'Solver {config.solver} not supported.')
# Select features and augmentation and rotation
augmentation_transform_spatial = None
augmentation_transform_audio = None
augmentation_transform_spec = None
rotations_transform = None
rotations_noise = None
if config.model_features_transform == 'stft_iv':
features_transform = Feature_StftPlusIV(nfft=512).to(device) # mag STFT with intensity vectors
elif config.model_features_transform == 'mel_iv':
features_transform = Feature_MelPlusIV().to(device) # mel spec with intensity vectors
elif config.model_features_transform == 'mel_phase':
features_transform = Feature_MelPlusPhase().to(device) # mel spec with phase difference
elif config.model_features_transform == 'bandpass':
features_transform = CustomFilter().to(device) # Custom Band pass filter to accomodate for the Eigenmike
else:
features_transform = None
print(features_transform)
if config.model_spatialmixup:
augmentation_transform_spatial = get_spatial_mixup(device=device, p_comp=0.0).to(device)
if config.model_augmentation:
augmentation_transform_audio = RandomAugmentations(p_comp=0.0).to(device)
if config.model_spec_augmentation:
augmentation_transform_spec = RandomSpecAugmentations(p_comp=0.0).to(device)
if config.model_rotations:
rotations_transform = get_rotations(device=device, p_comp=0.0).to(device)
if config.model_rotations_noise:
rotations_noise = get_rotations_noise(device=device, p_comp=0.0).to(device)
if 'samplecnn' in config.model:
class t_transform(nn.Sequential):
def __int__(self):
super().__init__()
def forward(self, input):
out = nn.functional.interpolate(input, scale_factor=(1, 0.1), mode='nearest-exact')
return out
target_transform = t_transform()
else:
target_transform = None
print(target_transform)
print(rotations_transform)
print(augmentation_transform_spatial)
print(augmentation_transform_audio)
print(augmentation_transform_spec)
# Initial loss:
x, target = dataloader_train.dataset[0]
if features_transform is not None:
x = features_transform(x.unsqueeze(0).to(device))
else:
x = x[None, ...].to(device)
if target_transform is not None:
target = target_transform(target[None, ...].to(device))
else:
target = target[None, ...].to(device)
solver.predictor.eval()
# To debug
#yolo = RandomSpecAugmentations()
#y = yolo(x)
out = solver.predictor(x)
#loss = solver.loss_fns[solver.loss_names['G_rec']](out, target)
loss = solver.loss_fns['G_rec' if config.solver == 'DAN' else 'G_rec'](out, target)
print('Initial loss = {:.6f}'.format(loss.item()))
# Profiling
if config.profiling:
prof = torch.profiler.profile(
schedule=torch.profiler.schedule(wait=1, warmup=1, active=3, repeat=2),
on_trace_ready=torch.profiler.tensorboard_trace_handler(config.logging_dir),
profile_memory=False,
record_shapes=True,
with_stack=False)
prof.start()
# Monitoring variables
train_loss, val_loss, seld_metrics_macro, seld_metrics_micro = 0, 0, None, None
best_val_step_macro, best_val_loss, best_metrics_macro = 0, 0, [0, 0, 0, 0, 99]
best_val_step_micro, best_val_loss_micro, best_metrics_micro = 0, 0, [0, 0, 0, 0, 99]
start_time = time.time()
if config.mode == 'train':
print('>>>>>>>> Training START <<<<<<<<<<<<')
iter_idx = 0
start_step_time = time.time()
for data in islice(dataloader_train, config.num_iters + 1):
#checkpoint_root = '/m/triton/scratch/work/falconr1/dcase2022/seld_dcase2022_ric/logging'
#checkpoints_path = 'dcase2022_plus_dcase22-sim_FIXED_w-aug_mixup_b32_sample-5573019_n-work:0_samplecnn_batchnorm_144000__2022-06-22-220123'
#checkpoints_name = 'model_step_170000.pth'
#checkpoint = os.path.join(checkpoint_root, checkpoints_path, checkpoints_name)
#solver = SolverBasic(config=config, model_checkpoint=checkpoint)
train_loss = train_iteration(config, data, iter_idx=iter_idx, start_time=start_time, start_time_step=start_step_time,
device=device, features_transform=features_transform, rotation_noise=rotations_noise,
augmentation_transform_spatial=augmentation_transform_spatial, augmentation_transform_spec=augmentation_transform_spec,
rotation_transform=rotations_transform, augmentation_transform_audio=augmentation_transform_audio,
target_transform=target_transform, solver=solver, writer=writer)
if iter_idx % config.print_every == 0 and iter_idx > 0:
start_step_time = time.time()
# Validation step
if iter_idx % config.logging_interval == 0 and iter_idx > 0:
seld_metrics_macro, seld_metrics_micro, val_loss, _ = validation_iteration(config, dataset=dataset_valid, iter_idx=iter_idx,
device=device, features_transform=features_transform, target_transform=target_transform,
solver=solver, writer=writer,
dcase_output_folder=config['directory_output_results'])
curr_time = time.time() - start_time
# Check for best validation step
if seld_metrics_macro[4] < best_metrics_macro[4]:
best_metrics_macro = seld_metrics_macro
best_val_step_macro = iter_idx
best_val_loss = val_loss
if seld_metrics_micro[4] < best_metrics_micro[4]:
best_metrics_micro = seld_metrics_micro
best_val_step_micro = iter_idx
if config.wandb:
wandb.log({'best_val_step_macro': best_val_step_macro})
wandb.summary['BestMACRO/SELD'] = best_metrics_macro[4]
wandb.summary['BestMACRO/ER'] = best_metrics_macro[0]
wandb.summary['BestMACRO/F'] = best_metrics_macro[1]
wandb.summary['BestMACRO/LE'] = best_metrics_macro[2]
wandb.summary['BestMACRO/LR'] = best_metrics_macro[3]
wandb.summary['Losses/valid'] = best_val_loss
wandb.summary['best_val_step_macro'] = best_val_step_macro
wandb.log({'best_val_step_micro': best_val_step_micro})
wandb.summary['BestMicro/SELD'] = best_metrics_micro[4]
wandb.summary['BestMicro/ER'] = best_metrics_micro[0]
wandb.summary['BestMicro/F'] = best_metrics_micro[1]
wandb.summary['BestMicro/LE'] = best_metrics_micro[2]
wandb.summary['BestMicro/LR'] = best_metrics_micro[3]
wandb.summary['Losses/valid'] = best_val_loss
wandb.summary['best_val_step_micro'] = best_val_step_micro
# Print metrics
print(f'Evaluating using overlap = 1 / {config["evaluation_overlap_fraction"]}')
print(
'iteration: {}/{}, time: {:0.2f}, '
'train_loss: {:0.4f}, val_loss: {:0.4f}, '
'p_comp: {:0.3f}, '.format(iter_idx, config.num_iters, curr_time,
train_loss.item(), val_loss,
solver.get_curriculum_params()[0]))
print('====== micro ======')
print(
'best_val_step_micro: {}, \t\t'
'micro: ER/F/LE/LR/SELD: {}, '.format(best_val_step_micro,
'{:0.4f}/{:0.4f}/{:0.4f}/{:0.4f}/\t/{:0.4f}'.format(*seld_metrics_micro[0:5]),))
print(
'best_val_step_micro: {}, \t'
'BEST-micro: ER/F/LE/LR/SELD: {}, '.format(best_val_step_micro,
'{:0.4f}/{:0.4f}/{:0.4f}/{:0.4f}/\t/{:0.4f}'.format(*best_metrics_micro[0:5]),))
print('====== MACRO ======')
print(
'best_val_step_macro: {}, \t\t'
'MACRO: ER/F/LE/LR/SELD: {}, '.format(best_val_step_macro,
'{:0.4f}/{:0.4f}/{:0.4f}/{:0.4f}/\t/{:0.4f}'.format(*seld_metrics_macro[0:5]),))
print(
'best_val_step_micro: {}, \t'
'BEST-MACRO: ER/F/LE/LR/SELD: {}, '.format(best_val_step_macro,
'{:0.4f}/{:0.4f}/{:0.4f}/{:0.4f}/\t/{:0.4f}'.format(*best_metrics_macro[0:5]),))
print('\n MACRO Classwise results on validation data')
print('Class\tER\t\tF\t\tLE\t\tLR\t\tSELD_score')
seld_metrics_class_wise = seld_metrics_macro[5]
for cls_cnt in range(config['unique_classes']):
print('{}\t\t{:0.2f}\t{:0.2f}\t{:0.2f}\t{:0.2f}\t{:0.2f}'.format(cls_cnt,
seld_metrics_class_wise[0][cls_cnt],
seld_metrics_class_wise[1][cls_cnt],
seld_metrics_class_wise[2][cls_cnt],
seld_metrics_class_wise[3][cls_cnt],
seld_metrics_class_wise[4][cls_cnt]))
print('================================================ \n')
# Schedulers
if iter_idx > 0:
solver.curriculum_scheduler_step(iter_idx,
val_loss if val_loss is not None else 0,
seld_metrics_macro[4] if seld_metrics_macro is not None else 0)
if iter_idx % config.lr_scheduler_step == 0 and iter_idx > 0:
solver.lr_step(seld_metrics_macro[4] if seld_metrics_macro is not None else 0) # LRstep scheduler based on validation SELD score
if config.solver == 'DAN':
if iter_idx > 0:
solver.lr_step_discriminator(seld_metrics_macro[4] if seld_metrics_macro is not None else 0)
iter_idx += 1
if config.profiling:
prof.step()
print('>>>>>>>> Training Finished <<<<<<<<<<<<')
if config.profiling:
prof.stop()
wandb.finish()
elif config.mode == 'eval':
checkpoint_root = '/m/triton/scratch/work/falconr1/dcase2022/seld_dcase2022_ric/logging'
#checkpoints_path = ['dcase22_plus_dcase22-sim_w-aug-5405063_n-work:0_crnn10_batchnorm_30480__2022-06-15-195028']
#checkpoints_name = 'model_step_90000.pth'
checkpoints_path = ['dcase2022_plus_dcase22-sim_w-aug_mixup_b32_sample-gru-5441557_n-work:0_samplecnn_batchnorm_144000__2022-06-19-001924']
checkpoints_name = 'model_step_190000.pth'
#checkpoints_path = ['dcase2022_plus_scase22-sim_w-aug_mixup_255_b32-5431498_n-work:0_crnn10_batchnorm_61200__2022-06-17-125119']
#checkpoints_name = 'model_step_190000.pth'
#checkpoints_path = ['dcase2022_plus_scase22-sim_w-aug_mixup_255_long-5423265_n-work:0_crnn10_batchnorm_61200__2022-06-16-150605']
#checkpoints_name = 'model_step_80000.pth'
checkpoint = os.path.join(checkpoint_root, checkpoints_path[0], checkpoints_name)
solver = SolverBasic(config=config, model_checkpoint=checkpoint)
dataset_eval = DCASE_SELD_Dataset(directory_root=config.dataset_root_eval,
list_dataset=config.dataset_list_eval,
chunk_size=config.dataset_chunk_size,
chunk_mode='full',
trim_wavs=config.dataset_trim_wavs,
multi_track=config.dataset_multi_track,
num_classes=config.unique_classes,
return_fname=True,
ignore_labels=True)
evaluation(config, dataset_eval, solver, features_transform,
target_transform=target_transform, dcase_output_folder=config['directory_output_results'],
device=device, detection_threshold=config.detection_threshold)
elif config.mode == 'valid': # Validation for those where I missed the performance
checkpoint_root = '/m/triton/scratch/work/falconr1/dcase2022/seld_dcase2022_ric/logging'
#checkpoints_path = ['dcase2022_plus_scase22-sim_w-aug_mixup_255-5411345_n-work:0_crnn10_batchnorm_61200__2022-06-15-222729']
#checkpoints_name = 'model_step_80000.pth'
#checkpoints_path = ['dcase2022_plus_dcase22-sim_w-aug_mixup_b32_sample-gru-5441557_n-work:0_samplecnn_batchnorm_144000__2022-06-19-001924']
#checkpoints_name = 'model_step_190000.pth'
checkpoints_path = ['dcase2022_plus_scase22-sim_w-aug_mixup_255_b32-5431498_n-work:0_crnn10_batchnorm_61200__2022-06-17-125119']
checkpoints_name = 'model_step_190000.pth'
checkpoints_path = ['dcase2022_plus_scase22-sim_w-aug_mixup_255_long-5423265_n-work:0_crnn10_batchnorm_61200__2022-06-16-150605']
checkpoints_name = 'model_step_80000.pth'
checkpoints_path = ['dcase2022_plus_dcase22-sim_no-aug_mixup_b32_-5440404_n-work:0_samplecnn_batchnorm_144000__2022-06-18-201151']
checkpoints_name = 'model_step_90000.pth'
checkpoints_path = ['third-2021-crnn10-2.55_spm+aug-5761546_n-work:0_crnn10_batchnorm_61200__2022-07-05-212417']
checkpoints_name = 'model_step_10000.pth'
#checkpoints_path = ['six-2021-features-no-grad-crnn10-2.55_base_mel_iv+spm+aug+rot-5820412_n-work:0_crnn10_batchnorm_61200__2022-07-09-201438']
#checkpoints_name = 'model_step_200000.pth'
checkpoints_path = ['six-2021-features-no-grad-spec-crnn10-2.55_base_stft_iv+spm+aug+rot-5836347_n-work:0_crnn10_batchnorm_61200__2022-07-11-211006']
checkpoints_name = 'model_step_180000.pth'
checkpoints_path = ['compare-labels-2021-crnn10-2.55_sony_mel_iv+rot-6332347_6__n-work:0_crnn10_curr:linear_batchnorm_61200__2022-07-28-191723']
checkpoints_name = 'model_step_190000.pth'
checkpoint = os.path.join(checkpoint_root, checkpoints_path[0], checkpoints_name)
if config.oracle_mode:
solver = SolverBasic(config=config) # TODO this is for loss upper bound only
else:
solver = SolverBasic(config=config, model_checkpoint=checkpoint)
seld_metrics_macro, seld_metrics_micro, val_loss, all_outputs = validation_iteration(config, dataset=dataset_valid, iter_idx=0,
device=device, features_transform=features_transform,
target_transform=target_transform, solver=solver, writer=None,
dcase_output_folder=config['directory_output_results'],
detection_threshold=config['detection_threshold'])
curr_time = time.time() - start_time
# Print metrics
print(f'Evaluating using overlap = 1 / {config["evaluation_overlap_fraction"]}')
print(
'iteration: {}/{}, time: {:0.2f}, '
'train_loss: {:0.4f}, val_loss: {:0.4f}, '
'p_comp: {:0.3f}, '.format(-1, config.num_iters, curr_time,
train_loss.item(), val_loss,
solver.get_curriculum_params()))
print('====== micro ======')
print(
'best_val_step_micro: {}, \t\t'
'micro: ER/F/LE/LR/SELD: {}, '.format(-1,
'{:0.4f}/{:0.4f}/{:0.4f}/{:0.4f}/\t/{:0.4f}'.format(*seld_metrics_micro[0:5]), ))
print('====== MACRO ======')
print(
'best_val_step_macro: {}, \t\t'
'MACRO: ER/F/LE/LR/SELD: {}, '.format(-1,
'{:0.4f}/{:0.4f}/{:0.4f}/{:0.4f}/\t/{:0.4f}'.format(*seld_metrics_macro[0:5]), ))
print('\n MACRO Classwise results on validation data')
print('Class\tER\t\tF\t\tLE\t\tLR\t\tSELD_score')
seld_metrics_class_wise = seld_metrics_macro[5]
for cls_cnt in range(config['unique_classes']):
print('{}\t\t{:0.2f}\t{:0.2f}\t{:0.2f}\t{:0.2f}\t{:0.2f}'.format(cls_cnt,
seld_metrics_class_wise[0][cls_cnt],
seld_metrics_class_wise[1][cls_cnt],
seld_metrics_class_wise[2][cls_cnt],
seld_metrics_class_wise[3][cls_cnt],
seld_metrics_class_wise[4][cls_cnt]))
print('================================================ \n')
print('Plotting results')
all_outputs_flat = torch.concat(all_outputs, dim=-1).detach().cpu()
all_outputs = [x[0, ...].detach().cpu() for x in all_outputs]
filename = 'results'
this_split = ''
if "2021" in config.dataset_list_train[0]:
set = "2021"
elif "2022" in config.dataset_list_train[0]:
set = "2022"
else:
raise ValueError('Not supported')
class_names, _ = analysis.get_classes_and_splits(set)
print('01 / 05 Plotting trajectories...')
plots.plot_labels_cross_sections(all_outputs[0], rlim=[0, 1], title=f'{filename}_{this_split}_Single wav', savefig=True)
analysis.plot_active_trajectories(all_outputs_flat, xlim=100000, title=f'{filename}_{this_split}_All wavs, trucated') # all wavs, flatted
print('02 / 05 Plotting azimuth/elevation...')
analysis.plot_histograms_bivariate_azi_ele(all_outputs_flat, filename=f'{filename}_{this_split}_azi-ele', split=this_split)
print('03 / 05 Plotting speed and accelerarion...')
analysis.plot_speed_and_acceleration(all_outputs_flat, num_classes=config.unique_classes,
filename=f'{filename}_{this_split}_speed')
# Grouped by splits
print('04 / 05 Plotting active per class...')
analysis.plot_histograms_active_per_class([all_outputs], splits=[this_split], class_labels=class_names, detection_threshold=0.5,
filename=f'{filename}_activity')
# Grouped by splits
print('05 / 05 Plotting polyphony...')
analysis.plot_histograms_polyphony([all_outputs], splits=[this_split],
detection_threshold=0.5, format_use_log=False, chunk_size=1, filename=f'{filename}_polyphony')
def train_iteration(config, data, iter_idx, start_time, start_time_step, device, features_transform: [nn.Sequential, None], rotation_noise: [nn.Sequential, None],
augmentation_transform_spatial: [nn.Sequential, None], rotation_transform: [nn.Sequential, None], augmentation_transform_spec: [nn.Sequential, None],
augmentation_transform_audio: [nn.Sequential, None], target_transform: [nn.Sequential, None], solver, writer):
# Training iteration
x, target = data
x, target = x.to(device), target.to(device)
# Rotation, Augmentation and Feature extraction
with torch.no_grad():
if rotation_transform is not None:
rotation_transform.reset_R(mode=config.model_rotations_mode)
rotation_transform.p_comp = solver.get_curriculum_params()[0]
x, target = rotation_transform(x, target)
if rotation_noise is not None:
rotation_noise.reset_R(mode='noise')
rotation_noise.p_comp = solver.get_curriculum_params()[0]
x, _ = rotation_noise(x)
if augmentation_transform_spatial is not None:
augmentation_transform_spatial.reset_G(G_type='spherical_cap_soft')
augmentation_transform_spatial.p_comp = solver.get_curriculum_params()[0]
if False: # Debugging
augmentation_transform_spatial.plot_response(plot_channel=0, plot_matrix=True, do_scaling=True, plot3d=False)
x = augmentation_transform_spatial(x)
if augmentation_transform_audio is not None:
augmentation_transform_audio = RandomAugmentations(p_comp=solver.get_curriculum_params()[0])
x = augmentation_transform_audio(x)
if features_transform is not None:
x = features_transform(x)
if augmentation_transform_spec is not None:
augmentation_transform_spec = RandomSpecAugmentations(p_comp=solver.get_curriculum_params()[0])
x = augmentation_transform_spec(x)
if target_transform is not None:
target = target_transform(target)
# Train step
solver.set_input(x, target)
solver.train_step()
# Useful debugging
#plots.plot_labels_cross_sections(target[0].detach().cpu(), n_classes=list(range(target[0].shape[-2])), plot_cartesian=True)
#plots.plot_labels(target[0].detach().cpu(), n_classes=list(range(target[0].shape[-2])), savefig=False, plot_cartesian=True)
# Output training stats
if config.solver == 'DAN':
train_loss = config['w_rec'] * solver.loss_values['G_rec'] + config['w_adv'] * solver.loss_values['G_adv']
else:
train_loss = solver.loss_values['G_rec']
# Logging and printing
if writer is not None:
step = iter_idx
if iter_idx % 200 == 0:
# Losses
writer.add_scalar('Losses/train', train_loss.item(), step)
if config.solver == 'DAN':
writer.add_scalar('Losses/g_rec', solver.loss_values['G_rec'].item(), step)
writer.add_scalar('Losses/g_adv', solver.loss_values['G_adv'].item(), step)
writer.add_scalar('Losses/d_real', solver.loss_values['D_real'].item(), step)
writer.add_scalar('Losses/d_fake', solver.loss_values['D_fake'].item(), step)
#if config.wandb:
# wandb.log({'Losses/train': train_loss.item()}, step=step)
# Learning rates
lr = solver.get_lrs()
writer.add_scalar('Lr/gen', lr[0], step)
if len(lr) == 2:
writer.add_scalar('Lr/disc', lr[1], step)
# Grad norm
grad_norm_model = solver.get_grad_norm()
writer.add_scalar('grad_norm/gen', grad_norm_model[0], step)
if len(grad_norm_model) == 2:
writer.add_scalar('grad_norm/disc', grad_norm_model[1], step)
# Scheduler
curr_params = solver.get_curriculum_params()
if augmentation_transform_audio is not None or rotation_transform is not None or rotation_noise is not None or augmentation_transform_spatial is not None or augmentation_transform_spec is not None:
writer.add_scalar('params/p_comp', curr_params[0], iter_idx)
if config.solver == 'DAN':
writer.add_scalar('params/curr_w_adv', curr_params[1], iter_idx)
writer.add_scalar('params/curr_d_threshold_min', curr_params[2], iter_idx)
writer.add_scalar('params/curr_d_threshold_max', curr_params[3], iter_idx)
# Print to console
if iter_idx % config.print_every == 0:
curr_time = time.time() - start_time
step_time = time.time() - start_time_step
if config.solver == 'DAN':
print('[%d/%d] iters \t Loss_gen: %.6f \t Loss_disc %.6f \t\t Step_time: %0.2f \t\t Elapsed time: %0.2f'
% (iter_idx, config.num_iters, train_loss.item(), solver.loss_values['D_real'].item() + solver.loss_values['D_fake'].item(),
step_time, curr_time))
else:
print('[%d/%d] iters \t Loss_rec: %.6f \t\t Step_time: %0.2f \t\t Elapsed time: %0.2f'
% (iter_idx, config.num_iters, train_loss.item(), step_time, curr_time))
# Log an example of the predicted labels
if (iter_idx % config.logging_interval == 0) and writer is not None:
fixed_output = solver.get_fixed_output()
fixed_label = solver.get_fixed_label()
if fixed_output is not None:
fixed_output = fixed_output[0, ...]
fixed_label = fixed_label[0, ...]
fixed_error = np.abs(fixed_output - fixed_label)
# Transform to spherical coordinates
fixed_output_sph = np.zeros_like(fixed_output)
fixed_label_sph = np.zeros_like(fixed_label)
fixed_error_sph = np.zeros_like(fixed_error)
for cc in range(fixed_output_sph.shape[1]):
tmp = utils.vecs2dirs(fixed_output[:, cc, :].squeeze().transpose(1, 0), include_r=True, use_elevation=True)
fixed_output_sph[:, cc, ::] = tmp.transpose(1, 0)
tmp = utils.vecs2dirs(fixed_label[:, cc, :].squeeze().transpose(1, 0), include_r=True, use_elevation=True)
fixed_label_sph[:, cc, ::] = tmp.transpose(1, 0)
tmp = utils.vecs2dirs(fixed_error[:, cc, :].squeeze().transpose(1, 0), include_r=True, use_elevation=True)
fixed_error_sph[:, cc, ::] = tmp.transpose(1, 0)
# Plot
fig = plots.plot_labels(fixed_output_sph, savefig=False, plot_cartesian=False, title='Output')
writer.add_figure('fixed_output/train', fig, iter_idx)
fig = plots.plot_labels(fixed_label_sph, savefig=False, plot_cartesian=False, title='Target')
writer.add_figure('fixed_label/train', fig, None)
#fig = plots.plot_labels(fixed_error_sph, savefig=False, plot_cartesian=False)
#writer.add_figure('fixed_error/train', fig, iter_idx)
# Save model to disk
if (iter_idx % config.logging_interval == 0) and iter_idx > 0:
torch.save(solver.predictor.state_dict(), os.path.join(config.logging_dir, f'model_step_{iter_idx}.pth'))
return train_loss
def validation_iteration(config, dataset, iter_idx, solver, features_transform, target_transform: [nn.Sequential, None], dcase_output_folder, device, writer, detection_threshold=0.5):
# Adapted from the official baseline
nb_test_batches, test_loss = 0, 0.
model = solver.predictor
model.eval()
file_cnt = 0
overlap = 1 / config['evaluation_overlap_fraction'] # defualt should be 1 TODO, onluy works for up to 1/32 , when the labels are 128 frames long.
outputs_for_plots = []
print(f'Validation: {len(dataset)} fnames in dataset.')
with torch.no_grad():
for ctr, (audio, target, fname) in enumerate(dataset):
# load one batch of data
audio, target = audio.to(device), target.to(device)
duration = dataset.durations[fname]
print(f'Evaluating file {ctr+1}/{len(dataset)}: {fname}')
print(f'Audio shape: {audio.shape}')
print(f'Target shape: {target.shape}')
warnings.warn('WARNING: Hard coded chunk size for evaluation')
audio_padding, labels_padding = _get_padders(chunk_size_seconds=config.dataset_chunk_size / dataset._fs[fname],
duration_seconds=math.floor(duration),
overlap=overlap,
audio_fs=dataset._fs[fname],
labels_fs=100)
# Split each wav into chunks and process them
audio = audio_padding['padder'](audio)
audio_chunks = audio.unfold(dimension=1, size=audio_padding['chunk_size'], step=audio_padding['hop_size']).permute((1, 0, 2))
if config.dataset_multi_track:
labels = labels_padding['padder'](target.permute(1,2,3,0))
labels_chunks = labels.unfold(dimension=-1, size=labels_padding['chunk_size'], step=labels_padding['hop_size'])
labels_chunks = labels_chunks.permute((3, 4, 0, 1, 2))
else:
labels = labels_padding['padder'](target)
labels_chunks = labels.unfold(dimension=-1, size=labels_padding['chunk_size'], step=labels_padding['hop_size'])
labels_chunks = labels_chunks.permute((2, 0, 1, 3))
full_output = []
full_loss = []
full_labels = []
if audio_chunks.shape[0] != labels_chunks.shape[0]:
a = 1
warnings.warn('WARNING: Possible error in padding.')
if audio_chunks.shape[0] > labels_chunks.shape[0]:
audio_chunks = audio_chunks[0:labels_chunks.shape[0], ...] # Mmm... lets drop the extra audio chunk if there are no labels for it
if audio_chunks.shape[0] < labels_chunks.shape[0]:
audio_chunks = torch.concat([audio_chunks, torch.zeros_like(audio_chunks[0:1])]) # Mmm... lets add an empty audio slice
tmp = torch.utils.data.TensorDataset(audio_chunks, labels_chunks)
loader = DataLoader(tmp, batch_size=1, shuffle=False, drop_last=False) # Loader per wav to get batches
for ctr, (audio, labels) in enumerate(loader):
if features_transform is not None:
audio = features_transform(audio)
if target_transform is not None:
labels = target_transform(labels)
output = model(audio)
if config.oracle_mode:
output = torch.zeros_like(labels) # TODO This is just to get the upper bound of the loss
if config.dataset_multi_track:
output = torch.zeros(size=(labels.shape[0], labels.shape[1], 3*3*12), device=device) # TODO This is just to get the upper bound of the loss wih mACCDOA
loss = solver.loss_fns[solver.loss_names[0]](output, labels)
full_output.append(output)
full_loss.append(loss)
if config.oracle_mode:
full_labels.append(labels) # TODO This is just to get the upper bound of the loss
if torch.isnan(loss):
raise ValueError('ERROR: NaNs in loss')
# Concatenate chunks across timesteps into final predictions
if config.dataset_multi_track:
output = torch.concat(full_output, dim=-2)
else:
if overlap == 1:
output = torch.concat(full_output, dim=-1)
if config.oracle_mode:
output = torch.concat(full_labels, dim=-1) # TODO This is just to get the upper bound of the loss
else:
# TODO: maybe this is ready now? at least until overlap 1/32
# TODO: No, it only works when validating the ground truth labels, but not the final predictions
# Rebuild when using overlap
# This is basically a folding operation, using an average of the predictions of each overlapped chunk
aa = len(full_output) - 1
if config.oracle_mode:
full_output = full_labels # TODO This is just to get the upper bound of the loss
resulton = torch.zeros(aa, labels.shape[-3], labels.shape[-2], labels_padding['full_size'] + labels_padding['padder'].padding[-3])
resulton = torch.zeros(aa, labels.shape[-3], labels.shape[-2],
labels_padding['full_size'] + labels_padding['padder'].padding[-3] + labels_padding['hop_size'])
weights = torch.zeros(1, labels_padding['full_size'] + labels_padding['padder'].padding[-3] + labels_padding['hop_size'])
for ii in range(0, aa):
#print(ii)
start_i = ii * labels_padding['hop_size']
end_i = start_i + round(labels_padding['hop_size'] * 1/1)
end_i = start_i + round(labels_padding['chunk_size'] * 1 / 1)
if end_i > resulton.shape[-1]: # Drop the last part
end_i = resulton.shape[-1]
#yolingon = full_output[ii][0]
try:
resulton[ii, :, :, start_i:end_i] = full_output[ii][0,..., 0:end_i-start_i]
except:
a = 1
warnings.warn('WARNING: Error while evaluating with overlap')
weights[:, start_i:end_i] = weights[:, start_i:end_i] + 1
output = torch.sum(resulton, dim=0, keepdim=True) / weights
if torch.any(torch.isnan(output)):
warnings.warn('WARNING: NaNs detected in output')
# Apply detection threshold based on vector norm
if config.dataset_multi_track:
pass
else:
norms = torch.linalg.vector_norm(output, ord=2, dim=-3, keepdims=True)
norms = (norms < detection_threshold).repeat(1, output.shape[-3], 1, 1)
output[norms] = 0.0
loss = torch.tensor([x for x in full_loss]).mean()
outputs_for_plots.append(output)
# Useful fo debug:
#output.detach().cpu().numpy()[0, 0]
#plots.plot_labels(labels.detach().cpu().numpy()[0])
#target.detach().cpu().numpy()[0]
# Downsample over frames:
if config.dataset_multi_track:
if target_transform is None:
output = nn.functional.interpolate(output.permute(0, 2, 1), scale_factor=(0.1), mode='nearest-exact').permute(0, 2, 1)
else:
if target_transform is None:
output = nn.functional.interpolate(output, scale_factor=(1, 0.1), mode='nearest-exact')
# I think the baseline code needs this in [batch, frames, classes*coords]
output = output.permute([0, 3, 1, 2])
output = output.flatten(2, 3)
if config['dataset_multi_track'] is True:
sed_pred0, doa_pred0, sed_pred1, doa_pred1, sed_pred2, doa_pred2 = get_multi_accdoa_labels(
output.detach().cpu().numpy(), config['unique_classes'])
sed_pred0 = cls_compute_seld_results.reshape_3Dto2D(sed_pred0)
doa_pred0 = cls_compute_seld_results.reshape_3Dto2D(doa_pred0)
sed_pred1 = cls_compute_seld_results.reshape_3Dto2D(sed_pred1)
doa_pred1 = cls_compute_seld_results.reshape_3Dto2D(doa_pred1)
sed_pred2 = cls_compute_seld_results.reshape_3Dto2D(sed_pred2)
doa_pred2 = cls_compute_seld_results.reshape_3Dto2D(doa_pred2)
else:
sed_pred, doa_pred = get_accdoa_labels(output.detach().cpu().numpy(), config['unique_classes'])
sed_pred = cls_compute_seld_results.reshape_3Dto2D(sed_pred)
doa_pred = cls_compute_seld_results.reshape_3Dto2D(doa_pred)
# dump SELD results to the correspondin file
tmp_name = fname.split('/')[-1]
output_file = os.path.join(dcase_output_folder, tmp_name.replace('.wav', '.csv'))
file_cnt += 1
output_dict = {}
if config['dataset_multi_track'] is True:
for frame_cnt in range(sed_pred0.shape[0]):
for class_cnt in range(sed_pred0.shape[1]):
# determine whether track0 is similar to track1
flag_0sim1 = determine_similar_location(sed_pred0[frame_cnt][class_cnt],
sed_pred1[frame_cnt][class_cnt],
doa_pred0[frame_cnt], doa_pred1[frame_cnt],
class_cnt, config['thresh_unify'],
config['unique_classes'])
flag_1sim2 = determine_similar_location(sed_pred1[frame_cnt][class_cnt],
sed_pred2[frame_cnt][class_cnt],
doa_pred1[frame_cnt], doa_pred2[frame_cnt],
class_cnt, config['thresh_unify'],
config['unique_classes'])
flag_2sim0 = determine_similar_location(sed_pred2[frame_cnt][class_cnt],
sed_pred0[frame_cnt][class_cnt],
doa_pred2[frame_cnt], doa_pred0[frame_cnt],
class_cnt, config['thresh_unify'],
config['unique_classes'])
# unify or not unify according to flag
if flag_0sim1 + flag_1sim2 + flag_2sim0 == 0:
if sed_pred0[frame_cnt][class_cnt] > 0.5:
if frame_cnt not in output_dict:
output_dict[frame_cnt] = []
output_dict[frame_cnt].append([class_cnt, doa_pred0[frame_cnt][class_cnt],
doa_pred0[frame_cnt][
class_cnt + config['unique_classes']],
doa_pred0[frame_cnt][
class_cnt + 2 * config['unique_classes']]])
if sed_pred1[frame_cnt][class_cnt] > 0.5:
if frame_cnt not in output_dict:
output_dict[frame_cnt] = []
output_dict[frame_cnt].append([class_cnt, doa_pred1[frame_cnt][class_cnt],
doa_pred1[frame_cnt][
class_cnt + config['unique_classes']],
doa_pred1[frame_cnt][
class_cnt + 2 * config['unique_classes']]])
if sed_pred2[frame_cnt][class_cnt] > 0.5:
if frame_cnt not in output_dict:
output_dict[frame_cnt] = []
output_dict[frame_cnt].append([class_cnt, doa_pred2[frame_cnt][class_cnt],
doa_pred2[frame_cnt][
class_cnt + config['unique_classes']],
doa_pred2[frame_cnt][
class_cnt + 2 * config['unique_classes']]])
elif flag_0sim1 + flag_1sim2 + flag_2sim0 == 1:
if frame_cnt not in output_dict:
output_dict[frame_cnt] = []
if flag_0sim1:
if sed_pred2[frame_cnt][class_cnt] > 0.5:
output_dict[frame_cnt].append([class_cnt, doa_pred2[frame_cnt][class_cnt],
doa_pred2[frame_cnt][
class_cnt + config['unique_classes']],
doa_pred2[frame_cnt][
class_cnt + 2 * config['unique_classes']]])
doa_pred_fc = (doa_pred0[frame_cnt] + doa_pred1[frame_cnt]) / 2
output_dict[frame_cnt].append([class_cnt, doa_pred_fc[class_cnt],
doa_pred_fc[class_cnt + config['unique_classes']],
doa_pred_fc[
class_cnt + 2 * config['unique_classes']]])
elif flag_1sim2:
if sed_pred0[frame_cnt][class_cnt] > 0.5:
output_dict[frame_cnt].append([class_cnt, doa_pred0[frame_cnt][class_cnt],
doa_pred0[frame_cnt][
class_cnt + config['unique_classes']],
doa_pred0[frame_cnt][
class_cnt + 2 * config['unique_classes']]])
doa_pred_fc = (doa_pred1[frame_cnt] + doa_pred2[frame_cnt]) / 2
output_dict[frame_cnt].append([class_cnt, doa_pred_fc[class_cnt],
doa_pred_fc[class_cnt + config['unique_classes']],
doa_pred_fc[
class_cnt + 2 * config['unique_classes']]])
elif flag_2sim0:
if sed_pred1[frame_cnt][class_cnt] > 0.5:
output_dict[frame_cnt].append([class_cnt, doa_pred1[frame_cnt][class_cnt],
doa_pred1[frame_cnt][
class_cnt + config['unique_classes']],
doa_pred1[frame_cnt][
class_cnt + 2 * config['unique_classes']]])
doa_pred_fc = (doa_pred2[frame_cnt] + doa_pred0[frame_cnt]) / 2
output_dict[frame_cnt].append([class_cnt, doa_pred_fc[class_cnt],
doa_pred_fc[class_cnt + config['unique_classes']],
doa_pred_fc[
class_cnt + 2 * config['unique_classes']]])
elif flag_0sim1 + flag_1sim2 + flag_2sim0 >= 2:
if frame_cnt not in output_dict:
output_dict[frame_cnt] = []
doa_pred_fc = (doa_pred0[frame_cnt] + doa_pred1[frame_cnt] + doa_pred2[frame_cnt]) / 3
output_dict[frame_cnt].append([class_cnt, doa_pred_fc[class_cnt],
doa_pred_fc[class_cnt + config['unique_classes']],
doa_pred_fc[class_cnt + 2 * config['unique_classes']]])
output_dict_polar = {}
for k, v in output_dict.items():
ss = []
for this_item in v: