HarmonyOS 鸿蒙Next mindspore lite转换模型失败 Convert failed. Ret: Common error code
HarmonyOS 鸿蒙Next mindspore lite转换模型失败 Convert failed. Ret: Common error code
环境ubuntu22.04,我确认满足文档写明的依赖:
GCC >= 7.3.0
CMake >= 3.18.3
Git >= 2.28.0
需要转换的模型是YOLO11.ONNX,请大神帮忙看看咋解决?
import { mindSporeLite } from '@kit.MindSporeLiteKit'
import Logger from '../common/utils/Logger';
// 封装工具类
export default async function modelPredict(
modelBuffer: ArrayBuffer, inputsBuffer: ArrayBuffer[]): Promise<mindSporeLite.MSTensor[]> {
let context: mindSporeLite.Context = {};
context.target = [‘cpu’];
context.cpu = {}
context.cpu.threadNum = 2;
context.cpu.threadAffinityMode = 1;
context.cpu.precisionMode = ‘enforce_fp32’;
let msLiteModel: mindSporeLite.Model = await mindSporeLite.loadModelFromBuffer(modelBuffer, context);
let modelInputs: mindSporeLite.MSTensor[] = msLiteModel.getInputs();
for (let i = 0; i < inputsBuffer.length; i++) {
let inputBuffer = inputsBuffer[i];
if (inputBuffer != null) {
modelInputs[i].setData(inputBuffer as ArrayBuffer);
}
}
Logger.info(’=========MS_LITE_LOG: MS_LITE predict start=====’);
let modelOutputs: mindSporeLite.MSTensor[] = await msLiteModel.predict(modelInputs);
return modelOutputs;
}
// 以下是主要推理代码
const startTime = new Date().getTime()
// predict
modelPredict(modelBuffer.buffer.slice(0), inputs).then(outputs => {
Logger.info(’=========MS_LITE_LOG: MS_LITE predict success=====’);
for (let i = 0; i < outputs.length; i++) {
let out = new Float32Array(outputs[i].getData());
const x_factor: number = info.size.width / 640;
const y_factor: number = info.size.height / 640;
// 矩阵转置 np.transpose(np.squeeze(out))
let row: number[][] = new Array(84)
for (let i = 0; i < 8400; i++) {
let temp: number[] = []
for (let j = 0; j < 84; j++) {
temp[j] = out[i + j * 8400]
}
row[i] = temp;
}
<span class="hljs-keyword">this</span>.maxArray = [];
<span class="hljs-keyword">this</span>.maxIndexArray = [];
<span class="hljs-keyword">this</span>.bboxes = [];
<span class="hljs-keyword">let</span> maxScore: number = <span class="hljs-number">0</span>
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">8400</span>; i++) {
<span class="hljs-keyword">const</span> classes_scores = row[i].slice(<span class="hljs-number">4</span>);
<span class="hljs-keyword">const</span> max_score: number = <span class="hljs-built_in">Math</span>.max(...classes_scores)
maxScore = max_score > maxScore ? max_score : maxScore
<span class="hljs-keyword">if</span> (max_score > <span class="hljs-keyword">this</span>.confidence_thres) {
<span class="hljs-keyword">const</span> x: number = row[i][<span class="hljs-number">0</span>];
<span class="hljs-keyword">const</span> y: number = row[i][<span class="hljs-number">1</span>];
<span class="hljs-keyword">const</span> w: number = row[i][<span class="hljs-number">2</span>];
<span class="hljs-keyword">const</span> h: number = row[i][<span class="hljs-number">3</span>];
<span class="hljs-keyword">const</span> left: number = ~~((x - w / <span class="hljs-number">2</span>) * x_factor)
<span class="hljs-keyword">const</span> top: number = ~~((y - h / <span class="hljs-number">2</span>) * y_factor)
<span class="hljs-keyword">const</span> width: number = ~~(w * x_factor)
<span class="hljs-keyword">const</span> height: number = ~~(h * y_factor)
<span class="hljs-keyword">const</span> class_id = classes_scores.findIndex(vo => vo === max_score)
<span class="hljs-keyword">this</span>.maxIndexArray.push(class_id)
<span class="hljs-keyword">this</span>.maxArray.push(max_score)
<span class="hljs-keyword">this</span>.bboxes.push([left, top, width, height])
}
}
Logger.info(<span class="hljs-string">'MAX_LOG: '</span> + maxScore);
}
<span class="hljs-keyword">this</span>.cost = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>().getTime() - startTime
Logger.info(<span class="hljs-string">'=========MS_LITE_LOG END========='</span>);
})</code></pre></div></div>
直接使用 官方pt模型也报错。
https://www.mindspore.cn/lite/docs/zh-CN/r2.4.0/converter/converter_tool.html
补充,参照这份文档下载了win11程序,设置环境变量之后,报一样的错误,这次换了yolov8n.onnx模型:
在面对HarmonyOS鸿蒙系统中的MindSpore Lite模型转换失败的问题时,首先需要确认几个关键点:
-
模型兼容性:确保你尝试转换的模型格式与MindSpore Lite支持的格式兼容。不支持的格式可能会导致转换失败。
-
版本匹配:检查MindSpore Lite的版本是否与你的HarmonyOS系统版本相匹配。版本不匹配也可能导致转换过程中出现问题。
-
转换工具与参数:确保你使用的转换工具(如Model Converter)正确无误,并且转换参数设置正确。错误的参数设置或工具使用不当均可能导致转换失败。
-
资源限制:确认设备或环境是否有足够的资源(如内存、存储空间)来完成模型转换。资源不足也可能导致转换失败。
-
错误代码解析:你提到的“Common error code”较为模糊,建议查看MindSpore Lite的官方文档或日志输出,找到具体的错误代码或描述,以便更精确地定位问题。
如果上述检查均无误,但问题依旧存在,可能是更复杂的系统或软件问题。此时,建议直接联系官网客服以获取专业的技术支持。官网地址是:https://www.itying.com/category-93-b0.html