我爱编程

安装并使用tensorflow c++ api

2018-06-08  本文已影响16人  一个假名字_a720
May 30,2018, by Wanghao, Zhejiang University of Technology

由于工业应用的需要,所以需要安装Tensorflow的c++库,并整合到已有的工程中去。Alibaba采用的python训练模型,在用java调用模型。本文主要是根据自己的实现过程的记录,仅作参考。

环境

  1. 安装Tensorflow时,需要根据指南安装依赖

    $ sudo apt-get install autoconf automake libtool curl make g++ unzip  # Protobuf Dependencies
    $ sudo apt-get install python-numpy swig python-dev python-wheel      # TensorFlow Dependencies
    $ git clone https://github.com/tensorflow/tensorflow                  # TensorFlow
    
  2. 由于解压出来的一级目录下有一个configure文件,需要首先配置该文件

    $ ./configure      # 如果只需要配置cpu环境就一直回车
    

    注意:如果会使用到OpenCV,这里的编译命令需要加入--config=monolithic。因为会导致cv::imread函数无法使用,详细的讨论参见: https://github.com/tensorflow/tensorflow/issues/14267

    $ bazel build tensorflow:libtensorflow_cc.so
    
    # 使用OpenCV的编译命令
    $ bazel build --config=monolithic tensorflow:libtensorflow_cc.so
    
    $ sudo cp bazel-bin/tensorflow/libtensorflow_cc.so /usr/local/lib
    $ sudo cp bazel-bin/tensorflow/libtensorflow_framework.so /usr/local/lib
    
  3. 由于可能存在的依赖问题需要根据当前的系统执行名字如build_all_linux.sh的文件

    $ ./tensorflow/contrib/makefile/build_all_linux.sh 
    

    复制源文件到 /usr/local/include/google ,并移除不需要的文件:

    $ sudo mkdir -p /usr/local/include/google/tensorflow
    $ sudo cp -r tensorflow /usr/local/include/google/tensorflow/
    $ sudo find /usr/local/include/google/tensorflow/tensorflow -type f  ! -name "*.h" -delete
    

    复制bazel-genfiles文件夹中所有生成的文件:

    $ sudo cp bazel-genfiles/tensorflow/core/framework/*.h  /usr/local/include/google/tensorflow/tensorflow/core/framework
    $ sudo cp bazel-genfiles/tensorflow/core/lib/core/*.h  /usr/local/include/google/tensorflow/tensorflow/core/lib/core
    $ sudo cp bazel-genfiles/tensorflow/core/protobuf/*.h  /usr/local/include/google/tensorflow/tensorflow/core/protobuf
    $ sudo cp bazel-genfiles/tensorflow/core/util/*.h  /usr/local/include/google/tensorflow/tensorflow/core/util
    $ sudo cp bazel-genfiles/tensorflow/cc/ops/*.h  /usr/local/include/google/tensorflow/tensorflow/cc/ops
    

    由于 kernels 目录下没有任何文件,所以这个指令并没有生效,目前没有问题。但是在/kernels/boosted_trees/ 目录下有一个.h文件,后期如果代码中报错了,可以把这个文件复制过去

    $ sudo cp bazel-genfiles/tensorflow/core/kernels/*.h  /usr/local/include/google/tensorflow/tensorflow/core/kernels   
    

    复制 third_party 文件夹:

    $ sudo cp -r third_party /usr/local/include/google/tensorflow/
    $ sudo rm -r /usr/local/include/google/tensorflow/third_party/py
    
    # Note: newer versions of TensorFlow do not have the following directory
    $ sudo rm -r /usr/local/include/google/tensorflow/third_party/avro
    
  4. 使用测试程序,看看是否配置成功

    #include "tensorflow/cc/client/client_session.h"
    #include "tensorflow/cc/ops/standard_ops.h"
    #include "tensorflow/core/framework/tensor.h"
    
    int main()
    {
        using namespace tensorflow;
        using namespace tensorflow::ops;
        Scope root = Scope::NewRootScope();
        // Matrix A = [3 2; -1 0]
        auto A = Const(root, { {3.f, 2.f}, {-1.f, 0.f} });
        // Vector b = [3 5]
        auto b = Const(root, { {3.f, 5.f} });
        // v = Ab^T
        auto v = MatMul(root.WithOpName("v"), A, b, MatMul::TransposeB(true));
        std::vector<Tensor> outputs;
        ClientSession session(root);
        // Run and fetch v
        TF_CHECK_OK(session.Run({v}, &outputs));
        // Expect outputs[0] == [19; -3]
        LOG(INFO) << outputs[0].matrix<float>();
        return 0;
    }
    
  5. 可能会遇到这个问题:

     fatal error: unsupported/Eigen/CXX11/Tensor: No such file or directory
    

    建议根据https://www.cnblogs.com/newneul/p/8256803.html通过源码安装Eigen3.3或以上版本

参考资料

Integrate TensorFlow with CMake projects effortlessly
tensorflow c/c++库使用方法
TensorFlow 的 c ++ 实践及各种坑!

上一篇 下一篇

猜你喜欢

热点阅读