-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_trainingSet.py
213 lines (159 loc) · 7.27 KB
/
create_trainingSet.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
import os
import re
import sys
import json
import librosa
import multiprocessing
import numpy as np
import pandas as pd
import soundfile as sf
from buzzcode.utils import save_pickle, setthreads
from buzzcode.audio import frame_audio
from buzzcode.analysis import melt_coverage
setthreads(1)
def overlaps(range_frame, range_event, overlap_min):
range_overlap = (range_frame[0] + overlap_min, range_frame[1] - overlap_min)
# if event ends before frame starts or event starts after frame ends, False
if (range_event[1] < range_overlap[0]) or (range_event[0] > range_overlap[1]):
return False
else:
# otherwise, it must be overlapping, no matter how!
return True
# dir_annotations = './localData/annotations/2024-03-15'; metadataname = 'metadata_intermediate_new.csv'; framehop=0.2; overlap_annotation=0.4; overlap_event=0.1; embeddername='yamnet'; cpus=7; setname=None
def cache_training(dir_annotations, metadataname, framehop=0.2, overlap_annotation=0.4, overlap_event=0.1, setname = None,
embeddername='yamnet', cpus=4):
metadataname = os.path.splitext(metadataname)[0] # drop file extension, if given
if setname is None:
setname = embeddername + '_' + re.search('metadata_(.*)', metadataname).group(1)
dir_set = './training/' + setname
os.makedirs(dir_set)
metadata = pd.read_csv(os.path.join(dir_annotations, metadataname + '.csv'))
classes = sorted(metadata['classification'].unique())
metadata.to_csv(os.path.join(dir_set, 'metadata.csv'), index=False)
dir_raw = './localData/raw experiment audio' # WITHOUT trailing slash, for creating path_raw
idents = metadata['ident'].unique()
q_idents = multiprocessing.Queue()
for i in idents:
q_idents.put(i)
for _ in range(cpus):
q_idents.put('terminate')
q_data = multiprocessing.Queue()
q_config = multiprocessing.Queue() # TODO: use more appropriate method than queue!
def worker_constructor(worker_id):
# TODO: figure out why I can't import get_embedder in parent context!
from buzzcode.embeddings import load_embedder
embedder, config = load_embedder(embeddername)
q_config.put(config)
framelength = config['framelength']
annotation_extension = ((1 - overlap_annotation) * framelength)
ident = q_idents.get()
while ident != 'terminate':
print(f'constructor {worker_id}: starting ident {ident}')
data_ident = []
path_raw = dir_raw + ident + '.mp3'
track = sf.SoundFile(path_raw)
sr_native = track.samplerate
audio_duration = librosa.get_duration(path=path_raw)
sub = metadata[metadata['ident'] == ident].copy()
fold = sub['fold'].unique()
if len(fold) > 1:
print(f'constructor {worker_id}: FATAL ERROR, multiple folds for {ident}')
sys.exit(0)
coverage = melt_coverage(sub)
for c, chunk in enumerate(coverage):
print(f'constructor {worker_id}: starting chunk {(round(chunk[0], 1), round(chunk[1], 1))}')
start = max(
chunk[0] - annotation_extension,
0 # if start < 0, round up to 0
)
end = min(
chunk[1] + (annotation_extension * 1.1), # rounding makes the extension slightly low sometimes
audio_duration # if end > file length, round down to file length
)
if (end - start) < framelength:
print(
f'constructor {worker_id}: unexpandable sub-frame audio; skipping') # ugh, this could be avoided if I also framed backwards
continue
start_sample = round(sr_native * start)
samples_to_read = round(sr_native * (end - start))
track.seek(start_sample)
audio_data = track.read(samples_to_read)
audio_data = librosa.resample(y=audio_data, orig_sr=sr_native, target_sr=config['samplerate'])
frames = frame_audio(
audio_data=audio_data,
framelength=framelength,
samplerate=config['samplerate'],
framehop=framehop
)
embeddings = embedder(frames)
events = []
for i, frame in enumerate(frames):
s = start + (i * framehop * framelength)
e = s + framelength
sub['overlaps'] = [overlaps((s, e), (e_start, e_end), framelength * overlap_event) for
e_start, e_end in zip(sub['start'], sub['end'])]
e = sub[sub['overlaps']]['classification'].unique().tolist()
if e == []:
print(f'!!! NULL EVENT IN {ident} {chunk}, FRAME {i} !!!')
events.append(e)
data_ident.extend(list(zip(embeddings, events, [fold]*len(frames))))
q_data.put(data_ident)
ident = q_idents.get()
print(f'constructor {worker_id}: terminate signal received; exiting')
def worker_writer():
alldata = []
idents_remaining = len(idents)
while idents_remaining > 0:
print(f'writer: idents remaining: {idents_remaining}')
data_ident = q_data.get()
idents_remaining -= 1
alldata.extend(data_ident)
print('writer: all data received; writing')
inputs, events, folds = zip(*alldata) # outputs as tuples
y_blank = [0 for _ in classes]
def events_to_targets(events_in):
y_true = y_blank.copy()
for event in events_in:
y_true[classes.index(event)] = 1
return np.array(y_true, dtype=np.int64)
targets = [events_to_targets(e) for e in events]
data_full = (list(inputs), targets, list(events), list(folds))
save_pickle(os.path.join(dir_set, 'data_cache'), data_full)
print('writer: saving config')
config = {
'setname': setname,
'overlap_annotation': overlap_annotation,
'overlap_event': overlap_event,
'classes': classes
}
config.update(q_config.get())
with open(os.path.join(dir_set, 'config.txt'), 'x') as f:
f.write(json.dumps(config))
print('writer: exiting')
proc_writer = multiprocessing.Process(target=worker_writer)
proc_writer.start()
proc_constructors = []
for a in range(cpus):
proc_constructors.append(
multiprocessing.Process(target=worker_constructor, name=f"constructor_proc{a}", args=([a])))
proc_constructors[-1].start()
pass
proc_writer.join()
if __name__ == '__main__':
# full re-set
cpus = 2
cache_training(
dir_annotations='./localData/annotations/2024-03-15',
metadataname='metadata_intermediate_new',
cpus=cpus
)
cache_training(
dir_annotations='./localData/annotations/2024-03-15',
metadataname='metadata_intermediate',
cpus=cpus
)
cache_training(
dir_annotations='./localData/annotations/2024-03-15',
metadataname='metadata_strict',
cpus=cpus
)