-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspeakerDeepLearning.py
175 lines (131 loc) · 4.56 KB
/
speakerDeepLearning.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
from __future__ import absolute_import
from __future__ import print_function
import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.layers.normalization import BatchNormalization
from keras.layers.advanced_activations import PReLU
from keras.utils import np_utils, generic_utils
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import classification_report
import seaborn as sns
sns.set(style='ticks', palette='Set2')
import gzip
from pylab import *
np.random.seed(1337) # for reproducibility
def load_data(path, train=True):
df = pd.read_csv(path)
X = df.values.copy()
if train:
np.random.shuffle(X) # https://youtu.be/uyUXoap67N8
X, labels = X[:, 1:-1].astype(np.float32), X[:, -1]
return X, labels
else:
X, ids = X[:, 1:-1].astype(np.float32), X[:, -1]
return X, ids
def preprocess_data(X, scaler=None):
if not scaler:
scaler = StandardScaler()
scaler.fit(X)
X = scaler.transform(X)
return X, scaler
def preprocess_labels(labels, encoder=None, categorical=True):
if not encoder:
encoder = LabelEncoder()
encoder.fit(labels)
y = encoder.transform(labels).astype(np.int32)
if categorical:
y = np_utils.to_categorical(y)
return y, encoder
def make_submission(y_prob, ids, encoder, fname):
with open(fname, 'w') as f:
f.write('id,')
f.write(','.join([str(i) for i in encoder.classes_]))
f.write('\n')
for i, probs in zip(ids, y_prob):
probas = ','.join([i] + [str(p) for p in probs.tolist()])
f.write(probas)
f.write('\n')
gzip_file(fname)
print("Wrote submission to file {}.".format(fname))
def gzip_file(file):
with open(file, "rb") as file_in:
# Open output file.
with gzip.open(file + ".tar.gz", "wb") as file_out:
# Write output.
file_out.writelines(file_in)
print("Loading data...")
X, labels = load_data('both.csv', train=True)
X, scaler = preprocess_data(X)
y, encoder = preprocess_labels(labels)
# X_test, ids = load_data('../raw/test.csv', train=False)
# X_test, _ = preprocess_data(X_test, scaler)
nb_classes = y.shape[1]
print(nb_classes, 'classes')
dims = X.shape[1]
print(dims, 'dims')
print("Building model...")
testVal = 512
model = Sequential()
model.add(Dense(dims, testVal, init='glorot_uniform'))
model.add(PReLU((testVal,)))
model.add(BatchNormalization((testVal,)))
model.add(Dropout(0.2))
model.add(Dense(testVal, testVal, init='glorot_uniform'))
model.add(PReLU((testVal,)))
model.add(BatchNormalization((testVal,)))
model.add(Dropout(0.2))
model.add(Dense(testVal, testVal, init='glorot_uniform'))
model.add(PReLU((testVal,)))
model.add(BatchNormalization((testVal,)))
model.add(Dropout(0.2))
model.add(Dense(testVal, nb_classes, init='glorot_uniform'))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer="adadelta")
print("Training model...")
model.fit(X, y, nb_epoch=100, batch_size=5, validation_split=0.01)
print("Generating results...")
proba = model.predict_proba(X)
print(model.evaluate(X,y, show_accuracy=True))
print(proba)
print(model.predict_classes(X), labels)
print(classification_report(labels-1, model.predict_classes(X)))
plt.figure()
plt.scatter(proba[:,0], proba[:,1])
savefig('Classified.png',bbox_inches='tight')
print("Unshuffled")
X, labels = load_data('both.csv', train=False)
X = scaler.transform(X)
y, encoder = preprocess_labels(labels)
newProba = model.predict_proba(X)
print(model.evaluate(X,y, show_accuracy=True))
print(newProba[2])
print(newProba[11])
print(newProba[21])
print("1")
print(newProba[0:9])
print("2")
print(newProba[10:19])
print("3")
print(newProba[20:-1])
print(labels)
print(model.predict_classes(X))
from sklearn.manifold import TSNE
model = TSNE(n_components=nb_classes, random_state=0, init='pca')
toPlot = model.fit_transform(newProba)
title = "t-SNE embedding of the spectrograms"
x_min, x_max = np.min(toPlot, 0), np.max(toPlot, 0)
toPlot = (toPlot - x_min) / (x_max - x_min)
print(toPlot.shape)
labelsName = ["bob", "steve", "dave"]
cmap = sns.color_palette("Set2", n_colors=3)
plt.figure()
for i in range(toPlot.shape[0]):
plt.text(toPlot[i, 0], toPlot[i, 1], labelsName[int(labels[i])-1] + "_" + str(i),
color=cmap[int(labels[i])-1],
fontdict={'weight': 'bold', 'size': 9})
plt.xticks([]), plt.yticks([])
sns.despine()
savefig('ClassifiedTSNE.png',bbox_inches='tight')