RK3588推理RetinaFace出现问题
在RK3588上测试RockChip提供的RetinaFace模型时,出现下面的问题
$ python RetinaFace_pic.py
done
--> Init runtime environment
I RKNN: [02:27:16.501] RKNN Runtime Information: librknnrt version: 1.5.2 (c6b7b351a@2023-08-23T15:28:22)
I RKNN: [02:27:16.501] RKNN Driver Information: version: 0.9.2
I RKNN: [02:27:16.501] RKNN Model Information: version: 6, toolkit version: 2.0.0b0+9bab5682(compiler version: 2.0.0b0 (35a6907d79@2024-03-24T02:34:11)), target: RKNPU v2, target platform: rk3588, framework name: ONNX, framework layout: NCHW, model inference type: static_shape
W RKNN: [02:27:16.501] RKNN Model version: 2.0.0 not match with rknn runtime version: 1.5.2
W RKNN: [02:27:16.511] query RKNN_QUERY_INPUT_DYNAMIC_RANGE error, rknn model is static shape type, please export rknn with dynamic_shapes
W Query dynamic range failed. Ret code: RKNN_ERR_MODEL_INVALID. (If it is a static shape RKNN model, please ignore the above warning message.)
done
--> Running model
E Catch exception when setting inputs.
E Traceback (most recent call last):
File "/home/pi/archiconda3/envs/py38/lib/python3.8/site-packages/rknnlite/api/rknn_lite.py", line 201, in inference
self.rknn_runtime.set_inputs(inputs, data_type, data_format, inputs_pass_through=inputs_pass_through)
File "rknnlite/api/rknn_runtime.py", line 1082, in rknnlite.api.rknn_runtime.RKNNRuntime.set_inputs
Exception: The input[0] need 4dims input, but 3dims input buffer feed.
Traceback (most recent call last):
File "RetinaFace_pic.py", line 175, in <module>
loc, conf, landmarks = outputs
TypeError: cannot unpack non-iterable NoneType object
从报错信息上看,感觉像维度问题,并且打印出来的outputs为None
观察RetinaFace的RKNN模型的输入
发现输入的维度是四维,而我的输入图片未做任何的处理,仅为三维
我在推理的代码前添加下面的代码,使infer_img
原本的形状(H, W, C)
,变为(1, H, W, C)
后再进行推理 :
infer_img = np.expand_dims(infer_img, axis=0)
np.expand_dims
函数可以增加数组的维度。这通常用于将形状为(H, W, C)
的RGB图像的3个颜色通道数据增加一个维度,使其形状变为(1, H, W, C)
,其中H
代表高度,W
代表宽度,C
代表通道数。
infer_img = np.expand_dims(infer_img, axis=0)
的作用是将infer_img
数组增加一个维度,使其成为四维数组。
axis=0
:指定在数组的第0维位置增加维度。
在查找资料的时候也发现了 github上的issue有人也出现了这个问题,使用了相同的方法
https://github.com/airockchip/rknn_model_zoo/issues/105