-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset_dlclde.py
1535 lines (1306 loc) · 78.7 KB
/
dataset_dlclde.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 datetime
import json
import os
import pathlib
import shutil
import sys
# import fairseq
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pytz
import scipy
import soundfile as sf
import torch
import torchaudio
import torchaudio.functional as F
from torch.utils.tensorboard import SummaryWriter
from torchvision.utils import make_grid
from PIL import Image
# from maad import util
from tqdm import tqdm
import torch.optim as optim
from sklearn.model_selection import train_test_split
import copy
import suntime
import pytz
from transformers import ClapModel, ClapProcessor
from transformers import pipeline
import models
import utils as u
import torch.optim.lr_scheduler as lr_scheduler
torchaudio.set_audio_backend(backend='soundfile')
# matplotlib.use('TkAgg')
# Get the color map by name:
cm = plt.get_cmap('jet')
class LifeWatchDataset:
def __init__(self, config):
# Spectrogram settings
self.duration = config['duration']
self.overlap = config['overlap'] # overlap of the chunks in %
self.desired_fs = config['desired_fs']
self.channel = config['channel']
self.log = config['log']
self.color = config['color']
# Folders
self.wavs_folder = pathlib.Path(config['wavs_folder'])
self.dclde_folder = pathlib.Path(config['dclde_folder'])
self.dataset_folder = pathlib.Path(config['dataset_folder'])
self.images_folder = self.dataset_folder.joinpath('images')
self.labels_folder = self.dataset_folder.joinpath('labels')
self.d_train_path=config['d_train_path']
self.d_valid_path=config['d_valid_path']
self.annotations_file = config['annotations_file']
self.nfft = config['nfft']
self.win_len = config['win_len']
self.hop_length = int(self.win_len / config['hop_ratio'])
self.win_overlap = self.win_len - self.hop_length
self.normalization_style = config['normalization_style']
if 'min_duration' in config.keys():
self.MIN_DURATION = config['min_duration']
else:
self.MIN_DURATION = self.nfft / self.desired_fs
if 'max_duration' in config.keys():
self.MAX_DURATION = config['max_duration']
else:
self.MAX_DURATION = self.duration / 2
self.MIN_SNR = 10
self.blocksize = int(self.duration * self.desired_fs)
self.config = config
def __setitem__(self, key, value):
if key in self.config.keys():
self.config[key] = value
self.__dict__[key] = value
def save_config(self, config_path):
with open(config_path, 'w') as f:
json.dump(self.config, f)
def create_spectrograms(self, overwrite=False, save_image=True, model=None):
# First, create all the images
for wav_path in tqdm(list(self.wavs_folder.glob('**/*.wav')),
total=len(list(self.wavs_folder.glob('**/*.wav')))):
waveform_info = torchaudio.info(wav_path)
i = 0.0
while (i * self.duration + self.duration) < (waveform_info.num_frames / waveform_info.sample_rate):
img_path = self.images_folder.joinpath(wav_path.name.replace('.wav', '_%s.png' % i))
# img_path = output.joinpath('images', station_name + '_' + wav_name.replace('.wav', '_%s.png' % i))
if overwrite or (not img_path.exists()):
start_chunk = int(i * self.blocksize)
start_chunk_s = start_chunk / self.desired_fs
if waveform_info.sample_rate > self.desired_fs:
start_chunk_old_fs = int(start_chunk_s * waveform_info.sample_rate)
blocksize_old_fs = int(self.duration * waveform_info.sample_rate)
chunk_old_fs, fs = torchaudio.load(wav_path,
normalize=True,
frame_offset=start_chunk_old_fs,
num_frames=blocksize_old_fs)
chunk = F.resample(waveform=chunk_old_fs[0, :], orig_freq=fs, new_freq=self.desired_fs)
else:
chunk, fs = torchaudio.load(wav_path, normalize=True, frame_offset=start_chunk,
num_frames=self.blocksize)
chunk = chunk[0, :]
if len(chunk) == self.blocksize:
if self.normalization_style == 'noisy':
img, f = self.create_chunk_spectrogram_noisy(chunk)
else:
img, f = self.create_chunk_spectrogram_low_freq(chunk)
if model is not None:
results = model(source=np.ascontiguousarray(np.flipud(img)[:, :, ::-1]),
project=str(self.dataset_folder),
name='predictions',
save=False, show=False, save_conf=True, save_txt=False, conf=0.1,
save_crop=False, agnostic_nms=False, stream=False, verbose=False,
imgsz=640, exist_ok=True)
for r in results:
r.save_txt(self.dataset_folder.joinpath('predictions', 'labels', img_path.name.replace('.png', '.txt')))
if save_image:
if self.log:
fig, ax = plt.subplots()
ax.pcolormesh(img[:, :, ::-1])
ax.set_yscale('symlog')
plt.axis('off')
plt.ylim(bottom=3)
plt.savefig(img_path, bbox_inches='tight', pad_inches=0)
else:
Image.fromarray(np.flipud(img)).save(img_path)
plt.close()
i += self.overlap
def create_chunk_spectrogram_noisy(self, chunk):
f, t, sxx = scipy.signal.spectrogram(chunk, fs=self.desired_fs, window=('hamming'),
nperseg=self.win_len,
noverlap=self.win_overlap, nfft=self.nfft,
detrend=False,
return_onesided=True, scaling='density', axis=-1,
mode='magnitude')
sxx = sxx[f > 50, :]
sxx = 10 * np.log10(sxx)
per_min = np.percentile(sxx.flatten(), 1)
per_max = np.percentile(sxx.flatten(), 99)
sxx = (sxx - per_min) / (per_max - per_min)
sxx[sxx < 0] = 0
sxx[sxx > 1] = 1
sxx = cm(sxx) # convert to color
img = np.array(sxx[:, :, :3] * 255, dtype=np.uint8)
return img, f
def create_chunk_spectrogram_low_freq(self, chunk):
sos = scipy.signal.iirfilter(20, [5, 124], rp=None, rs=None, btype='band',
analog=False, ftype='butter', output='sos',
fs=self.desired_fs)
chunk = scipy.signal.sosfilt(sos, chunk)
f, t, sxx = scipy.signal.spectrogram(chunk, fs=self.desired_fs, window=('hamming'),
nperseg=self.win_len,
noverlap=self.win_overlap, nfft=self.nfft,
detrend=False,
return_onesided=True, scaling='density', axis=-1,
mode='magnitude')
sxx = 1 - sxx
per = np.percentile(sxx.flatten(), 98)
sxx = (sxx - sxx.min()) / (per - sxx.min())
sxx[sxx > 1] = 1
img = np.array(sxx * 255, dtype=np.uint8)
return img
def convert_raven_annotations_to_yolo(self, labels_to_exclude=None, values_to_replace=0):
"""
:param annotations_file:
:param labels_to_exclude: list
:param values_to_replace: should be a dict with the name of the Tag as a key and an int as the value, for the
yolo classes
:return:
"""
for selections in self.load_relevant_selection_table(labels_to_exclude=labels_to_exclude):
selections['height'] = (selections['High Freq (Hz)'] - selections['Low Freq (Hz)']) / (self.desired_fs / 2)
# The y is from the TOP!
selections['y'] = 1 - (selections['High Freq (Hz)'] / (self.desired_fs / 2))
# compute the width in pixels
selections['width'] = ((selections['End Time (s)'] - selections['Begin Time (s)']) / self.duration)
# Remove selections smaller than 2 pixels and longer than half the duration
selections = selections.loc[(selections['End Time (s)'] - selections['Begin Time (s)']) < self.MAX_DURATION]
selections = selections.loc[(selections['End Time (s)'] - selections['Begin Time (s)']) > self.MIN_DURATION]
pbar = tqdm(total=len(selections['Begin File'].unique()))
for wav_name, wav_selections in selections.groupby('Begin File'):
if os.path.isdir(self.wavs_folder):
wav_file_path = self.wavs_folder.joinpath(wav_name)
else:
wav_file_path = self.wavs_folder
waveform_info = torchaudio.info(wav_file_path)
fs = waveform_info.sample_rate
waveform_duration = waveform_info.num_frames / fs
# Re-compute the samples to match the new sampling rate
wav_selections['End File Samp (samples)'] = wav_selections[
'End File Samp (samples)'] / fs * self.desired_fs
wav_selections['Beg File Samp (samples)'] = wav_selections[
'Beg File Samp (samples)'] / fs * self.desired_fs
i = 0.0
while (i * self.duration + self.duration) < waveform_duration:
start_sample = int(i * self.blocksize)
chunk_selection = wav_selections.loc[(wav_selections['Beg File Samp (samples)'] >= start_sample) &
(wav_selections[
'Beg File Samp (samples)'] <= start_sample + self.blocksize)]
chunk_selection = chunk_selection.assign(
x=(chunk_selection['Beg File Samp (samples)'] - i * self.blocksize) / self.blocksize)
chunk_selection.loc[
chunk_selection['width'] + chunk_selection['x'] > 1, 'width'] = 1 - chunk_selection['x']
# Save the chunk detections so that they are with the yolo format
# <class > < x > < y > < width > < height >
chunk_selection['x'] = (chunk_selection['x'] + chunk_selection['width'] / 2)
chunk_selection['y'] = (chunk_selection['y'] + chunk_selection['height'] / 2)
if ((chunk_selection.x > 1).sum() > 0) or ((chunk_selection.y > 1).sum() > 0):
print('hey error')
if isinstance(values_to_replace, dict):
chunk_selection = chunk_selection.replace(values_to_replace)
else:
chunk_selection['Tags'] = 0
chunk_selection[[
'Tags',
'x',
'y',
'width',
'height']].to_csv(self.labels_folder.joinpath(wav_name.replace('.wav', '_%s.txt' % i)),
header=None, index=None, sep=' ', mode='w')
# Add the station if the image adds it as well!
i += self.overlap
pbar.update(1)
pbar.close()
def convert_raven_annotations_to_df(self, labels_to_exclude=None, values_to_replace=0):
total_selections = pd.DataFrame()
for selections in self.load_relevant_selection_table(labels_to_exclude=labels_to_exclude):
selections['height'] = (selections['High Freq (Hz)'] - selections['Low Freq (Hz)']) / (self.desired_fs / 2)
# compute the width in pixels
selections['width'] = ((selections['End Time (s)'] - selections['Begin Time (s)']) / self.duration)
# The y is from the TOP!
selections['y'] = 1 - (selections['High Freq (Hz)'] / (self.desired_fs / 2)) + selections['height'] / 2
# Remove selections smaller than 2 pixels and longer than half the duration
selections = selections.loc[(selections['End Time (s)'] - selections['Begin Time (s)']) < self.MAX_DURATION]
selections = selections.loc[(selections['End Time (s)'] - selections['Begin Time (s)']) > self.MIN_DURATION]
selections['wav'] = np.nan
selections['wav_name'] = selections['Begin File']
selections['duration'] = selections.width * self.duration
selections['min_freq'] = 1 - (selections.y + selections.height / 2)
selections['max_freq'] = 1 - (selections.y - selections.height / 2)
if isinstance(values_to_replace, dict):
selections = selections.replace(values_to_replace)
else:
selections['Tags'] = 0
total_selections = pd.concat([total_selections, selections])
return total_selections
def all_predictions_to_dataframe(self, labels_folder, overwrite=True):
detected_foregrounds = pd.DataFrame()
for txt_label in tqdm(labels_folder.glob('*.txt'), total=len(list(labels_folder.glob('*.txt')))):
name_parts = txt_label.name.split('_')
wav_name = '_'.join(name_parts[:-1]) + '.wav'
original_wav = self.wavs_folder.joinpath(wav_name)
offset_seconds = float(name_parts[-1].split('.txt')[0])
detections = pd.read_table(txt_label, header=None, sep=' ', names=['class', 'x', 'y',
'width', 'height', 'confidence'])
detections['wav'] = str(original_wav)
detections['wav_name'] = wav_name
detections['start_seconds'] = (detections.x - detections.width / 2 + offset_seconds) * self.duration
detections['duration'] = detections.width * self.duration
detections['min_freq'] = 1 - (detections.y + detections.height / 2)
detections['max_freq'] = 1 - (detections.y - detections.height / 2)
detections['image'] = txt_label.name.replace('.txt', '')
detected_foregrounds = pd.concat([detected_foregrounds, detections], ignore_index=True)
return detected_foregrounds
def convert_detections_to_raven(self, predictions_folder, add_station_name=False, min_conf=None):
# Convert to DataFrame
detected_foregrounds = self.all_predictions_to_dataframe(predictions_folder.joinpath('labels'))
if min_conf is not None:
detected_foregrounds = detected_foregrounds.loc[detected_foregrounds.confidence >= min_conf]
# Convert to RAVEN format
columns = ['Selection', 'View', 'Channel', 'Begin File', 'End File', 'Begin Time (s)', 'End Time (s)',
'Beg File Samp (samples)', 'End File Samp (samples)', 'Low Freq (Hz)', 'High Freq (Hz)', 'Tags']
detected_foregrounds['fs'] = np.nan
detected_foregrounds['cummulative_sec'] = np.nan
cummulative_seconds = 0
for wav_file_path in list(self.wavs_folder.glob('**/*.wav')):
if add_station_name:
wav_path_name = wav_file_path.parent.parent.parent.name.split('_')[0] + '_' + wav_file_path.name
else:
wav_path_name = wav_file_path.name
waveform_info = torchaudio.info(wav_file_path)
mask = detected_foregrounds['wav_name'] == wav_path_name
detected_foregrounds.loc[mask, 'cummulative_sec'] = cummulative_seconds
detected_foregrounds.loc[mask, 'fs'] = waveform_info.sample_rate
cummulative_seconds += waveform_info.num_frames / waveform_info.sample_rate
detected_foregrounds = detected_foregrounds.reset_index(names='Selection')
detected_foregrounds['Selection'] = detected_foregrounds['Selection'] + 1
detected_foregrounds['View'] = 'Spectrogram 1'
detected_foregrounds['Channel'] = 1
detected_foregrounds['Begin File'] = detected_foregrounds['wav_name']
detected_foregrounds['End File'] = detected_foregrounds['wav_name']
detected_foregrounds['Begin Time (s)'] = detected_foregrounds['start_seconds']
detected_foregrounds['End Time (s)'] = detected_foregrounds['start_seconds'] + detected_foregrounds['duration']
detected_foregrounds['Beg File Samp (samples)'] = (detected_foregrounds['Begin Time (s)']
* detected_foregrounds['fs']).astype(int)
detected_foregrounds['End File Samp (samples)'] = (detected_foregrounds['End Time (s)']
* detected_foregrounds['fs']).astype(int)
detected_foregrounds['Begin Time (s)'] = detected_foregrounds['Begin Time (s)'] + detected_foregrounds[
'cummulative_sec']
detected_foregrounds['End Time (s)'] = detected_foregrounds['End Time (s)'] + detected_foregrounds[
'cummulative_sec']
detected_foregrounds['Low Freq (Hz)'] = detected_foregrounds['min_freq'] * self.desired_fs / 2
detected_foregrounds['High Freq (Hz)'] = detected_foregrounds['max_freq'] * self.desired_fs / 2
detected_foregrounds['Tags'] = detected_foregrounds['class']
detected_foregrounds.loc[detected_foregrounds['Low Freq (Hz)'] < 0, 'Low Freq (Hz)'] = 0
detected_foregrounds = detected_foregrounds.loc[detected_foregrounds['Low Freq (Hz)'] <= self.desired_fs / 2]
detected_foregrounds.loc[detected_foregrounds['High Freq (Hz)'] > self.desired_fs / 2,
'High Freq (Hz)'] = self.desired_fs / 2
detected_foregrounds.to_csv(predictions_folder.joinpath('roi_detections.txt'), sep='\t', index=False)
clean_detections = pd.DataFrame()
for _, class_detections in detected_foregrounds.groupby('Tags'):
clean_detections_class = self.join_overlapping_detections(class_detections)
clean_detections = pd.concat([clean_detections, clean_detections_class])
clean_detections['Selection'] = clean_detections['Selection'].astype(int)
clean_detections_path = predictions_folder.joinpath('roi_detections_clean.txt')
clean_detections.to_csv(clean_detections_path, sep='\t', index=False)
return clean_detections, clean_detections_path
@staticmethod
def join_overlapping_detections(raven_detections_df, iou_threshold=0.5):
# join all the detections overlapping an iou more than the threshold %
selected_ids = []
for wav_file_name, wav_selections in tqdm(raven_detections_df.groupby('Begin File'),
total=len(raven_detections_df['Begin File'].unique())):
already_joined_ids = []
if len(wav_selections) > 1:
# wav_selections = wav_selections.sort_values('Begin Time (s)')
for i, one_selection in wav_selections.iterrows():
selections_to_check = wav_selections.loc[~wav_selections.index.isin(already_joined_ids)].copy()
if i not in already_joined_ids:
min_end = np.minimum(one_selection['End Time (s)'], selections_to_check['End Time (s)'])
max_start = np.maximum(one_selection['Begin Time (s)'], selections_to_check['Begin Time (s)'])
max_bottom = np.maximum(one_selection['Low Freq (Hz)'], selections_to_check['Low Freq (Hz)'])
min_top = np.minimum(one_selection['High Freq (Hz)'], selections_to_check['High Freq (Hz)'])
# possible_overlaps = selections_to_check.loc[(min_end > max_start) & (min_top > max_bottom)]
inter = (min_end - max_start).clip(0) * (min_top - max_bottom).clip(0)
union = ((one_selection['End Time (s)'] - one_selection['Begin Time (s)']) *
(one_selection['High Freq (Hz)'] - one_selection['Low Freq (Hz)'])) + \
((selections_to_check['End Time (s)'] - selections_to_check['Begin Time (s)']) *
(selections_to_check['High Freq (Hz)'] - selections_to_check['Low Freq (Hz)'])) - inter
iou = inter / union
overlapping_selections = selections_to_check.loc[iou > iou_threshold]
if len(overlapping_selections) > 1:
already_joined_ids = np.concatenate([already_joined_ids,
overlapping_selections.index.values])
raven_detections_df.loc[i, 'Begin Time (s)'] = overlapping_selections[
'Begin Time (s)'].min()
raven_detections_df.loc[i, 'End Time (s)'] = overlapping_selections['End Time (s)'].max()
raven_detections_df.loc[i, 'Beg File Samp (samples)'] = overlapping_selections[
'Beg File Samp (samples)'].max()
raven_detections_df.loc[i, 'End File Samp (samples)'] = overlapping_selections[
'End File Samp (samples)'].max()
raven_detections_df.loc[i, 'Low Freq (Hz)'] = overlapping_selections['Low Freq (Hz)'].min()
raven_detections_df.loc[i, 'High Freq (Hz)'] = overlapping_selections[
'High Freq (Hz)'].max()
raven_detections_df.loc[i, 'confidence'] = overlapping_selections['confidence'].max()
else:
already_joined_ids = np.concatenate([already_joined_ids, [i]])
selected_ids.append(i)
else:
selected_ids += [wav_selections.index.values[0]]
cleaned_detections = raven_detections_df.loc[selected_ids]
return cleaned_detections
@staticmethod
def compute_detection_overlap_with_dataset(detected_foregrounds, df_to_compare):
detected_foregrounds['iou'] = np.nan
for i, d in tqdm(detected_foregrounds.iterrows(), total=len(detected_foregrounds)):
w = df_to_compare.width.copy()
w.loc[w > d.width] = d.width
top = df_to_compare.max_freq.copy()
top.loc[top > d.max_freq] = d.max_freq
bottom = df_to_compare.min_freq.copy()
bottom.loc[bottom < d.min_freq] = d.min_freq
iou = (top - bottom) * w / (d.height * d.width)
iou[iou < 0] = 0
detected_foregrounds.loc[i, 'iou'] = np.percentile(iou, 90)
return detected_foregrounds
# def plot_annotation(self, chunk_selection, sxx):
# px_freq = self.desired_fs / self.nfft
# sxx_width = int((self.desired_fs * self.duration) / self.hop_length) + 1
# sxx_height = self.desired_fs / px_freq / 2 + 1
#
# if len(chunk_selection) > 0:
# fig, ax = plt.subplots()
# im = ax.pcolormesh(sxx, cmap='Greys_r')
#
# for _, box in chunk_selection.iterrows():
# rect = patches.Rectangle(xy=(box.x * sxx_width, (1 - (box.y + box.height)) * sxx_height),
# width=box.width * sxx_width, height=box.height * sxx_height,
# linewidth=1, edgecolor='r', facecolor='none')
# ax.add_patch(rect)
# ax.text(box.x * sxx_width, (1 - box.y) * sxx_height, box.Tags)
# plt.title(len(chunk_selection))
# plt.colorbar(im)
# plt.savefig(output.joinpath('detections_bbox', img_path.name))
# plt.close()
def all_snippets(self, detected_foregrounds, labels_to_exclude=None):
file_list = os.listdir(self.wavs_folder)
for i, row in tqdm(detected_foregrounds.iterrows(), total=len(detected_foregrounds)):
wav_path = list(self.wavs_folder.glob('**/' + row['Begin File']))[0]
# wav_path = self.wavs_folder.joinpath(row['Begin File'])
waveform_info = torchaudio.info(wav_path)
# If the selection is in between two files, open both and concatenate them
if row['Beg File Samp (samples)'] > row['End File Samp (samples)']:
waveform1, fs = torchaudio.load(wav_path,
frame_offset=row['Beg File Samp (samples)'],
num_frames=waveform_info.num_frames - row[
'Beg File Samp (samples)'])
wav_path2 = self.wavs_folder.joinpath(file_list[file_list.index(row['Begin File']) + 1])
waveform2, fs = torchaudio.load(wav_path2,
frame_offset=0,
num_frames=row['End File Samp (samples)'])
waveform = torch.cat([waveform1, waveform2], -1)
else:
waveform, fs = torchaudio.load(wav_path,
frame_offset=row['Beg File Samp (samples)'],
num_frames=row['End File Samp (samples)'] - row[
'Beg File Samp (samples)'])
if waveform_info.sample_rate > self.desired_fs:
waveform = F.resample(waveform=waveform, orig_freq=fs, new_freq=self.desired_fs)[self.channel, :]
else:
waveform = waveform[self.channel, :]
yield i, waveform
def encode_aves(self, strategy='mean', labels_to_exclude=None, use_raven_filename=True):
output_name = 'AVES_features_space_%s' % strategy
features_path = self.dataset_folder.joinpath(output_name + '.pt')
if not features_path.exists():
model_list, cfg, task = fairseq.checkpoint_utils.load_model_ensemble_and_task([
'/data/cleap/models/aves-base-bio.pt'])
model = model_list[0]
model.feature_extractor.requires_grad_(False)
model.eval()
features_list = []
idxs = []
for detected_foregrounds in self.load_relevant_selection_table(labels_to_exclude=labels_to_exclude):
detected_foregrounds['height'] = detected_foregrounds[
'High Freq (Hz)'] - detected_foregrounds['Low Freq (Hz)']
detected_foregrounds['width'] = detected_foregrounds[
'End Time (s)'] - detected_foregrounds['Begin Time (s)']
for i, waveform in self.all_snippets(labels_to_exclude=labels_to_exclude):
if strategy == 'mean':
features = model.extract_features(waveform.expand(1, -1))[0].mean(dim=1)
elif strategy == 'max':
features = model.extract_features(waveform.expand(1, -1))[0].max(dim=1).values
else:
raise Exception('Strategy %s is not defined. Only mean or max' % strategy)
features_list.append(features.squeeze(dim=0).detach().numpy())
idxs.append(i)
features_space = torch.Tensor(np.stack(features_list).astype(float))
torch.save(features_space, features_path)
df = pd.DataFrame(features_space.numpy())
df.index = idxs
columns = ['Low Freq (Hz)', 'High Freq (Hz)', 'height', 'width', 'SNR NIST Quick (dB)', 'Tags']
if 'SNR NIST Quick (dB)' not in detected_foregrounds.columns:
columns = ['Low Freq (Hz)', 'High Freq (Hz)', 'height', 'width', 'Tags']
total_df = pd.merge(df, detected_foregrounds[columns],
left_index=True, right_index=True)
total_df = total_df.rename(
columns={'Low Freq (Hz)': 'min_freq', 'High Freq (Hz)': 'max_freq', 'height': 'bandwidth',
'width': 'duration', 'SNR NIST Quick (dB)': 'snr',
'Tags': 'label'})
total_df.to_pickle(self.dataset_folder.joinpath(output_name + '.pkl'))
else:
total_df = pd.read_pickle(self.dataset_folder.joinpath(output_name + '.pkl'))
return total_df
def encode_clap(self, labels_to_exclude=None):
output_name = 'CLAP_features_space'
features_path = self.dataset_folder.joinpath(output_name + '.pkl')
if not features_path.exists():
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = ClapModel.from_pretrained("davidrrobinson/BioLingual",
cache_dir="D:/USERS/clea.parcerisas/models/huggingface").to(device)
processor = ClapProcessor.from_pretrained("davidrrobinson/BioLingual", sampling_rate=self.desired_fs,
cache_dir="D:/USERS/clea.parcerisas/models/huggingface")
features_list = []
idxs = []
for detected_foregrounds in self.load_relevant_selection_table(labels_to_exclude=labels_to_exclude):
detected_foregrounds['height'] = detected_foregrounds[
'High Freq (Hz)'] - detected_foregrounds['Low Freq (Hz)']
detected_foregrounds['width'] = detected_foregrounds[
'End Time (s)'] - detected_foregrounds['Begin Time (s)']
for i, waveform in self.all_snippets(detected_foregrounds=detected_foregrounds,
labels_to_exclude=labels_to_exclude):
inputs = processor(audios=waveform, return_tensors="pt", sampling_rate=self.desired_fs).to(device)
audio_embed = model.get_audio_features(**inputs)
features_list.append(audio_embed.squeeze(dim=0).detach().numpy())
idxs.append(i)
features_space = torch.Tensor(np.stack(features_list).astype(float))
torch.save(features_space, features_path)
df = pd.DataFrame(features_space.numpy())
df.index = idxs
columns = ['Low Freq (Hz)', 'High Freq (Hz)', 'height', 'width', 'SNR NIST Quick (dB)', 'Tags']
if 'SNR NIST Quick (dB)' not in detected_foregrounds.columns:
columns = ['Low Freq (Hz)', 'High Freq (Hz)', 'height', 'width', 'Tags']
total_df = pd.merge(df, detected_foregrounds[columns],
left_index=True, right_index=True)
total_df = total_df.rename(
columns={'Low Freq (Hz)': 'min_freq', 'High Freq (Hz)': 'max_freq', 'height': 'bandwidth',
'width': 'duration', 'SNR NIST Quick (dB)': 'snr',
'Tags': 'label'})
total_df.to_pickle(self.dataset_folder.joinpath(output_name + '.pkl'))
else:
total_df = pd.read_pickle(self.dataset_folder.joinpath(output_name + '.pkl'))
return total_df
def zero_shot_learning(self, labels_to_exclude=None):
output_name = 'zero_shot_space'
features_path = self.dataset_folder.joinpath(output_name + '.pkl')
if not features_path.exists():
audio_classifier = pipeline(task="zero-shot-audio-classification", model="davidrrobinson/BioLingual")
features_list = []
idxs = []
for detected_foregrounds in self.load_relevant_selection_table(labels_to_exclude=labels_to_exclude):
detected_foregrounds['height'] = detected_foregrounds[
'High Freq (Hz)'] - detected_foregrounds['Low Freq (Hz)']
detected_foregrounds['width'] = detected_foregrounds[
'End Time (s)'] - detected_foregrounds['Begin Time (s)']
detected_foregrounds = detected_foregrounds.loc[~detected_foregrounds.Tags.isna()]
detected_foregrounds = detected_foregrounds.replace({'jackhammer': 'repetitive sound of a fish',
'grunt': 'grunting sound of a fish',
'whistle': 'whistle sound of proably a dolphin',
'loud_ship': 'sound of a ship passing by',
'tap_dancing': 'pseudo-noise of invertebrates tapping the hydrophone',
'metallic_bell': 'metallic sound like a bell with harmonics',
'metallic_bell_nh': 'quiet metallic sound like a bell with no harmonics',
'siren': 'sound like a siren',
'hf_click': 'high frequency impulsive broaddband sound',
'castanets': 'sound like repetitive castanets',
'low_tap': 'low frequency impulsive sound',
'rain': 'rain like sound',
'chirp': 'seal chirp',
'constant_freq': 'sound of constant frequency',
'wave': 'sound of wave passing by',
'boat_background': 'sound of a boat in the background',
'woop': 'three consecutive woop sounds,maybe fish'})
candidates_labels = detected_foregrounds.Tags.unique()
for i, waveform in self.all_snippets(detected_foregrounds=detected_foregrounds,
labels_to_exclude=labels_to_exclude):
output = audio_classifier(waveform.numpy(), candidate_labels=candidates_labels)
max_score = 0
max_label = ''
for o in output:
if o['score'] > max_score:
max_label = o['label']
max_score = o['score']
features_list.append(max_label)
idxs.append(i)
df = pd.DataFrame(features_list)
df.index = idxs
columns = ['Low Freq (Hz)', 'High Freq (Hz)', 'height', 'width', 'SNR NIST Quick (dB)', 'Tags']
if 'SNR NIST Quick (dB)' not in detected_foregrounds.columns:
columns = ['Low Freq (Hz)', 'High Freq (Hz)', 'height', 'width', 'Tags']
total_df = pd.merge(df, detected_foregrounds[columns],
left_index=True, right_index=True)
total_df = total_df.rename(
columns={'Low Freq (Hz)': 'min_freq', 'High Freq (Hz)': 'max_freq', 'height': 'bandwidth',
'width': 'duration', 'SNR NIST Quick (dB)': 'snr',
'Tags': 'label'})
total_df.to_pickle(self.dataset_folder.joinpath(output_name + '.pkl'))
else:
total_df = pd.read_pickle(self.dataset_folder.joinpath(output_name + '.pkl'))
return total_df
def load_relevant_selection_table(self, labels_to_exclude=None):
annotations_file = pathlib.Path(self.annotations_file)
if annotations_file.is_dir():
selections_list = list[annotations_file.glob('*.txt')]
else:
selections_list = [annotations_file]
for selection_table_path in selections_list:
selections = pd.read_table(selection_table_path)
if 'Tags' in selections.columns:
if labels_to_exclude is not None:
selections = selections.loc[~selections.Tags.isin(labels_to_exclude)]
# Filter the selections
selections = selections.loc[selections['Low Freq (Hz)'] < (self.desired_fs / 2)]
selections = selections.loc[selections.View == 'Spectrogram 1']
if 'SNR NIST Quick (dB)' in selections.columns:
selections = selections.loc[selections['SNR NIST Quick (dB)'] > self.MIN_SNR]
selections.loc[selections['High Freq (Hz)'] > (self.desired_fs / 2), 'High Freq (Hz)'] = self.desired_fs / 2
selections = selections.loc[(selections['End Time (s)'] - selections['Begin Time (s)']) >= self.MIN_DURATION]
selections = selections.loc[(selections['End Time (s)'] - selections['Begin Time (s)']) <= self.MAX_DURATION]
yield selections
def convert_raven_to_ae_format(self, labels_to_exclude=None):
total_encoder = pd.DataFrame()
for selection_table in self.load_relevant_selection_table(labels_to_exclude=labels_to_exclude):
for wav_file, wav_selections in selection_table.groupby('Begin File'):
wav = sf.SoundFile(self.wavs_folder.joinpath(wav_file))
duration = wav_selections['End Time (s)'] - wav_selections['Begin Time (s)']
two_files = wav_selections['Beg File Samp (samples)'] > wav_selections['End File Samp (samples)']
pos = wav_selections['Beg File Samp (samples)'] / wav.samplerate + duration / 2
encoder_df = pd.DataFrame({'begin_sample': wav_selections['Beg File Samp (samples)'],
'end_sample': wav_selections['End File Samp (samples)'],
'pos': pos,
'duration': duration,
'filename': wav_selections['Begin File'],
'label': wav_selections['Tags'],
'min_freq': wav_selections['Low Freq (Hz)'],
'max_freq': wav_selections['High Freq (Hz)'],
'two_files': two_files})
if 'SNR NIST Quick (dB)' in wav_selections.columns:
encoder_df['snr'] = wav_selections['SNR NIST Quick (dB)']
total_encoder = pd.concat([total_encoder, encoder_df])
return total_encoder
def encode_ae(self, model_path, nfft, sample_dur, n_mel, bottleneck, labels_to_exclude=None, input_type='fixed',):
self.MIN_DURATION = nfft / self.desired_fs
features_path = self.dataset_folder.joinpath('CAE_%s_%s_%s_%s_%s_features_space.pkl' %
(input_type, nfft, sample_dur, n_mel, bottleneck))
if not features_path.exists():
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
if input_type == 'fixed':
frontend = models.frontend(sr=self.desired_fs, nfft=nfft, sampleDur=sample_dur, n_mel=n_mel).to(device)
elif input_type == 'crops_duration':
frontend = models.frontend_crop_duration(sr=self.desired_fs, nfft=nfft,
sampleDur=sample_dur, n_mel=n_mel).to(device)
elif input_type == 'crops':
frontend = models.frontend_crop()
encoder = models.sparrow_encoder(bottleneck // (n_mel // 32 * 4), (n_mel // 32, 4))
decoder = models.sparrow_decoder(bottleneck, (n_mel // 32, 4))
model = torch.nn.Sequential(encoder, decoder).to(device)
model.load_state_dict(torch.load(model_path))
model.eval()
detections = self.convert_raven_to_ae_format(labels_to_exclude=labels_to_exclude)
if input_type == 'fixed':
annotations_ds = u.Dataset(df=detections, audiopath=str(self.wavs_folder), sr=self.desired_fs,
sampleDur=sample_dur, channel=self.channel)
elif input_type == 'crops_duration':
annotations_ds = u.DatasetCropsDuration(detections, str(self.wavs_folder), self.desired_fs,
winsize=nfft, n_mel=n_mel, win_overlap=self.win_overlap,
sampleDur=sample_dur)
elif input_type == 'crops':
annotations_ds = u.DatasetCrops(detections, str(self.wavs_folder), self.desired_fs,
winsize=nfft, n_mel=n_mel, win_overlap=self.win_overlap,
sampleDur=sample_dur)
loader = torch.utils.data.DataLoader(annotations_ds, batch_size=16, shuffle=False,
num_workers=0, collate_fn=u.collate_fn)
encodings, idxs = [], []
with torch.no_grad():
for x, name in tqdm(loader, leave=False):
label = frontend(x.to(device))
encoding = model[:1](label)
idxs.extend(name.numpy())
encodings.extend(encoding.cpu().detach())
encodings = np.stack(encodings)
features_space = torch.Tensor(np.stack([encodings]).astype(float))
features_space = pd.DataFrame(features_space.numpy()[0])
idxs = np.stack(idxs)
features_space.index = idxs
columns = ['min_freq', 'max_freq', 'duration', 'label', 'snr']
if 'snr' not in detections.columns:
columns = ['min_freq', 'max_freq', 'duration', 'label']
total_features = pd.merge(features_space, detections[columns],
left_index=True, right_index=True)
total_features['bandwidth'] = total_features['max_freq'] - total_features['min_freq']
total_features.to_pickle(features_path)
else:
total_features = pd.read_pickle(features_path)
return total_features
# def train_ae(self, sample_dur=5, bottleneck=48, n_mel=128, nfft=2048, input_type='fixed'):
# device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# encoder = models.sparrow_encoder(bottleneck // (n_mel // 32 * 4), (n_mel // 32, 4))
# decoder = models.sparrow_decoder(bottleneck, (n_mel // 32, 4))
# model = torch.nn.Sequential(encoder, decoder).to(device)
# if input_type == 'fixed':
# frontend = models.frontend(sr=self.desired_fs, nfft=nfft, sampleDur=sample_dur, n_mel=n_mel).to(device)
# elif input_type == 'crops_duration':
# frontend = models.frontend_crop_duration(sr=self.desired_fs, nfft=nfft,
# sampleDur=sample_dur, n_mel=n_mel).to(device)
# elif input_type == 'crops':
# frontend = models.frontend_crop()
# # training / optimisation setup
# lr, wdL2, batch_size = 0.00001, 0.0, 64 if torch.cuda.is_available() else 16
# optimizer = torch.optim.AdamW(model.parameters(), weight_decay=wdL2, lr=lr, betas=(0.8, 0.999))
# scheduler = torch.optim.lr_scheduler.LambdaLR(optimizer, lambda epoch: .99 ** epoch)
# vgg16 = models.vgg16.eval().to(device)
# loss_fun = torch.nn.MSELoss()
# detections = self.convert_raven_to_ae_format(labels_to_exclude=None)
# if input_type == 'fixed':
# annotations_ds = u.Dataset(df=detections, audiopath=str(self.wavs_folder), sr=self.desired_fs,
# sampleDur=sample_dur, channel=self.channel)
# elif input_type == 'crops_duration':
# annotations_ds = u.DatasetCropsDuration(detections, str(self.wavs_folder), self.desired_fs,
# winsize=nfft, n_mel=n_mel, win_overlap=self.win_overlap,
# sampleDur=sample_dur)
# elif input_type == 'crops':
# annotations_ds = u.DatasetCrops(detections, str(self.wavs_folder), self.desired_fs,
# winsize=nfft, n_mel=n_mel, win_overlap=self.win_overlap,
# sampleDur=sample_dur)
# loader = torch.utils.data.DataLoader(annotations_ds, batch_size=16,
# shuffle=False, num_workers=0, collate_fn=u.collate_fn)
# modelname = f'CAE_{input_type}_{bottleneck}_mel{n_mel}'
# step, writer = 0, SummaryWriter(str(self.dataset_folder.joinpath(modelname)))
# print(f'Go for model {modelname} with {len(detections)} vocalizations')
# for epoch in range(100):
# for x, name in tqdm(loader, desc=str(epoch), leave=False):
# optimizer.zero_grad()
# label = frontend(x.to(device))
# x = encoder(label)
# pred = decoder(x)
# vgg_pred = vgg16(pred.expand(pred.shape[0], 3, *pred.shape[2:]))
# vgg_label = vgg16(label.expand(label.shape[0], 3, *label.shape[2:]))
# score = loss_fun(vgg_pred, vgg_label)
# score.backward()
# optimizer.step()
# writer.add_scalar('loss', score.item(), step)
# if step % 50 == 0:
# images = [(e - e.min()) / (e.max() - e.min()) for e in label[:8]]
# grid = make_grid(images)
# writer.add_image('target', grid, step)
# writer.add_embedding(x.detach(), global_step=step, label_img=label)
# images = [(e - e.min()) / (e.max() - e.min()) for e in pred[:8]]
# grid = make_grid(images)
# writer.add_image('reconstruct', grid, step)
# step += 1
# if step % 500 == 0:
# scheduler.step()
# torch.save(model.state_dict(), str(self.dataset_folder.joinpath(modelname + '.weights')))
# return str(self.dataset_folder.joinpath(modelname + '.weights'))
def train_clap(self, model_path='davidrrobinson/BioLingual', epochs=1, lr=1e-5,
batch_size=8, stop_shuffle=False, sample_dur=10):
self.desired_fs = 48000
# device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
if torch.cuda.is_available():
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print("Selected CUDA device:", torch.cuda.get_device_name(device))
else:
print("CUDA is not available. Using CPU.")
# device =torch.device('cuda:0')
weights = {
'distance_weight': 1,
'speed_weight': 0,
'activity_weight': 0,
'vessel_type_weight': 0
}
comment="filtered_classes_window4"
weights_str = '_'.join([f"{u.float_to_string(value)}" for _, value in weights.items()])+"_"+ comment
log_path = f'roi/BioLingual/logs_{weights_str}.log'
log_file = open(log_path, mode='w')
# detections = self.convert_raven_to_ae_format(labels_to_exclude=None)
# detections = detections.loc[~detections.label.isna()]
d_train_path= "data/train.txt"
d_valid_path= "data/val.txt"
d_test_path= "data/test.txt"
d_dclde_path= "data/test_dclde.txt"
d_dclde_train= "data/train_dclde.txt"
# print("path ", d_train_path)
split = np.genfromtxt(d_train_path, dtype='str', delimiter=' ')
d_train_loc = np.array([os.path.join(self.wavs_folder, i) for i in split[:, 0]])
split = np.genfromtxt(d_valid_path, dtype='str', delimiter=' ')
d_valid_loc = np.array([os.path.join(self.wavs_folder, i) for i in split[:, 0]])
split = np.genfromtxt(d_test_path, dtype='str', delimiter=' ')
d_test_loc = np.array([os.path.join(self.wavs_folder, i) for i in split[:, 0]])
split = np.genfromtxt(d_dclde_path, dtype='str', delimiter=' ')
d_dclde_loc = np.array([os.path.join(self.dclde_folder, i) for i in split[:, 0]])
split = np.genfromtxt(d_dclde_train, dtype='str', delimiter=' ')
d_dclde_train_loc = np.array([os.path.join(self.dclde_folder, i) for i in split[:, 0]])
# split = np.genfromtxt(self.d_train_path, dtype='str', delimiter=' ')
# d_train_loc = np.array([os.path.join(self.wavs_folder, i) for i in split[:, 0]])
# split = np.genfromtxt(self.d_valid_path, dtype='str', delimiter=' ')
# d_valid_loc = np.array([os.path.join(self.wavs_folder, i) for i in split[:, 0]])
d_train=u.process_filenames(d_train_loc).iloc[0:400]
d_valid=u.process_filenames(d_valid_loc).iloc[0:100]
d_test=u.process_filenames(d_test_loc).iloc[0:100]
d_dclde=u.process_filenames_dclde(d_dclde_loc).iloc[0:100]
d_dclde_train=u.process_filenames_dclde(d_dclde_train_loc).iloc[0:100]
# d_train=d_train[0:100]
# ship_type_categories = {
# 'Tanker': 'Tanker',
# 'Cargo': 'Cargo',
# 'Dredging': 'Dredging',
# 'High-Speed-Craft': 'High-Speed-Craft',
# 'Fishing': 'Fishing',
# 'Passenger': 'Passenger',
# 'Tug': 'Tug',
# 'Pilot': 'Pilot'
# }
# # Group ship types into specified categories, labeling others as 'Other'
# d_train['ship_type'] = d_train['ship_type'].apply(lambda x: ship_type_categories.get(x, 'Other'))
# d_test['ship_type'] = d_test['ship_type'].apply(lambda x: ship_type_categories.get(x, 'Other'))
# d_valid['ship_type'] = d_valid['ship_type'].apply(lambda x: ship_type_categories.get(x, 'Other'))
# ship_activity_categories = {
# 'underway-using-engine': 'underway-using-engine',
# 'restricted-maneuverability': 'restricted-maneuverability',
# 'moored': 'moored',
# 'engaged-in-fishing': 'engaged-in-fishing',
# 'constrained-by-her-draught': 'restricted-maneuverability'
# }
# # # Group ship types into specified categories
# d_train['activity'] = d_train['activity'].apply(lambda x: ship_activity_categories.get(x))
# d_test['activity'] = d_test['activity'].apply(lambda x: ship_activity_categories.get(x))
# d_valid['activity'] = d_valid['activity'].apply(lambda x: ship_activity_categories.get(x))
# # Filter out rows with ship types not in the specified categories
# d_train = d_train.dropna(subset=['activity'])
# d_test = d_test.dropna(subset=['activity'])
# d_valid = d_valid.dropna(subset=['activity'])
# ship_type_categories = {
# 'underway-using-engine': 'underway-using-engine',
# 'restricted-maneuverability': 'restricted-maneuverability',
# 'moored': 'moored',
# 'engaged-in-fishing': 'engaged-in-fishing',
# 'constrained-by-her-draught': 'restricted-maneuverability',
# 'at-anchor': 'restricted-maneuverability'
# }
# # Group ship types into specified categories, labeling others as 'Other'
# d_train['ship_type_category'] = d_train['ship_type'].apply(lambda x: ship_type_categories.get(x, 'underway-using-engine'))
# d_test['ship_type_category'] = d_test['ship_type'].apply(lambda x: ship_type_categories.get(x, 'underway-using-engine'))
# d_valid['ship_type_category'] = d_valid['ship_type'].apply(lambda x: ship_type_categories.get(x, 'underway-using-engine'))
train_labels = set(d_train["label"])
# Count the occurrences of each label in d_train
label_counts_train = d_train["label"].value_counts()
# Filter out labels which have less than 5 samples in d_train
valid_train_labels = label_counts_train[label_counts_train >= 5].index
# Filter d_train and d_valid based on valid_train_labels
d_train = d_train[d_train["label"].isin(valid_train_labels)]
d_valid = d_valid[d_valid["label"].isin(valid_train_labels)]
d_test = d_test[d_test["label"].isin(valid_train_labels)]
d_dclde= d_dclde[d_dclde["label"].isin(valid_train_labels)]
d_dclde_train= d_dclde_train[d_dclde_train["label"].isin(valid_train_labels)]
# Display the number of unique classes
num_classes = len(valid_train_labels)
ids={lbl: i for i, lbl in enumerate(d_train['label'].unique())}
similarity_matrix=u.similarity_distance(ids,device)
# similarity_matrix_distance=u.similarity(ids,device,distance_weight = 1)
# similarity_matrix_speed=u.similarity(ids,device,speed_weight = 1)
# similarity_matrix_activity=u.similarity(ids,device,activity_weight = 1)
# similarity_matrix_type=u.similarity(ids,device,vessel_type_weight = 1)
model = models.CLAPClassifier(model_path, num_classes, sr=self.desired_fs, device=device, similarity_matrix=similarity_matrix, multi_label=False)
dataloader_train = torch.utils.data.DataLoader(
dataset=u.DatasetWaveform(df=d_train, wavs_folder=self.wavs_folder, desired_fs=self.desired_fs,
max_duration=sample_dur,ids=ids),
batch_size=batch_size,
shuffle=not stop_shuffle)
dataloader_val = torch.utils.data.DataLoader(
dataset=u.DatasetWaveform(df=d_valid, wavs_folder=self.wavs_folder, desired_fs=self.desired_fs,
max_duration=sample_dur,ids=ids),
batch_size=batch_size,
shuffle=not stop_shuffle)
dataloader_test = torch.utils.data.DataLoader(
dataset=u.DatasetWaveform(df=d_test, wavs_folder=self.wavs_folder, desired_fs=self.desired_fs,
max_duration=sample_dur,ids=ids),
batch_size=batch_size,
shuffle=not stop_shuffle)
dataloader_dclde_test = torch.utils.data.DataLoader(
dataset=u.DatasetWaveform(df=d_dclde, wavs_folder=self.dclde_folder, desired_fs=self.desired_fs,
max_duration=sample_dur,ids=ids),
batch_size=batch_size,
shuffle=not stop_shuffle)
dataloader_dclde_train = torch.utils.data.DataLoader(
dataset=u.DatasetWaveform(df=d_dclde_train, wavs_folder=self.dclde_folder, desired_fs=self.desired_fs,
max_duration=sample_dur,ids=ids),
batch_size=batch_size,
shuffle=not stop_shuffle)
valid_metric_best = 0.
valid_metric_previous=0
best_model = None
log_file.write("lr = {}\n".format(lr))
# print("lr = {}".format(lr), file=log_file)