You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TensorFlow Lite C++构建后调用API触发访问违规问题排查

TensorFlow Lite C++ API访问违规问题排查

我在Stack Overflow发布了关于TensorFlow Lite C++ API触发访问违规的问题,但至今没有得到解答。我推测问题出在从Bazel构建结果中选择了不匹配的头文件与库文件。

我是按照YouTube教程和TensorFlow官方文档的步骤来获取头文件和库文件的:

  • 安装Python 3.9.5版本
  • 安装本地依赖包
  • 安装Bazel 3.7.2和MSYS2,执行pacman -S git patch unzip并将相关路径添加到系统环境变量
  • 确认VS 2019社区版已安装必要的C++组件(MSVC v142和Windows 10 SDK)
  • 从GitHub下载并解压TensorFlow 2.5.3版本源码
  • 在源码目录执行python .\configure.py配置Bazel构建,仅对override eigen strong inline选择Yes,其余保持默认选项
  • 在GitBash中执行bazel build -c opt //tensorflow/lite:tensorflowlite完成构建
  • 构建成功后得到bazel-bin、bazel-out等文件夹
  • 创建tensorflow/include/tensorflow/lite、tensorflow/include/core、tensorflow/include/flatbuffers目录用于存放头文件,tensorflow/lib目录存放库文件
  • 将tensorflowlite.dll、tensorflow.dll.if.lib及flatbuffers.lib复制到tensorflow/lib目录
  • 将flatbuffers头文件复制到tensorflow/include/flatbuffers目录
  • 将源码中的tensorflow/lite和core目录内容复制到对应的include目录

之后我创建了VS项目并配置了路径,编写了如下测试代码:

#include "tensorflow/lite/interpreter.h"
#include "tensorflow/lite/kernels/register.h"
#include "tensorflow/lite/model.h"
#include "tensorflow/lite/optional_debug_tools.h"

#define TFLITE_MINIMAL_CHECK(x)                                  
    if (!(x))                                                    
    {                                                            
        fprintf(stderr, "Error at %s:%d\n", __FILE__, __LINE__); 
        exit(1);                                                 
    }

int main()
{
    std::string filename = "C:/project/tflitetesting/models/classification/mobilenet_v1_1.0_224_quant.tflite";
    std::unique_ptr<tflite::FlatBufferModel> model =
        tflite::FlatBufferModel::BuildFromFile(filename.c_str());
    tflite::ops::builtin::BuiltinOpResolver resolver;
    tflite::InterpreterBuilder builder(*model, resolver);
    std::unique_ptr<tflite::Interpreter> interpreter;
    builder(&interpreter);

    TFLITE_MINIMAL_CHECK(interpreter->AllocateTensors() == kTfLiteOk);
    printf("=== Pre-invoke Interpreter State ===\n");
    tflite::PrintInterpreterState(interpreter.get());

    interpreter->SetAllowFp16PrecisionForFp32(true);
    interpreter->SetNumThreads(1);
    // Get Input Tensor Dimensions
    unsigned char* input = interpreter->typed_input_tensor<unsigned char>(0);
}

但运行时仍在interpreter.h的以下代码处触发访问违规:

const Subgraph& primary_subgraph() const {
    return *subgraphs_.front();  // Safe as subgraphs_ always has 1 entry.
}

我不想构建共享库,因为目标设备Coral Edge可以直接访问这些函数,请问我哪里操作出错了?

内容的提问来源于stack exchange,提问作者Daniel Klauser

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 11:51:32