OpenHarmony开发者论坛
标题: 通过C++的接口调用OH_AI_ModelPredict, 但是推理结果总是nan [打印本页]
作者: 保持微笑 时间: 2024-4-23 16:23
标题: 通过C++的接口调用OH_AI_ModelPredict, 但是推理结果总是nan
【问题描述】
问题现象和发生的背景
预计通过创建ms模型,调用推理接口得到正确推理结果,但是目前得到的结果都是nan。我看官方的demo用了mobilenetv2.ms进行推理,结果也都是nan,为什么?
相关的代码,截图,日志信息
auto inputs = OH_AI_ModelGetInputs(model);
FillInputTensors(inputs);
auto outputs =OH_AI_ModelGetOutputs(model);
// 执行推理并打印输出
auto predict_ret =OH_AI_ModelPredict(model, inputs, &outputs, nullptr, nullptr);
auto tensor = outputs.handle_list;
LOGI("- Tensor %{public}d name is:%{public}s.\n", static_cast<int>(i), OH_AI_TensorGetName(tensor));
LOGI("- Tensor %{public}d size is:%{public}d. eleNum is:%{public}d\n", static_cast<int>(i),(int)OH_AI_TensorGetDataSize(tensor), (int)OH_AI_TensorGetElementNum(tensor));
out_data = reinterpret_cast<const float*>(OH_AI_TensorGetData(tensor));
LOGI("Output data is\n");
for (int j = 0; (j <OH_AI_TensorGetElementNum(tensor)) && (j <= 20); j++) {
LOGI("datadata: %{public}f\n", out_data[j]);
}
//上面是推理的的过程,打印出的datadata都是nan,下面是FillInputTensorsintFillInputTensors(OH_AI_TensorHandleArray &inputs) {
for (size_t i = 0; i < inputs.handle_num; i++) {
LOGI("fill tensort handlenum is %{public}d\n",inputs.handle_num);// =1
FillTensorWithRandom(inputs.handle_list);
}
return OH_AI_STATUS_SUCCESS;
}voidFillTensorWithRandom(OH_AI_TensorHandle msTensor) {
auto size = OH_AI_TensorGetDataSize(msTensor);
LOGI("fill tensort size is %{public}d, image h/w/c,%{public}d,%{public}d,%{public}d\n", size, image_hw1.rows, image_hw1.cols,image_hw1.channels()); // 1280*1280 * 3
char *data = (char *)OH_AI_TensorGetMutableData(msTensor);
if (size != (size_t)(image_hw1.rows * image_hw1.cols *image_hw1.channels())) {
LOGI("Tensor size does not match image size.");
}
memcpy(data, image_hw1.data, size);
// for (size_t i = 0; i <size; i++) {
// data = (char)(rand() %256);
// }
// int cur = 0;
// for (int i = 0; i <image.rows; ++i) {
// for (int j = 0; j <image.cols; ++j) {
// cv::Vec3b pixel =image.at<cv::Vec3b>(i, j);
// for (int c = 0; c< image.channels(); ++c) {
// *data++ =static_cast<uchar>(pixel[c]);
// }
// }
// }
}
我尝试过的解决方法和结果
目的是将cv::Mat image_hw1的数据填充到msTensor中,尝试过随机填充、按像素点填充、以及memcpy填充,但是结果都是nan
我想要达到的结果
至少推理的结果不是nan,才能进一步判断是否正确
【运行环境】
硬件:RK3568
ROM版本:未知
DevEvoStudio版本:4.0release
SDK版本:10
欢迎光临 OpenHarmony开发者论坛 (https://forums.openharmony.cn/) |
Powered by Discuz! X3.5 |