-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeature_extraction.py
More file actions
268 lines (203 loc) · 7.15 KB
/
feature_extraction.py
File metadata and controls
268 lines (203 loc) · 7.15 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
#!/usr/bin/env python
# coding: utf-8
# ## Start Feature Extraction from the collected Dataset
from src.settings import DATA_DIR, PROCESSED_DIR
from src.utils import norm_spec
import os
import pandas as pd
import librosa
import numpy as np
import matplotlib.pyplot as plt
# os.chdir("../")
# ## Model Building
from src.model import Net
import torch
import torch.nn as nn
from torch import Tensor
from torchvision import transforms
from torch.utils.data import DataLoader
from torch.utils.data.dataset import Dataset
from plot_helper import PlotHelp
from real_time_inference import RecordThread
file_path = PROCESSED_DIR
meta_data = pd.read_csv(os.path.join(DATA_DIR, "meta_data.csv"))
## if this doesn't hold .. some deep problem we gotta fix my myan
assert all(meta_data.start_time.isna() == meta_data.end_time.isna())
meta_data["label"] = ~meta_data.start_time.isna()
meta_data["label"] = meta_data["label"].astype(int)
def get_melspectrogram_db(
file_path,
sr=44100,
n_fft=2048,
hop_length=512,
n_mels=128,
fmin=20,
fmax=8300,
top_db=80,
):
wav, sr = librosa.load(file_path, sr=sr)
spec = librosa.feature.melspectrogram(
wav,
sr=sr,
n_fft=n_fft,
hop_length=hop_length,
n_mels=n_mels,
fmin=fmin,
fmax=fmax,
)
spec_db = librosa.power_to_db(spec, top_db=top_db)
return spec_db
plot_help = PlotHelp()
# ## Check the Traning Example Visually
train_path = os.path.join(PROCESSED_DIR, "train")
test_path = os.path.join(PROCESSED_DIR, "test")
meta_data_train = meta_data[:1701]
meta_data_test = meta_data[1701:]
pos_examples = meta_data_train[meta_data_train.label == 1].sample(4)
neg_examples = meta_data_train[meta_data_train.label == 0].sample(4)
mel_spec_pos = [
get_melspectrogram_db(os.path.join(train_path, pos_example.filename), 44100)
for _, pos_example in pos_examples.iterrows()
]
mel_spec_pos_norm = [norm_spec(i) for i in mel_spec_pos]
mel_spec_neg = [
get_melspectrogram_db(os.path.join(train_path, neg_example.filename), 44100)
for _, neg_example in neg_examples.iterrows()
]
mel_spec_neg_norm = [norm_spec(i) for i in mel_spec_neg]
mel_scale_max = mel_spec_pos[0].shape[1]
time_scale_max = 8000 ## ms
potential_range_in_freq_domain = [
(
mel_scale_max * pos_example.start_time / time_scale_max,
mel_scale_max * pos_example.end_time / time_scale_max,
)
for _, pos_example in pos_examples.iterrows()
]
plot_help.plot_examples(mel_spec_pos_norm, potential_range_in_freq_domain)
meta_data[:1701].shape, meta_data[1701:].shape
class AudioLoader(Dataset):
def __init__(self, meta_data, transform=None, mode="train"):
# setting directories for data
data_root = PROCESSED_DIR
self.mode = mode
if self.mode is "train":
self.data_dir = os.path.join(data_root, "train")
self.csv_file = meta_data_train
elif self.mode is "test":
self.data_dir = os.path.join(data_root, "test")
self.csv_file = meta_data_test
self.transform = transform
def __len__(self):
return self.csv_file.shape[0]
def __getitem__(self, idx):
filename = self.csv_file["filename"].iloc[idx]
file_path = os.path.join(self.data_dir, filename)
data, sr = librosa.load(file_path, sr=44100)
if self.transform is not None:
data = self.transform(data)
label = self.csv_file["label"].iloc[idx]
return data, label
# Transformation using Librosa
audio_transformation = transforms.Compose(
[
lambda x: librosa.feature.melspectrogram(
x, sr=44100, n_fft=2048, hop_length=512, n_mels=128, fmin=20, fmax=8300
), # MFCC
lambda x: librosa.power_to_db(x, top_db=80),
# lambda x: norm_spec(x),
lambda x: x.reshape(1, 128, 690)
# lambda x: Tensor(x)
]
)
# Transformation in Training Set
from audio_transformations import waveform_augment
training_transformation = transforms.Compose(
[
lambda x: waveform_augment(x, 44100),
lambda x: librosa.feature.melspectrogram(
x, sr=44100, n_fft=2048, hop_length=512, n_mels=128, fmin=20, fmax=8300
), # MFCC
lambda x: librosa.power_to_db(x, top_db=80),
# lambda x: norm_spec(x),
lambda x: x.reshape(1, 128, 690)
# lambda x: Tensor(x)
]
)
BATCH_SIZE = 8
# todo: multiprocessing, padding data
trainloader = DataLoader(
AudioLoader(
meta_data=meta_data_train, transform=training_transformation, mode="train"
),
batch_size=BATCH_SIZE,
shuffle=True,
num_workers=0,
)
# todo: multiprocessing, padding data
testloader = DataLoader(
AudioLoader(meta_data=meta_data_test, transform=audio_transformation, mode="test"),
batch_size=BATCH_SIZE,
shuffle=True,
num_workers=0,
)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print('Device to train : ', device)
# defining the model
model = Net().to(device)
# defining the optimizer
optimizer = torch.optim.Adam(model.parameters())
# defining the loss function
criterion = nn.BCELoss().to(device)
# checking if GPU is available
print(model)
# ## Training the model
def calc_accuracy(outputs, labels):
total_examples = len(outputs)
correct_pred = torch.sum((outputs >= 0.5) == labels).to("cpu").item()
return correct_pred / total_examples
for epoch in range(50): # loop over the dataset multiple times
model.train()
running_loss = 0.0
training_acc = []
val_acc = []
for i, data in enumerate(trainloader, 0):
# get the inputs; data is a list of [inputs, labels]
inputs, labels = data[0].to(device), data[1].to(device)
labels = labels.unsqueeze(1)
# zero the parameter gradients
optimizer.zero_grad()
# forward + backward + optimize
outputs = model(inputs)
labels = labels.type_as(outputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# print statistics
running_loss += loss.item()
training_acc.append(calc_accuracy(outputs, labels))
if i % 50 == 0: #
curr_training_loss = sum(training_acc) / len(training_acc)
print(
f"At {i+1}th iter, Epoch {epoch+1} : Loss accumulated upto : {running_loss} || Running Train Accuracy : {curr_training_loss}"
)
model.eval()
val_loss = 0.0
for i, data in enumerate(testloader, 0):
inputs, labels = data[0].to(device), data[1].to(device)
labels = labels.unsqueeze(1)
output_val = model(inputs)
labels = labels.type_as(outputs)
loss_val = criterion(output_val, labels)
val_loss += loss_val.item()
val_acc.append(calc_accuracy(output_val, labels))
curr_training_loss = sum(training_acc) / len(training_acc)
curr_val_loss = sum(val_acc) / len(val_acc)
print(
f"After Epoch {i+1} : Training Loss {running_loss} || Validation loss {val_loss}"
)
print(
f"Training Accuracy {curr_training_loss} || Validation Accuracy {curr_val_loss}"
)
print(f"Saving at Epoch {epoch} ..")
torch.save(model.state_dict(), "my_dummy_model")