-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathread_model_and_test.py
179 lines (162 loc) · 7.32 KB
/
read_model_and_test.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
import tensorflow as tf
from file_utils import read_file_and_to_numpy
# 加载模型文件,对测试数据进行测试
import configparser
import os
import pandas as pd
from data_process import generate_vector,write_vector_to_file
# from file_utils512 import read_file_and_to_numpy_val
# from batch_read256 import read_csv_,TFRecordReader
from offer_data import read_data_return
file = './config/parameter_256_3_5.ini'
# import matplotlib
# matplotlib.use('Agg')
# import matplotlib.pyplot as plt
import numpy as np
import time
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
# 读取配置文件,拿到参数
def read_config(file):
cf = configparser.ConfigParser()
cf.read(file)
learning_rate = cf.getfloat('model','learning_rate')
training_steps = cf.getint('model','train_steps')
batch_size = cf.getint('model','batch_size')
display_step = cf.getint('model','display_step')
test_steps = cf.getint('model','test_steps')
num_input = cf.getint('model','num_input')
num_hidden = cf.getint('model','num_hidden')
num_classes = cf.getint('model','num_classes')
timesteps = cf.getint('model','timesteps')
epoch_num = cf.getint('model','epoch_num')
Is_Vali = cf.getint('model','Is_Vali')
train_file = cf.get('file','train_file')
test_file = cf.get('file','test_file')
val_file = cf.get('file','val_file')
model_file = cf.get('file','model_file')
test_file_path = cf.get('file','data_file_path')
plot_test_step = cf.getint('model','plot_test_steps')
return learning_rate,training_steps,batch_size,display_step,test_steps,num_input,\
num_hidden,num_classes,timesteps,epoch_num,Is_Vali,train_file,test_file,val_file,\
model_file,test_file_path,plot_test_step
def read_model(filename,test_file,num_input,timesteps,batch_size,test_steps,test_file_path):
# filename 为模型文件保存路径
with tf.Session() as sess:
model_file = filename+"model.meta"
model_saver = tf.train.import_meta_graph(model_file)
filename = tf.train.latest_checkpoint(filename)
model_saver.restore(sess,filename)
# 拿到损失函数和准确率指标
loss = tf.get_collection('loss')[0]
accuracy = tf.get_collection('accuracy')[0]
prediction = tf.get_collection('prediction')[0]
tf.summary.scalar('test_loss',loss)
tf.summary.scalar('test_acc',accuracy)
meraged = tf.summary.merge_all()
write = tf.summary.FileWriter('./tensorboard/',sess.graph)
# 加载placeholder
graph = tf.get_default_graph()
X = graph.get_operation_by_name('X').outputs[0]
Y = graph.get_operation_by_name('Y').outputs[0]
# test_data,test_label,test_file_list = TFRecordReader(test_file,0)
test_data,test_label,file_list= read_data_return(False,test_file)
temp_label = test_label
test_label = tf.cast(test_label,tf.int32)
# test_label = tf.one_hot(test_label,3)
test_label = sess.run(test_label)
acc_list = []
loss_list = []
epoch_list = []
count = 0
acc_sum = 0
loss_sum = 0
# test_data = test_data[:51200]
# test_label = test_label[:51200]
# 循环测试所有的csv测试文件
start_time = time.time()
for i in range(len(test_data)):
# file = str(test_file_list[i])
file = file_list[i]
batch_data = test_data[i]
batch_label = test_label[i]
batch_data = np.reshape(batch_data,[1,256,30])
# batch_data = batch_data.reshape(len(batch_data),1,num_input)
print(str(file)+":"+"Final Test Accuracy:",\
sess.run(accuracy,feed_dict={X:batch_data,Y:batch_label}))
# print(file+":"+"Final Test Loss:",\
# sess.run(loss,feed_dict={X:batch_data,Y:batch_label}))
# pre = sess.run(tf.argmax(prediction,1),feed_dict={X:test_data})
# print(sess.run(tf.argmax(prediction,1),feed_dict={X:test_data}))
end_time = time.time()
cost_time_sum = end_time - start_time
print("测试文件的总时间为:"+str(cost_time_sum))
print("平均的测试时间:"+str(cost_time_sum/len(test_data)))
print("计算平均准确率中.................")
for i in range(len(test_data)):
# file = str(test_file_list[i])
batch_data = test_data[i]
batch_label = test_label[i]
batch_data = np.reshape(batch_data,[1,256,30])
print(file+":"+"Final Test Accuracy:",\
sess.run(accuracy,feed_dict={X:batch_data,Y:batch_label}))
print(file+":"+"Final Test Loss:",\
sess.run(loss,feed_dict={X:batch_data,Y:batch_label}))
acc_sum = acc_sum+sess.run(accuracy,feed_dict={X:batch_data,Y:batch_label})
loss_sum = loss_sum+sess.run(loss,feed_dict={X:batch_data,Y:batch_label})
print("平均准确率为:"+str(acc_sum/len(test_data)))
print("平均损失为:"+str(loss_sum/len(test_data)))
# pre_list = np.array( sess.run(tf.argmax(prediction,1),feed_dict={X:batch_data,Y:batch_label}))
# wirte_csv = open('./pre.csv','a')
# zeros_list = []
# zeros_sum = 0
# one_sum = 0
# two_sum = 0
# temp_count_0 = 0
# temp_count_1 = 0
# temp_count_2 = 0
# for i in pre_list:
# print(i)
# zeros_list.append(i)
# wirte_csv.write(str(i))
# wirte_csv.write('\n')
# for i in range(len(zeros_list)):
# if temp_label[0][i] == 0:
# zeros_sum+=1
# if temp_label[0][i] == 1:
# one_sum+=1
# if temp_label[0][i] == 2:
# two_sum+=1
# for i in range(len(zeros_list)):
# if zeros_list[i] == 0:
# if temp_label[0][i] == 0:
# temp_count_0+=1
# if zeros_list[i] == 1:
# if temp_label[0][i] == 1:
# temp_count_1+=1
# if zeros_list[i]==2:
# if temp_label[0][i] == 2:
# temp_count_2+=1
# print(temp_count_0/zeros_sum)
# print(temp_count_1/one_sum)
# print(temp_count_2/two_sum)
def read_file_produce_vector(path,i):
# 拿到文件夹下所有的csv文件
filename = "test"+str(i)+"csv"
csv_path = path+filename
# 将当前文件转换成目标向量
df = pd.read_csv(csv_path,usecols=["Label","Data"])
data = list(df.get("Data"))
label = list(df.get("Label"))
# 此处参数只要不填1,2,3即可
label_list , pri_list = generate_vector(filename,data,label,5)
temp_file = 'one_test_vector.csv'
# 将当前测试文件写入csv文件
write_vector_to_file(filename,temp_file,label_list,pri_list,0)
label,data = read_file_and_to_numpy(temp_file,0)
return label,data,filename
if __name__ == "__main__":
learning_rate,training_steps,batch_size,display_step,test_steps,\
num_input,num_hidden,num_classes,timesteps,epoch_num,Is_Vali,\
train_file,test_file,val_file,model_file,test_file_path,plot_test_step = read_config(file)
read_model(model_file,test_file,num_input,timesteps,batch_size,test_steps,test_file_path)