-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset_preprocessing.py
147 lines (141 loc) · 6.61 KB
/
dataset_preprocessing.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
import matplotlib.pyplot as plt
from scipy import signal
from scipy.io import wavfile
import os
import numpy as np
import librosa
import librosa.display
import matplotlib.pyplot as plt
import numpy as np
import cv2
from sklearn.model_selection import train_test_split
def CalculateMelSpectrogram(file_location):
print(file_location)
y, sr = librosa.load(file_location)
melSpec = librosa.feature.melspectrogram(y=y, sr=sr)
melSpec_dB = librosa.power_to_db(melSpec, ref=np.max)
dim = (64, 64)
resized = cv2.resize(melSpec_dB, dim, interpolation = cv2.INTER_AREA)
return resized
def PrepareDataset():
## Dictionary preparing for labels
dic= {}
count=0
mainPath= "./GoogleSpeechCommands"
for i in os.listdir(mainPath):
dic[i]=count
count+=1
###### ##
testpath=mainPath+"/testing_list.txt"
valpath=mainPath+"/validation_list.txt"
testpaths = open(testpath,"r").readlines()
valpaths = open(valpath,"r").readlines()
testpaths = [mainPath+"/"+i for i in testpaths]
valpaths = [mainPath+"/"+i for i in valpaths]
trainpaths = []
for i in os.listdir(mainPath):
if i.endswith(".txt"):
continue
for j in os.listdir(mainPath+"/"+i):
trainingfile = mainPath+"/"+i+"/"+j
if trainingfile in testpaths or trainingfile in valpaths:
continue
trainpaths.append(trainingfile)
x_train, y_train, x_test, y_test, x_val, y_val = [],[],[],[],[],[]
bg=[]
count=0
for i in trainpaths:
count+=1
print("Processed ", count," / ", len(trainpaths))
x_train.append(CalculateMelSpectrogram(i))
y_train.append(dic[i.split("/")[2]])
count=0
for i in testpaths:
count += 1
print("Processed ", count, " / ", len(testpaths))
if "\n" in i:
i = i.replace("\n","")
x_test.append(CalculateMelSpectrogram(i))
y_test.append(dic[i.split("/")[2]])
count=0
for i in valpaths:
count += 1
print("Processed ", count, " / ", len(valpaths))
if "\n" in i:
i = i.replace("\n","")
x_val.append(CalculateMelSpectrogram(i))
y_val.append(dic[i.split("/")[2]])
# bgNse = []
# for bg in backgroundNoise:
# bgNse.append(CalculateMelSpectrogram(bg))
#
return np.array(x_train), np.array(y_train), np.array(x_test), np.array(y_test), np.array(x_val), np.array(y_val)
x_train, y_train, x_test, y_test, x_val, y_val = PrepareDataset()
np.save("/root/volume/DataSets/PreparedDatasets/GoogleCommands/x_train", x_train)
np.save("/root/volume/DataSets/PreparedDatasets/GoogleCommands/y_train", y_train)
np.save("/root/volume/DataSets/PreparedDatasets/GoogleCommands/x_test", x_test)
np.save("/root/volume/DataSets/PreparedDatasets/GoogleCommands/y_test", y_test)
np.save("/root/volume/DataSets/PreparedDatasets/GoogleCommands/x_val", x_val)
np.save("/root/volume/DataSets/PreparedDatasets/GoogleCommands/y_val", y_val)
# x_train = np.load("googleCommandDataset/x_train.npy",allow_pickle=True)
# y_train = np.load("googleCommandDataset/y_train.npy",allow_pickle=True)
# x_test = np.load("googleCommandDataset/x_test.npy",allow_pickle=True)
# y_test = np.load("googleCommandDataset/y_test.npy",allow_pickle=True)
# x_val = np.load("googleCommandDataset/x_val.npy",allow_pickle=True)
# y_val = np.load("googleCommandDataset/y_val.npy",allow_pickle=True)
# x_train = x_train.reshape((x_train.shape[0],32,32,1))
# x_test = x_test.reshape((x_test.shape[0],32,32,1))
# x_val = x_val.reshape((x_val.shape[0],32,32,1))
#
# print(x_train.shape, y_train.shape, x_test.shape, y_test.shape, x_val.shape, y_val.shape)
# print(np.unique(y_train))
def PrepareMNISTData():
path= "/root/BSSL/DataSets/MNIST/"
x =[]
y= []
for i in os.listdir(path):
x.append(CalculateMelSpectrogram(path+i))
y.append(int(i.split("_")[0]))
x = np.array(x)
x=x.reshape((x.shape[0],32,32,1))
y= np.array(y)
x_train, y_train, x_test, y_test = None,None, None, None
status=True
for i in np.unique(y):
X_train, X_test, y_tra, y_te = train_test_split(x[i==y], y[i==y], test_size = 0.2,random_state = 42)
if status:
status=False
x_train, x_test, y_train, y_test = X_train, X_test, y_tra, y_te
x_train = np.concatenate((x_train, X_train), axis=0)
y_train = np.concatenate((y_train, y_tra),axis=0)
x_test = np.concatenate((x_test, X_test), axis=0)
y_test = np.concatenate((y_test, y_te),axis=0)
return x_train, y_train, x_test, y_test
# x_train, y_train, x_test, y_test= PrepareMNISTData()
# np.save("/root/BSSL/DataSets/PreparedDatasets/MNIST/x_train", x_train)
# np.save("/root/BSSL/DataSets/PreparedDatasets/MNIST/y_train", y_train)
# np.save("/root/BSSL/DataSets/PreparedDatasets/MNIST/x_test", x_test)
# np.save("/root/BSSL/DataSets/PreparedDatasets/MNIST/y_test", y_test)
x_train = np.load("/root/BSSL/DataSets/PreparedDatasets/MNIST/x_train.npy",allow_pickle=True)
y_train = np.load("/root/BSSL/DataSets/PreparedDatasets/MNIST/y_train.npy",allow_pickle=True)
x_test = np.load("/root/BSSL/DataSets/PreparedDatasets/MNIST/x_test.npy",allow_pickle=True)
y_test = np.load("/root/BSSL/DataSets/PreparedDatasets/MNIST/y_test.npy",allow_pickle=True)
print(x_train.shape, y_train.shape, x_test.shape, y_test.shape)
print(np.unique(y_train), np.unique(y_test))
# x_train, y_train, x_test, y_test=PrepareMNISTData()
print(np.unique(y_train),np.unique(y_test))
# x_train, y_train, x_test, y_test, x_val, y_val = PrepareDataset()
# np.save("/root/BSSL/DataSets/PreparedDatasets/GoogleCommands/x_train", x_train)
# np.save("/root/BSSL/DataSets/PreparedDatasets/GoogleCommands/y_train", y_train)
# np.save("/root/BSSL/DataSets/PreparedDatasets/GoogleCommands/x_test", x_test)
# np.save("/root/BSSL/DataSets/PreparedDatasets/GoogleCommands/y_test", y_test)
# np.save("/root/BSSL/DataSets/PreparedDatasets/GoogleCommands/x_val", x_val)
# np.save("/root/BSSL/DataSets/PreparedDatasets/GoogleCommands/y_val", y_val)
x_train = np.load("/root/BSSL/DataSets/PreparedDatasets/GoogleCommands/x_train.npy", allow_pickle=True)
y_train = np.load("/root/BSSL/DataSets/PreparedDatasets/GoogleCommands/y_train.npy", allow_pickle=True)
x_test = np.load("/root/BSSL/DataSets/PreparedDatasets/GoogleCommands/x_test.npy", allow_pickle=True)
y_test = np.load("/root/BSSL/DataSets/PreparedDatasets/GoogleCommands/y_test.npy",allow_pickle=True )
x_val = np.load("/root/BSSL/DataSets/PreparedDatasets/GoogleCommands/x_val.npy", allow_pickle=True )
y_val = np.load("/root/BSSL/DataSets/PreparedDatasets/GoogleCommands/y_val.npy", allow_pickle=True)
print(x_train.shape,y_train.shape, x_test.shape, y_test.shape, x_val.shape, y_val.shape)
print("Total Samples ", x_train.shape[0]+x_test.shape[0]+x_val.shape[0])