summaryrefslogtreecommitdiff
path: root/test_meek.py
blob: 49d9d52dbe4028adc5251321de085d0e79ebc8c4 (plain)
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
#!/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)