-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate_fixed_list.py
More file actions
312 lines (264 loc) · 12.5 KB
/
create_fixed_list.py
File metadata and controls
312 lines (264 loc) · 12.5 KB
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
import torch
from audiodataloader import AudioDataLoader, AudioSegment,split_list_after_speaker
import pickle
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
import librosa
from dataclasses import dataclass
import data_augmentation
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
from sklearn.preprocessing import MinMaxScaler
import cv2
@dataclass
class TrainSegment:
start_time: float
end_time: float
mfcc: np.ndarray
mel: np.ndarray
stt: np.ndarray
sample_rate: int
label_word: str #word for example "sonne"
label_path: str # sigmatism or normal
path: str # which file it is from
augmented: str #if that was augmented with pitch/noise
def compute_mfcc_features(signal, sample_rate, n_mfcc=112, n_mels=128, frame_size=25e-3, hop_size=5e-3, n_fft=2048):
try:
# Convert frame and hop size from seconds to samples
frame_length = int(frame_size * sample_rate)
hop_length = int(hop_size * sample_rate)
# Compute the static MFCCs using librosa's mfcc function
mfccs = librosa.feature.mfcc(y=signal, sr=sample_rate, n_mfcc=n_mfcc,
n_fft=n_fft, hop_length=hop_length, win_length=frame_length, n_mels=n_mels)
# Compute the first-order difference (Delta MFCCs) using a 5-frame window
mfcc_delta = librosa.feature.delta(mfccs, width=5)
# Compute the second-order difference (Delta-Delta MFCCs)
#mfcc_delta2 = librosa.feature.delta(mfccs, order=2, width=3)
# Concatenate static, delta, and delta-delta features to form a 24-dimensional feature vector per frame
mfcc_features = np.concatenate([mfccs, mfcc_delta], axis=0)
return mfcc_features
except:
print("ERROR: ",np.shape(signal), signal.size)
def compute_melspectogram_features(signal, sample_rate=16000, n_mels=128, frame_size=25e-3, hop_size=5e-3, n_fft=2048):
# Convert frame and hop size from seconds to samples
frame_length = int(frame_size * sample_rate)
hop_length = int(hop_size * sample_rate)
mel_spectrogram = librosa.feature.melspectrogram(y=signal, sr=sample_rate, n_mels=n_mels,n_fft=n_fft,hop_length=hop_length,win_length=frame_length)
mel_spectrogram_db = librosa.power_to_db(mel_spectrogram, ref=np.max)
return mel_spectrogram_db
def make_STT_heatmap(audio,processor,model):
inputs = processor(audio, sampling_rate=16000, return_tensors="pt", padding=True)
with torch.no_grad():
logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
return logits
def create_list(word_segments):
"""
creates the list for training
for every word stt heatmap, mel and mfcc are created
modifies every word 3 times and puts it in the list 3 times
"""
MODEL_ID = "jonatasgrosman/wav2vec2-large-xlsr-53-german"
processor = Wav2Vec2Processor.from_pretrained(MODEL_ID)
model = Wav2Vec2ForCTC.from_pretrained(MODEL_ID)
feature_dim={
"n_mfcc":112,
"n_mels":128,
"frame_size":0.025,
"hop_size":0.005,
"n_fft":2048,
"target_length": 224
}
output_file = "mother_list_augment.pkl"
data = []
for entry in tqdm(word_segments):
start_time = entry.start_time
end_time = entry.end_time
audio_normal = entry.audio_data
sample_rate = entry.sample_rate
label_word = entry.label
label_path = entry.label_path
path = entry.path
#Put every word 3 times in the list. Normal , with noise, with pitch
audio_noise = data_augmentation.add_gaussian_noise(audio_normal,sample_rate)
audio_pitch = data_augmentation.pitch_shift(audio_normal,sample_rate)
"""mel specto"""
feature_normal_mel = compute_melspectogram_features(audio_normal,sample_rate,feature_dim["n_mels"],feature_dim["frame_size"],feature_dim["hop_size"],feature_dim["n_fft"])
feature_noise_mel = compute_melspectogram_features(audio_noise,sample_rate,feature_dim["n_mels"],feature_dim["frame_size"],feature_dim["hop_size"],feature_dim["n_fft"])
feature_pitch_mel = compute_melspectogram_features(audio_pitch,sample_rate,feature_dim["n_mels"],feature_dim["frame_size"],feature_dim["hop_size"],feature_dim["n_fft"])
"""mfcc"""
feature_normal_mfcc = compute_mfcc_features(audio_normal,sample_rate,feature_dim["n_mfcc"],feature_dim["n_mels"],feature_dim["frame_size"],feature_dim["hop_size"],feature_dim["n_fft"])
feature_noise_mfcc = compute_mfcc_features(audio_noise,sample_rate,feature_dim["n_mfcc"],feature_dim["n_mels"],feature_dim["frame_size"],feature_dim["hop_size"],feature_dim["n_fft"])
feature_pitch_mfcc = compute_mfcc_features(audio_pitch,sample_rate,feature_dim["n_mfcc"],feature_dim["n_mels"],feature_dim["frame_size"],feature_dim["hop_size"],feature_dim["n_fft"])
"""STT"""
feature_normal_stt = make_STT_heatmap(audio_normal,processor,model)
feature_noise_stt = make_STT_heatmap(audio_noise,processor,model)
feature_pitch_stt = make_STT_heatmap(audio_pitch,processor,model)
featur_object = TrainSegment(
start_time = start_time,
end_time = end_time,
mfcc = feature_normal_mfcc,
mel = feature_normal_mel,
stt = feature_normal_stt,
sample_rate = sample_rate,
label_word = label_word,
label_path = label_path,
path = path,
augmented = False)
featur_object_noise = TrainSegment(
start_time = start_time,
end_time = end_time,
mfcc = feature_noise_mfcc,
mel = feature_noise_mel,
stt = feature_noise_stt,
sample_rate = sample_rate,
label_word = label_word,
label_path = label_path,
path = path,
augmented = True)
featur_object_pitch = TrainSegment(
start_time = start_time,
end_time = end_time,
mfcc = feature_pitch_mfcc,
mel = feature_pitch_mel,
stt = feature_pitch_stt,
sample_rate = sample_rate,
label_word = label_word,
label_path = label_path,
path = path,
augmented = True)
data.append(featur_object)
data.append(featur_object_noise)
data.append(featur_object_pitch)
print(f"Saving processed data to {output_file}...")
with open(output_file, 'wb') as f:
pickle.dump(data, f)
import pickle
from tqdm import tqdm
import numpy as np
import cv2
from sklearn.preprocessing import MinMaxScaler
# Function to augment a list of audio word segments
def augment_audio_list(word_segments):
output_file = "augmented_audios_list.pkl"
data = []
# Iterate through each word segment
for entry in tqdm(word_segments):
# Extract relevant attributes from the entry
start_time = entry.start_time
end_time = entry.end_time
audio_normal = entry.audio_data
sample_rate = entry.sample_rate
label_word = entry.label
label_path = entry.label_path
path = entry.path
# Apply data augmentation: add noise and pitch shift
audio_noise = data_augmentation.add_gaussian_noise(audio_normal, sample_rate)
audio_pitch = data_augmentation.pitch_shift(audio_normal, sample_rate)
# Create original TrainSegment (not augmented)
featur_object = TrainSegment(
start_time=start_time,
end_time=end_time,
data=audio_normal,
sample_rate=sample_rate,
label_word=label_word,
label_path=label_path,
path=path,
augmented=False
)
# Create augmented segment with noise
featur_object_noise = TrainSegment(
start_time=start_time,
end_time=end_time,
data=audio_noise,
sample_rate=sample_rate,
label_word=label_word,
label_path=label_path,
path=path,
augmented=True
)
# Create augmented segment with pitch shift
featur_object_pitch = TrainSegment(
start_time=start_time,
end_time=end_time,
data=audio_pitch,
sample_rate=sample_rate,
label_word=label_word,
label_path=label_path,
path=path,
augmented=True
)
# Append original and augmented segments to the data list
data.append(featur_object)
data.append(featur_object_noise)
data.append(featur_object_pitch)
# Save all augmented data to a pickle file
print(f"Saving processed data to {output_file}...")
with open(output_file, 'wb') as f:
pickle.dump(data, f)
# Function to compute mean and standard deviation of raw audio data for normalization
def compute_mean_sdt_for_normalization_audio(data):
# Split data into training, validation, and test sets
segments_train, segments_val, segments_test = split_list_after_speaker(data)
train_samples = [f.audio_data for f in segments_train]
print(np.shape(train_samples))
# Concatenate all training samples into a single array
train_samples = np.concatenate(train_samples)
# Compute mean and standard deviation of the training audio
train_mean = np.mean(train_samples)
train_std = np.std(train_samples)
print("train_mean:", train_mean, " train_std:", train_std)
# Function to compute normalization statistics (mean, std) for MFCC or Mel features
def compute_mean_std_for_mfccormel_normalization(words_list):
# Split data into training, validation, and test sets
segments_train, segments_val, segments_test = split_list_after_speaker(words_list)
# Resize all mel features to 224x224 and collect them
resized_mfccs = [
cv2.resize(segment.mel, (224, 224), interpolation=cv2.INTER_LINEAR)
for segment in segments_train
]
# Concatenate all frames for global statistics computation
all_mfcc_frames = np.concatenate(resized_mfccs)
# Normalize using Min-Max scaling to [0, 1]
scaler = MinMaxScaler(feature_range=(0, 1))
all_mfcc_scaled = scaler.fit_transform(all_mfcc_frames)
# Compute global mean and standard deviation
global_mean = np.mean(all_mfcc_scaled)
global_std = np.std(all_mfcc_scaled)
print("global_mean : ", global_mean)
print("global_std : ", global_std)
# Function to compute normalization statistics for STT (e.g., spectrogram) embeddings
def compute_mean_std_for_stt_normalization(words_list):
# Split data into training, validation, and test sets
segments_train, segments_val, segments_test = split_list_after_speaker(words_list)
# Resize all STT features to 224x224 and collect them
resized_mfccs = [
cv2.resize(segment.stt.detach().cpu().numpy()[0], (224, 224), interpolation=cv2.INTER_LINEAR)
for segment in segments_train
]
# Concatenate all frames for global statistics computation
all_mfcc_frames = np.concatenate(resized_mfccs)
# Normalize using Min-Max scaling to [0, 1]
scaler = MinMaxScaler(feature_range=(0, 1))
all_mfcc_scaled = scaler.fit_transform(all_mfcc_frames)
# Compute global mean and standard deviation
global_mean = np.mean(all_mfcc_scaled)
global_std = np.std(all_mfcc_scaled)
print("global_mean : ", global_mean)
print("global_std : ", global_std)
if __name__ == "__main__":
#============================create fixedlists ==========================================
# Before change what you want to have in the dataloader
#for train put every word in there 3 times
# Load your dataset
loader = AudioDataLoader(config_file='config.json', word_data=False, phone_data=False, sentence_data=False, get_buffer=False)
# Load preprocessed audio segments from a pickle file
#phones_segments = loader.load_segments_from_pickle("data_lists\phone_normalized_16kHz.pkl")
words_segments = loader.load_segments_from_pickle("data_lists/words_normalized_16kHz.pkl")
print(np.shape(words_segments))
#word = words_segments[10000]
#print(np.shape(word.mfcc),np.shape(word.mel),np.shape(word.stt.detach().cpu().numpy()[0]))
#compute_mean_std_for_stt_normalization(words_segments)
#word = words_segments[100]
#plotting.visualize_augmentations(word.audio_data,word.sample_rate)
#augment_audio_list(words_segments[:60])
create_list(words_segments)