#!/usr/bin/env python # -*- coding: utf-8 -*- import keras_metrics as km from keras import backend as K import random from keras.utils import np_utils from keras.optimizers import Adamax import numpy as np import os import sys import pickle from keras.layers.core import Activation, Flatten, Dense, Dropout from keras.initializers import glorot_uniform from keras import Model from keras.models import load_model from sklearn.metrics import confusion_matrix,classification_report def load(fname): f=np.load(fname,allow_pickle=True) x_test=np.array(f["meek"]) x2_test=np.array(f["bk"]) y_test=np.array([1 for i in range(x_test.shape[0])]) y2_test=np.array([0 for i in range(x2_test.shape[0])]) print "meek数据: ", x_test.shape[0] print "非meek数据:", x2_test.shape[0] return x_test, y_test, x2_test, y2_test def action(filepath,modelpath): # Testing the DF model BATCH_SIZE = 128 # Batch size VERBOSE = 2 # Output display mode NB_CLASSES = 2 # number of outputs = number of classes print ("开始进行meek模型的测试,测试数据集路径:") print(filepath) print("模型路径:") print(h5model) K.set_image_dim_ordering("tf") # tf is tensorflow # Convert data as float32 type X_test,y_test,X2_test,y2_test=load(filepath) X_test = X_test.astype('float32') y_test = y_test.astype('float32') # we need a [Length x 1] x n shape as input to the DF CNN (Tensorflow) X_test = X_test[:, :,np.newaxis] # Convert class vectors to categorical classes matrices y_test = np_utils.to_categorical(y_test, NB_CLASSES) X2_test = X2_test.astype('float32') y2_test = y2_test.astype('float32') # we need a [Length x 1] x n shape as input to the DF CNN (Tensorflow) X2_test = X2_test[:, :,np.newaxis] # Convert class vectors to categorical classes matrices y2_test = np_utils.to_categorical(y2_test, NB_CLASSES) # Building and training model #print ("loading DF model") model=load_model(h5model,custom_objects={'binary_precision':km.binary_precision(), 'binary_recall':km.binary_recall()}) print "模型加载成功" print "开始对meek测试集进行识别" score_test = model.evaluate(X_test, y_test, verbose=VERBOSE) predictions=model.predict(X_test, batch_size=BATCH_SIZE, verbose=VERBOSE) predictions = np.argmax(predictions, axis=1) num=0 for item in list(predictions): if item == 1: num+=1 print "识别结果:识别成功的数量:",str(num) print "识别失败的数量:",str(int(X_test.shape[0])-num) print "识别准确率为:", score_test[1] print "开始对非meek测试集进行识别" score_test = model.evaluate(X2_test, y2_test, verbose=VERBOSE) predictions=model.predict(X2_test, batch_size=BATCH_SIZE, verbose=VERBOSE) predictions = np.argmax(predictions, axis=1) knum=0 for item in list(predictions): if item == 0: knum+=1 print "识别结果:识别成功的数量:",str(knum) print "识别失败的数量:",str(int(X2_test.shape[0])-knum) print "识别准确率为:", score_test[1] print "总识别准确率为:",1.0*(num+knum)/(int(X_test.shape[0])+int(X2_test.shape[0])) if __name__ == "__main__": reload(sys) sys.setdefaultencoding('utf-8') random.seed(0) os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # Use only CPU os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" # see issue #152 os.environ["CUDA_VISIBLE_DEVICES"] = "" filepath=sys.argv[1] filename=filepath.split('/')[-1] h5model=sys.argv[2] action(filepath, h5model)