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]的基础上改的,可能别的地方我没有注意到,请各位帮帮忙。