/
DNNRegression.py
345 lines (292 loc) · 12.9 KB
/
DNNRegression.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
'''
Created on Jul 1, 2015
@author: peter
'''
import sys
import getopt
import os
import numpy as np
import matplotlib.pyplot as plt
import scipy.io
import data_loader
import cPickle as pkl
from pylearn2.scripts import train as pylearn2train
from pylearn2.utils import serial
from pylearn2.datasets import cache
from theano import tensor as theanoTensor
from theano import function as theanoFunction
class DNNregression:
def __init__(self, train_network, yaml_path, stimulus_data_path,
response_data_path, weight_path, visualize, context_length,
direction, valid_data_entries_path, forking, verbosity,
debug):
self.train_network = train_network
self.yaml_path = yaml_path
self.stimulus_data_path = stimulus_data_path
self.response_data_path = response_data_path
self.visualize = visualize
self.context_length = context_length
self.direction = direction
self.valid_data_entries_path = valid_data_entries_path
self.forking = forking
self.verbosity = verbosity
self.debug = debug
self.model_path = "./network_best.pkl"
self.weight_path= weight_path
if weight_path[-4:] != ".pkl":
self.model = None
else:
self.model = weight_path
def train(self):
"""
See module-level docstring of /pylearn2/scripts/train.py for a more details.
"""
print 'make argument parser'
parser = pylearn2train.make_argument_parser()
print 'train network'
pylearn2train.train(self.yaml_path)
def save_weights(self):
"""
Saves all weights in a .txt, .npy or .mat file depending on the ending of the 'weight_path'.
If the path ends in .pkl, the entire model is stored.
"""
model = serial.load(self.model_path)
weight_dict = {}
for layer in model.layers:
try:
weight_dict[layer.layer_name] = layer.get_weights()
except:
layer_weights = layer.get_weights_topo()
weight_dict[layer.layer_name] = layer_weights# without reshaping since it the input/output vector would need to reshaped in the same way which might lead to problems
if self.weight_path[-4:] == '.pkl':
print 'saving model ', self.weight_path
serial.save(self.weight_path, model)
elif self.weight_path[-4:] == '.mat':
scipy.io.savemat(self.weight_path[:-4]+'.mat', weight_dict)
elif self.weight_path[-4:] == '.npy':
np.save(self.weight_path[:-4], weight_dict)
else:
raise Exception('Only ".mat", ".pkl" and ".npy" files are supported as data formats.')
def get_predictions(self, prediction_type="regression"):
if self.model == None:
print 'no evaluation possible since no model was provided.'
return
try:
model = serial.load(self.model)
except Exception as e:
print("error loading {}:".format(self.model))
print(e)
raise Exception("error loading {}:".format(self.model))
X = model.get_input_space().make_theano_batch()
Y = model.fprop(X)
if prediction_type == "classification":
Y = theanoTensor.argmax(Y, axis=1)
else:
assert prediction_type == "regression"
f = theanoFunction([X], Y, allow_input_downcast=True)
print("loading data and predicting...")
data = data_loader.Data('test')
if self.direction == 'reverse':
input_data = data.response_data
output_path = self.stimulus_data_path
m, r, c = input_data.shape
input_data = input_data.reshape((m, r, c , 1))
elif self.direction == 'forward':
input_data = data.stimulus_data
output_path = self.response_data_path
else:
raise Exception('Specify either "reverse" or "forward" as direction.')
prediction = f(input_data)
print("writing predictions...")
if output_path[-4:] == '.mat':
scipy.io.savemat(output_path, {'data': prediction})
elif output_path[-4:] == '.txt':
np.savetxt(output_path, prediction)
elif output_path[-4:] == '.npy':
np.save(output_path, prediction)
else:
raise Exception('Only ".mat", ".txt" and ".npy" files are supported as data formats.')
#------------------------------------------------------------------------------
# for testing
#------------------------------------------------------------------------------
if self.debug:
try:
test_output = np.squeeze(data_loader.load_data_from_file('testUnattendedAudioOrg.mat'))
test_predictions = np.squeeze(data_loader.load_data_from_file(self.stimulus_data_path))
print 'DEBUG: test_predictions.shape', test_predictions.shape, 'test_output.shape', test_output.shape
print 'DEBUG: test_predictions.max', np.max(test_predictions), np.argmax(test_predictions)
print 'DEBUG: ', np.corrcoef(test_predictions, test_output)
except:
print 'Detailed correlation analysis only possible for the hello world example.'
pass
def visulize_results(self):
if self.direction == 'reverse':
upper_title = 'Predicted Stimulus'
lower_title = 'Response'
else:
upper_title = 'Stimulus'
lower_title = 'Predicted Response'
plt.subplot(2,1,1)
plt.title(upper_title)
plt.plot(self.stimulus_data_path)
plt.subplot(2,1,2)
plt.title(lower_title)
plt.plot(self.response_data_path)
plt.savefig(self.visualize)
# def show_weights(self, model_path, out):
# """
# Show or save weights to an image for a pickled model
#
# Parameters
# ----------
# model_path : str
# Path of the model pkl to show weights for
# border : bool, optional
# out : str
# Output file to save weights to
# """
# pv = get_weights_report.get_weights_report(model_path=model_path,
# rescale="individual")
#
# if out is not None:
# pv.save(out)
def main():
#------------------------------------------------------------------------------
# set parameters
#------------------------------------------------------------------------------
train_network = None
yaml = None
stimulus_data = None
response_data = None
weight_path = None
visualize = False
context_length = 25
direction = 'reverse'
valid_data_entries = -1 # this encodes the default to use all entries
forking = False
verbosity = 1
debug = 0
num_training_epochs = 100
num_neurons = 5
num_channels = 32
#------------------------------------------------------------------------------
# parse arguments
#------------------------------------------------------------------------------
def usage():
print 'Call python DNNRegression.py with the following arguments: \n', \
'{--train, --predict} \n', \
'-m model.yaml \n', \
'-s stimulus_data [i/o, default text, or .mat file] \n', \
'-r response_data [i/o, default text, or .mat file] \n', \
'-w weights [i/o, default text, or .pkl] \n', \
'[--visual visualize.png] \n', \
'[--context N for N>=1 *25] \n', \
'[--numEpochs N default is 100]', \
'[--numNeurons N default is 5]', \
'[--numChannels N default is 32]', \
'[--dir forward/reverse*] \n', \
'[--valid which parts are valid, in case of concatenating trials, default all valid.] \n', \
'[--forking for matlab sake] \n'
try:
opts, _ = getopt.getopt(sys.argv[1:],
"htpm:s:r:w:", ["help", "train", "predict", "model=",
"stimulus=", "response=", "weights=",
"visual=", "context=", "dir=", "valid=",
"forking=", "verbosity=", "debug",
"numEpochs=", "numNeurons=", "numChannels="])
except getopt.GetoptError as err:
print str(err) # will print something like "option -a not recognized"
usage()
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit()
elif opt in ("-p", "--predict"):
train_network = 0
elif opt in ("-t", "--train"):
train_network = 1
elif opt in ("-m", "--model"):
yaml = arg
elif opt in ("-s", "--stimulus"):
stimulus_data = arg
elif opt in ("-r", "--response"):
response_data = arg
elif opt in ("-w", "--weights"):
weight_path = arg
elif opt in ("--visual"):
visualize = arg
elif opt in ("--context"):
context_length = int(arg)
elif opt in ("--numEpochs"):
num_training_epochs = int(arg)
elif opt in ("--numNeurons"):
num_neurons = int(arg)
elif opt in ("--numChannels"):
num_channels = int(arg)
elif opt in ("--dir"):
direction = arg
elif opt in ("--valid"):
valid_data_entries = arg
elif opt in ("--forking"):
print "forking is not supported for now"
# forking = True
elif opt in ("--verbosity"):
verbosity = arg
elif opt in ("--debug"):
debug = 1
else:
assert False, "unhandled option"
if train_network is None:
print 'You need to define wheter to predict outputs or to train the network.'
usage()
sys.exit(2)
if yaml is None:
print 'You need to define a valid model path / yaml file.'
usage()
sys.exit(2)
if stimulus_data is None:
print 'You need to define a valid stimulus data path.'
usage()
sys.exit(2)
if response_data is None:
print 'You need to define a valid response data path.'
usage()
sys.exit(2)
if weight_path is None:
print 'You need to define a valid path containing the weights of the mode.'
usage()
sys.exit(2)
#------------------------------------------------------------------------------
# Save environment for other parts of the DNN
#------------------------------------------------------------------------------
os.environ["EEGTOOLS_TRAIN_NETWORK"] = str(train_network)
os.environ["EEGTOOLS_STIMULUS_DATA_PATH"] = stimulus_data
os.environ["EEGTOOLS_RESPONSE_DATA_PATH"] = response_data
os.environ["EEGTOOLS_VALID_DATA_PATH"] = str(valid_data_entries)
os.environ["EEGTOOLS_CONTEXT_LENGTH"] = str(context_length)
os.environ["EEGTOOLS_DIRECTION"] = direction
os.environ["EEGTOOLS_DEBUG"] = str(debug)
os.environ["EEGTOOLS_NUM_CHANNELS"] = str(num_channels)
#------------------------------------------------------------------------------
# Save imports for the YAML file
#------------------------------------------------------------------------------
pkl.dump(context_length, open( 'context_length.pkl', 'wb'))
pkl.dump(num_training_epochs, open( 'num_training_epochs.pkl', 'wb'))
pkl.dump(num_neurons, open( 'num_neurons.pkl', 'wb'))
pkl.dump(num_channels, open( 'num_channels.pkl', 'wb'))
#------------------------------------------------------------------------------
# train / run DNN
#------------------------------------------------------------------------------
net = DNNregression(train_network, yaml, stimulus_data, response_data,
weight_path, visualize, context_length, direction,
valid_data_entries, forking, verbosity, debug)
if net.train_network:
net.train()
net.save_weights()
else:
net.get_predictions()
if net.visualize:
net.show_weights()
if __name__ == "__main__":
main()