-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_hlf_cache.py
148 lines (95 loc) · 5.4 KB
/
test_hlf_cache.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
#!/usr/bin/env python
import os
import sys
import numpy as np
import sklearn
import neukrill_net.utils
import neukrill_net.highlevelfeatures
import neukrill_net.stacked
import time
from sklearn.externals import joblib
import sklearn.ensemble
import sklearn.feature_selection
def predict(cache_paths, out_fname, clf, settings, generate_heldout=True):
t0 = time.time()
print 'loading data'
X_train = joblib.load(cache_paths[0])
X_test = joblib.load(cache_paths[1])
X_test[np.isnan(X_test)] = 0
X_paths,y = settings.flattened_train_paths(settings.classes)
y_train = np.array(y)
n_augments = X_train.shape[0]
XX_train = X_train.reshape((X_train.shape[0]*X_train.shape[1],X_train.shape[2]))
XX_test = X_test.reshape((X_test.shape[0]*X_test.shape[1],X_test.shape[2]))
yy_train = np.tile(y_train, n_augments)
pcfilter = sklearn.feature_selection.SelectPercentile(sklearn.feature_selection.f_classif, percentile=95)
XX_train = pcfilter.fit_transform(XX_train, yy_train)
XX_test = pcfilter.transform(XX_test)
print '{}: training'.format(time.time()-t0)
clf.fit(XX_train,yy_train)
print '{}: predicting'.format(time.time()-t0)
p = clf.predict_proba(XX_test)
p = np.reshape(p, (X_test.shape[0], X_test.shape[1], p.shape[1]))
p_avg = p.mean(0)
names = [os.path.basename(path) for path in settings.image_fnames['test']]
print '{}: writing predictions to disk'.format(time.time()-t0)
neukrill_net.utils.write_predictions(out_fname, p_avg, names, settings.classes)
if not generate_heldout:
return
print '{}: generating held out predictions'.format(time.time()-t0)
li_test = neukrill_net.utils.train_test_split_bool(settings.image_fnames, 'test', train_split=0.8, classes=settings.classes)
li_nottest = np.logical_not(li_test)
X2_train = X_train[:,li_nottest,:]
X2_test = X_train[:,li_test,:]
XX_train = X2_train.reshape((X2_train.shape[0]*X2_train.shape[1],X2_train.shape[2]))
XX_test = X2_test.reshape((X2_test.shape[0]*X2_test.shape[1],X2_test.shape[2]))
yy_train = np.tile(y_train[li_nottest], n_augments)
yy_test = y_train[li_test]
XX_train = pcfilter.transform(XX_train)
XX_test = pcfilter.transform(XX_test)
print '{}: training without heldout'.format(time.time()-t0)
clf.fit(XX_train,yy_train)
print '{}: predicting on heldout'.format(time.time()-t0)
p = clf.predict_proba(XX_test)
p = np.reshape(p, (X2_test.shape[0], X2_test.shape[1], p.shape[1]))
p_avg = p.mean(0)
nll = sklearn.metrics.log_loss(yy_test, p_avg)
print 'NLL score is {}'.format(nll)
print '{}: writing heldout to disk'.format(time.time()-t0)
joblib.dump( (p_avg, yy_test), out_fname + '_heldout.pkl', )
cache_paths = [
('/disk/data1/s1145806/cached_hlf_train_data_raw_ranged.pkl' , '/disk/data1/s1145806/cached_hlf_test_data_raw_ranged.pkl' ),
('/disk/data1/s1145806/cached_hlf_train3_data_raw_ranged.pkl' , '/disk/data1/s1145806/cached_hlf_test3_data_raw_ranged.pkl' ),
('/disk/data1/s1145806/cached_hlf_train6_data_raw_ranged.pkl' , '/disk/data1/s1145806/cached_hlf_test6_data_raw_ranged.pkl' ),
('/disk/data1/s1145806/cached_hlf_train8_data_raw_ranged.pkl' , '/disk/data1/s1145806/cached_hlf_test8_data_raw_ranged.pkl' ),
('/disk/data1/s1145806/cached_hlf_train10_data_raw_ranged.pkl' , '/disk/data1/s1145806/cached_hlf_test10_data_raw_ranged.pkl' ),
('/disk/data1/s1145806/cached_hlf_train15_data_raw_ranged.pkl' , '/disk/data1/s1145806/cached_hlf_test15_data_raw_ranged.pkl' ),
('/disk/data1/s1145806/cached_hlf_train15alt_data_raw_ranged.pkl', '/disk/data1/s1145806/cached_hlf_test15alt_data_raw_ranged.pkl'),
]
cache_paths = [
('/disk/data1/s1145806/cached_hlf_train_data_raw_ranged.pkl' , '/disk/data1/s1145806/cached_hlf_test_data_raw_ranged.pkl' )
]
n_trees = 500
max_depth = 10
n_jobs = 24
print "{} trees, {} deep, (n_jobs={})".format(n_trees,max_depth,n_jobs)
settings = neukrill_net.utils.Settings('settings.json')
clf = sklearn.ensemble.RandomForestClassifier(n_estimators=n_trees, max_depth=max_depth, min_samples_leaf=3, n_jobs=n_jobs, random_state=42)
for pathpair in cache_paths:
print pathpair
out_fname = pathpair[0][:-4] + "{}trees_{}deep".format(n_trees,max_depth) + '_predictions.csv'
predict(pathpair, out_fname, clf, settings)
cache_paths = [
('/disk/data1/s1145806/cached_hlf_train15alt_data_raw_ranged.pkl', '/disk/data1/s1145806/cached_hlf_test15alt_data_raw_ranged.pkl'),
('/disk/data1/s1145806/cached_hlf_train15_data_raw_ranged.pkl' , '/disk/data1/s1145806/cached_hlf_test15_data_raw_ranged.pkl' ),
]
n_trees = 2000
max_depth = 25
n_jobs = 1
print "{} trees, {} deep, (n_jobs={})".format(n_trees,max_depth,n_jobs)
settings = neukrill_net.utils.Settings('settings.json')
clf = sklearn.ensemble.RandomForestClassifier(n_estimators=n_trees, max_depth=max_depth, min_samples_leaf=8, n_jobs=n_jobs, random_state=42)
for pathpair in cache_paths:
print pathpair
out_fname = pathpair[0][:-4] + "{}trees_{}deep".format(n_trees,max_depth) + '_predictions.csv'
predict(pathpair, out_fname, clf, settings)