Я запускаю свой код в системе 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