首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用libtensorflow-core.a构建静态库,如何使用?

如何使用libtensorflow-core.a构建静态库,如何使用?
EN

Stack Overflow用户
提问于 2019-05-22 12:31:41
回答 2查看 722关注 0票数 1

我使用TensorFlow-1.13.0-rc2Ubuntu 16系统上运行我的代码。

首先,我运行bazel build //tensorflow:libtensorflow_cc.so生成一些*.pb.h头文件。然后运行./tensorflow/contrib/makefile/build_all_linux.sh,然后获取以下文件:- tensorflow/contrib/makefile/gen/lib/libtensorflow-core.a - tensorflow/contrib/makefile/gen/protobuf/lib/libprotobuf.a - tensorflow/contrib/makefile/downloads/nsync/builds/default.linux.c++11/libnsync.a

然后,标题如下:

// RecogLetter.h

代码语言:javascript
复制
#include <string>
#include <vector>

#include <opencv2/opencv.hpp>

#include "tensorflow/core/framework/tensor_shape.pb.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/lib/strings/str_util.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/core/platform/default/logging.h"
#include "tensorflow/core/public/session.h"
#include "tensorflow/core/platform/types.h"

// using tensorflow::string;
using tensorflow::Status;
using tensorflow::Tensor;
using tensorflow::int32;
class RecogLetter
{
    public:
    RecogLetter();
    ~RecogLetter();

    public:
    int init(const std::string &model_path, const char* txtmap_path);
    int recog(cv::Mat *cv_image, char* label_index, float* score);

    private:
    int init_dictionary(const std::string& filename);

    Status LoadGraph(std::string graph_file_name,
                 std::unique_ptr<tensorflow::Session>* session);
    Status GetTopLabels(const std::vector<Tensor>& outputs, int how_many_labels,
                    Tensor* out_indices, Tensor* out_scores) ;
    Status PrintTopLabels(const std::vector<Tensor>& outputs, 
                    std::string labels_file_name, char* label_index, float* score); 
    Status CheckTopLabel(const std::vector<Tensor>& outputs, int expected,
                     bool* is_expected);

    private:
    std::unique_ptr<tensorflow::Session> session;
    std::string labels =  "";

    int32 _how_many_labels = 26;
    int32 input_channel = 1;
    int32 input_width = 32;
    int32 input_height = 32;
    int32 input_mean = 0;
    int32 input_std = 255;
    std::string input_layer = "input_node";
    std::string output_layer = "output_node";
    bool self_test = false;
    std::string root_dir = "";

    std::unordered_map<int, char> mapping;
};

而RecogLetter.cc是这个https://github.com/jesen8/recog_letter的实现,包含了所有的代码。

CMakeLists.txt在下面

代码语言:javascript
复制
cmake_minimum_required(VERSION 2.8)

SET(CMAKE_CXX_FLAGS "-std=c++11")

set(tensoflow_source_dir /home/swls/work_dir/github/tensorflow-1.13.0-rc2)

find_package(OpenCV REQUIRED)


include_directories(
    ${OpenCV_INCLUDE_DIRS}
    ${tensoflow_source_dir}
    ${tensoflow_source_dir}/bazel-out/k8-opt/genfiles
    ${tensoflow_source_dir}/tensorflow/contrib/makefile/gen/proto
    ${tensoflow_source_dir}/tensorflow/contrib/makefile/gen/proto_text
    ${tensoflow_source_dir}/tensorflow/contrib/makefile/gen/host_obj
    ${tensoflow_source_dir}/tensorflow/contrib/makefile/gen/protobuf/include
    ${tensoflow_source_dir}/tensorflow/contrib/makefile/downloads/eigen
    ${tensoflow_source_dir}/tensorflow/contrib/makefile/downloads/nsync/public
    ${tensoflow_source_dir}/tensorflow/contrib/makefile/downloads/absl
    ${tensoflow_source_dir}/tensorflow/contrib/makefile/downloads/googletest/googletest/include
    )
    # ${tensoflow_source_dir}/include)

link_directories(
    ${OpenCV_LIBRARY_DIRS}
    # /usr/local/lib
    # ${tensoflow_source_dir}/bazel-bin/tensorflow
    # ${tensoflow_source_dir}/tensorflow/contrib/makefile/downloads/nsync/builds/default.linux.c++11
    # ${tensoflow_source_dir}/tensorflow/contrib/makefile/gen/lib
    # ${tensoflow_source_dir}/tensorflow/contrib/makefile/gen/protobuf/lib
    )


SET(param "-std=c++11 -Wall -L${tensoflow_source_dir}/tensorflow/contrib/makefile/gen/protobuf-host/lib -Wl,--allow-multiple-definition -Wl,--whole-archive  ${tensoflow_source_dir}/tensorflow/contrib/makefile/gen/lib/libtensorflow-core.a -Wl,--no-whole-archive ${tensoflow_source_dir}/tensorflow/contrib/makefile/downloads/nsync/builds/default.linux.c++11/nsync.a -lstdc++ -l:libprotobuf.a -lz -lm -ldl -lpthread -lrt")

set(SIMPLE_MODEL "recog_letter")

add_library(${SIMPLE_MODEL} STATIC RecogLetter.cc RecogLetter.h)


target_link_libraries(${SIMPLE_MODEL} ${OpenCV_LIBS})

target_link_libraries(${SIMPLE_MODEL} ${param})

并且可以成功构建,然后获得librecog_letter.a

代码语言:javascript
复制
total 540
drwxrwxr-x 3 swls swls   4096 5月  22 11:13 ./
drwxrwxr-x 5 swls swls   4096 5月  22 11:10 ../
-rw-rw-r-- 1 swls swls  13272 5月  22 11:12 CMakeCache.txt
drwxrwxr-x 5 swls swls   4096 5月  22 11:13 CMakeFiles/
-rw-rw-r-- 1 swls swls   1477 5月  22 11:10 cmake_install.cmake
-rw-rw-r-- 1 swls swls 511870 5月  22 11:13 librecog_letter.a
-rw-rw-r-- 1 swls swls   5222 5月  22 11:13 Makefile

最后,我将使用librecog_letter.a。

代码非常少

代码语言:javascript
复制
#include "RecogLetter.h"

int main(int argc, char* argv[]) {

  RecogLetter recog_letter;

  int ret_code = recog_letter.init(argv[1], argv[2]);

  return ret_code;
}

我也可以成功地构建。

代码语言:javascript
复制
drwxrwxr-x 3 swls swls      4096 5月  22 11:28 ./
drwxrwxr-x 3 swls swls      4096 5月  22 11:20 ../
-rw-rw-r-- 1 swls swls     12123 5月  22 11:20 CMakeCache.txt
drwxrwxr-x 5 swls swls      4096 5月  22 11:28 CMakeFiles/
-rw-rw-r-- 1 swls swls      1487 5月  22 11:20 cmake_install.cmake
-rw-rw-r-- 1 swls swls      5175 5月  22 11:28 Makefile
-rwxrwxr-x 1 swls swls 158587704 5月  22 11:28 recog_letter_test*



is there anyone can help me? thanks so much.

there is some info for help my build
[https://github.com/tensorflow/tensorflow/issues/28388#issuecomment-490670167](https://github.com/tensorflow/tensorflow/issues/28388#issuecomment-490670167)

但是当我运行./recog_letter_test **.output.pb **.label时,错误如下:

代码语言:javascript
复制
2019-05-22 11:29:48.447661: E tensorflow/core/framework/op_kernel.cc:1325] OpKernel ('op: "StatelessMultinomial" device_type: "CPU" constraint { name: "T" allowed_values { list { type: DT_DOUBLE } } } constraint { name: "output_dtype" allowed_values { list { type: DT_INT64 } } }') for unknown op: StatelessMultinomial
2019-05-22 11:29:48.447896: E tensorflow/core/framework/op_kernel.cc:1325] OpKernel ('op: "StatelessMultinomial" device_type: "CPU" constraint { name: "T" allowed_values { list { type: DT_DOUBLE } } } constraint { name: "output_dtype" allowed_values { list { type: DT_INT32 } } }') for unknown op: StatelessMultinomial
2019-05-22 11:29:48.447906: E tensorflow/core/framework/op_kernel.cc:1325] OpKernel ('op: "StatelessMultinomial" device_type: "CPU" constraint { name: "T" allowed_values { list { type: DT_FLOAT } } } constraint { name: "output_dtype" allowed_values { list { type: DT_INT64 } } }') for unknown op: StatelessMultinomial
2019-05-22 11:29:48.447912: E tensorflow/core/framework/op_kernel.cc:1325] OpKernel ('op: "StatelessMultinomial" device_type: "CPU" constraint { name: "T" allowed_values { list { type: DT_FLOAT } } } constraint { name: "output_dtype" allowed_values { list { type: DT_INT32 } } }') for unknown op: StatelessMultinomial
2019-05-22 11:29:48.447918: E tensorflow/core/framework/op_kernel.cc:1325] OpKernel ('op: "StatelessMultinomial" device_type: "CPU" constraint { name: "T" allowed_values { list { type: DT_HALF } } } constraint { name: "output_dtype" allowed_values { list { type: DT_INT64 } } }') for unknown op: StatelessMultinomial
2019-05-22 11:29:48.447945: E tensorflow/core/framework/op_kernel.cc:1325] OpKernel ('op: "StatelessMultinomial" device_type: "CPU" constraint { name: "T" allowed_values { list { type: DT_HALF } } } constraint { name: "output_dtype" allowed_values { list { type: DT_INT32 } } }') for unknown op: StatelessMultinomial
2019-05-22 11:29:48.447983: E tensorflow/core/framework/op_kernel.cc:1325] OpKernel ('op: "LookupTableFindV2" device_type: "CPU"') for unknown op: LookupTableFindV2
2019-05-22 11:29:48.448003: E tensorflow/core/framework/op_kernel.cc:1325] OpKernel ('op: "MutableHashTable" device_type: "CPU" constraint { name: "key_dtype" allowed_values { list { type: DT_STRING } } } constraint { name: "value_dtype" allowed_values { list { type: DT_INT64 } } }') for unknown op: MutableHashTable
2019-05-22 11:29:48.448010: E tensorflow/core/framework/op_kernel.cc:1325] OpKernel ('op: "MutableHashTable" device_type: "CPU" constraint { name: "key_dtype" allowed_values { list { type: DT_STRING } } } constraint { name: "value_dtype" allowed_values { list { type: DT_INT32 } } }') for unknown op: MutableHashTable
2019-05-22 11:29:48.448016: E tensorflow/core/framework/op_kernel.cc:1325] OpKernel ('op: "MutableHashTable" device_type: "CPU" constraint { name: "key_dtype" allowed_values { list { type: DT_STRING } } } constraint { name: "value_dtype" allowed_values { list { type: DT_FLOAT } } }') for unknown op: MutableHashTable
2019-05-22 11:29:48.448022: E tensorflow/core/framework/op_kernel.cc:1325] OpKernel ('op: "MutableHashTable" device_type: "CPU" constraint { name: "key_dtype" allowed_values { list { type: DT_STRING } } } constraint { name: "value_dtype" allowed_values { list { type: DT_DOUBLE } } }') for unknown op: MutableHashTable
2019-05-22 11:29:48.448031: E tensorflow/core/framework/op_kernel.cc:1325] OpKernel ('op: "MutableHashTable" device_type: "CPU" constraint { name: "key_dtype" allowed_values { list { type: DT_STRING } } } constraint { name: "value_dtype" allowed_values { list { type: DT_BOOL } } }') for unknown op: MutableHashTable
2019-05-22 11:29:48.448041: E tensorflow/core/framework/op_kernel.cc:1325] OpKernel ('op: "MutableHashTable" device_type: "CPU" constraint { name: "key_dtype" allowed_values { list { type: DT_INT64 } } } constraint { name: "value_dtype" allowed_values { list { type: DT_VARIANT } } }') for unknown op: MutableHashTable

...
...
2019-05-22 11:29:48.460537: E tensorflow/core/framework/op_kernel.cc:1325] OpKernel ('op: "HashTable" device_type: "CPU" constraint { name: "key_dtype" allowed_values { list { type: DT_INT32 } } } constraint { name: "value_dtype" allowed_values { list { type: DT_INT32 } } }') for unknown op: HashTable
2019-05-22 11:29:48.460547: E tensorflow/core/framework/op_kernel.cc:1325] OpKernel ('op: "HashTable" device_type: "CPU" constraint { name: "key_dtype" allowed_values { list { type: DT_INT32 } } } constraint { name: "value_dtype" allowed_values { list { type: DT_FLOAT } } }') for unknown op: HashTable
2019-05-22 11:29:48.460557: E tensorflow/core/framework/op_kernel.cc:1325] OpKernel ('op: "HashTable" device_type: "CPU" constraint { name: "key_dtype" allowed_values { list { type: DT_INT32 } } } constraint { name: "value_dtype" allowed_values { list { type: DT_DOUBLE } } }') for unknown op: HashTable
2019-05-22 11:29:48.460831: E tensorflow/core/framework/op_kernel.cc:1325] OpKernel ('op: "StatelessRandomUniform" device_type: "CPU" constraint { name: "dtype" allowed_values { list { type: DT_DOUBLE } } } host_memory_arg: "shape" host_memory_arg: "seed"') for unknown op: StatelessRandomUniform
2019-05-22 11:29:48.460841: E tensorflow/core/framework/op_kernel.cc:1325] OpKernel ('op: "StatelessRandomUniform" device_type: "CPU" constraint { name: "dtype" allowed_values { list { type: DT_FLOAT } } } host_memory_arg: "shape" host_memory_arg: "seed"') for unknown op: StatelessRandomUniform
2019-05-22 11:29:48.460850: E tensorflow/core/framework/op_kernel.cc:1325] OpKernel ('op: "StatelessRandomUniform" device_type: "CPU" constraint { name: "dtype" allowed_values { list { type: DT_BFLOAT16 } } } host_memory_arg: "shape" host_memory_arg: "seed"') for unknown op: StatelessRandomUniform
2019-05-22 11:29:48.460860: E tensorflow/core/framework/op_kernel.cc:1325] OpKernel ('op: "StatelessRandomUniform" device_type: "CPU" constraint { name: "dtype" allowed_values { list { type: DT_HALF } } } host_memory_arg: "shape" host_memory_arg: "seed"') for unknown op: StatelessRandomUniform
2019-05-22 11:29:48.460870: E tensorflow/core/framework/op_kernel.cc:1325] OpKernel ('op: "StatelessRandomNormal" device_type: "CPU" constraint { name: "dtype" allowed_values { list { type: DT_DOUBLE } } } host_memory_arg: "shape" host_memory_arg: "seed"') for unknown op: StatelessRandomNormal
2019-05-22 11:29:48.460880: E tensorflow/core/framework/op_kernel.cc:1325] OpKernel ('op: "StatelessRandomNormal" device_type: "CPU" constraint { name: "dtype" allowed_values { list { type: DT_FLOAT } } } host_memory_arg: "shape" host_memory_arg: "seed"') for unknown op: StatelessRandomNormal
2019-05-22 11:29:48.460890: E tensorflow/core/framework/op_kernel.cc:1325] OpKernel ('op: "StatelessRandomNormal" device_type: "CPU" constraint { name: "dtype" allowed_values { list { type: DT_BFLOAT16 } } } host_memory_arg: "shape" host_memory_arg: "seed"') for unknown op: StatelessRandomNormal
2019-05-22 11:29:48.460900: E tensorflow/core/framework/op_kernel.cc:1325] OpKernel ('op: "StatelessRandomNormal" device_type: "CPU" constraint { name: "dtype" allowed_values { list { type: DT_HALF } } } host_memory_arg: "shape" host_memory_arg: "seed"') for unknown op: StatelessRandomNormal
2019-05-22 11:29:48.460909: E tensorflow/core/framework/op_kernel.cc:1325] OpKernel ('op: "StatelessTruncatedNormal" device_type: "CPU" constraint { name: "dtype" allowed_values { list { type: DT_DOUBLE } } } host_memory_arg: "shape" host_memory_arg: "seed"') for unknown op: StatelessTruncatedNormal
2019-05-22 11:29:48.460919: E tensorflow/core/framework/op_kernel.cc:1325] OpKernel ('op: "StatelessTruncatedNormal" device_type: "CPU" constraint { name: "dtype" allowed_values { list { type: DT_FLOAT } } } host_memory_arg: "shape" host_memory_arg: "seed"') for unknown op: StatelessTruncatedNormal
2019-05-22 11:29:48.460929: E tensorflow/core/framework/op_kernel.cc:1325] OpKernel ('op: "StatelessTruncatedNormal" device_type: "CPU" constraint { name: "dtype" allowed_values { list { type: DT_BFLOAT16 } } } host_memory_arg: "shape" host_memory_arg: "seed"') for unknown op: StatelessTruncatedNormal
2019-05-22 11:29:48.460939: E tensorflow/core/framework/op_kernel.cc:1325] OpKernel ('op: "StatelessTruncatedNormal" device_type: "CPU" constraint { name: "dtype" allowed_values { list { type: DT_HALF } } } host_memory_arg: "shape" host_memory_arg: "seed"') for unknown op: StatelessTruncatedNormal
2019-05-22 11:29:48.460950: E tensorflow/core/framework/op_kernel.cc:1325] OpKernel ('op: "StatelessRandomUniformInt" device_type: "CPU" constraint { name: "dtype" allowed_values { list { type: DT_INT64 } } } host_memory_arg: "shape" host_memory_arg: "seed" host_memory_arg: "minval" host_memory_arg: "maxval"') for unknown op: StatelessRandomUniformInt
2019-05-22 11:29:48.460961: E tensorflow/core/framework/op_kernel.cc:1325] OpKernel ('op: "StatelessRandomUniformInt" device_type: "CPU" constraint { name: "dtype" allowed_values { list { type: DT_INT32 } } } host_memory_arg: "shape" host_memory_arg: "seed" host_memory_arg: "minval" host_memory_arg: "maxval"') for unknown op: StatelessRandomUniformInt
EN

回答 2

Stack Overflow用户

发布于 2019-05-23 03:38:26

我的一个来自Materport RCNN的模型也有类似的问题

我得到以下错误

错误:在creative2上运行的二进制文件中,Op类型未注册'DenseToDenseSetOperation‘。确保在此进程中运行的二进制文件中注册了Op和Kernel。请注意,如果您正在加载一个使用来自tf.contrib的操作的已保存的图形,则应该在导入图形之前访问(例如) tf.contrib.resampler,因为在第一次访问模块时会延迟注册contrib操作。

Creative2是客户端计算机名称

有一个快速的GitHub搜索,似乎这个行动是一件事

https://github.com/tensorflow/tensorflow/blob/9590c4c32dd4346ea5c35673336f5912c6072bf2/tensorflow/core/ops/set_ops.cc

因此,鉴于它在核心中,我不知道如何确保它是在同一软件的动态版本中初始化的

相同的

票数 0
EN

Stack Overflow用户

发布于 2019-05-25 12:11:01

答案在这里:

https://github.com/tensorflow/tensorflow/issues/28942#issuecomment-494989399

请参阅文档

最常见的破坏原因是已经添加到Bazel构建脚本中的文件,但makefile不知道这些文件。这种情况的典型症状包括链接器错误,其中提到了缺少的符号或没有找到的协议aren头。要解决这些问题,可以查看tensorflow/contrib/makefile中的*.txt文件。如果您有一个新的运算符,则可能需要将其添加到tf_op_files.txt,或者为tf_proto_files.txt添加一个新的原型。

您可以找到哪个.cc文件包含您的操作符,并将其添加到相应的文本文件中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56249350

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档