Skip to content

Commit

Permalink
add prediction
Browse files Browse the repository at this point in the history
  • Loading branch information
gkfthddk committed Dec 5, 2017
1 parent d959ada commit ec30d0a
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 43 deletions.
10 changes: 6 additions & 4 deletions YJ-Keras/pred.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

parser=argparse.ArgumentParser()
parser.add_argument("--rat",type=float,default=0.6,help='ratio for weak qg batch')
parser.add_argument("--epoch",type=int,default=10,help='epoch')
parser.add_argument("--load",type=str,default="weakdijet_0.8",help='load name')
parser.add_argument("--save",type=str,default=1,help='rch')
args=parser.parse_args()

Expand All @@ -47,8 +49,8 @@
# optimizer=keras.optimizers.Adadelta(),
# metrics=['accuracy'])
#model=keras.models.load_model('save/fullydijetsame_10')
savename=args.save
model=keras.models.load_model(savename)
savename="save/"+args.load
model=keras.models.load_model(savename+"/_"+str(args.epoch))

#train=wkiter(["root/cutb/q"+str(int(args.rat*100))+"img.root","root/cutb/g"+str(int(args.rat*100))+"img.root"],batch_size=batch_size,end=1,istrain=1,friend=0)
test=wkiter("root",friend=20,begin=5./7.,end=1.,batch_size=batch_size)
Expand All @@ -74,13 +76,13 @@
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")
plt.savefig(savename+"like.png")
plt.savefig(savename+"/like"+str(args.epoch)+".png")
t_fpr,t_tpr,_=roc_curve(x,y)
t_fnr=1-t_fpr
train_auc=np.around(auc(t_fpr,t_tpr),4)
plt.figure(2)
plt.plot(t_tpr,t_fnr,alpha=0.5,label="AUC={}".format(train_auc),lw=2)
plt.legend(loc='lower left')
plt.savefig(savename+"roc.png")
plt.savefig(savename+"/roc"+str(args.epoch)+".png")
#print(b,c)

83 changes: 83 additions & 0 deletions YJ-Keras/symbols/asvgg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
from keras.models import Sequential
from keras.layers.core import Flatten, Dense, Dropout
from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D
from keras.optimizers import SGD
import cv2, numpy as np

def get_symbol(input_shape,num_classes,weights_path=None):
model = Sequential()
model.add(ZeroPadding2D((1,1),input_shape=input_shape))
model.add(Convolution2D(64, 3, 1, activation='relu'))
model.add(Convolution2D(64, 1, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(64, 3, 1, activation='relu'))
model.add(Convolution2D(64, 1, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))

model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, 3, 1, activation='relu'))
model.add(Convolution2D(128, 1, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, 3, 1, activation='relu'))
model.add(Convolution2D(128, 1, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))

model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 1, activation='relu'))
model.add(Convolution2D(256, 1, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 1, activation='relu'))
model.add(Convolution2D(256, 1, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 1, activation='relu'))
model.add(Convolution2D(256, 1, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
"""
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 1, activation='relu'))
model.add(Convolution2D(512, 1, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 1, activation='relu'))
model.add(Convolution2D(512, 1, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 1, activation='relu'))
model.add(Convolution2D(512, 1, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 1, activation='relu'))
model.add(Convolution2D(512, 1, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 1, activation='relu'))
model.add(Convolution2D(512, 1, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 1, activation='relu'))
model.add(Convolution2D(512, 1, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
"""
model.add(Flatten())
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))

if weights_path:
model.load_weights(weights_path)

return model

if __name__ == "__main__":
im = cv2.resize(cv2.imread('cat.jpg'), (224, 224)).astype(np.float32)
im[:,:,0] -= 103.939
im[:,:,1] -= 116.779
im[:,:,2] -= 123.68
im = im.transpose((2,0,1))
im = np.expand_dims(im, axis=0)

# Test pretrained model
model = VGG_16('vgg16_weights.h5')
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(optimizer=sgd, loss='categorical_crossentropy')
out = model.predict(im)
print np.argmax(out)
52 changes: 13 additions & 39 deletions YJ-Keras/weak.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
from importlib import import_module
import os
config =tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction=0.4
set_session(tf.Session(config=config))
Expand All @@ -27,6 +28,7 @@

parser=argparse.ArgumentParser()
parser.add_argument("--rat",type=float,default=0.6,help='ratio for weak qg batch')
parser.add_argument("--save",type=str,default="weakdijet_",help='save name')
args=parser.parse_args()

# input image dimensions
Expand All @@ -44,45 +46,27 @@
#x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (3,img_rows, img_cols)

#x_train = x_train.astype('float32')
#x_test = x_test.astype('float32')
#x_train /= 255
#x_test /= 255
#print('x_train shape:', x_train.shape)
#print(x_train.shape[0], 'train samples')
#print(x_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
#y_train = keras.utils.to_categorical(y_train, num_classes)
#y_test = keras.utils.to_categorical(y_test, num_classes)

"""model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))"""

net=import_module('symbols.'+"vgg")
net=import_module('symbols.'+"asvgg")
model=net.get_symbol(input_shape,num_classes)
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
optimizer=keras.optimizers.SGD(),
metrics=['accuracy'])

#ab=int(len(x_train)/10)

train=wkiter(["root/cutb/q"+str(int(args.rat*100))+"img.root","root/cutb/g"+str(int(args.rat*100))+"img.root"],batch_size=batch_size,end=1,istrain=1,friend=0)
test=wkiter("root",friend=20,begin=5./7.,end=1.,batch_size=batch_size)
train=wkiter(["root/cutb/q"+str(int(args.rat*100))+"img.root","root/cutb/g"+str(int(args.rat*100))+"img.root"],batch_size=batch_size,end=0.01,istrain=1,friend=0)
test=wkiter("root",friend=20,begin=5./7.,end=5.1/7.,batch_size=batch_size)
savename='save/'+str(args.save)+str(args.rat)
os.system("mkdir "+savename)
print ("train",train.totalnum(),"eval",test.totalnum())
#logger=keras.callbacks.CSVLogger(savename+'/log.log',append=True)
logger=keras.callbacks.TensorBoard(log_dir=savename+'/logs',histogram_freq=0, write_graph=True , write_images=True, batch_size=batch_size)

for i in range(epochs):
print("epoch",i)
checkpoint=keras.callbacks.ModelCheckpoint(filepath='save/weakdijet_'+str(args.rat)+'_'+str(i),monitor='val_loss',verbose=0,save_best_only=False,mode='auto')
model.fit_generator(train.next(),steps_per_epoch=train.totalnum(),validation_data=test.next(),validation_steps=test.totalnum(),epochs=1,verbose=1,callbacks=[checkpoint])
checkpoint=keras.callbacks.ModelCheckpoint(filepath=savename+'/_'+str(i),monitor='val_loss',verbose=0,save_best_only=False,mode='auto')
model.fit_generator(train.next(),steps_per_epoch=train.totalnum(),validation_data=test.next(),validation_steps=test.totalnum(),epochs=1,verbose=1,callbacks=[logger,checkpoint])
train.reset()
test.reset()
"""while True:
Expand All @@ -108,13 +92,3 @@



score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])


"""while True:
i+=1
a,b=train.next()
print(a.shape,b.shape,i)
if(train.endfile==1):break"""

0 comments on commit ec30d0a

Please sign in to comment.