使用 OpenVino 进行手动转换(RVC2 和 RVC3)
概述
Command Line
1pip install modelconvCommand Line
1modelconverter shell <platform>platform 表示你要为目标平台进行转换,即 rvc2 或 rvc3。这相当于从 luxonis/modelconverter-<platform>:latest 镜像启动一个 Docker 容器, 并将其作为交互式终端会话(-it)运行,同时使用 --rm 标志确保退出会话后容器自动删除:Command Line
1docker run --rm -it \
2 -v $(pwd)/shared_with_container:/app/shared_with_container/ \
3 luxonis/modelconverter-<platform>:latestCommand Line
1pip install openvino-dev==2022.3简化模型(可选)
.onnx 模型,可以运行如下简化命令:Command Line
1pip install onnxsim
2onnxsim <onnx模型路径> <简化后的onnx模型路径>编译 OpenVINO IR
.xml 文件,以及存储 模型权重和偏置的 .bin 文件。 使用 OpenVINO 模型优化器(v2022.3.0) 进行此转换,支持以下源模型格式:- ONNX
- TensorFlow
- PyTorch
- PaddlePaddle
- MXNet
- Kaldi
- Caffe
Command Line
1mo --input_model <(未简化/已简化的)源模型路径> --compress_to_fp16--compress_to_fp16 参数以在设备上获得最佳性能。 更多详情请参见此处。有关转换选项的完整列表,请查阅
mo --help。如果计 划在 Luxonis 生态系统中使用模型,建议设置标志,使模型期望未归一化的 BGR 输入。因此,如果模型期望 RGB 输入或需要归一化,请务必设置 --reverse_input_channels、--mean_values 和 --scale_values 标志。量化(仅 RVC3)
编译 BLOB
Command Line
1compile_tool -d MYRIAD -m <.xml 模型路径(确保 .bin 在同一根目录下)>.../tools/compile_tool 文件夹下。 你可以按如下方式运行:Command Line
1cd .../tools/compile_tool
2./compile_tool -d MYRIAD -m ...有关转换选项的完整列表,请查阅
compile_tool -h。高级
模型优化器
均值和缩放值
--mean_values 和 --scale_values 实现。 默认情况下,来自 Camera 节点的帧是 U8 数据类型,取值范围为 [0,255]。然而,模型通常使用归一化帧进行训练,范围在 [-1,1] 或 [0,1] 之间。 为确保推理结果准确,需要事先对帧进行归一化。虽然可以创建自定义模型在推理前进行归一化 (此处示例), 但在模型优化器步骤中使用标志将归一化直接包含在模型内部更为高效。以下是一些常见的归一化选项(假设初始输入范围为 [0,255]):- 如果要求输入值在 0 到 1 之间,使用 mean=0 和 scale=255,计算为
([0,255] - 0) / 255 = [0,1]。 - 如果要求输入值在 -1 到 1 之间,使用 mean=127.5 和 scale=127.5,计算为
([0,255] - 127.5) / 127.5 = [-1,1]。 - 如果要求输入值在 -0.5 到 0.5 之间,使用 mean=127.5 和 scale=255,计算为
([0,255] - 127.5) / 255 = [-0.5,0.5]。
模型布局
--layout 参数定义模型布局。例如:Command Line
1--layout NCHW- N - 批次大小
- C - 通道数
- H - 高度
- W - 宽度
[NeuralNetwork(0)] [warning] Input image (416x416) does not match NN (3x416)在请求输出时,你可以通过 API 在交错(Interleaved)/HWC 和平面(Planar)/CHW 布局之间切换:Python
1import depthai as dai
2pipeline = dai.Pipeline()
3cam = pipeline.create(dai.node.Camera).build()
4output = cam.requestOutput(
5 size=SIZE, type=dai.ImgFrame.Type.BGR888i # 或 BGR888p(i 表示交错,p 表示平面)
6)颜色顺序
Camera 节点 输出 BGR 格式的帧。输入帧颜色顺序与训练模型不匹配可能导致预测不准确。 为此,使用 --reverse_input_channels 标志。此外,还可以通过 API 将相机输出切换为 RGB,无需使用该标志:Python
1import depthai as dai
2pipeline = dai.Pipeline()
3cam = pipeline.create(dai.node.Camera).build()
4output = cam.requestOutput(
5 size=SIZE, type=dai.ImgFrame.Type.RGB888p
6)编译工具
输入层精度
-ip U8 将在模型的所有输入层上添加一个 U8->FP16 转换层, 这通常是期望的配置。然而,在特定场景下,例如处理非帧数据时, 直接使用 FP16 精度是必要的。在这种情况下,可以选择 -ip FP16。