c++ - 如何使用 libtensorflow-core.a 构建静态库以及如何使用它?
问题描述
我在Ubuntu 16
系统上运行我的代码TensorFlow-1.13.0-rc2
。
首先,我运行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
#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 在下面
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
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。
代码很少
#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;
}
我也可以成功构建。
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
,错误如下:
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
解决方案
答案在这里找到:
https://github.com/tensorflow/tensorflow/issues/28942#issuecomment-494989399
查看文档
损坏的最常见原因是已添加到 Bazel 构建脚本中的文件,但 makefile 不知道这些文件。这种情况的典型症状包括链接器错误,提到缺少符号或未找到的 protobuf 标头。要解决这些问题,请查看 tensorflow/contrib/makefile 中的 *.txt 文件。如果您有一个新的操作符,您可能需要将其添加到 tf_op_files.txt,或者将新的原型添加到 tf_proto_files.txt。
您可以找到哪个 .cc 文件具有您的运算符并将其添加到相应的文本文件中。
推荐阅读
- python - 如何通过双击 .py 文件来判断脚本是否运行?
- php - 在 codeigniter 中进行客户反馈
- spring-saml - spring saml 对 spring 5 的扩展支持
- angular - 解析器在组件的 ngoninit 之后执行
- c# - 当 ShowInTaskBar 属性为 false 时,LoginForm 会自动隐藏或关闭
- typescript - 在调用 `super(...)` 之前先关闭 `this`
- java - MapReduce - 迭代 Iterable 的偶数值
- java - 带有弹簧启动 2.0.0 的 Keycloak rest api 包装器
- c++ - 打包结构成员的传递地址
- linux - 在 Windows 上的 Docker 容器中编辑 ASP.NET Core 网站并在 Ubuntu VPN 上运行