|
ONNX的分类模型转换成rknn模型之后进行推理时与原模型推理得到的结果不一致,想问下是为什么,下边是模型转换的代码和推理的代码
模型转换:
# -*- coding: utf-8 -*-
'''
convert the onnx model to rknn model
'''
from rknn.api import RKNN
INPUT_SIZE = 112
if __name__ == '__main__':
# 创建RKNN执行对象
rknn = RKNN(verbose=True, verbose_file='./log/convert_smoke.log')
# 配置模型输入,用于NPU对数据输入的预处理
# channel_mean_value='0 0 0 255',那么模型推理时,将会对RGB数据做如下转换
# (R - 0)/255, (G - 0)/255, (B - 0)/255。推理时,RKNN模型会自动做均值和归一化处理
# reorder_channel=’0 1 2’用于指定是否调整图像通道顺序,设置成0 1 2即按输入的图像通道顺序不做调整
# reorder_channel=’2 1 0’表示交换0和2通道,如果输入是RGB,将会被调整为BGR。如果是BGR将会被调整为RGB
# 图像通道顺序不做调整
rknn.config(batch_size=256,
channel_mean_value='0 0 0 1',
reorder_channel='0 1 2'
)
# 加载ONNX模型
# tf_pb='digital_gesture.pb'指定待转换的TensorFlow模型
# inputs指定模型中的输入节点
# outputs指定模型中输出节点
# input_size_list指定模型输入的大小
print('--> Loading model')
rknn.load_onnx('./model/resnet-nobn-v4.onnx')
print('done')
# 创建解析pb模型
# do_quantization=False指定不进行量化
# 量化会减小模型的体积和提升运算速度,但是会有精度的丢失
print('--> Building model')
rknn.build(do_quantization=False)
print('done')
# 导出保存rknn模型文件
rknn.export_rknn('./model/resnet_classify_batchsize256.rknn')
# Release RKNN Context
rknn.release()
推理:
# -*- coding: utf-8 -*-
'''
inference the rknn model and get output
'''
import numpy as np
import cv2
import os
from rknn.api import RKNN
import time
# 解析模型的输出,获得概率最大的手势和对应的概率
def get_predict(probability):
data = probability[0][0]
data = data.tolist()
max_prob = max(data)
return data.index(max_prob), max_prob, data
def load_model():
# 创建RKNN对象
rknn = RKNN(verbose=False)
# 载入RKNN模型
print('-->loading model')
rknn.load_rknn('./model/resnet_classify_batchsize256.rknn')
print('loading model done')
# 初始化RKNN运行环境
print('--> Init runtime environment')
# todo:suport 'rk3399pro','rk1808'; default is NoneC
ret = rknn.init_runtime(target=None, perf_debug=False)
if ret != 0:
print('Init runtime environment failed')
exit(ret)
print('done')
return rknn
def img_preprocess(img, width, height):
'''
BGRimg--->RGBimg
:param img:
:param width:
:param height:
:return:
'''
img_BGR = cv2.resize(img, (width, height))
img = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2RGB)
img = img - 127.5
input_image = img[:, :, :, np.newaxis]
input_image = input_image.transpose([3, 2, 0, 1]).astype('float32')
return input_image
def predict(rknn, img_dir):
for file in os.listdir(img_dir):
if not file.endswith('.jpg'):
continue
img_file = os.path.join(img_dir, file)
print(img_file)
img = cv2.imread(img_file)
imput_image = img_preprocess(img, 112, 112)
begin = time.time()
outputs = rknn.inference(inputs=[imput_image]) # 运行推理,得到推理结果
end = time.time()
print('it costs %0.3fms' % ((end - begin) * 1000))
pred, prob, output_list = get_predict(outputs) # 将推理结果转化为可视信息
print('result:%s' % pred)
# print('max_val:', prob)
print('output_list', output_list)
print('---------------------------')
if __name__ == "__main__":
rknn = load_model()
predict(rknn, img_dir='/home/DATA/')
rknn.release()
想知道问题出在了哪
|
|