-
Notifications
You must be signed in to change notification settings - Fork 0
/
01b_DataPreprocessing_ReflexCoughs.py
163 lines (131 loc) · 6.03 KB
/
01b_DataPreprocessing_ReflexCoughs.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Authors: Stefan Jokic, Andrea Schwaller, Filipe Barata, David Cleres
"""
The following articles provided a basis for the implementation:
"One Shot Learning with Siamese Networks using Keras" (September 2019)
- Link to Article: https://towardsdatascience.com/one-shot-learning-with-siamese-networks-using-keras-17f34e75bb3d
- Link to GitHub Repository: https://sorenbouma.github.io/blog/oneshot/
"One Shot Learning and Siamese Networks in Keras" (September 2019)
- Link to Article: https://sorenbouma.github.io/blog/oneshot/
- Link to GitHub Repository: https://github.com/sorenbouma/keras-oneshot
"Building a Speaker Identification System from Scratch with Deep Learning" (September 2019)
- Link to Article: https://medium.com/analytics-vidhya/building-a-speaker-identification-system-from-scratch-with-deep-learning-f4c4aa558a56
- Link to GitHub Repository: https://github.com/oscarknagg/voicemap
"""
# This script loads cough data from audio files,
# processes and converts them to mel-spectrograms,
# and stores them into pickle files.
# --> training, validation, test set
# Libraries
import os
import pickle
import librosa
import numpy as np
np.random.seed(0)
import numpy.random as rng
# Parameters
import Parameters as param # "Parameters" must be the name of .py script containing parameters, c.f. params directory
# Load & Process Training, Validation & Test Data
# Extracts a window around the maximum of the signal
def extract_Signal_Of_Importance(signal, window, sample_rate):
window_size = int(window * sample_rate)
start = max(0, np.argmax(np.abs(signal)) - (window_size // 2))
end = min(np.size(signal), start + window_size)
signal = signal[start:end]
length = np.size(signal)
assert length <= window_size, "extracted signal is longer than the allowed window size"
if length < window_size:
signal = np.concatenate((signal, np.zeros(window_size - length))) # Pad zeros to the signal if too short
return signal
def standardize(signal):
maxValue = np.max(signal)
minValue = np.min(signal)
signal = (signal - minValue) / (maxValue - minValue)
return signal
# Converts audio to mel-spectrogram
def preprocess(sound_file, window, bands, hop_length):
time_signal, sample_rate = librosa.load(sound_file, mono=True, res_type="kaiser_fast")
time_signal = extract_Signal_Of_Importance(time_signal, window, sample_rate)
time_signal = standardize(time_signal)
mel_spec = librosa.feature.melspectrogram(
y=time_signal, sr=sample_rate, power=1, n_mels=bands, hop_length=hop_length
)
return mel_spec
# Loads audio files of all participants from a directory into a tensor
# Returns a tensor X (#participants, #samples, width, heigth) and a vector containing the number of samples per participant
def loadimgs(path, window, bands, hop_length, img_height, img_width, n_samples_max):
X = []
n_samples = []
# Load every participant seperately
for participant in os.listdir(path):
print("Loading participant: ", participant)
if participant != ".DS_Store":
participant_path = os.path.join(path, participant)
mel_spec_images = []
# Read all/50 random audio files of the current participant and convert them to mel-spectrograms
n_samples_participant = len(os.listdir(participant_path))
if n_samples_participant > n_samples_max:
filelist = rng.choice(os.listdir(participant_path), size=(n_samples_max,), replace=False)
n_samples_participant = 500
else:
filelist = os.listdir(participant_path)
for filename in filelist:
if filename == ".DS_Store":
print("Filename: ", filename)
audio_path = os.path.join(participant_path, filename)
mel_spec = preprocess(audio_path, window=window, bands=bands, hop_length=hop_length)
mel_spec_images.append(mel_spec)
# Fill remaining empty image slots with NaN to have same amount of samples for each participant
print(np.asarray(mel_spec_images).shape)
print(np.asarray(np.full((n_samples_max - n_samples_participant, img_height, img_width), np.nan).shape))
mel_spec_images = np.concatenate(
(mel_spec_images, np.full((n_samples_max - n_samples_participant, img_height, img_width), np.nan))
)
X.append(mel_spec_images)
n_samples.append(n_samples_participant)
return np.asarray(X), n_samples
if __name__ == "__main__":
# Load training data
X_train, n_samples_train = loadimgs(
param.train_folder,
param.window,
param.bands,
param.hop_length,
param.img_height,
param.img_width,
param.n_samples_max,
)
# Load validation data
X_val, n_samples_val = loadimgs(
param.val_folder,
param.window,
param.bands,
param.hop_length,
param.img_height,
param.img_width,
param.n_samples_max,
)
# Load test data
X_test, n_samples_test = loadimgs(
param.test_folder,
param.window,
param.bands,
param.hop_length,
param.img_height,
param.img_width,
param.n_samples_max,
)
# # Store Training, Validation & Test Data into Pickle Files
with open(os.path.join(param.data_path, "train.pickle"), "wb") as f:
pickle.dump((X_train, n_samples_train), f)
with open(os.path.join(param.data_path, "val.pickle"), "wb") as f:
pickle.dump((X_val, n_samples_val), f)
with open(os.path.join(param.data_path, "test.pickle"), "wb") as f:
pickle.dump((X_test, n_samples_test), f)
with open(os.path.join(param.data_path, "train.pickle"), "rb") as f:
(X_train, n_samples_train) = pickle.load(f)
n_participants, n_examples, h, w = X_train.shape
with open(os.path.join(param.data_path, "val.pickle"), "rb") as f:
(X_val, n_samples_train) = pickle.load(f)
n_participants, n_examples, h, w = X_val.shape