HarmonyOS鸿蒙Next中MindSpore Lite实现图像分类
HarmonyOS鸿蒙Next中MindSpore Lite实现图像分类 问题1:根据官方mindspore-lite-2.3.0-linux-x64.tar.gz工具将(./converter_lite --fmk=ONNX --modelFile=mobilenetv2.onnx --optimize=ascend_oriented --outputFile=mobilenetv2) mobilenetv2.onnx模型转换为mobilenetv2.ms,但我想启用硬件NPU加速,可当我在转换过程中设置–optimize=ascend_oriented会失败(其他参数均可以成功none、general、gpu_oriented)。[mobilenetv2.onnx模型是我通过torchvision.models.mobilenet_v2(weights=‘DEFAULT’)下载并导出为.onnx,输入形状为(1, 3, 224, 224)]
报错信息如下:
./converter_lite --fmk=ONNX --modelFile=…/…/…/…/mobilenetv2.onnx --outputDataFormat=NCHW --optimize=ascend_oriented --outputFile=mobilenetv2 [ERROR] LITE(3838,7faabb570f40,converter_lite):2026-01-21-18:03:33.161.620 [mindspore-lite/src/extendrt/cxx_api/dlutils.h:77] FindSoPath] Could not find target so libascend_pass_plugin.so in /home/chenben/workspace/HarmonyOS_ai/mindspore-lite-2.7.1-linux-x64/tools/converter/lib/ [ERROR] LITE(3838,7faabb570f40,converter_lite):2026-01-21-18:03:33.161.727 [mindspore-lite/tools/converter/adapter/acl/plugin/acl_pass_plugin.cc:58] GetPluginSoPath] Get real path of libascend_pass_plugin.so failed. [ERROR] LITE(3838,7faabb570f40,converter_lite):2026-01-21-18:03:33.161.738 [mindspore-lite/tools/converter/adapter/acl/plugin/acl_pass_plugin.cc:80] CreateAclPassInner] Failed to get path of libascend_pass_plugin.so [ERROR] LITE(3838,7faabb570f40,converter_lite):2026-01-21-18:03:33.161.745 [mindspore-lite/tools/converter/anf_transform.cc:485] RunConvertPass] Failed to create acl pass [ERROR] LITE(3838,7faabb570f40,converter_lite):2026-01-21-18:03:33.161.752 [mindspore-lite/tools/converter/anf_transform.cc:682] RunPass] Run convert pass failed. [ERROR] LITE(3838,7faabb570f40,converter_lite):2026-01-21-18:03:33.161.759 [mindspore-lite/tools/converter/anf_transform.cc:783] TransformFuncGraph] Proc online transform failed. [ERROR] LITE(3838,7faabb570f40,converter_lite):2026-01-21-18:03:33.161.788 [mindspore-lite/tools/converter/anf_transform.cc:894] Transform] optimizer failed. [ERROR] LITE(3838,7faabb570f40,converter_lite):2026-01-21-18:03:33.161.796 [mindspore-lite/tools/converter/converter_funcgraph.cc:646] Optimize] Transform anf graph failed. [ERROR] LITE(3838,7faabb570f40,converter_lite):2026-01-21-18:03:33.161.809 [mindspore-lite/tools/converter/converter.cc:1211] HandleGraphCommon] Optimize func graph failed: -2 NULL pointer returned. [ERROR] LITE(3838,7faabb570f40,converter_lite):2026-01-21-18:03:33.161.819 [mindspore-lite/tools/converter/converter.cc:1160] Convert] Handle graph failed: -2 NULL pointer returned. [ERROR] LITE(3838,7faabb570f40,converter_lite):2026-01-21-18:03:33.161.827 [mindspore-lite/tools/converter/converter.cc:1353] RunConverter] Convert model failed [ERROR] LITE(3838,7faabb570f40,converter_lite):2026-01-21-18:03:33.161.837 [mindspore-lite/tools/converter/cxx_api/converter.cc:361] Convert] Convert model failed, ret=NULL pointer returned.
更多关于HarmonyOS鸿蒙Next中MindSpore Lite实现图像分类的实战教程也可以访问 https://www.itying.com/category-93-b0.html
【解决方案】
开发者你好,根据您提供的报错信息来看,是因为–optimize不属于端侧可使用的参数,不需要使用–optimize参数,optimize参数是针对云侧lite的,参考使用MindSpore Lite进行模型转换的参数说明。
更多关于HarmonyOS鸿蒙Next中MindSpore Lite实现图像分类的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
谢谢您,是的,后面我发现这个问题了。但我有个疑问,MindSpore Lite转换后的模型是否支持NNRT加速推理是由什么决定?为何有的模型不支持NNRT只支持CPU?我要如何才能使得模型100%执行NNRT推理?我参考官方文档,貌似是需要源码编译转换工具,如下是官方的说明:
当指定NPU后端进行推理时,需要自定义关闭clip算子融合,模型转换工具需要通过源码编译方式获取。否则可能会发生类似报错:BuildKirinNPUModel# Create full model kernel failed。
开发者你好,是因为有的算子在NPU上目前无法支持,模型中某些算子在NPU上存在兼容性限制,会把整图或者一部分子图回退到CPU上执行,另外模型中某些算子的数据类型在NPU上也存在兼容性限制,而且模型如果是动态shape一些算子在NPU上也会回退。 要让模型100%执行NNRT推理,需要模型中避免使用NPU上存在兼容性限制的算子和数据类型。 具体支持的算子列表参考昇思大模型开源社区文档:https://www.mindspore.cn/lite/docs/zh-CN/r2.7.1/reference/operator_list_lite.html。
期待HarmonyOS能在未来带来更多创新的技术和理念。
您好,谢谢,了解了,感谢您的耐心指导
1
HarmonyOS Next中,MindSpore Lite通过加载预训练的模型文件(如.ms格式)实现图像分类。开发时需在模块级build.gradle中引入MindSpore Lite依赖。核心流程包括:初始化模型、创建张量输入、执行推理以及解析输出结果。图像数据需预处理为模型要求的格式(如归一化、调整尺寸)。该框架支持CPU/GPU/NPU异构调度,可直接调用鸿蒙NDK接口完成计算。
根据你提供的错误信息,核心问题是转换工具无法找到NPU(Ascend)相关的插件库 libascend_pass_plugin.so。这通常是因为你使用的MindSpore Lite转换工具包(x86_64 Linux版本)默认不包含或未正确配置Ascend后端的支持。
问题分析:
- 工具包兼容性:你下载的
mindspore-lite-2.3.0-linux-x64.tar.gz是通用的Linux x86_64版本。要在模型转换阶段启用--optimize=ascend_oriented优化(针对华为Ascend NPU),需要使用包含Ascend后端支持的特定版本转换工具。 - 缺失插件:错误日志明确指出在工具包的
lib/目录下找不到libascend_pass_plugin.so文件。这个插件是负责将模型算子转换、优化为能在NPU上高效运行的格式的关键组件。
解决方案: 你需要获取并部署支持Ascend NPU的MindSpore Lite转换工具。
-
获取正确的工具包:
- 访问 MindSpore Lite官方发布页面(通常与HarmonyOS Next或Ascend AI处理器相关的资源一起发布)。
- 寻找明确标注支持 “Ascend” 或 “NPU” 的Linux版本转换工具包。其命名可能类似于
mindspore-lite-{version}-linux-x64-ascend.tar.gz或包含在更大的AI工具套件中。 - 注意:确保该工具包的版本(如2.3.0)与你后续在HarmonyOS Next应用开发中使用的MindSpore Lite推理运行时版本兼容。
-
部署与验证:
- 下载正确的工具包后,解压并替换或使用其中的
converter_lite可执行文件及配套的lib/目录内容。 - 确保
libascend_pass_plugin.so存在于tools/converter/lib/或指定的库路径下。 - 重新运行你的转换命令。
- 下载正确的工具包后,解压并替换或使用其中的
补充说明:
--optimize=ascend_oriented参数的目的是在模型转换阶段就进行针对NPU硬件特性的优化(如算子选择、融合、量化等),生成一个能充分发挥NPU性能的.ms模型文件。如果缺少对应后端的插件,此优化流程无法执行。- 成功转换后,在HarmonyOS Next应用中使用此
.ms模型进行推理时,也需要确保你的应用工程中集成了支持Ascend NPU的MindSpore Lite推理库,并在代码中正确指定推理上下文(Context)以使用NPU设备(deviceType: lite::DT_ASCEND或类似配置)。
请根据上述方向,检查并获取正确的工具资源。

