gpt4 book ai didi

c++ - 如何使用 libtensorflow-core.a 构建静态库以及如何使用它?

转载 作者:太空宇宙 更新时间:2023-11-04 12:36:23 31 4
gpt4 key购买 nike

我正在使用 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

#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

最佳答案

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

出现以下错误

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

Creative2 是客户端机器名

快速搜索一下 GitHub,这个操作似乎很不错

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

所以鉴于它在核心中,我不知道该怎么做才能确保它被初始化,它在同一软件的动态版本中

山姆

关于c++ - 如何使用 libtensorflow-core.a 构建静态库以及如何使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56249350/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com