OpenHarmony开发者论坛
标题:
为什么通过N-API 方式调用OpenHarmony 4.0集成的MindSpore,推理的结果总是nan?
[打印本页]
作者:
保持微笑
时间:
2024-4-23 16:10
标题:
为什么通过N-API 方式调用OpenHarmony 4.0集成的MindSpore,推理的结果总是nan?
[md]```
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前20个总是nan,以下是填充input的过程,cv::Mat image_hw1是输入的图片,能正常显示。
int FillInputTensors(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;
}
void FillTensorWithRandom(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]);
// }
// }
// }
}
三种填充方式的推理结果都是nan,请问是哪里有问题? 还有size的大小总是比模型的输入大四倍,不清楚原因。
```
[/md]
欢迎光临 OpenHarmony开发者论坛 (https://forums.openharmony.cn/)
Powered by Discuz! X3.5