-
Notifications
You must be signed in to change notification settings - Fork 1
/
PreprocessData.py
204 lines (156 loc) · 7.29 KB
/
PreprocessData.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
# %matplotlib inline
from __future__ import print_function
from six.moves import cPickle as pickle
from six.moves import range
import os
import numpy as np
import librosa
import librosa.display
# http://opihi.cs.uvic.ca/sound/genres.tar.gz
# GTZAN genre collection.
################## Data Loading #####################
print("Loading Data")
# function to read and concatenated features from the files
def read_data(directory='./genres'):
all_data_mfcc = np.zeros((1000, 20, 1400), dtype=float)
all_data_spectrogram = np.zeros((1000, 20, 1400), dtype=float)
all_data_beats = np.zeros((1000, 1, 1400), dtype=float)
all_data_contrast = np.zeros((1000, 7, 1400), dtype=float)
all_data_cq = np.zeros((1000, 12, 1400), dtype=float)
all_data_cens = np.zeros((1000, 12, 1400), dtype=float)
all_data_stft = np.zeros((1000, 12, 1400), dtype=float)
all_data_centroid = np.zeros((1000, 1, 1400), dtype=float)
all_data_bandwidth = np.zeros((1000, 1, 1400), dtype=float)
all_data_rmse = np.zeros((1000, 1, 1400), dtype=float)
all_labels = np.zeros(1000)
label_index = 0
image_index = 0
for _, dirs, _ in os.walk(directory):
for dir in dirs:
for _, _, files in os.walk(directory + '/' + dir):
for file in files:
print("Loading features of: " + file)
y, sr = librosa.load(directory + '/' + dir + '/' + file)
mfcc = librosa.feature.mfcc(y=y, sr=sr)
S = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=20, fmax=1400)
tempo, beats = librosa.beat.beat_track(y=y, sr=sr)
rmse = librosa.feature.rmse(y=y)
cent = librosa.feature.spectral_centroid(y=y, sr=sr)
spec_bw = librosa.feature.spectral_bandwidth(y=y, sr=sr)
chroma_stft = librosa.feature.chroma_stft(y=y, sr=sr)
chroma_cens = librosa.feature.chroma_cens(y=y, sr=sr)
chroma_cq = librosa.feature.chroma_cqt(y=y, sr=sr)
Stft = np.abs(librosa.stft(y))
contrast = librosa.feature.spectral_contrast(S=Stft, sr=sr)
#
all_data_spectrogram[image_index, :, 0:S.shape[1]] = S
all_data_mfcc[image_index, :, 0:mfcc.shape[1]] = mfcc
all_data_beats[image_index, :, 0:beats.shape[0]] = beats
all_data_beats[image_index, :, beats.shape[0] + 1] = tempo
#
all_data_rmse[image_index, :, 0:rmse.shape[1]] = rmse
all_data_centroid[image_index, :, 0:cent.shape[1]] = cent
all_data_bandwidth[image_index, :, 0:spec_bw.shape[1]] = spec_bw
all_data_stft[image_index, :, 0:chroma_stft.shape[1]] = chroma_stft
all_data_cens[image_index, :, 0:chroma_cens.shape[1]] = chroma_cens
all_data_cq[image_index, :, 0:chroma_cq.shape[1]] = chroma_cq
all_data_contrast[image_index, :, 0:contrast.shape[1]] = contrast
all_labels[image_index] = label_index
image_index = image_index + 1
label_index = label_index + 1
all_data = np.concatenate((all_data_mfcc, all_data_spectrogram), axis=1)
all_data = np.concatenate((all_data, all_data_beats), axis=1)
all_data = np.concatenate((all_data, all_data_rmse), axis=1)
all_data = np.concatenate((all_data, all_data_centroid), axis=1)
all_data = np.concatenate((all_data, all_data_bandwidth), axis=1)
all_data = np.concatenate((all_data, all_data_cens), axis=1)
all_data = np.concatenate((all_data, all_data_stft), axis=1)
all_data = np.concatenate((all_data, all_data_cq), axis=1)
all_data = np.concatenate((all_data, all_data_contrast), axis=1)
return all_data, all_labels
all_data, all_labels = read_data()
print("Shape of data:")
print(all_data.shape)
print("Shape of labels:")
print(all_labels.shape)
################## Normalization #####################
# function to standarize the data x=(x-mean)/std
def standarize(x):
mean = x.mean(axis=0)
std = x.std(axis=0)
diff = x - mean
z_scores_np = np.divide(diff, std, out=np.zeros_like(diff), where=std != 0)
print('Mean after standarization: %.1f%%' % z_scores_np.mean())
print('std after standarization: %.1f%%' % z_scores_np.std())
return z_scores_np
# function to min_max normalize *currently not used*
def min_max_normalize(x):
min = x.min(axis=0)
max = x.max(axis=0)
diff = x - min
range = max - min
np_minmax = np.divide(diff, range, out=np.zeros_like(diff), where=range != 0)
print('Min after normalization: %.1f%%' % np_minmax.min())
print('Max after normalization: %.1f%%' % np_minmax.max())
return np_minmax
# function to shuffle the data
def randomize(dataset, labels):
permutation = np.random.permutation(labels.shape[0])
shuffled_dataset = dataset[permutation, :, :]
shuffled_labels = labels[permutation]
return shuffled_dataset, shuffled_labels
print("Shuffling data")
all_data, all_labels = randomize(all_data, all_labels)
print("Standarizing data")
all_data_standard = standarize(all_data)
################## Splitting #####################
# function to split data into training and testing (default is 90% for training and 10% for testing)
def split_data(dataset, labels, num_classes=10, test_images_for_class=10):
test_counter = np.zeros(num_classes)
if dataset.ndim == 3:
test_set = np.zeros((num_classes * test_images_for_class, dataset.shape[1], dataset.shape[2]), dtype=float)
elif dataset.ndim == 2:
test_set = np.zeros((num_classes * test_images_for_class, dataset.shape[1]), dtype=float)
test_labels = np.zeros(num_classes * test_images_for_class)
deleted_index = []
test_index = 0
for i in range(labels.shape[0]):
_class = int(labels[i])
if test_counter[_class] >= test_images_for_class:
continue
test_counter[_class] = test_counter[_class] + 1
deleted_index.append(i)
test_labels[test_index] = labels[i]
test_set[test_index] = dataset[i]
test_index = test_index + 1
dataset = np.delete(dataset, deleted_index, axis=0)
labels = np.delete(labels, deleted_index, axis=0)
return dataset, labels, test_set, test_labels
print("Splitting data into training and testing")
train_set_std, train_labels_std, test_set_std, test_labels_std = split_data(all_data_standard, all_labels)
print("Shape of training set:")
print(train_set_std.shape)
print("Shape of training labels:")
print(train_labels_std.shape)
print("Shape of test set:")
print(test_set_std.shape)
print("Shape of test labels:")
print(test_labels_std.shape)
################## Saving data #####################
# save data to a pickle file to load when training
print("Saving data into pickle file")
pickle_file = 'dataset_standarized_all_10.pickle'
try:
f = open(pickle_file, 'wb')
save = {
'train_dataset': train_set_std,
'train_labels': train_labels_std,
'test_dataset': test_set_std,
'test_labels': test_labels_std,
}
pickle.dump(save, f, pickle.HIGHEST_PROTOCOL)
f.close()
print("Done")
except Exception as e:
print('Unable to save data to', pickle_file, ':', e)
raise