-
Notifications
You must be signed in to change notification settings - Fork 20
/
generate_predictions_job.py
342 lines (237 loc) · 9.47 KB
/
generate_predictions_job.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
"""
generate final predictions job
"""
from matplotlib.mlab import specgram
import numpy as np
import scipy.signal as sig
from numpy.lib import stride_tricks
import time
import cPickle as pickle
from scipy import ndimage
import kmeans
import sys
import os
sys.path.insert(0, "/mnt/storage/usr/avdnoord/sklearn/")
from sklearn import cross_validation, ensemble, metrics, svm
BASE_PATH = "/mnt/storage/usr/sedielem/whales"
# BASE_PATH = "/home/sander/projects/whales"
DATA_PATH = os.path.join(BASE_PATH, "X_train.npy")
LABEL_PATH = os.path.join(BASE_PATH, "Y_train.npy")
TEST_DATA_PATH = os.path.join(BASE_PATH, "X_test.npy")
BEST_RESULTS_PATH = os.path.join(BASE_PATH, "best_results.pkl")
RESULTS_DIR = os.path.join(BASE_PATH, "results")
PREDICTIONS_DIR = os.path.join(BASE_PATH, "predictions")
if len(sys.argv) != 2:
sys.exit("Usage: generate_predictions_job.py <index>")
index = int(sys.argv[1])
with open(BEST_RESULTS_PATH, 'r') as f:
best_results_filenames = pickle.load(f)
results_path = os.path.join(RESULTS_DIR, best_results_filenames[index])
print "Using results in %s" % results_path
pred_basename = os.path.basename(results_path).replace("results-", "predictions-").replace(".pkl", ".txt")
predictions_path = os.path.join(PREDICTIONS_DIR, pred_basename)
print "Storing predictions in %s" % predictions_path
with open(results_path, 'r') as f:
results = pickle.load(f)
settings = results['settings']
params = results['params']
P = params['P']
means = params['means']
centroids = params['centroids']
fmins = params['fmins']
fmaxs = params['fmaxs']
print "GENERATING PREDICTIONS FOR: %s" % results_path
print
print settings
print
start_time = time.time()
def tock():
elapsed = time.time() - start_time
print " running for %.2f s" % elapsed
# load data
print "Load training data"
X = np.load(DATA_PATH)
Y = np.load(LABEL_PATH)
tock()
# downsample
print "Downsample"
X_downsampled = sig.decimate(X, 2, axis=1).astype(X.dtype)
tock()
# normalise
if settings['normalise_volume']:
print "Normalise volume"
X_downsampled -= X_downsampled.mean(1).reshape(-1, 1)
X_downsampled /= X_downsampled.std(1).reshape(-1, 1)
tock()
# compute spectrograms
print "Compute spectrograms"
nfft = settings['specgram_num_components']
noverlap = nfft * (1 - 1. / settings['specgram_redundancy'])
log_scale = settings['log_scale']
dummy = specgram(X_downsampled[0], NFFT=nfft, noverlap=noverlap)[0] # to get the dimensions
X_specgram = np.zeros((X.shape[0], dummy.shape[0], dummy.shape[1]), dtype=X.dtype)
for k in xrange(X.shape[0]):
X_specgram[k] = specgram(X_downsampled[k], NFFT=nfft, noverlap=noverlap)[0]
X_specgram = np.log(1 + log_scale * X_specgram)
tock()
if 'lnorm' in settings and settings['lnorm']: # check presence first for backwards compatibility
print "Locally normalise spectrograms"
lnorm_sigma_mean = settings['lnorm_sigma_mean']
lnorm_sigma_std = settings['lnorm_sigma_std']
def local_normalise(im, sigma_mean, sigma_std):
"""
based on matlab code by Guanglei Xiong, see http://www.mathworks.com/matlabcentral/fileexchange/8303-local-normalization
"""
means = ndimage.gaussian_filter(im, sigma_mean)
im_centered = im - means
stds = np.sqrt(ndimage.gaussian_filter(im_centered**2, sigma_std))
return im_centered / stds
for k in xrange(X_specgram.shape[0]):
X_specgram[k] = local_normalise(X_specgram[k], lnorm_sigma_mean, lnorm_sigma_std)
tock()
# patch extraction
print "Patch extraction"
w, h = settings['patch_width'], settings['patch_height']
shape = X_specgram.shape
strides = X_specgram.strides
new_shape = (shape[0], shape[1] - h + 1, h, shape[2] - w + 1, w)
new_strides = (strides[0], strides[1], strides[1], strides[2], strides[2])
patches = stride_tricks.as_strided(X_specgram, shape=new_shape, strides=new_strides)
tock()
# feature extraction and summarisation
print "Feature extraction and summarisation"
threshold = settings['threshold']
PC = np.dot(P, centroids.T)
# def summarise_features(features):
# features = features.reshape(features.shape[0], -1, features.shape[3]) # merge time and frequency axes
# return np.hstack([features.mean(1), features.std(1), features.min(1), features.max(1)]) # summarize over time axis
# # return features.mean(1)
# # return features.max(1)
def summarise_features(features):
features_freq_pooled = features.max(1) # max pool over frequency
n_timesteps = features_freq_pooled.shape[1]
# quadrant pooling over time
parts = [features_freq_pooled.max(1)] # max pooling over the whole timelength
n_timeslices = 4
slice_size = n_timesteps // n_timeslices # floor
for k in xrange(n_timeslices):
features_slice = features_freq_pooled[:, k*slice_size:(k+1)*slice_size].max(1)
parts.append(features_slice)
return np.hstack(parts)
# return features_freq_pooled.max(1) # time pooling
def extract_features(batch, means, PC, threshold=None):
batch_shape = batch.shape
batch = batch.transpose(0,1,3,2,4).reshape(-1, h * w)
features = np.dot(batch - means.reshape(1, -1), PC)
if threshold is not None: # if no threshold specified, use linear features
features = np.maximum(features - threshold, 0) # thresholding
# features = features.reshape(batch_shape[0], -1, PC.shape[1]) # split examples and other axes
features = features.reshape(batch_shape[0], batch_shape[1], batch_shape[3], PC.shape[1]) # (examples, frequency bins, time, features)
# import pdb; pdb.set_trace()
features = summarise_features(features)
return features
batch_size = 100
num_examples = patches.shape[0]
num_batches = int(np.ceil(num_examples / float(batch_size)))
features = np.zeros((num_examples, 5 * PC.shape[1]), dtype=X.dtype)
for b in xrange(num_batches):
print " batch %d of %d" % (b+1, num_batches)
batch = patches[b*batch_size:(b+1)*batch_size]
current_batch_size = batch.shape[0]
batch_features = extract_features(batch, means, PC, threshold)
features[b*batch_size:b*batch_size + current_batch_size] = batch_features
tock()
# interval normalisation
print "Normalisation of features"
features -= fmins.reshape(1, -1)
features /= (fmaxs - fmins).reshape(1, -1)
tock()
print "Do some memory cleanup"
del X
del X_downsampled
del X_specgram
del patches
# classifier training
print "Classifier training"
# clf = svm.LinearSVC(C=10e-3)
# clf = ensemble.RandomForestClassifier(n_estimators=200, n_jobs=4, verbose=1)
clf = ensemble.GradientBoostingClassifier(n_estimators=50, min_samples_split=2, max_features=int(np.sqrt(1000)))
clf.fit(features, Y)
tock()
print "Further cleanup: no longer need training data"
del features
del Y
# load test data
print "Load test data"
X_test = np.load(TEST_DATA_PATH).astype('float32')
tock()
# downsample
print "Downsample (test)"
X_downsampled = sig.decimate(X_test, 2, axis=1).astype(X_test.dtype)
tock()
# normalise
if settings['normalise_volume']:
print "Normalise volume"
X_downsampled -= X_downsampled.mean(1).reshape(-1, 1)
X_downsampled /= X_downsampled.std(1).reshape(-1, 1)
tock()
# compute spectrograms
print "Compute spectrograms (test)"
nfft = settings['specgram_num_components']
noverlap = nfft * (1 - 1. / settings['specgram_redundancy'])
log_scale = settings['log_scale']
dummy = specgram(X_downsampled[0], NFFT=nfft, noverlap=noverlap)[0] # to get the dimensions
X_specgram = np.zeros((X_test.shape[0], dummy.shape[0], dummy.shape[1]), dtype=X_test.dtype)
for k in xrange(X_test.shape[0]):
X_specgram[k] = specgram(X_downsampled[k], NFFT=nfft, noverlap=noverlap)[0]
X_specgram = np.log(1 + log_scale * X_specgram)
del X_test
del X_downsampled
tock()
if 'lnorm' in settings and settings['lnorm']:
print "Locally normalise spectrograms"
lnorm_sigma_mean = settings['lnorm_sigma_mean']
lnorm_sigma_std = settings['lnorm_sigma_std']
for k in xrange(X_specgram.shape[0]):
X_specgram[k] = local_normalise(X_specgram[k], lnorm_sigma_mean, lnorm_sigma_std)
tock()
# patch extraction
print "Patch extraction (test)"
w, h = settings['patch_width'], settings['patch_height']
shape = X_specgram.shape
strides = X_specgram.strides
new_shape = (shape[0], shape[1] - h + 1, h, shape[2] - w + 1, w)
new_strides = (strides[0], strides[1], strides[1], strides[2], strides[2])
patches = stride_tricks.as_strided(X_specgram, shape=new_shape, strides=new_strides)
tock()
# feature extraction and summarisation
print "Feature extraction and summarisation (test)"
threshold = settings['threshold']
PC = np.dot(P, centroids.T)
batch_size = 100
num_examples = patches.shape[0]
num_batches = int(np.ceil(num_examples / float(batch_size)))
features = np.zeros((num_examples, 5 * PC.shape[1]), dtype=X_specgram.dtype)
for b in xrange(num_batches):
print " batch %d of %d" % (b+1, num_batches)
batch = patches[b*batch_size:(b+1)*batch_size]
current_batch_size = batch.shape[0]
batch_features = extract_features(batch, means, PC, threshold)
features[b*batch_size:b*batch_size + current_batch_size] = batch_features
tock()
# interval normalisation
print "Normalisation of features (test)"
features -= fmins.reshape(1, -1)
features /= (fmaxs - fmins).reshape(1, -1)
tock()
print "Do some memory cleanup (again)"
del X_specgram
del patches
print "Compute predictions"
# scores = clf.decision_function(features)
scores = clf.predict_proba(features)[:, 1]
print "Store predictions"
with open(predictions_path, 'w') as f:
for score in scores:
f.write(str(float(score)) + '\n')
print " stored in %s" % predictions_path