• Lv0
    粉丝0

积分1 / 贡献0

提问2答案被采纳0文章0

为什么通过N-API 方式调用OpenHarmony 4.0集成的MindSpore,推理的结果总是nan?

保持微笑 显示全部楼层 发表于 2024-4-23 16:10:35

    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[i];
    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[i]);
    }
    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[i] = (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的大小总是比模型的输入大四倍,不清楚原因。

您尚未登录,无法参与评论,登录后可以:
参与开源共建问题交流
认同或收藏高质量问答
获取积分成为开源共建先驱

Copyright   ©2023  OpenHarmony开发者论坛  京ICP备2020036654号-3 |技术支持 Discuz!

返回顶部