簡體   English   中英

使用bazel構建TensorFlow C ++調用模型時,.so文件不起作用

[英]When using bazel to build TensorFlow C++ call model, .so file doesn't work

我想使用TensorFlow C ++ API調用模型並預測答案。 在fisrt,我克隆了tensorflow回購

git clone-遞歸https://github.com/tensorflow/tensorflow

然后,我編寫如下的C ++代碼:

一個代碼是調用TensorFlow api的類,頭文件如下:

#ifndef _DEEPMODEL_H_
#define _DEEPMODEL_H_

#include <iostream>
#include <string>
#include <vector>
#include "tensorflow/core/public/session.h"
#include "tensorflow/core/protobuf/meta_graph.pb.h"
#include "tensorflow/cc/client/client_session.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/core/framework/tensor.h"

using namespace std;
using namespace tensorflow;

class DeepModel{
public:
    DeepModel(const string graph_path, const string checkpoint_path);
    virtual ~DeepModel();

    bool onInit();
    void unInit();
    vector<float> predict(vector<vector<float>>& x, string input_name, string output_name);
private:
    string graph_path;
    string checkpoint_path;
    MetaGraphDef graph_def;
    Session* my_sess;
};

#endif

之后,我編寫了一個簡單的封裝代碼。 我想編譯一個.so,並在將來不使用tensorflow源代碼的情況下使用.so。 我的封裝代碼如下:

#ifndef _MODEL_HELPER_H_
#define _MODEL_HELPER_H_

#include <vector>
#include <string>

using namespace std;

class ModelHelper{
public:
    ModelHelper(const string graph_path, const string checkpoint_path);
    virtual ~ModelHelper();

    vector<float> predict(vector<vector<float> >& x, string input_name, string output_name);
private:
    string graph_path;
    string checkpoint_path;
};

#endif

我已經編寫了代碼來測試上面的代碼,它運行良好。 然后我想使用bazel編譯.so。

我的BUILD文件如下:

load("//tensorflow:tensorflow.bzl", "tf_cc_binary")

tf_cc_binary(
    name = "my_helper.so",
    srcs = ["model_helper.cc", "model_helper.h", "deepmodel.cc", "deepmodel.h"],
    linkshared = 1,
    deps = [
        "//tensorflow/cc:cc_ops",
        "//tensorflow/cc:client_session",
        "//tensorflow/core:tensorflow"
        ],
)

然后我將model_helper.so重命名為libmodel_helper.so,並編寫cpp代碼以測試.so文件。 我要編譯代碼,命令是這樣的

g++ -std=c++11 test_so.cpp -L./ -lmy_helper -I./ -o my_helper

然后我遇到錯誤:

.//libmy_helper.so: undefined reference to `stream_executor::cuda::ScopedActivateExecutorContext::~ScopedActivateExecutorContext()'
.//libmy_helper.so: undefined reference to `stream_executor::cuda::ScopedActivateExecutorContext::ScopedActivateExecutorContext(stream_executor::StreamExecutor*)'
.//libmy_helper.so: undefined reference to `tensorflow::DeviceName<Eigen::GpuDevice>::value[abi:cxx11]'
collect2: error: ld returned 1 exit status

我真的不知道為什么 我不能單獨使用.so嗎?

您應該在您的makefile中引用libtensorflow_frameowork.so。 就像下面的代碼:

g++ -std=c++11 test_so.cpp -L./ -lmy_helper -ltensorflow_framework -I./ -o my_helper

我猜bazel在編譯我的代碼時會錯過tensorflow到.so中的某些源代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM