-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdlutils.py
361 lines (318 loc) · 15.5 KB
/
dlutils.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
This code includes utility functions to build the neural networks.
The DNN part is largely based on Theano's tutorial
http://deeplearning.net/software/theano/tutorial/examples.html
and on the deeplearning.net tutorials
http://deeplearning.net/tutorial/
Copyright 2017 by Branislav Gerazov.
See the file LICENSE for the licence associated with this software.
Author:
Branislav Gerazov, April 2017
"""
from __future__ import division
import numpy as np
from matplotlib import pyplot as plt
from scipy.io import wavfile
import os
from scipy import fftpack as fftp
from scipy import signal as sig
import scipy.interpolate as interp
import theano as th
import theano.tensor as T
from theano.tensor.signal import pool
class fc_layer(object):
'''
I wanted to avoid making a class but if you want to make the whole
network creation process dynamic you need it.
'''
def __init__(self, rng, srng, tensor_in, n_in, n_neurons=1, activation=None,
lvl=0, use_dropout=False, dropout=0, params=None):
self.input = tensor_in
self.n_in = n_in
self.n_neurons = n_neurons
self.activation = activation
self.lvl = lvl
if params is None:
self.init_params(rng)
w, b = self.w, self.b
else: # used to generate test net without dropout
self.w, self.b = params
# self.w, self.b = params[0] * (1 - dropout), params[1]
w, b = self.w, self.b
output = T.dot(tensor_in, w) + b
if activation is not None:
output = activation(output)
# dropout using code from https://blog.wtf.sg/2014/07/23/dropout-using-theano/
if use_dropout:
mask = srng.binomial(size=output.shape,p=1-dropout,
dtype=th.config.floatX)
output = T.switch(mask, output,0)
# output = mask * output
# output = T.switch(srng.normal(output.shape) > dropout,output,0)
else:
output = (1-dropout) * output
# output = output
self.params = [self.w, self.b]
self.output = output
def init_params(self, rng, reset=False):
'''
Function to initialise weights and biases based on the activation func
also used to reset them in the k-fold loop.
'''
n_in = self.n_in
n_neurons = self.n_neurons
activation = self. activation
lvl = self.lvl
W_bound = (6 / (n_in + n_neurons))**.5 # from deeplearning.net tutorial
inits = {T.nnet.relu : ((rng.randn(n_in, n_neurons)* 0.1).astype(th.config.floatX),
np.ones(n_neurons).astype(th.config.floatX)),
T.nnet.sigmoid : ((rng.uniform(-W_bound, W_bound,
size=(n_in, n_neurons))*4).astype(th.config.floatX),
np.zeros(n_neurons).astype(th.config.floatX)),
T.tanh : (rng.uniform(-W_bound, W_bound,
size=(n_in, n_neurons)).astype(th.config.floatX),
np.zeros(n_neurons).astype(th.config.floatX))}
if not reset:
w = th.shared(inits[activation][0], name='w_'+str(lvl), borrow=True)
b = th.shared(inits[activation][1], name='b_'+str(lvl), borrow=True)
self.w, self.b = w, b
else:
self.w.set_value(inits[activation][0], borrow=True)
self.b.set_value(inits[activation][1], borrow=True)
class cnn_pool_layer(object):
'''
Convolutional NN layer with pooled output.
'''
def __init__(self, rng, srng, tensor_in, shape_input,
shape_filter=(12, 1, 5, 5),
border='valid', stride=(1, 1),
pool_stride=(2, 2),
lvl=0, activation=None,
use_dropout=False, dropout=0,
params=None):
'''
shape_in : (batch size (b), input channels (c),
input rows (i1), input columns (i2))
shape_filter : (output channels (c1), input channels (c2),
filter rows (k1), filter columns (k2))
border : 'valid', 'half', 'full' or zero padding (p_1, p_2)
stride : stride along each axis
pool_stride : stride of pooling layer
'''
self.input = tensor_in
self.shape_input = shape_input
self.shape_filter = shape_filter
self.border = border
self.stride = stride
self.pool_stride = pool_stride
self.lvl = lvl
self.activation = activation
if params is None:
self.init_params(rng)
# filters, b = self.filters, self.b
else: # used to generate test net without dropout
# filters, b = params
self.filters, self.b = params
cnn_output = T.nnet.conv2d(tensor_in, self.filters,
input_shape=shape_input,
filter_shape=shape_filter,
border_mode=border,
subsample=stride)
# output.shape[2] == (i1 + 2 * p1 - k1) // s1 + 1
# output.shape[3] == (i2 + 2 * p2 - k2) // s2 + 1
pool_output = pool.pool_2d(cnn_output,
ws=pool_stride,
ignore_border=True,
mode='max') # can be sum,
# average_inc_pad,
# average_exc_pad
output = pool_output + self.b.dimshuffle('x', 0, 'x', 'x')
# from deeplearning CNN tutorial:
# add the bias term. Since the bias is a vector (1D array), we first
# reshape it to a tensor of shape (1, n_filters, 1, 1). Each bias will
# thus be broadcasted across mini-batches and feature map
# width & height
if activation is not None:
output = activation(output)
# dropout using code from https://blog.wtf.sg/2014/07/23/dropout-using-theano/
if use_dropout:
mask = srng.binomial(size=output.shape,
p=1-dropout,
dtype=th.config.floatX)
output = T.switch(mask, output, 0)
else:
output = (1-dropout) * output
self.output = output
self.params = [self.filters, self.b]
def init_params(self, rng, reset=False):
'''
Function to initialise filters and biases based on the activation func
also used to reset them in the k-fold loop.
'''
shape_filter = self.shape_filter
pool_stride = self.pool_stride
activation = self.activation
lvl = self.lvl
# according to deeplearning cnn tutorial:
fan_in = np.prod(shape_filter [1:])
fan_out = (shape_filter[0] * np.prod(shape_filter[2:]) //
np.prod(pool_stride))
W_bound = np.sqrt(6. / (fan_in + fan_out))
inits = {T.nnet.relu : ((rng.standard_normal(shape_filter)* 0.1).astype(
th.config.floatX),
np.ones(shape_filter[0]).astype(th.config.floatX)),
T.nnet.sigmoid : ((rng.uniform(-W_bound, W_bound,
size=(shape_filter))*4).astype(th.config.floatX),
np.zeros(shape_filter[0]).astype(th.config.floatX)),
T.tanh : (rng.uniform(-W_bound, W_bound,
size=(shape_filter)).astype(th.config.floatX),
np.zeros(shape_filter[0]).astype(th.config.floatX))}
if not reset:
filters = th.shared(inits[activation][0], name='filt_'+str(lvl), borrow=True)
b = th.shared(inits[activation][1], name='b_'+str(lvl), borrow=True)
self.filters, self.b = filters, b
else:
self.filters.set_value(inits[activation][0], borrow=True)
self.b.set_value(inits[activation][1], borrow=True)
def construct_mlp(rng, srng, x, n_feats, batch_size, cnn_layers, mlp_layers, dropouts):
'''
Construct nnet based on input parameters and include dropout for training.
'''
# reshape x into a tensor
n_rows, n_cols = n_feats
next_input = x.reshape((batch_size, 1, n_rows, n_cols))
next_input_test = next_input
next_shape_in = (batch_size, 1, n_rows, n_cols)
next_channels_in = 1
mlp = []
mlp_test = []
if cnn_layers is not None:
for layer, (n_filters, filter_size, border, pool_stride,
activation, dropout) in enumerate(cnn_layers):
new_layer = cnn_pool_layer(rng, srng,
next_input,
shape_input=next_shape_in,
shape_filter=(n_filters,
next_channels_in,
filter_size[0],
filter_size[1]),
border=border,
stride=(1, 1),
pool_stride=pool_stride,
lvl=layer,
activation=activation,
dropout=dropout,
params=None)
# shape_in : (batch size (b), input channels (c),
# input rows (i1), input columns (i2))
# shape_filter : (output channels (c1), input channels (c2),
# filter rows (k1), filter columns (k2))
# border : 'valid', 'half', 'full' or zero padding (p_1, p_2)
# stride : stride along each axis
# pool_stride : stride of pooling layer
mlp.append(new_layer)
if np.any(dropouts):
new_layer_test = cnn_pool_layer(rng, srng,
next_input_test,
shape_input=next_shape_in,
shape_filter=(n_filters,
next_channels_in,
filter_size[0],
filter_size[1]),
border=border,
stride=(1, 1),
pool_stride=pool_stride,
lvl=layer,
activation=activation,
dropout=dropout,
params=new_layer.params,
use_dropout=False)
mlp_test.append(new_layer_test)
next_input_test = new_layer_test.output
next_input = new_layer.output
if border == 'valid':
pad = [0, 0]
elif border == 'half':
pad = np.asarray(filter_size) // 2
in_rows, in_cols = next_shape_in[2:]
# output.shape[2] == (i1 - k1 + 2 * p1) // s1 + 1
out_rows = (in_rows - filter_size[0] + 2*pad[0] + 1) // pool_stride[0]
out_cols = (in_cols - filter_size[1] + 2*pad[1] + 1) // pool_stride[1]
next_shape_in = (batch_size, n_filters, out_rows, out_cols)
print(next_shape_in)
next_channels_in = n_filters
last_cnn_layer = layer + 1 # to continue counting the fc layers
next_input = next_input.flatten(2)
next_input_test = next_input_test.flatten(2)
next_size_in = np.prod(next_shape_in[1:])
print(next_size_in)
for layer, (n_neurons, activation, dropout) in enumerate(mlp_layers):
new_layer = fc_layer(rng, srng,
next_input, next_size_in,
n_neurons,
activation,
layer+last_cnn_layer,
dropout=dropout)
mlp.append(new_layer)
if np.any(dropouts):
new_layer_test = fc_layer(rng, srng,
next_input_test,
next_size_in,
n_neurons,
activation,
layer+last_cnn_layer,
dropout=dropout,
params=new_layer.params,
use_dropout=False)
mlp_test.append(new_layer_test)
next_input_test = new_layer_test.output
next_input = new_layer.output
next_size_in = n_neurons
if not np.any(dropouts): # all are 0s
mlp_test = mlp
return mlp, mlp_test
def construct_dnn(rng, x, n_in0, mlp_layers, dropouts=0):
'''
A watered down version of construct_mlp which does not have
convolutional layers.
'''
next_input = x
next_size_in = n_in0
mlp = []
if not np.any(dropouts): # all are 0s
for layer, (n_neurons, activation, dropout) in enumerate(mlp_layers):
new_layer = fc_layer(rng, next_input, next_size_in, n_neurons,
activation, layer, dropout=0)
mlp.append(new_layer)
next_input = new_layer.output
next_size_in = n_neurons
mlp_test = mlp
else:
mlp_test = []
next_input_test = x
for layer, (n_neurons, activation, dropout) in enumerate(mlp_layers):
new_layer = fc_layer(rng, next_input, next_size_in, n_neurons,
activation, layer,
use_dropout=False, dropout=dropout)
mlp.append(new_layer)
new_layer_test = fc_layer(rng, next_input_test, next_size_in, n_neurons,
activation, layer,
use_dropout=False, dropout=dropout,
params=new_layer.params)
mlp_test.append(new_layer_test)
next_input = new_layer.output
next_input_test = new_layer_test.output
next_size_in = n_neurons
return mlp, mlp_test
def share_data(data_x, data_y, borrow=True):
""" Function that loads the dataset into shared variables
adapted from http://deeplearning.net/tutorial/logreg.html#logreg
"""
shared_x = th.shared(np.asarray(data_x, dtype=th.config.floatX),
borrow=borrow)
shared_y = th.shared(np.asarray(data_y, dtype=th.config.floatX),
borrow=borrow)
# return shared_x, T.cast(shared_y, 'int32')
return shared_x, shared_y