-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_models.py
executable file
·218 lines (191 loc) · 8.43 KB
/
test_models.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
import numpy as np
from sklearn import cross_validation
import os
import json
import time
from sklearn import ensemble
from sklearn import metrics
from sklearn import tree
import csv
from sklearn import naive_bayes
from sklearn import svm
from sklearn import linear_model
def get_X_and_Y(input_path):
num_samples = 0
for subdir, dirs, files in os.walk(input_path):
for file_i in files:
if("features" in file_i and "test" not in file_i):
class_label = int(file_i[1])
print "class label is %d" % class_label
file_i_path = os.path.join(subdir, file_i)
print file_i_path
fd = open(file_i_path, 'r')
num_lines = 0
for line in fd:
json_obj = json.loads(line)
features = np.asarray(json_obj["features"])
if num_samples == 0:
X = features
else:
X = np.vstack((X, features))
num_samples += 1
num_lines += 1
if class_label == 0:
print "num lines"
print num_lines
Y = np.zeros((num_lines, 1))
else:
print "num lines"
print num_lines
y_curr = class_label*np.ones((num_lines, 1))
print y_curr
Y = np.vstack((Y, y_curr))
fd.close()
print "Number of samples = %d" % num_samples
print "X shape"
print "Y shape"
print X.shape
print Y.shape
return X, Y
def create_submission_file(filename_vec, y_hat, test_submission_filename):
with open(test_submission_filename, 'w') as outfd:
csv_writer = csv.writer(outfd, delimiter=',')
header = ['img','c0','c1','c2','c3','c4','c5','c6','c7','c8','c9']
csv_writer.writerow(header)
# row_template = ['','0','0','0','0','0','0','0','0','0','0']
for i in range(len(y_hat)):
# print "filename:"
filename_i = str(filename_vec[i][0])
# print filename_i
# print "label:"
label = int(y_hat[i])
# print label
row_i_arr = ['','0','0','0','0','0','0','0','0','0','0']
row_i_arr[label + 1] = "1.0"
row_i_arr[0] = filename_i
# print row_i_arr
# row_i = ','.join(row_i_arr)
csv_writer.writerow(row_i_arr)
def div_by_2(x):
return float(x)/2.0
def create_submission_file_probabilities(filename_vec, probabilities_matrix, y_hat, test_submission_filename):
with open(test_submission_filename, 'w') as outfd:
csv_writer = csv.writer(outfd, delimiter=',')
header = ['img','c0','c1','c2','c3','c4','c5','c6','c7','c8','c9']
csv_writer.writerow(header)
# row_template = ['','0','0','0','0','0','0','0','0','0','0']
for i in range(len(probabilities_matrix)):
# print "filename:"
filename_i = str(filename_vec[i][0])
# print filename_i
# print "label:"
# print label
# row_i_arr = ['','0','0','0','0','0','0','0','0','0','0']
# row_i_arr[label + 1] = "1.0"
# row_i_arr[0] = filename_i
prob_vec = probabilities_matrix[i, :]
prob_vec = prob_vec.tolist()
label = int(y_hat[i])
# Decrease multi-class loss by strengthening labeling:
# prob_vec[label] += 1.0
# prob_vec = map(div_by_2, prob_vec)
prob_vec = map(str, prob_vec)
prob_vec.insert(0, filename_i)
row_i_arr = prob_vec
# print row_i_arr
# row_i = ','.join(row_i_arr)
csv_writer.writerow(row_i_arr)
def create_kaggle_submission_for_test_features(test_features_doc, test_submission_filename, model):
fd = open(test_features_doc, 'r')
num_samples = 0
for line in fd:
json_obj = json.loads(line)
features = np.asarray(json_obj["features"])
print "processing file %s" % json_obj["name"]
if num_samples == 0:
X_test = features
filename_vec = np.empty([1,1], dtype=object)
filename_i = json_obj["name"].split('/')[-1]
filename_vec[0] = filename_i
else:
filename_vec_i = np.empty([1,1], dtype=object)
filename_i = json_obj["name"].split('/')[-1]
filename_vec_i[0] = filename_i
X_test = np.vstack((X_test, features))
filename_vec = np.vstack((filename_vec, filename_vec_i))
num_samples += 1
fd.close()
X_test_filename = '/media/sf_ubuntu_vm/statefarm_data/feature_set_1st_2nd_hog_deriv/X_test_submission.npy'
filenames_vec_test = '/media/sf_ubuntu_vm/statefarm_data/feature_set_1st_2nd_hog_deriv/filenames_test_submission.npy'
np.save(X_test_filename, X_test)
np.save(filenames_vec_test, filename_vec)
# X_test_filename = '/media/sf_ubuntu_vm/statefarm_data/feature_set_1st_2nd_hog_deriv/X_test_submission.npy'
# filenames_vec_test = '/media/sf_ubuntu_vm/statefarm_data/feature_set_1st_2nd_hog_deriv/filenames_test_submission.npy'
# X_test = np.load(X_test_filename)
# filename_vec = np.load(filenames_vec_test)
# print 'dim check:'
# print X_test.shape
# print filename_vec.shape
y_hat = model.predict(X_test)
probabilities_matrix = model.predict_proba(X_test)
# create_submission_file(filename_vec, y_hat, test_submission_filename)
create_submission_file_probabilities(filename_vec, probabilities_matrix, y_hat, test_submission_filename)
if __name__ == "__main__":
start_time = time.time()
# input_path = "/media/sf_ubuntu_vm/statefarm_data/feature_set_100_hog_bins"
input_path = "/media/sf_ubuntu_vm/statefarm_data/feature_set_1st_2nd_hog_deriv"
# X_filename = 'statefarm_X.npy'
# Y_filename = 'statefarm_Y.npy'
X_filename = 'statefarm_X_1st_2nd_hog.npy'
Y_filename = 'statefarm_Y_1st_2nd_hog.npy'
# GET X and Y from json object files:
X, Y = get_X_and_Y(input_path)
np.save(X_filename, X)
np.save(Y_filename, Y)
# GET X and Y from saved numpy arrays:
# X = np.load(X_filename)
# Y = np.load(Y_filename)
# SPLIT into train and test:
test_frac = 0.3
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, Y, test_size=test_frac, random_state=0)
print "TRAIN SET SHAPE:"
print X_train.shape
print y_train.shape
print X_train
print y_train
print "TEST SET SHAPE:"
print X_test.shape
print y_test.shape
# TRAIN AND CROSS VALIDATE MODELS:
model = ensemble.RandomForestClassifier(n_estimators=255)
# model = naive_bayes.MultinomialNB()
# model = svm.SVC()
max_tree_depth = 30
# model = ensemble.AdaBoostClassifier(base_estimator=tree.DecisionTreeClassifier(max_depth=max_tree_depth), n_estimators=600, algorithm="SAMME.R", random_state=0)
# model.fit(X_train, y_train)
# Use full train data for submission:
model.fit(X, Y)
y_hat = model.predict(X_test)
accuracy = metrics.accuracy_score(y_test, y_hat)
precision = metrics.precision_score(y_test, y_hat)
recall = metrics.recall_score(y_test, y_hat)
print model.predict_proba(X_test)
print y_test
print y_hat
print "ACCURACY = %f" % accuracy
print "PRECISION = %f" % precision
print "RECALL = %f" % recall
# Use full train data for submission:
# model = ensemble.RandomForestClassifier(n_estimators=255)
# model = ensemble.AdaBoostClassifier(base_estimator=tree.DecisionTreeClassifier(max_depth=max_tree_depth), n_estimators=255, algorithm="SAMME.R", random_state=0)
# model.fit(X, Y)
# print "CROSS VALIDATION:"
# num_folds = 5
# cross_val_arr = cross_validation.cross_val_score(model, X, Y.flatten(), cv=num_folds)
# mean_cross_val_score = sum(cross_val_arr)/float(len(cross_val_arr))
# print "MEAN %d-FOLD CROSS VALIDATION SCORE = %f" % (num_folds, mean_cross_val_score)
# OUTPUT TEST RESULTS FOR KAGGLE:
test_features_doc = '/media/sf_ubuntu_vm/statefarm_data/feature_set_1st_2nd_hog_deriv/test_features'
test_submission_filename = '/media/sf_ubuntu_vm/statefarm_data/feature_set_1st_2nd_hog_deriv/test_submission.csv'
create_kaggle_submission_for_test_features(test_features_doc, test_submission_filename, model)
print "------------------ %f minutes elapsed ------------------------" % ((time.time() - start_time)/60.0)