Centos C/C++库 libtensorflow.so libtensorflow_framework.so 测试
- 测试C库
创建任意测试目录$TEST_PATH
tensorflow的git目录为$TENSORFLOW_PATH
protobuf的安卓目录为$PROTOBUF_PATH
cd $TEST_PATH
mkdir lib
cp \$TENSORFLOW_PATH/bazel-bin/tensorflow/libtensorflow.so \$TENSORFLOW_PATH/bazel-bin/tensorflow/libtensorflow_framework.so \$PROTOBUF_PATH/lib/libprotobuf.a lib/
mkdir include
cd $TENSORFLOW_PATH
find ./tensorflow/ -name "*.h"|cpio -pdm /home/prefer/src/temp/include
cd $TEST_PATH
vim test.c
#include <stdio.h> #include "tensorflow/c/c_api.h" int main() { printf("Hello from TensorFlow C library version %s\n", TF_Version()); return 0; }
export LD_LIBRARY_PATH=$TEST_PATH/lib
gcc test.c -Iinclude -Llib -ltensorflow -ltensorflow_framework
./a.out
- 测试C++库
创建任意测试目录$TEST_PATH
tensorflow的git目录为$TENSORFLOW_PATH
protobuf的安卓目录为$PROTOBUF_PATH
cd $TEST_PATH
mkdir lib
cp \$TENSORFLOW_PATH/bazel-bin/tensorflow/libtensorflow_cc.so \$TENSORFLOW_PATH/bazel-bin/tensorflow/libtensorflow_framework.so \$PROTOBUF_PATH/lib/libprotobuf.a lib/
mkdir include
cd $TENSORFLOW_PATH
find ./tensorflow/ -name "*.h"|cpio -pdm /home/prefer/src/temp/include
cd bazel-genfiles/
find ./tensorflow/ -name "*.h"|cpio -pdm /home/prefer/src/temp/include
cd $PROTOBUF_PATH/include
find ./include/ -name "*.h"|cpio -pdm /home/prefer/src/temp/include
cd $TEST_PATH
cp -r $TEST_PATH/include/tensorflow/contrib/makefile/downloads/absl/absl/ include
vim test.py 预生成模型
import tensorflow as tf from tensorflow.python.framework.graph_util import convert_variables_to_constants tf.reset_default_graph() a = tf.placeholder(tf.int32, shape=(1,2), name="a") b = tf.Variable(tf.constant([[4,7]]), name="b") c = tf.add(a, b, name="c") init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) print sess.run([c], feed_dict={a:[[2,88]]}) graph_out = convert_variables_to_constants(sess, sess.graph_def, output_node_names=['c']) with tf.gfile.GFile("model.pb", "wb") as output: output.write(graph_out.SerializeToString()) #上面是Python生成模型 import sys sys.exit(0) #下面是Python加载模型 tf.reset_default_graph() d = tf.placeholder(tf.int32, shape=(1,2), name="a") with open("model.pb", "rb") as input: graph = tf.GraphDef() graph.ParseFromString(input.read()) c = tf.import_graph_def(graph, input_map={'a': d}, return_elements=['c:0']) with tf.Session() as sess: print sess.run([c], feed_dict={d:[[2,88]]})
vim test.cpp 加载模型
#include <iostream> #include <string> #include <map> #include <vector> #include <tensorflow/core/platform/env.h> #include <tensorflow/core/public/session.h> using namespace std; using namespace tensorflow; int main() { Session *session; Status status; status = NewSession(SessionOptions(), &session); if (!status.ok()) { cout << status.ToString() << endl; return 1; } cout << "Session Succ" << endl; GraphDef graphdef; status = ReadBinaryProto(Env::Default(), "model.pb", &graphdef); if (!status.ok()) { cout << status.ToString() << endl; return 1; } cout << "Read Graph Succ" << endl; status = session->Create(graphdef); if (!status.ok()) { cout << status.ToString() << endl; return 1; } cout << "Session Create Succ" << endl; Tensor input(DT_INT32, TensorShape({1,2})); int *pointor = input.flat<int>().data(); *pointor = 1; *(pointor+1) = 2; vector<pair<string, Tensor> > inputs = { {"a", input} }; vector<Tensor> outputs; status = session->Run(inputs, {"c"}, {}, &outputs); if (!status.ok()) { cout << status.ToString() << endl; return 1; } cout << "Session Run Succ" << endl; cout << outputs[0].flat<int>() << endl; return 0; }
g++ test.cpp -std=c++11 -Iinclude -Llib -ltensorflow_cc -ltensorflow_framework
./a.out
Centos 编译TensorFlow C/C++库 libtensorflow.so libtensorflow_framework.so
Centos C/C++库 libtensorflow.so libtensorflow_framework.so 测试
参考文献:
https://www.cnblogs.com/hrlnw/p/7007648.html
https://tensorflow.google.cn/guide/extend/cc
https://blog.csdn.net/qq_25109263/article/details/81285952
https://blog.csdn.net/rockingdingo/article/details/75452711
https://github.com/tensorflow/tensorflow/issues/22320
https://www.cnblogs.com/hrlnw/p/7383951.html
https://www.jianshu.com/p/d46596558640
https://blog.csdn.net/fly_time2012/article/details/80841369