• TensorFlow基础笔记(15) 编译TensorFlow.so,提供给C++平台调用


    参考 

    http://blog.csdn.net/rockingdingo/article/details/75452711

    https://www.cnblogs.com/hrlnw/p/7007648.html

    https://www.cnblogs.com/hrlnw/p/7383951.html

    1.下载tensorflow源码

      git clone  https://github.com/tensorflow/tensorflow

    2.安装bazel

    sudo apt-get update && sudo apt-get install bazel  

    升级bazel

    sudo apt-get upgrade bazel

    使用bazel编译出现最低版本要求 0.4.5, 而本身的版本为0.11.1的时候,

    将 最低版本要求 0.4.5文件中的0.4.5修改为0.0.0

    3.编译源码,生成so库

    ## 进入tensoflow源码根目录后编译
    # 编译生成.so文件, 编译C++ API的库 (建议)
    bazel build //tensorflow:libtensorflow_cc.so

    # 也可以选择,编译C API的库
    bazel build //tensorflow:libtensorflow.so

    编译出现问题,建议Git clone最新的tensoflow代码 重新编译

    编译完成,可以看到编译生成的文件路径

    tensorflow/cc/gradients/data_flow_grad.cc:151:1: note: in expansion of macro 'REGISTER_GRADIENT_OP'
    REGISTER_GRADIENT_OP("ParallelDynamicStitch", DynamicStitchGrad);
    ^
    Target //tensorflow:libtensorflow_cc.so up-to-date:
    bazel-bin/tensorflow/libtensorflow_cc.so
    INFO: Elapsed time: 1051.314s, Critical Path: 74.06s
    INFO: Build completed successfully, 2355 total actions

    4 编译完成,配准调用环境

    在等待30多分钟后, 如果编译成功,在tensorflow根目录下出现 bazel-bin, bazel-genfiles 等文件夹,

    按顺序执行以下命令将对应的libtensorflow_cc.so文件和其他文件拷贝进入 /usr/local/lib/ 目录

    mkdir /usr/local/include/tf  

    cp -r bazel-genfiles/ /usr/local/include/tf/  

    cp -r tensorflow /usr/local/include/tf/  

    cp -r third_party /usr/local/include/tf/  

    cp -r bazel-bin/tensorflow/libtensorflow_cc.so /usr/local/lib/ 

    这一步完成后,我们就准备好了libtensorflow_cc.so文件等,后面在自己的C++编译环境和代码目录下编译时链接这些库即可。

     

    5 c++调用libtensorflow_cc.so

     

     

     

    安装protobuf出现了问题

    + autoreconf -f -i -Wall,no-obsolete
    configure.ac:30: error: possibly undefined macro: AC_PROG_LIBTOOL
    If this token and others are legitimate, please use m4_pattern_allow.
    See the Autoconf documentation.
    autoreconf: /usr/bin/autoconf failed with exit status: 1

    网上的方案

    https://superuser.com/questions/565988/autoconf-libtool-and-an-undefined-ac-prog-libtool

    已经安装了 libtool 

    sudo apt-get install libtool (失败)

    可以解决的方法

    You need to reinstall it in order to fix the error so follow these steps :

    1] Remove current libtool if installed: sudo apt-get purge libtool

    2] Download it from official website https://www.gnu.org/software/libtool/

    3] Untar it: tar -xzvf "name of the tar_file"

    4] Enter folder and type: ./configure && make

    5] Install it: sudo make install

    And you are done, error should be fixed !

    安装步骤

    git clone https://github.com/google/protobuf.git 

    (0)./autogen.sh   

    (1)./configure  --prefix=/usr/local/protobuf  

    (2)make   

    (3)make check   

    (4)make install  

     

    注意:
    安装成功后,将它的bin和lib目录分别加入到PATH和LD_LIBRARY_PATH环境变量,以方便直接调用。
    设置环境变量过程:编辑/etc/profile,在文件末尾添加:

    注意:这里添加的路径不同版本protobuf可能不同,需要根据提示确认路径,否则会出现root用户和普通用户版本不同的问题。

    export PATH=$PATH:/usr/local/protobuf/bin
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/protobuf/lib
    此时最好重启一次。

    查看protobuf版本的命令为:protoc --version,如果普通用户无法查看,或者与sudo protoc --version查看的版本不同,就是出现了上述路径添加错误的情况。

  • 相关阅读:
    gvim e303 无法打开 “[未命名]“的交换文件,恢复将不可能
    AspectJ获取方法注解的信息
    type parameters of <T>T cannot be determined; no unique maximal instance exists for type variable T with upper bounds int,java.lang.Object
    MySQL@淘宝 资料分享
    MySQL的语句执行顺序
    关于HttpClient上传中文乱码的解决办法
    使用IntelliJ IDEA查看类的继承关系图形
    javax.net.ssl.SSLException: Certificate doesn't match any of the subject alternative names
    Failed to process import candidates for configuration class [com.simple.....]
    安装npm及cnpm(Windows)
  • 原文地址:https://www.cnblogs.com/adong7639/p/8618344.html
Copyright © 2020-2023  润新知