-
Notifications
You must be signed in to change notification settings - Fork 3
/
crossval_hyper.py
369 lines (305 loc) · 12.5 KB
/
crossval_hyper.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
#Python2,3 compatible headers
from __future__ import unicode_literals,division
from builtins import int
from builtins import range
#System packages
import torch
from torch.autograd import Variable,grad
import torch.nn.functional as F
import torch.nn as nn
import torch.optim as optim
import numpy
import scipy
import scipy.misc
import math
import time
import random
import argparse
import sys
import os
import re
import copy
import importlib
from collections import namedtuple
from collections import OrderedDict
from itertools import chain
import PIL.Image
import torchvision.datasets.folder
import torchvision.transforms.functional as Ft
import torchvision.transforms as Ts
import PIL.Image as Image
import torch.utils.data.dataloader as myDataLoader
import skimage.io
import util.db as db
import util.smartparse as smartparse
import util.file
import util.session_manager as session_manager
import dataloader
import sklearn.metrics
from hyperopt import hp, tpe, fmin
# Training settings
def default_params():
params=smartparse.obj();
#Data
params.nsplits=5;
params.pct=0.8
#Model
params.arch='arch.mlpv2';
params.nh=256;
params.nlayers=3;
#Optimization
params.batch=256;
params.lr=1e-3;
params.epochs=300;
params.decay=1e-4;
#MISC
params.session_dir=None;
params.budget=10000;
return params
def create_session(params):
session=session_manager.Session(session_dir=params.session_dir); #Create session
torch.save({'params':params},session.file('params.pt'));
pmvs=vars(params);
pmvs=dict([(k,pmvs[k]) for k in pmvs if not(k=='stuff')]);
print(pmvs);
util.file.write_json(session.file('params.json'),pmvs); #Write a human-readable parameter json
session.file('model','dummy');
return session;
params = smartparse.parse()
params = smartparse.merge(params, default_params())
params.argv=sys.argv;
data=dataloader.new('data_r4v2.pt');
data.cuda();
params.stuff=data.preprocess();
session=create_session(params);
params.session=session;
#Hyperparam search config
hp_config=[];
# Architectures
#archs=['arch.mlpv2','arch.mlpv3','arch.mlpv4','arch.mlpv5','arch.mlpv6'];
archs=['arch.mlp_set_color_v2xy'];
hp_config.append(hp.choice('arch',archs));
hp_config.append(hp.qloguniform('nh',low=math.log(16),high=math.log(384),q=4));
hp_config.append(hp.qloguniform('nh2',low=math.log(16),high=math.log(384),q=4));
hp_config.append(hp.quniform('nlayers',low=1,high=10,q=1));
hp_config.append(hp.quniform('nlayers2',low=1,high=10,q=1));
hp_config.append(hp.quniform('nlayers3',low=1,high=10,q=1));
# OPT
hp_config.append(hp.qloguniform('epochs',low=math.log(5),high=math.log(200),q=1));
hp_config.append(hp.loguniform('lr',low=math.log(1e-4),high=math.log(5e-2)));
hp_config.append(hp.loguniform('decay',low=math.log(1e-6),high=math.log(1e-3)));
hp_config.append(hp.qloguniform('batch',low=math.log(16),high=math.log(64),q=1));
#Function to compute performance
def configure_pipeline(params,arch,nh,nh2,nlayers,nlayers2,nlayers3,epochs,lr,decay,batch):
params_=smartparse.obj();
params_.arch=arch;
params_.nh=int(nh);
params_.nh2=int(nh2);
params_.nlayers=int(nlayers);
params_.nlayers2=int(nlayers2);
params_.nlayers3=int(nlayers3);
params_.epochs=epochs;
params_.lr=lr;
params_.batch=batch;
params_=smartparse.merge(params_,params);
return params_;
crossval_splits=[];
for i in range(params.nsplits):
data_train,data_test=data.generate_random_crossval_split(pct=params.pct);
data_val,data_test=data_test.generate_random_crossval_split(pct=0.5);
crossval_splits.append((data_train,data_val,data_test));
best_loss_so_far=-1e10;
def run_crossval(p):
global best_loss_so_far
max_batch=16;
arch,nh,nh2,nlayers,nlayers2,nlayers3,epochs,lr,decay,batch=p;
params_=configure_pipeline(params,arch,nh,nh2,nlayers,nlayers2,nlayers3,epochs,lr,decay,batch);
arch_=importlib.import_module(params_.arch);
#Random splits N times
auc=[];
ce=[];
cepre=[];
results_by_key={};
t0=time.time();
ensemble=[];
for split_id,split in enumerate(crossval_splits):
data_train,data_val,data_test=split;
net=arch_.new(params_).cuda();
opt=optim.Adam(net.parameters(),lr=params_.lr); #params_.lr
#Train loop
best_loss=-1e10;
best_net=copy.deepcopy(net);
for iter in range(params_.epochs):
net.train();
loss_total=[];
for data_batch in data_train.batches(params_.batch,shuffle=True):
opt.zero_grad();
net.zero_grad();
data_batch.cuda();
C=data_batch['label'];
data_batch.delete_column('label');
scores_i=net(data_batch);
#loss=F.binary_cross_entropy_with_logits(scores_i,C.float());
spos=scores_i.gather(1,C.view(-1,1)).mean();
sneg=torch.exp(scores_i).mean();
loss=-(spos-sneg+1);
#print(float(loss))
l2=0;
for p in net.parameters():
l2=l2+(p**2).sum();
loss=loss+l2*params_.decay;
loss.backward();
loss_total.append(float(loss));
opt.step();
loss_total=sum(loss_total)/len(loss_total);
#Eval every epoch
#net.eval();
#scores=[];
#gt=[]
#for data_batch in data_val.batches(max_batch):
# data_batch.cuda();
#
# C=data_batch['label'];
# data_batch.delete_column('label');
# scores_i=net(data_batch);
# scores.append(scores_i.data.cpu());
# gt.append(C.data.cpu());
#scores=torch.cat(scores,dim=0);
#gt=torch.cat(gt,dim=0);
#auc_i=sklearn.metrics.roc_auc_score(gt.numpy(),scores.numpy());
#loss_i=float(F.binary_cross_entropy_with_logits(scores,gt.float()));
#if best_loss<auc_i:
# best_loss=auc_i;
# best_net=copy.deepcopy(net);
#print('train %.4f, loss %.4f, auc %.4f'%(float(loss_total),float(loss_i),float(auc_i)))
#Temperature-scaling calibration on val
#net=best_net;
net.eval();
scores=[];
gt=[];
for data_batch in data_val.batches(max_batch):
data_batch.cuda();
C=data_batch['label'];
data_batch.delete_column('label');
scores_i=net.logp(data_batch);
scores.append(scores_i.data);
gt.append(C);
scores=torch.cat(scores,dim=0);
gt=torch.cat(gt,dim=0);
T=torch.Tensor(1).fill_(0).cuda();
T.requires_grad_();
opt2=optim.Adamax([T],lr=3e-2);
for iter in range(500):
opt2.zero_grad();
loss=F.binary_cross_entropy_with_logits(scores*torch.exp(-T),gt.float());
loss.backward();
opt2.step();
#Eval
net.eval();
scores=[];
scores_pre=[];
gt=[]
for data_batch in data_test.batches(max_batch):
data_batch.cuda();
C=data_batch['label'];
data_batch.delete_column('label');
scores_i=net.logp(data_batch);
scores.append((scores_i*torch.exp(-T)).data.cpu());
scores_pre.append(scores_i.data.cpu());
gt.append(C.data.cpu());
scores=torch.cat(scores,dim=0);
scores_pre=torch.cat(scores_pre,dim=0);
gt=torch.cat(gt,dim=0);
def compute_metrics(scores,gt,keys=None):
if not keys is None:
#slicing
categories=set([k for k in keys if not k is None]);
results={};
for c in categories:
ind=[i for i,k in enumerate(keys) if k==c or k is None];
scores_c=[scores[i] for i in ind];
gt_c=[gt[i] for i in ind];
auc_c,ce_c=compute_metrics(scores_c,gt_c);
results[c]={'auc':auc_c,'ce':ce_c};
return results;
else:
#Overall
auc=float(sklearn.metrics.roc_auc_score(torch.LongTensor(gt).numpy(),torch.Tensor(scores).numpy()));
ce=float(F.binary_cross_entropy_with_logits(torch.Tensor(scores),torch.Tensor(gt)));
return auc,ce;
auc_i,ce_i=compute_metrics(scores.tolist(),gt.tolist());
_,ce_pre_i=compute_metrics(scores_pre.tolist(),gt.tolist());
results_i=compute_metrics(scores.tolist(),gt.tolist(),data_test.data['table_ann']['nclasses'])
for k in results_i:
if k in results_by_key:
results_by_key[k]['auc'].append(results_i[k]['auc'])
results_by_key[k]['ce'].append(results_i[k]['ce']);
else:
results_by_key[k]={'auc':[],'ce':[]};
#results_i=compute_metrics(scores.tolist(),gt.tolist(),data_test.data['table_ann']['arch'])
#for k in results_i:
# if k in results_by_key:
# results_by_key[k]['auc'].append(results_i[k]['auc'])
# results_by_key[k]['ce'].append(results_i[k]['ce']);
# else:
# results_by_key[k]={'auc':[],'ce':[]};
results_i=compute_metrics(scores.tolist(),gt.tolist(),data_test.data['table_ann']['trigger'])
for k in results_i:
if k in results_by_key:
results_by_key[k]['auc'].append(results_i[k]['auc'])
results_by_key[k]['ce'].append(results_i[k]['ce']);
else:
results_by_key[k]={'auc':[],'ce':[]};
try:
results_i=compute_metrics(scores.tolist(),gt.tolist(),data_test.data['table_ann']['trigger_subtype'])
except:
results_i={};
for k in results_i:
if k in results_by_key:
results_by_key[k]['auc'].append(results_i[k]['auc'])
results_by_key[k]['ce'].append(results_i[k]['ce']);
else:
results_by_key[k]={'auc':[],'ce':[]};
auc.append(auc_i);
ce.append(ce_i);
cepre.append(ce_pre_i);
session.log('Split %d, loss %.4f (%.4f), auc %.4f, time %f'%(split_id,ce_i,ce_pre_i,auc_i,time.time()-t0));
ensemble.append({'net':net.cpu().state_dict(),'params':params_,'T':float(T.data.cpu())})
auc=torch.Tensor(auc);
ce=torch.Tensor(ce);
cepre=torch.Tensor(cepre);
if float(auc.mean())>best_loss_so_far:
best_loss_so_far=float(auc.mean());
torch.save(ensemble,session.file('model.pt'))
session.log('AUC: %f + %f, CE: %f + %f, CEpre: %f + %f (%s (%d,%d,%d), epochs %d, batch %d, lr %f, decay %f)'%(auc.mean(),2*auc.std(),ce.mean(),2*ce.std(),cepre.mean(),2*cepre.std(),arch,nlayers,nlayers2,nh,epochs,batch,lr,decay));
goal=-float(auc.mean()-2*auc.std());
for k in results_by_key:
auc=torch.Tensor(results_by_key[k]['auc']);
ce=torch.Tensor(results_by_key[k]['ce']);
session.log('\t KEY %s, AUC: %f + %f, CE: %f + %f'%(k,auc.mean(),2*auc.std(),ce.mean(),2*ce.std()));
return goal;
#Get results from hyper parameter search
best=fmin(run_crossval,hp_config,algo=tpe.suggest,max_evals=params.budget)
#best=util.macro.obj(best);
params_=configure_pipeline(**best);
hyper_params_str=json.dumps(best);
session.log('Best hyperparam (%s)'%(hyper_params_str));
#Load extracted features
#fvs_0=torch.load('fvs.pt');
#fvs_1=torch.load('fvs_1.pt');
#fvs=db.union(db.Table.from_rows(fvs_0),db.Table.from_rows(fvs_1));
#fvs.add_index('model_id');
#Load labels
#label=[];
#for i in range(200):
# fname='/work/projects/trojai-example/data/trojai-round0-dataset/id-%08d/ground_truth.csv'%i;
# f=open(fname,'r');
# for line in f:
# line.rstrip('\n').rstrip('\r')
# label.append(int(line));
# break;
#
# f.close();
#fvs['label']=label;
#data=db.DB({'table_ann':fvs});
#data.save('data.pt');