-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest.py
295 lines (224 loc) · 8.98 KB
/
test.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
import string
import sys
import numpy as np
import sklearn
from datetime import datetime
import buffering
import pathfinder
import utils
from configuration import config, set_configuration
import logger
import app
import torch
import os
import cPickle
from torch.autograd import Variable
import argparse
parser = argparse.ArgumentParser(description='Evaluate dataset on trained model.')
save_dir = "../data/temp/"
parser.add_argument("config_name", type=str, help="Config name")
parser.add_argument("eval", type=str, help="test/valid/feat/train/test_tta/valid_tta")
parser.add_argument("--dump", type=int, default=0, help="Should we store the predictions in raw format")
parser.add_argument("--best", type=int, default=0, help="Should we use the best model instead of the last model")
args = parser.parse_args()
config_name = args.config_name
set_configuration('configs', config_name)
all_tta_feat = args.eval == 'all_tta_feat'
feat = args.eval == 'feat'
train = args.eval == 'train'
train_tta = args.eval == 'train_tta'
train_tta_feat = args.eval == 'train_tta_feat'
valid = args.eval == 'valid'
valid_tta = args.eval == 'valid_tta'
valid_tta_feat = args.eval == 'valid_tta_feat'
valid_tta_majority = args.eval == 'valid_tta_majority'
test = args.eval == 'test'
test_tta = args.eval == 'test_tta'
test_tta_feat = args.eval == 'test_tta_feat'
test_tta_majority = args.eval == 'test_tta_majority'
dump = args.dump
best = args.best
# metadata
metadata_dir = utils.get_dir_path('models', pathfinder.METADATA_PATH)
metadata_path = utils.find_model_metadata(metadata_dir, config_name, best=best)
metadata = utils.load_pkl(metadata_path)
expid = metadata['experiment_id']
if best:
expid += "-best"
print("logs")
# logs
logs_dir = utils.get_dir_path('logs', pathfinder.METADATA_PATH)
sys.stdout = logger.Logger(logs_dir + '/%s-test.log' % expid)
sys.stderr = sys.stdout
print("prediction path")
# predictions path
predictions_dir = utils.get_dir_path('model-predictions', pathfinder.METADATA_PATH)
outputs_path = predictions_dir + '/' + expid
if valid_tta_feat or test_tta_feat or all_tta_feat or train_tta_feat:
outputs_path += '/features'
utils.auto_make_dir(outputs_path)
if dump:
prediction_dump = os.path.join(outputs_path, expid + "_" + args.eval + "_predictions.p")
print('Build model')
model = config().build_model()
model.l_out.load_state_dict(metadata['param_values'])
model.l_out.cuda()
model.l_out.eval()
criterion = config().build_objective()
if test:
data_iterator = config().test_data_iterator
elif feat:
data_iterator = config().feat_data_iterator
def get_preds_targs(data_iterator):
print('Data')
print('n', sys.argv[2], ': %d' % data_iterator.nsamples)
validation_losses = []
preds = []
targs = []
ids = []
for n, (x_chunk, y_chunk, id_chunk) in enumerate(buffering.buffered_gen_threaded(data_iterator.generate())):
inputs, labels = Variable(torch.from_numpy(x_chunk).cuda(), volatile=True), Variable(
torch.from_numpy(y_chunk).cuda(), volatile=True)
predictions = model.l_out(inputs)
loss = criterion(predictions, labels)
validation_losses.append(loss.cpu().data.numpy()[0])
targs.append(y_chunk)
if feat:
for idx, img_id in enumerate(id_chunk):
np.savez(open(outputs_path + '/' + str(img_id) + '.npz', 'w'), features=predictions[idx])
preds.append(predictions.cpu().data.numpy())
# print id_chunk, targets, loss
if n % 50 == 0:
print(n, 'batches processed')
ids.append(id_chunk)
preds = np.concatenate(preds)
targs = np.concatenate(targs)
ids = np.stack(ids)
print('Validation loss', np.mean(validation_losses))
return preds, targs, ids
def get_preds_targs_tta(data_iterator, aggregation="mean", threshold=0.5):
print('Data')
print('n', sys.argv[2], ': %d' % data_iterator.nsamples)
# validation_losses = []
preds = []
targs = []
ids = []
for n, (x_chunk, y_chunk, id_chunk) in enumerate(buffering.buffered_gen_threaded(data_iterator.generate())):
# load chunk to GPU
# if n == 10:
# break
inputs, labels = Variable(torch.from_numpy(x_chunk).cuda(), volatile=True), Variable(
torch.from_numpy(y_chunk).cuda(), volatile=True)
predictions = model.l_out(inputs)
predictions = predictions.cpu().data.numpy()
if aggregation == "majority":
final_prediction = np.zeros((predictions.shape[1],))
for dim in range(predictions.shape[1]):
count = np.bincount(predictions[:, dim] > threshold, minlength=2)
final_prediction[dim] = 1 if count[1] >= predictions.shape[0] / 2.0 else 0
elif aggregation == "mean":
final_prediction = np.mean(predictions, axis=0)
# avg_loss = np.mean(loss, axis=0)
# validation_losses.append(avg_loss)
targs.append(y_chunk[0])
ids.append(id_chunk)
preds.append(final_prediction)
if n % 1000 == 0:
print(n, 'batches processed')
preds = np.stack(preds)
targs = np.stack(targs)
ids = np.stack(ids)
# print 'Validation loss', np.mean(validation_losses)
return preds, targs, ids
def get_preds_targs_tta_feat(data_iterator, prelabel=''):
print('Data')
print('n', sys.argv[2], ': %d' % data_iterator.nsamples)
for n, (x_chunk, y_chunk, id_chunk) in enumerate(buffering.buffered_gen_threaded(data_iterator.generate())):
# load chunk to GPU
# if n == 10:
# break
inputs, labels = Variable(torch.from_numpy(x_chunk).cuda(), volatile=True), Variable(
torch.from_numpy(y_chunk).cuda(), volatile=True)
predictions = model.l_out(inputs, feat=True)
predictions = predictions.cpu().data.numpy()
# final_prediction = np.mean(predictions.cpu().data.numpy(), axis=0)
# avg_loss = np.mean(loss, axis=0)
# validation_losses.append(avg_loss)
# print(predictions.shape)
# print(id_chunk)
for i in range(predictions.shape[0]):
file = open(os.path.join(outputs_path, prelabel + str(id_chunk) + "_" + str(i) + ".npy"), "wb")
np.save(file, predictions[i])
file.close()
if n % 1000 == 0:
print(n, 'batches processed')
if train_tta_feat:
train_it = config().tta_train_data_iterator
get_preds_targs_tta_feat(train_it)
if all_tta_feat:
all_it = config().tta_all_data_iterator
get_preds_targs_tta_feat(all_it)
if train or train_tta:
if train:
train_it = config().trainset_valid_data_iterator
preds, targs, ids = get_preds_targs(train_it)
elif train_tta:
train_it = config().tta_train_data_iterator
preds, targs, ids = get_preds_targs_tta(train_it)
if dump:
file = open(prediction_dump, "wb")
cPickle.dump([preds, targs, ids], file)
file.close()
if valid_tta_feat:
valid_it = config().tta_valid_data_iterator
get_preds_targs_tta_feat(valid_it)
if valid or valid_tta or valid_tta_majority:
if valid:
valid_it = config().valid_data_iterator
preds, targs, ids = get_preds_targs(valid_it)
elif valid_tta:
valid_it = config().tta_valid_data_iterator
preds, targs, ids = get_preds_targs_tta(valid_it)
elif valid_tta_majority:
valid_it = config().tta_valid_data_iterator
preds, targs, ids = get_preds_targs_tta(valid_it, aggregation="majority", threshold=0.53)
if dump:
file = open(prediction_dump, "wb")
cPickle.dump([preds, targs, ids], file)
file.close()
tps = [np.sum(qpreds[:, i] * targs[:, i]) for i in range(17)]
fps = [np.sum(qpreds[:, i] * (1 - targs[:, i])) for i in range(17)]
fns = [np.sum((1 - qpreds[:, i]) * targs[:, i]) for i in range(17)]
print('TP')
print(np.int32(tps))
print('FP')
print(np.int32(fps))
print('FN')
print(np.int32(fns))
print('worst classes')
print(app.get_headers())
print(4 * np.array(fps) + np.array(fns))
if test_tta_feat:
test_it = config().tta_test_data_iterator
get_preds_targs_tta_feat(test_it, prelabel='test_')
test2_it = config().tta_test2_data_iterator
get_preds_targs_tta_feat(test2_it, prelabel='file_')
if test or test_tta or test_tta_majority:
imgid2pred = {}
imgid2raw = {}
if test:
test_it = config().test_data_iterator
preds, _, ids = get_preds_targs(test_it)
elif test_tta:
test_it = config().tta_test_data_iterator
preds, _, ids = get_preds_targs_tta(test_it)
elif test_tta_majority:
test_it = config().tta_test_data_iterator
preds, _, ids = get_preds_targs_tta(test_it, aggregation="majority")
predictions_dir = utils.get_dir_path('model-predictions', pathfinder.METADATA_PATH)
outputs_path = predictions_dir + '/' + expid
utils.auto_make_dir(outputs_path)
output_pickle_file = outputs_path + '/%s-%s.pkl' % (expid, args.eval)
file = open(output_pickle_file, "wb")
cPickle.dump(imgid2raw, file)
file.close()