forked from GorillaBus/urban-audio-classifier
-
Notifications
You must be signed in to change notification settings - Fork 1
/
script_augmented_pre_processing.py
161 lines (117 loc) · 4.09 KB
/
script_augmented_pre_processing.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
import sys
import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import librosa
import librosa.display
import pickle
from include import helpers
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from keras.utils import to_categorical
# Set your path to the dataset
us8k_path = os.path.abspath('./UrbanSound8K')
audio_path = os.path.join(us8k_path, 'audio')
augmented_path = os.path.join(audio_path, 'augmented')
# Metadata
metadata_augmented_path = os.path.abspath('data/augmented-data.csv')
# Load the metadata from the generated CSV
metadata = pd.read_csv(metadata_augmented_path)
# Examine dataframe
print("Metadata length:", len(metadata))
metadata.tail()
# Iterate through all audio files and extract MFCC
features = []
labels = []
frames_max = 0
counter = 0
total_samples = len(metadata)
n_mfcc = 40
for index, row in metadata.iterrows():
file_path = os.path.join(os.path.abspath(audio_path), 'fold' + str(row["fold"]), str(row["file"]))
class_label = row["class"]
# Extract MFCCs (do not add padding)
mfccs = helpers.get_mfcc(file_path, 0, n_mfcc)
# Save current frame count
num_frames = mfccs.shape[1]
# Add row (feature / label)
features.append(mfccs)
labels.append(class_label)
# Update frames maximum
if (num_frames > frames_max):
frames_max = num_frames
print("Progress: {}/{}".format(index+1, total_samples))
print("Last file: ", file_path)
counter += 1
print("Finished: {}/{}".format(index, total_samples))
padded = []
# Add padding
mfcc_max_padding = frames_max
for i in range(len(features)):
size = len(features[i][0])
if (size < mfcc_max_padding):
pad_width = mfcc_max_padding - size
px = np.pad(features[i],
pad_width=((0, 0), (0, pad_width)),
mode='constant',
constant_values=(0,))
padded.append(px)
# Convert features (X) and labels (y) to Numpy arrays
X = np.array(padded)
y = np.array(labels)
# Optionally save the features to disk
np.save("data/X-mfcc-augmented", X)
np.save("data/y-mfcc-augmented", y)
# Verify shapes
print("Raw features length: {}".format(len(features)))
print("Padded features length: {}".format(len(padded)))
print("Feature labels length: {}".format(len(features)))
print("X: {}, y: {}".format(X.shape, y.shape))
# Iterate through all audio files and extract Log-Mel Spectrograms
features = []
labels = []
frames_max = 0
counter = 0
total_samples = len(metadata)
n_mels = 40
for index, row in metadata.iterrows():
file_path = os.path.join(os.path.abspath(audio_path), 'fold' + str(row["fold"]), str(row["file"]))
class_label = row["class"]
# Extract Log-Mel Spectrograms (do not add padding)
mels = helpers.get_mel_spectrogram(file_path, 0, n_mels=n_mels)
# Save current frame count
num_frames = mels.shape[1]
# Add row (feature / label)
features.append(mels)
labels.append(class_label)
# Update frames maximum
if (num_frames > frames_max):
frames_max = num_frames
print("Progress: {}/{}".format(index+1, total_samples))
print("Last file: ", file_path)
counter += 1
print("Finished: {}/{}".format(index, total_samples))
padded = []
# Add padding
mels_max_padding = frames_max
for i in range(len(features)):
size = len(features[i][0])
if (size < mels_max_padding):
pad_width = mels_max_padding - size
px = np.pad(features[i],
pad_width=((0, 0), (0, pad_width)),
mode='constant',
constant_values=(0,))
padded.append(px)
# Convert features (X) and labels (y) to Numpy arrays
X = np.array(padded)
y = np.array(labels)
# Optionally save the features to disk
np.save("data/X-mel_spec-augmented", X)
np.save("data/y-mel_spec-augmented", y)
# Verify shapes
print("Raw features length: {}".format(len(features)))
print("Padded features length: {}".format(len(padded)))
print("Feature labels length: {}".format(len(features)))
print("X: {}, y: {}".format(X.shape, y.shape))