-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
225 lines (158 loc) · 6.86 KB
/
train.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
from sklearn.preprocessing import LabelBinarizer
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import KFold
from sklearn.metrics import precision_score, recall_score, f1_score
# from tools.dataset import FeatureExtractor
import numpy as np
import pandas as pd
import tensorflow as tf
np.set_printoptions(threshold=np.inf)
# Possible sets are:
# ('cel/', 'cla/', 'flu/', 'gac/', 'gel/', 'org/', 'pia/', 'sax/', 'tru/', 'vio/', 'voi/')
# Current train size: 2107
# Current test size: 1357
train_folders = ('gel/', 'pia/', 'sax/')
test_folders = ('Part1/', 'Part2/', 'Part3/')
num_classes = len(train_folders)
def one_hot_encode(labels):
valid_labels = []
for folder in train_folders:
valid_labels.append(folder[:-1])
enc = LabelBinarizer()
enc.fit(valid_labels)
labels = enc.transform(labels)
final_label = np.zeros((1, num_classes))
for label in labels:
final_label = np.add(final_label, label)
return final_label
def MLP(train_x, train_y, test_x, test_y, folder):
tf.reset_default_graph()
training_epochs = 20000
n_dim = train_x.shape[1]
n_classes = num_classes
n_hidden_units_one = 280
n_hidden_units_two = 300
sd = 1 / np.sqrt(n_dim)
learning_rate = 0.3
X = tf.placeholder(tf.float32, [None, n_dim], name="X")
Y = tf.placeholder(tf.float32, [None, n_classes], name="y")
W1 = tf.Variable(tf.random_normal([n_dim, n_hidden_units_one]), name="input_weight")
b1 = tf.Variable(tf.random_normal([n_hidden_units_one]), name="input_bias")
layer1 = tf.nn.tanh(tf.matmul(X, W1) + b1, name="input_layer")
W2 = tf.Variable(tf.random_normal(
[n_hidden_units_one, n_hidden_units_two]), name="hidden_weight")
b2 = tf.Variable(tf.random_normal([n_hidden_units_two]), name="hidden_bias")
layer2 = tf.nn.sigmoid(tf.matmul(layer1, W2) + b2, name="hidden_layer")
W = tf.Variable(tf.random_normal([n_hidden_units_two, n_classes]), name="output_weight")
b = tf.Variable(tf.random_normal([n_classes]), name="output_bias")
hypothesis = tf.nn.sigmoid(tf.matmul(layer2, W) + b, name="output_layer")
# Cont approx of hamming loss
cost_function = tf.reduce_mean(Y*(1-hypothesis) + (1-Y)*hypothesis, name="hamming_loss")
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(
cost_function)
predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32, name="predictions")
intermediate = tf.equal(predicted, Y)
accuracy = tf.reduce_mean(tf.cast(intermediate, dtype=tf.float32), name="accuracy")
accuracy_summary = tf.summary.scalar(name="accuracy_summary", tensor=accuracy)
saver = tf.train.Saver()
y_true, y_pred = None, None
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
writer = tf.summary.FileWriter('./model-data/' + folder, sess.graph)
for epoch in range(training_epochs):
_, cost, acc_sum = sess.run([optimizer, cost_function, accuracy_summary],
feed_dict={X: train_x, Y: train_y})
if epoch + 1 % 5000 == 0 or epoch == 1:
writer.add_summary(acc_sum, epoch)
inter, pred, raw, acc = sess.run(
[intermediate, predicted, hypothesis, accuracy],
feed_dict={X: test_x,
Y: test_y})
saver.save(sess, './model-data/' + folder + 'MLP')
print("Accuracy is: ", acc * 100, "%")
print("Hamming Loss: ", cost)
return acc, cost, pred
def make_csv():
# Set features
features = ["mfcc_1"]
for i in range(2, 14):
features.append("mfcc_" + str(i))
for i in range(1, 41):
features.append("mel_" + str(i))
for i in range(1, 4):
features.append("class_" + str(i))
# Generate Features
fe = FeatureExtractor(train_folders=train_folders, test_folders=test_folders)
train_X, test_X, train_y, test_y = fe.load_test_train_data()
train = np.hstack([train_X, train_y])
test = np.hstack([test_X, test_y])
print("Train shape: ", train.shape)
print("Test shape: ", test.shape)
irmas_all = np.vstack([train, test])
data = pd.DataFrame(data=irmas_all, columns=features)
data.to_csv("data/gel-pia-sax[MFCC][MFCC_BANDS][SPEC][59].csv", index=False)
def main():
print("Reading Files...")
# Load Features
data = pd.read_csv("data/gel-pia-sax[MFCC][MFCC_BANDS][SPEC][59].csv")
print("Done!\nProcessing files...")
# Split data
train = data[:2107]
train_X = train.drop(["class_1", "class_2", "class_3"], axis=1)
train_y = train[["class_1", "class_2", "class_3"]]
# Fill in the empty values
train_y = train_y.fillna("")
# DataFram to np.array
train_y = train_y.values
# Dumb Binary Encoding!!!
train_y[:] = [one_hot_encode(instance) for instance in train_y]
train_y = train_y.astype(float)
# Scale Data
mms = MinMaxScaler()
train_X = mms.fit_transform(train_X)
# Shuffle the training data just in case...
train = np.append(train_X, train_y, axis=1)
np.random.shuffle(train)
train_X = train[:, :-3]
train_y = train[:, -3:]
print("Done Processing!!!")
print("Training MLP...")
# CrossFold validation, test set only for FINAL evaluation
accuracys = []
costs = []
precissions = []
recalls = []
f1_scores = []
kfolds = KFold(n_splits=3, shuffle=True, random_state=42)
num = 1
for train_index, test_index in kfolds.split(train_X, train_y):
train_X_folds = train_X[train_index]
train_y_folds = train_y[train_index]
test_X_folds = train_X[test_index]
test_y_folds = train_y[test_index]
acc, cost, pred = MLP(train_X_folds, train_y_folds,
test_X_folds, test_y_folds,
"MLP-" + str(num) + "/")
accuracys.append(acc)
costs.append(cost)
pred = pred.astype(float)
prec1 = precision_score(test_y_folds[:,0], pred[:, 0])
prec2 = precision_score(test_y_folds[:,1], pred[:, 1])
prec3 = precision_score(test_y_folds[:,2], pred[:, 2])
precissions.append(np.mean([prec1, prec2, prec3]))
rec1 = recall_score(test_y_folds[:,0], pred[:, 0])
rec2 = recall_score(test_y_folds[:,1], pred[:, 1])
rec3 = recall_score(test_y_folds[:,2], pred[:, 2])
recalls.append(np.mean([rec1, rec2, rec3]))
f1_sc1 = f1_score(test_y_folds[:,0], pred[:,0])
f1_sc2 = f1_score(test_y_folds[:,1], pred[:,1])
f1_sc3 = f1_score(test_y_folds[:,2], pred[:,2])
f1_scores.append(np.mean([f1_sc1, f1_sc2, f1_sc3]))
num = num + 1
print("Accuracys: ", accuracys)
print("Costs: ", costs)
print("Precissions: ", precissions)
print("Recalls: ", recalls)
print("F1 Scores: ", f1_scores)
if __name__ == "__main__":
main()