forked from keras-team/keras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
epred.py
executable file
·127 lines (116 loc) · 4.21 KB
/
epred.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
'''Trains a simple convnet on the MNIST dataset.
Gets to 99.25% test accuracy after 12 epochs
(there is still a lot of margin for parameter tuning).
16 seconds per epoch on a GRID K520 GPU.
'''
from __future__ import print_function
import os
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"]="1"
import argparse
import keras
from keras.models import Model
from keras.layers import Dense, Dropout, Concatenate, Average
from keras import backend as K
from titer import *
import numpy as np
import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
from importlib import import_module
import matplotlib.pyplot as plt
plt.switch_backend('agg')
config =tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction=0.2
set_session(tf.Session(config=config))
batch_size = 256
parser=argparse.ArgumentParser()
parser.add_argument("--rat",type=float,default=0.6,help='ratio for weak qg batch')
parser.add_argument("--end",type=float,default=1.,help='end ratio')
parser.add_argument("--epoch",type=int,default=10,help='epoch')
parser.add_argument("--net1",type=str,default="ten100grucnn",help='rch')
parser.add_argument("--net2",type=str,default="ten100grucnn",help='rch')
parser.add_argument("--rc",type=str,default='rc',help='rnn or cnn')
parser.add_argument("--pt",type=int,default=100,help='pt range pt~pt*1.1')
args=parser.parse_args()
# input image dimensions
img_rows, img_cols = 33, 33
# the data, shuffled and split between train and test sets
#(x_train, y_train), (x_test, y_test) = mnist.load_data()
#if K.image_data_format() == 'channels_first':
# x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
# x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
# input_shape = (1, img_rows, img_cols)
#else:
#x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
#x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape1= (9,33,33)
input_shape2= (20,10)
#model.compile(loss=keras.losses.categorical_crossentropy,
# optimizer=keras.optimizers.Adadelta(),
# metrics=['accuracy'])
#model=keras.models.load_model('save/fullydijetsame_10')
savename="save/ten"+str(args.pt)+str(args.net1)
epoch=eval(open(savename+"/history").readline())+1
model1=keras.models.load_model(savename+"/check_"+str(epoch))
for i in range(len(model1.layers)):
model1.layers[i].name+="_1"
savename="save/ten"+str(args.pt)+str(args.net2)
epoch=eval(open(savename+"/history").readline())+1
model2=keras.models.load_model(savename+"/check_"+str(epoch))
for i in range(len(model2.layers)):
model2.layers[i].name+="_2"
out=Average()([model1.outputs[0],model2.outputs[0]])
model=Model(inputs=[model1.input,model2.input],outputs=out,name='ensemble')
rc=""
for sha in model._feed_inputs:
if(len(sha._keras_shape)==4):
rc+="c"
if(len(sha._keras_shape)==3):
rc+="r"
tdata="sdata/dijet_{0}_{1}/dijet_{0}_{1}_test.root".format(args.pt,int(args.pt*1.1))
test=wkiter([tdata,tdata],batch_size=batch_size,end=args.end*1.,rc=rc)
gen=test.next()
from sklearn.metrics import roc_auc_score, auc,precision_recall_curve,roc_curve,average_precision_score
x=[]
y=[]
g=[]
q=[]
entries=300
batch_num=batch_size
print ("test",test.totalnum())
toten=test.totalnum()
for j in range(entries):
a,c=next(gen)
if(j>=toten):
break
b=model.predict(a,verbose=0)[:,0]
x=np.append(x,np.array(c[:,0]))
y=np.append(y,b)
for i in range(batch_num):
if(c[i][0]==1):
g.append(b[i])
else:
q.append(b[i])
plt.figure(1)
plt.hist(q,bins=50,weights=np.ones_like(q),histtype='step',alpha=0.7,label='quark')
plt.hist(g,bins=50,weights=np.ones_like(g),histtype='step',alpha=0.7,label='gluon')
plt.legend(loc="upper center")
savename="ensemble/"+args.net1+args.net2+str(args.pt)
plt.savefig(savename+"out.png")
f=open(savename+"out.dat",'w')
f.write(str(q)+"\n")
f.write(str(g))
f.close()
t_fpr,t_tpr,_=roc_curve(x,y)
t_fnr=1-t_fpr
test_auc=np.around(auc(t_fpr,t_tpr),4)
plt.figure(2)
plt.plot(t_tpr,t_fnr,alpha=0.5,label="AUC={}".format(test_auc),lw=2)
plt.legend(loc='lower left')
os.system("rm "+savename+"roc*.png")
plt.savefig(savename+"roc"+str(test_auc)+".png")
f=open(savename+"roc.dat",'w')
f.write(str(t_tpr.tolist())+"\n")
f.write(str(t_fnr.tolist()))
f.close()
#print(b,c)