【发布时间】:2021-05-13 09:36:31
【问题描述】:
我使用 Tensorflow 在 Python 中训练了一个模型并将其导出。我的愿望是使用 c++ 程序中的模型进行推理。
我关注了this guide,并成功使用 Bazel 构建了一些库。选择的 Tensorflow 版本是 v4.2.0,没有 GPU。
在tensorflow目录下生成了以下文件:
- tensorflow.dll
- tensorflow.dll.runfiles_manifest
- tf_custom_op_library_additional_deps.dll.gen.def
- tensorflow.dll-2.params
- tensorflow.lib
- tf_custom_op_library_additional_deps.dll.gen.def-0.params
- tensorflow.pdb
- tensorflow.dll.if.exp
- tensorflow.dll.if.lib
- tensorflow_filtered_def_file.def
- 包括
在 Visual Studio 2019 中,我为编译器添加了包含目录,并为链接器添加了 tensorflow.dll.if.lib 和 tensorflow.lib 作为输入。我解决了所有编译错误,并且没有收到任何链接器错误。我将 tensorflow.dll 复制到源目录。该程序不会抱怨缺少 dll。
当我运行以下代码时:
#define NOMINMAX
#include "tensorflow/core/framework/graph.pb.h"
#include "tensorflow/core/public/session.h"
#include "tensorflow/core/public/version.h"
#include <iostream>
int main() {
std::cout << tf_git_version() << std::endl;
std::unique_ptr<tensorflow::Session> session(tensorflow::NewSession({}));
return 0;
}
我得到以下输出:
unknown
C:\...\x64\Debug\TensorflowTest.exe (process 10236) exited with code -1073741819.
Press any key to close this window . . .
可以观察到这些功能没有按预期工作。第一个打印“未知”而不是版本号,第二个打印应用程序崩溃。
在调试器中运行return语句前的行后,抛出如下异常:
Exception thrown at 0x00007FFE6DF4B24B (tensorflow.dll) in TensorflowTest.exe: 0xC0000005: Access violation reading location 0x0000000000000018.
调用栈:
tensorflow.dll!google::protobuf::Map<int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::InnerMap::begin(void)
tensorflow.dll!google::protobuf::Map<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int>::clear(void)
tensorflow.dll!google::protobuf::Map<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int>::~Map<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int>(void)
tensorflow.dll!tensorflow::ConfigProto::~ConfigProto(void)
TensorflowTest.exe!tensorflow::SessionOptions::~SessionOptions()
请帮我解决这个问题。
【问题讨论】:
-
"It is not working" 对您的问题的描述非常模糊。 Edit您的问题并添加一些相关信息。
-
我不太确定是什么问题。这就是我问的原因。似乎它是链接的,但没有实现。我不是 DLL 专家。
-
你甚至不说你的实际问题是什么!有什么症状?我们应该怎么知道?我们无法控制您的屏幕。
-
对不起。问题是这些功能没有按预期工作。第一个打印“未知”而不是版本号,第二个使应用程序崩溃。
-
那么接下来:调试器观察,你的程序究竟在哪里停顿,此时的调用堆栈是什么,等等。
标签: c++ tensorflow dll