-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patheval.py
156 lines (130 loc) · 5.73 KB
/
eval.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
import argparse
import time
import numpy as np
import paddle.fluid as fluid
from utils import utils
from tqdm import tqdm
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--use_gpu", type=bool, default=True)
parser.add_argument("--img_size", type=int, default=224)
return parser.parse_args()
def evaluate_quant(data_list, model_path):
# 加载模型
def create_predictor(args):
place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace()
exe = fluid.Executor(place)
[program, feed_names, fetch_lists] = fluid.io.load_inference_model(dirname=model_path,
executor=exe,
model_filename='__model__',
params_filename='__params__')
compiled_program = fluid.compiler.CompiledProgram(program)
return exe, compiled_program, feed_names, fetch_lists
# 获取预处理op
def create_operators(args):
img_mean = [0.485, 0.456, 0.406]
img_std = [0.229, 0.224, 0.225]
img_scale = 1.0 / 255.0
decode_op = utils.DecodeImage()
resize_op = utils.ResizeImage(resize_short=256)
crop_op = utils.CropImage(size=(args.img_size, args.img_size))
normalize_op = utils.NormalizeImage(scale=img_scale, mean=img_mean, std=img_std)
totensor_op = utils.ToTensor()
return [decode_op, resize_op, crop_op, normalize_op, totensor_op]
# 执行预处理
def preprocess(fname, ops):
data = open(fname, 'rb').read()
for op in ops:
data = op(data)
return data
# 提取预测结果
def postprocess(outputs, topk=5):
output = outputs[0]
prob = np.array(output).flatten()
index = prob.argsort(axis=0)[-topk:][::-1].astype('int32')
return zip(index, prob[index])
args = parse_args()
operators = create_operators(args)
exe, program, feed_names, fetch_lists = create_predictor(args)
# 开始预测评估
with open(data_list, 'r', encoding='utf-8') as f:
lines = f.readlines()
results = []
print('start test %s model accuracy rate...' % model_path)
start = time.time()
for line in tqdm(lines):
path, id = line.replace('\n', '').split(' ')
data = preprocess(path, operators)
data = np.expand_dims(data, axis=0)
outputs = exe.run(program,
feed={feed_names[0]: data},
fetch_list=fetch_lists,
return_numpy=False)
lab, porb = postprocess(outputs).__next__()
if lab == int(id):
results.append(1)
end = time.time()
t = int(round((end - start) * 1000)) / len(lines)
print("准确率:%0.5f, 平均预测时间为:%d" % (sum(results) / len(lines), t))
print('=' * 70)
def evaluate_infer(data_list, model_path):
# 加载模型
def create_predictor(args):
place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace()
exe = fluid.Executor(place)
[program, feed_names, fetch_lists] = fluid.io.load_inference_model(dirname=model_path,
executor=exe,
model_filename='__model__',
params_filename='__params__')
compiled_program = fluid.compiler.CompiledProgram(program)
return exe, compiled_program, feed_names, fetch_lists
# 获取预处理op
def create_operators(args):
img_mean = [0.485, 0.456, 0.406]
img_std = [0.229, 0.224, 0.225]
img_scale = 1.0 / 255.0
decode_op = utils.DecodeImage()
resize_op = utils.ResizeImage(resize_short=256)
crop_op = utils.CropImage(size=(args.img_size, args.img_size))
normalize_op = utils.NormalizeImage(scale=img_scale, mean=img_mean, std=img_std)
totensor_op = utils.ToTensor()
return [decode_op, resize_op, crop_op, normalize_op, totensor_op]
# 执行预处理
def preprocess(fname, ops):
data = open(fname, 'rb').read()
for op in ops:
data = op(data)
return data
# 提取预测结果
def postprocess(outputs, topk=5):
output = outputs[0]
prob = np.array(output).flatten()
index = prob.argsort(axis=0)[-topk:][::-1].astype('int32')
return zip(index, prob[index])
args = parse_args()
operators = create_operators(args)
exe, program, feed_names, fetch_lists = create_predictor(args)
# 开始预测评估
with open(data_list, 'r', encoding='utf-8') as f:
lines = f.readlines()
results = []
print('start test %s model accuracy rate...' % model_path)
start = time.time()
for line in tqdm(lines):
path, id = line.replace('\n', '').split(' ')
data = preprocess(path, operators)
data = np.expand_dims(data, axis=0)
outputs = exe.run(program,
feed={feed_names[0]: data},
fetch_list=fetch_lists,
return_numpy=False)
lab, porb = postprocess(outputs).__next__()
if lab == int(id):
results.append(1)
end = time.time()
t = int(round((end - start) * 1000)) / len(lines)
print("准确率:%0.5f, 平均预测时间为:%dms" % (sum(results) / len(lines), t))
print('=' * 70)
if __name__ == '__main__':
evaluate_quant('dataset/test_list.txt', 'output/quant_inference_model')
evaluate_infer('dataset/test_list.txt', 'output/inference_model')