1)环境:
创建一个 python3.8的虚拟环境
采用rknn_toolkit-1.7.5-cp38-cp38-linux_x86_64.whl
1) pt -> onnx
使用RK的yolov8.
在满足 ./requirements.txt 的环境要求后,执行以下语句导出模型
# 调整 ./ultralytics/cfg/default.yaml 中 model 文件路径,默认为 yolov8n.pt,若自己训练模型,请调接至对应的路径。支持检测、分割模型。
# 如填入 yolov8n.pt 导出检测模型
# 如填入 yolov8-seg.pt 导出分割模型
export PYTHONPATH=./
python ./ultralytics/engine/exporter.py
# 执行完毕后,会生成 ONNX 模型. 假如原始模型为 yolov8n.pt,则生成 yolov8n.onnx 模型。
执行之后,就会在当前目录下生成 对应的ONNX模型。我们打开这个模型看一下,就会发现outputs输出是9个。如果不用这个方式,outputs会是一个,导致后面的无法正常使用。RK的GIT上相关的问题,都没人回答,这是一个坑,被我踩了一次。
2) ONNX->rknn
进入rknn_model_zoo/example/yolov8/python
python3 convert.py best.onnx rv1126
W rknn-toolkit version: 1.7.5
--> Config model
done
--> Loading model
W Catch exception when optimize onnx model, skip it! Exception: Unresolved value references: /model.10/Constant_1_output_0,/model.10/Constant_output_0,/model.13/Constant_1_output_0,/model.13/Constant_output_0,/model.22/Constant_1_output_0,/model.22/Constant_2_output_0,/model.22/Constant_3_output_0,/model.22/Constant_4_output_0,/model.22/Constant_5_output_0,/model.22/Constant_output_0,
/usr/local/lib/python3.8/dist-packages/rknn/api/rknn.py:160: FutureWarning: Possible nested set at position 7
ret = self.rknn_base.load_onnx(model, inputs, input_size_list, outputs)
--> Building model
W found outlier value, this may affect quantization accuracy
const name abs_mean abs_std outlier value
Conv_/model.0/conv/Conv_226:weight 2.32 2.26 14.828
W:tensorflow:From /usr/local/lib/python3.8/dist-packages/tensorflow/python/framework/func_graph.py:981: py_func (from tensorflow.python.ops.script_ops) is deprecated and will be removed in a future version.
Instructions for updating:
tf.py_func is deprecated in TF V2. Instead, there are two
options available in V2.
- tf.py_function takes a python function which manipulates tf eager
tensors instead of numpy arrays. It's easy to convert a tf eager tensor to
an ndarray (just call tensor.numpy()) but having access to eager tensors
means `tf.py_function`s can use accelerators such as GPUs as well as
being differentiable using a gradient tape.
- tf.numpy_function maintains the semantics of the deprecated tf.py_func
(it is not differentiable, and manipulates numpy arrays). It drops the
stateful argument making all functions stateful.
W:tensorflow:From /usr/local/lib/python3.8/dist-packages/tensorflow/python/ops/resource_variable_ops.py:1659: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.
Instructions for updating:
If using Keras pass *_constraint arguments to layers.
done
--> Export rknn model
done
打开生成的rknn看一下
3)进行在线预编译:
在线预编译模型需要板子接入网线或者跟电脑连接。用的是adb connect指令直接连接ip。
#执行指令
adb connect 192.168.2.17
#得到结果
already connected to 192.168.2.17:5555
#连接成功
cd到toolkit的examples->common_function_demos->export_rknn_precompile_model文件夹下,将刚刚得到的best.rknn模型拷贝进去。
执行如下指令:
python export_rknn_precompile_model.py best.rknn yolov8_best_pre.rknn rv1126
运行结束后得到yolov8_best_pre.rknn。如果不使用预编译,运行速度会非常慢。