-
Notifications
You must be signed in to change notification settings - Fork 15
/
train_cnn.py
416 lines (302 loc) · 12.5 KB
/
train_cnn.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
'''
If you are using a GPU, write the following in ~/.theanorc.
[global]
device=gpu
floatX=float32
[blas]
ldflags=-lopenblas
[cuda]
root=/opt/apps/cuda/7.0
[nvcc]
fastmath=True
'''
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import os
import sys
import time
import cPickle as pickle
import numpy as np
import theano
import theano.tensor as T
import lasagne
from lasagne import layers
from lasagne.updates import nesterov_momentum
from lasagne.objectives import categorical_crossentropy
from lasagne.nonlinearities import leaky_rectify
from lasagne.init import Orthogonal, Constant
from nolearn.lasagne import NeuralNet
from nolearn.lasagne import BatchIterator
from lasagne.nonlinearities import softmax
import bmc
X = np.load("../data/sdss_training_images.npy")
print("X.shape = {}, X.min = {}, X.max = {}".format(X.shape, X.min(), X.max()))
y = np.load("../data/sdss_training_labels.npy")
print("y.shape = {}, y.min = {}, y.max = {}".format(y.shape, y.min(), y.max()))
def renormalize(array):
return (array - array.min()) / (array.max() - array.min())
for i in range(5):
X[:, i, :, :] = renormalize(X[:, i, :, :])
y = renormalize(y).astype(np.int32)
print("X.shape = {}, X.min = {}, X.max = {}".format(X.shape, X.min(), X.max()))
print("y.shape = {}, y.min = {}, y.max = {}".format(y.shape, y.min(), y.max()))
def compute_PCA(array):
nimages0, nchannels0, height0, width0 = array.shape
rolled = np.transpose(array, (0, 2, 3, 1))
# transpose from N x channels x height x width to N x height x width x channels
nimages1, height1, width1, nchannels1 = rolled.shape
# check shapes
assert nimages0 == nimages1
assert nchannels0 == nchannels1
assert height0 == height1
assert width0 == width1
# flatten
reshaped = rolled.reshape(nimages1 * height1 * width1, nchannels1)
from sklearn.decomposition import PCA
pca = PCA()
pca.fit(reshaped)
cov = pca.get_covariance()
eigenvalues, eigenvectors = np.linalg.eig(cov)
return eigenvalues, eigenvectors
class AugmentedBatchIterator(BatchIterator):
def __init__(self, batch_size, crop_size=8, testing=False):
super(AugmentedBatchIterator, self).__init__(batch_size)
self.crop_size = crop_size
self.testing = testing
def transform(self, Xb, yb):
Xb, yb = super(AugmentedBatchIterator, self).transform(Xb, yb)
batch_size, nchannels, width, height = Xb.shape
if self.testing:
if self.crop_size % 2 == 0:
right = left = self.crop_size // 2
else:
right = self.crop_size // 2
left = self.crop_size // 2 + 1
X_new = Xb[:, :, right: -left, right: -left]
return X_new, yb
eigenvalues, eigenvectors = compute_PCA(Xb)
# Flip half of the images horizontally at random
indices = np.random.choice(batch_size, batch_size // 2, replace=False)
Xb[indices] = Xb[indices, :, :, ::-1]
# Crop images
X_new = np.zeros(
(batch_size, nchannels, width - self.crop_size, height - self.crop_size),
dtype=np.float32
)
for i in range(batch_size):
# Choose x, y pixel posiitions at random
px, py = np.random.choice(self.crop_size, size=2)
sx = slice(px, px + width - self.crop_size)
sy = slice(py, py + height - self.crop_size)
# Rotate 0, 90, 180, or 270 degrees at random
nrotate = np.random.choice(4)
# add random color perturbation
alpha = np.random.normal(loc=0.0, scale=0.5, size=5)
noise = np.dot(eigenvectors, np.transpose(alpha * eigenvalues))
for j in range(nchannels):
X_new[i, j] = np.rot90(Xb[i, j, sx, sy] + noise[j], k=nrotate)
return X_new, yb
class SaveParams(object):
def __init__(self, name):
self.name = name
def __call__(self, nn, train_history):
if train_history[-1]["valid_loss_best"]:
nn.save_params_to("{}.params".format(self.name))
with open("{}.history".format(self.name), "w") as f:
pickle.dump(train_history, f)
class UpdateLearningRate(object):
def __init__(self, start=0.001, stop=0.0001):
self.start, self.stop = start, stop
self.ls = None
def __call__(self, nn, train_history):
if self.ls is None:
self.ls = np.linspace(self.start, self.stop, nn.max_epochs)
epoch = train_history[-1]['epoch']
new_value = np.float32(self.ls[epoch - 1])
getattr(nn, "update_learning_rate").set_value(new_value)
class TrainSplit(object):
def __init__(self, eval_size):
self.eval_size = eval_size
def __call__(self, X, y, net):
if self.eval_size:
X_train, y_train = X[:-self.eval_size], y[:-self.eval_size]
X_valid, y_valid = X[-self.eval_size:], y[-self.eval_size:]
else:
X_train, y_train = X, y
X_valid, y_valid = _sldict(X, slice(len(y), None)), y[len(y):]
return X_train, X_valid, y_train, y_valid
net = NeuralNet(
layers=[
('input', layers.InputLayer),
('conv11', layers.Conv2DLayer),
('conv12', layers.Conv2DLayer),
('pool1', layers.MaxPool2DLayer),
('conv21', layers.Conv2DLayer),
('conv22', layers.Conv2DLayer),
('conv23', layers.Conv2DLayer),
('pool2', layers.MaxPool2DLayer),
('conv31', layers.Conv2DLayer),
('conv32', layers.Conv2DLayer),
('conv33', layers.Conv2DLayer),
('pool3', layers.MaxPool2DLayer),
('dropout4', layers.DropoutLayer),
('hidden4', layers.DenseLayer),
('dropout5', layers.DropoutLayer),
('hidden5', layers.DenseLayer),
('output', layers.DenseLayer),
],
input_shape=(None, 5, 44, 44),
conv11_num_filters=32, conv11_filter_size=(5, 5),
conv11_nonlinearity=leaky_rectify,
conv11_W=Orthogonal(np.sqrt(2 / (1 + 0.01**2))), conv11_b=Constant(0.1),
conv12_num_filters=32, conv12_filter_size=(3, 3), conv12_pad=1,
conv12_nonlinearity=leaky_rectify,
conv12_W=Orthogonal(np.sqrt(2 / (1 + 0.01**2))), conv12_b=Constant(0.1),
pool1_pool_size=(2, 2),
conv21_num_filters=64, conv21_filter_size=(3, 3), conv21_pad=1,
conv21_nonlinearity=leaky_rectify,
conv21_W=Orthogonal(np.sqrt(2 / (1 + 0.01**2))), conv21_b=Constant(0.1),
conv22_num_filters=64, conv22_filter_size=(3, 3), conv22_pad=1,
conv22_nonlinearity=leaky_rectify,
conv22_W=Orthogonal(np.sqrt(2 / (1 + 0.01**2))), conv22_b=Constant(0.1),
conv23_num_filters=64, conv23_filter_size=(3, 3), conv23_pad=1,
conv23_nonlinearity=leaky_rectify,
conv23_W=Orthogonal(np.sqrt(2 / (1 + 0.01**2))), conv23_b=Constant(0.1),
pool2_pool_size=(2, 2),
conv31_num_filters=128, conv31_filter_size=(3, 3), conv31_pad=1,
conv31_nonlinearity=leaky_rectify,
conv31_W=Orthogonal(np.sqrt(2 / (1 + 0.01**2))), conv31_b=Constant(0.1),
conv32_num_filters=128, conv32_filter_size=(3, 3), conv32_pad=1,
conv32_nonlinearity=leaky_rectify,
conv32_W=Orthogonal(np.sqrt(2 / (1 + 0.01**2))), conv32_b=Constant(0.1),
conv33_num_filters=128, conv33_filter_size=(3, 3), conv33_pad=1,
conv33_nonlinearity=leaky_rectify,
conv33_W=Orthogonal(np.sqrt(2 / (1 + 0.01**2))), conv33_b=Constant(0.1),
pool3_pool_size=(2, 2),
hidden4_num_units=2048,
hidden4_nonlinearity=leaky_rectify,
hidden4_W=Orthogonal(np.sqrt(2 / (1 + 0.01**2))), hidden4_b=Constant(0.01),
dropout4_p=0.5,
hidden5_num_units=2048,
hidden5_nonlinearity=leaky_rectify,
hidden5_W=Orthogonal(np.sqrt(2 / (1 + 0.01**2))), hidden5_b=Constant(0.01),
dropout5_p=0.5,
output_num_units=2,
output_nonlinearity=softmax,
update_learning_rate=theano.shared(np.float32(0.003)),
update_momentum=0.9,
objective_loss_function=categorical_crossentropy,
regression=False,
max_epochs=750,
batch_iterator_train=AugmentedBatchIterator(batch_size=128, crop_size=4),
batch_iterator_test=AugmentedBatchIterator(batch_size=128, crop_size=4, testing=True),
on_epoch_finished=[
UpdateLearningRate(start=0.003, stop=0.0001),
SaveParams("net")
],
verbose=2,
train_split=TrainSplit(eval_size=15000)
)
net.fit(X, y)
best_valid_loss = min([row['valid_loss'] for row in net.train_history_])
print("Best valid loss: {}".format(best_valid_loss))
X_valid = X[-15000:]
y_valid = y[-15000:]
for i in range(5):
X_valid[:, i, :, :] = renormalize(X_valid[:, i, :, :])
y_valid = renormalize(y_valid).astype(np.int32)
y_pred_valid = np.zeros((len(y_valid), 64))
class AugmentedBatchIterator(BatchIterator):
def __init__(self, batch_size, crop_size=8, validation=False, testing=False, startx=None, starty=None, rotate=None):
super(AugmentedBatchIterator, self).__init__(batch_size)
self.crop_size = crop_size
self.validation = validation
self.testing = testing
self.startx, self.starty = startx, starty
self.rotate = rotate
def transform(self, Xb, yb):
Xb, yb = super(AugmentedBatchIterator, self).transform(Xb, yb)
batch_size, nchannels, width, height = Xb.shape
if self.validation:
if self.crop_size % 2 == 0:
right = left = self.crop_size // 2
else:
right = self.crop_size // 2
left = self.crop_size // 2 + 1
X_new = Xb[:, :, right: -left, right: -left]
return X_new, yb
if not self.testing:
eigenvalues, eigenvectors = compute_PCA(Xb)
# Flip half of the images horizontally at random
indices = np.random.choice(batch_size, batch_size // 2, replace=False)
Xb[indices] = Xb[indices, :, :, ::-1]
# Crop images
X_new = np.zeros(
(batch_size, nchannels, width - self.crop_size, height - self.crop_size),
dtype=np.float32
)
for i in range(batch_size):
if self.testing:
px, py = self.startx, self.starty
else:
# Choose x, y pixel posiitions at random
px, py = np.random.choice(self.crop_size, size=2)
sx = slice(px, px + width - self.crop_size)
sy = slice(py, py + height - self.crop_size)
# Rotate 0, 90, 180, or 270 degrees at random
if self.testing:
nrotate = self.rotate
noise = np.zeros(nchannels)
else:
nrotate = np.random.choice(4)
# add random color perturbation
alpha = np.random.normal(loc=0.0, scale=0.5, size=5)
noise = np.dot(eigenvectors, np.transpose(alpha * eigenvalues))
for j in range(nchannels):
X_new[i, j] = np.rot90(Xb[i, j, sx, sy] + noise[j], k=nrotate)
return X_new, yb
count = 0
print("Starting model combination...")
for startx in range(4):
for starty in range(4):
for rotate in range(4):
net.batch_iterator_test=AugmentedBatchIterator(
batch_size=128,
crop_size=4,
testing=True,
startx=startx,
starty=starty,
rotate=rotate
)
y_pred_valid[:, count] = net.predict_proba(X_valid)[:, 1]
count += 1
print("Iteration: {} / 64".format(count))
combine = bmc.BMC()
combine.fit(y_pred_valid, y_valid)
print("Validation set done.")
X_test = np.load("../data/sdss_test_images.npy")
y_test = np.load("../data/sdss_test_labels.npy")
for i in range(5):
X_test[:, i, :, :] = renormalize(X_test[:, i, :, :])
y_test = renormalize(y_test).astype(np.int32)
y_pred_test = np.zeros((len(y_test), 64))
count = 0
for startx in range(4):
for starty in range(4):
for rotate in range(4):
net.batch_iterator_test=AugmentedBatchIterator(
batch_size=128,
crop_size=4,
testing=True,
startx=startx,
starty=starty,
rotate=rotate
)
y_pred_test[:, count] = net.predict_proba(X_test)[:, 1]
count += 1
print("Iteration: {} / 64".format(count))
y_pred = combine.predict_proba(y_pred_test)
np.save("sdss_convnet_pred.npy", y_pred)
print("Testing set done.")