板端python如何正确输入数据

1.芯片型号:XJ3

2.天工开物开发包OpenExplorer版本:docker 2.6.4

3.问题定位:板端部署

4.问题具体描述

我将自己的unet多分类模型进行了模型量化和转换,在开发机测试量化后的onnx可以得到预期结果,但是在板端用python进行推理的时候得不到预期结果,打印模型输出发现和onnx输出不同。

我使用hb_verifier进行检查,没有异常输出,之后用hrt_model_exec单独执行推理,再把dump的txt文件内容读取,输入到后处理函数,也能得到和量化模型的相同输出,由此确定可能是输入数据的方式出了问题。

这是我的部分输入代码,完整代码和数据我上传到了百度云,包括原模型,量化结果和其他必要文件[https://pan.baidu.com/s/1fpCRTsPAx7wOTOpvC8xzSg?pwd=9ib4]:


# 板端数据预处理函数, 注意输入的img是bgr通道
def preprocess_onboard(img: np.ndarray, modelh, modelw) -> np.ndarray:
  img = cv2.resize(img, (modelw, modelh))# Resize图像尺寸
  img = np.expand_dims(img, 0) # 增加一维,此时维度为1HWC
# img = np.ascontiguousarray(img) # 板端的推理是封装的C++,安全起见这里约束矩阵内存连续
return img

def get_bgr_image(imgpath: str) -> np.ndarray:
  img = cv2.imread(imgpath)
if len(img.shape) == 2:
    img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
return img

dataroot = ""
imgpath = os.path.join(dataroot, "dummy.jpg")
binpath = os.path.join(dataroot, "unet_256_gpu.bin")

img = get_bgr_image(imgpath)
models = dnn.load(binpath)

# print properties of input tensor
print_properties(models[0].inputs[0].properties)
# tensor type: BGR
# data type: uint8
# layout: NHWC
# shape: (1, 256, 256, 3)

# print properties of output tensor
print_properties(models[0].outputs[0].properties)
# tensor type: float32
# data type: float32
# layout: NCHW
# shape: (1, 4, 256, 256)

model_h, model_w = get_hw(models[0].inputs[0].properties)

datain = preprocess_onboard(img, model_h, model_w) # 1x256x256x3

outputs = models[0].forward(datain)

output_batch = outputs[0].buffer  # 获取输出的buffer

print('Debug: output_batch.shape:', output_batch.shape)
print('Debug: output_batch:', output_batch)

# 处理每张图片
for j in range(output_batch.shape[0]):
    single_output = output_batch[j]
    pred_rgb = postprocess(single_output)
    pred_bgr = cv2.cvtColor((pred_rgb * 255).astype(np.uint8), cv2.COLOR_RGB2BGR)
    cv2.imwrite(os.path.join(dataroot, f"pred_bin_b{j}.png"), pred_bgr)

板端执行工具是封装好的,我看不到具体过程,这些代码是在博客[https://blog.csdn.net/Zhaoxi_Li/article/details/127820841]的基础上改的,可能别的地方我没有注意到,请各位帮帮忙。

/app/pydev_demo/04_segment_sample 里面的代码运行结果又是稳定的了,似乎区别就是nv12的输入。

重新导出nv12模型之后用示例函数直接输入一维数据进去,得到了预期结果。

不对,看起来不是我的问题,我刚刚把 https://developer.horizon.cc/forumDetail/198686198578007569 的模型和代码放在板子上跑了一下,结果也是没有输出。这个帖子的预处理看起来也是按照输入尺寸给图片加一个维度,之后就输入了,处理过程基本是一样的。-

板子是XJ3 SOC,前天怀疑是软件包问题的时候装了3.0.0的beta image上去,和之前的系统镜像(忘记是什么版本了,文件名没区别)不一样,得到的结果也就不一样。

猜测是软件包和硬件不匹配,在之前的系统镜像上相同模型的输出就完全不一样。当时的输出也是不正常,只是没有搜到用工具检查的方法。是因为新系统底层的调用方式和hrt_model_exec的输入方式不同吗?

补充一下,晚上我也重新测试了我的代码,相同图片得到的推理结果和白天的也不一样。

我试试再去看看相关调用接口,或者烧录旧一点的系统镜像再试试。