/
train_demo.py
executable file
·183 lines (128 loc) · 4.98 KB
/
train_demo.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
# This program is a demo for using deep learning algorithm for OCR digit recognition
# Based on the tutorial here: http://deeplearning.net/tutorial/mlp.html
# A stripped down version: https://gist.github.com/honnibal/6a9e5ef2921c0214eeeb
#
# This version of the demo was tested on Linux
import os
import sys
import time
from os import path
from os.path import isfile, join
import random
import gzip
import cPickle
import time
import neuralnetwork as nn
import numpy
import theano
import spectrogram as sg
def demo_load(train_dir):
train_input = [f for f in os.listdir(train_dir) if isfile(join(train_dir, f))]
classes = []
train_set = []
test_set = []
print 'loading data'
# Training data
ize = 1
for element in train_input:
elem_id = element.split(".")[0].split("_")[0]
print elem_id
try:
index = classes.index(elem_id)
except:
index = len(classes)
classes.append(elem_id)
stgrm = sg.generate_spectrogram(join(train_dir, element))
if ize == 3:
exit(1)
ize = ize + 1
train_set.extend(_make_array(stgrm, index))
train_min_nr = min([len(x[0]) for x in train_set])
train_data = [x[:train_min_nr] for x in train_set]
train_data = numpy.random.permutation(train_data)
test_data = train_data[:130]
train_data = train_data[1300:]
return train_data, test_data, classes
def load_data(train_dir, test_dir, noise_dir):
train_input = [f for f in os.listdir(train_dir) if isfile(join(train_dir, f))]
test_input = [f for f in os.listdir(test_dir) if isfile(join(test_dir, f))]
classes = []
train_set = []
test_set = []
# Training data
for element in train_input:
elem_id = element.split(".")[0].split("_")[0]
try:
index = classes.index(elem_id)
except:
index = len(classes)
classes.append(elem_id)
stgrm = sg.generate_spectrogram(join(train_dir, element))
train_set.append(_make_array(stgrm, index))
# Test data
for element in test_input:
elem_id = element.split(".")[0].split("_")[0]
test_set.append([join(test_dir,element),elem_id])
train_min_nr = min([len(x) for x in train_set])
train_data = []
for x in train_set:
indexes = numpy.random.choice(range(len(x)), train_min_nr, replace=False)
train_data.extend([x[i] for i in indexes])
return numpy.random.permutation(train_data), test_set, classes
def _make_array(x, y):
return zip(
numpy.asarray(x, dtype=theano.config.floatX),
numpy.asarray([y]*len(x), dtype='int32')
)
def main(train_dir='train', test_dir='test', noise_dir='pnoise'):
print '... loading data'
train_set, test_set, classes = load_data(train_dir, test_dir, noise_dir)
women_set = [x for x in test_set if x[1][0]=="F"]
men_set = [x for x in test_set if x[1][0]=="M"]
g = open('o_err.txt','a')
f = open('m_err.txt','a')
h = open('f_err.txt','a')
# train_set, test_set, classes = load_data2()
#train_set, test_set, classes = demo_load("mienk")
print '... building the model'
n = nn.Net(learning_rate=0.000075, train_data=train_set, classes=classes, L2_reg=0.000001)
n.add_hidden_layer(1201, 1500)
n.add_hidden_layer(1500, 1250)
n.add_hidden_layer(1250, 1000)
n.add_hidden_layer(1000, 500)
print '... compiling the model'
n.compile_model()
current_error = 1
error = 1
layers_old = n.getlayers();
layers_new = None;
# We train the network 100 times
# Each time we evaluate the results and write out the error percentage
for epoch in range(1, 2000):
if error < current_error:
current_error = error
print 'Saving matrix...'
n.save()
print 'Save completed...'
z = time.time()
# print '... training'
print 'Training...'
for i in xrange(len(train_set)):
n.train_model(i)
print 'training took {}'.format(time.time()-z)
print '... calculating error'
# compute zero-one loss on validation set
z = time.time()
error = numpy.mean([int(x[1] != n.evaluate(x[0])) for x in women_set])
h.write("{}. epoch: {}\n".format(epoch, error*100))
error = numpy.mean([int(x[1] != n.evaluate(x[0])) for x in men_set])
f.write("{}. epoch: {}\n".format(epoch, error*100))
error = numpy.mean([int(x[1] != n.evaluate(x[0])) for x in test_set])
g.write("{}. epoch: {}\n".format(epoch, error*100))
print 'error calc took {}'.format(time.time() - z)
print('epoch %i, validation error %f %%' % (epoch, error * 100))
print 'layers migration'
layers_new = n.getlayers()
print [numpy.linalg.norm(x[0]-x[1])/len(x[0]) for x in zip(layers_new, layers_old)]
if __name__ == '__main__':
main()