推理代码如下所示
from rknn.api import RKNN
import cv2
import numpy as np
def show_outputs(output):
output_sorted = sorted(output, reverse=True)
top5_str = '\n-----TOP 5-----\n'
for i in range(5):
value = output_sorted[i]
index = np.where(output == value)
for j in range(len(index)):
if (i + j) >= 5:
break
if value > 0:
topi = '{}: {}\n'.format(index[j], value)
else:
topi = '-1: 0.0\n'
top5_str += topi
print(top5_str)
def show_perfs(perfs):
perfs = 'perfs: {}\n'.format(perfs)
print(perfs)
def softmax(x):
return np.exp(x)/sum(np.exp(x))
if __name__ == '__main__':
rknn = RKNN(verbose=True)
rknn.config(
mean_values=[[123.675, 116.28, 103.53]], # mean_values表示预处理要减去的均值化参数
std_values=[[58.395, 58.395, 58.395]], # std_values表示预处理要除的标准化参数
target_platform="rk3588", # target_platform表示RKNN模型的运行平台
)
rknn.load_pytorch(
model="./resnet18.pt", # model表示加载模型的地址
input_size_list=[[1, 3, 224, 224]], # input_size_list表示模型输入节点对应图片的尺寸和通道数
)
rknn.build(
do_quantization=True, # do_quantization表示是否对RKNN模型进行量化操作
dataset="dataset.txt", # dataset表示要量化的图片
)
rknn.export_rknn(
export_path="resnet18.rknn", # export_path表示导出的RKNN模型路径
)
# 调用init_runtime接口初始化运行环境
rknn.init_runtime(
target="rk3588", # 表示RKNN模型运行平台
# target_sub_class=None,
device_id=None,
perf_debug=False, # 设置为True,可以打开性能评估的debugger模式
eval_mem=False, # 设置为True,表示使能内存评估模式
async_mode=False, # 表示是否使能异步模式
core_mask=RKNN.NPU_CORE_AUTO, # 可以设置运行时NPU的核心
)
# 使用opencv获取要推理的图片数据
img = cv2.imread(
filename="./space_shuttle_224.jpg", # 表示要读取的图片文件
flags=None,
)
print(img.shape)
# 进行数据格式转化
cv2.cvtColor(
src=img, # 要转换的数据
code=cv2.COLOR_BGR2RGB, # 转换码
)
# 进行推理测试
outputs = rknn.inference(
inputs=[img],
data_format="nhwc",
)
show_outputs(softmax(np.array(outputs[0][0])))
rknn.release()
此时进行连板推理,报错结果如下所示
D NPUTransfer: ERROR: socket write fd = 3, n = -1: Broken pipe
D NPUTransfer: Transfer client closed, fd = 3
E RKNNAPI: rknn_init, send(MsgLoad) fail, -9(ERROR_PIPE) != 12030409!
E init_runtime: The rknn_server on the concected device is abnormal, please start the rknn_server on the device according to:
https://github.com/rockchip-linux/rknpu2/blob/master/rknn_server_proxy.md
W init_runtime: ===================== WARN(1) =====================
E rknn-toolkit2 version: 1.4.0-22dcfef4
E init_runtime: Catch exception when init runtime!
E init_runtime: Traceback (most recent call last):
E init_runtime: File “rknn/api/rknn_base.py”, line 1985, in rknn.api.rknn_base.RKNNBase.init_runtime
E init_runtime: File “rknn/api/rknn_runtime.py”, line 362, in rknn.api.rknn_runtime.RKNNRuntime.build_graph
E init_runtime: File “rknn/api/rknn_log.py”, line 113, in rknn.api.rknn_log.RKNNLog.e
E init_runtime: ValueError: The rknn_server on the concected device is abnormal, please start the rknn_server on the device according to:
E init_runtime: https://github.com/rockchip-linux/rknpu2/blob/master/rknn_server_proxy.md
W inference: We need do some initial work, it will increase call time of inference for the first time.
I NPUTransfer: Starting NPU Transfer Client, Transfer version 2.1.0 (b5861e7@2020-11-23T11:50:36)
D NPUTransfer: Transfer spec = local:transfer_proxy
E NPUTransfer: Cannot connect to proxy: Connection refused
E RKNNAPI: rknn_init, server connect fail! ret = -4(ERROR_NO_DEVICE)!
E inference: The rknn_server on the concected device is abnormal, please start the rknn_server on the device according to:
此时我们注意观察重点
E RKNNAPI: rknn_init, send(MsgLoad) fail, -9(ERROR_PIPE) != 12030409!
ERROR_PIPE指的是错误的管道,而此时我们使用RK3588进行连板推理的数据线为USB3.0的数据线,此时我们更换为USB2.0的数据线再进行连板推理,问题解决。