YOLOV5-Face转float16的onnx模型推理报错
错误提示
onnxruntime.capi.onnxruntime_pybind11_state.Fail: [ONNXRuntimeError] : 1 : FAIL : Load model from checkpoints/yolov5s-face.onnx failed:Type Error: Type parameter (T) of Optype (Concat) bound to different types (tensor(float) and tensor(float16) in node (Concat_592).
解决方法
网上查询的很多, 没看到答案, 从报错可以看出是模型的某一层多个输入存在精度不一致
1: 首先使用模型查看工具https://netron.app/, 找到模型的报错位置, 如图所示
2: 我们可以看到错误是在不同维度解析的concat上, 然后我们需要找到模型对应的代码上
3: 在models/yolo.py代码中, 我们找到Detect类, 这个就是图中的对应的代码,我们看到对应的torch.cat()中box_xy,box_wh,landm1,landm2,landm3,landm4,landm5是float32类型, 和输入y类型不一致, 这样我们就定位了位置, 然后在cat前新增下面代码就可以解决报错问题啦
#
box_xy,box_wh,landm1,landm2,landm3,landm4,landm5 = box_xy.half(),box_wh.half(),landm1.half(),landm2.half(),landm3.half(),landm4.half(),landm5.half()