Ubuntu18安装tensorflow2 (c++版本)

安装tensorflow (c++版本)

环境:Ubuntu 18.04 LTS、g++ 7.5.0、CUDA 10.1、cuDNN 7.6.4

安装位置:新建文件夹 $HOME/TF,命名为 $TENSORFLOW,位置、命名可随意更改。

  1. 安装bazelisk

    $ sudo curl -Lo /usr/local/bin/bazel https://github.com/bazelbuild/bazelisk/releases/download/vx.x.x/bazelisk-linux-amd64
    $ sudo chmod +x /usr/local/bin/bazel 
    

    ​ 具体版本号 vx.x.x 到该网址上查看,最新即可: https://github.com/bazelbuild/bazelisk/releases

  2. 下载tensorflow

    $ cd $TENSORFLOW
    $ git clone https://github.com/tensorflow/tensorflow
    
  3. 查看所需bazel版本并下载bazel

    $ cd tensorflow
    $ grep -r _TF_MAX_BAZEL_VERSION .
    ./config.py:_TF_MAX_BAZEL_VERSION = 'x.x.x'
    $ echo "x.x.x" > .bazelversion
    $ bazel version
    
  4. 配置tensorflow并开始编译

    $ ./configure
    # 按照安装需求写y/n和路径,CUDA项可填y,其他不懂的填n,默认路径不对的重新填,否则回车过。
    $ bazel build --config=opt --config=cuda --config=monolithic \ //tensorflow:libtensorflow_cc.so //tensorflow:libtensorflow_framework.so
    

    经过很长一段时间结束编译,一般bazel版本正确不会有问题,可能会有网络下载问题。编译结束后在 $TENSORFLOW/tensorflow 下生成4个bazel开头的文件夹。

  5. 下载安装eigen,版本貌似选最新即可

    $ cd $TENSORFLOW
    $ git clone https://gitlab.com/libeigen/eigen.git
    # 貌似gitlab上的是最新,github上的有错。
    $ cd eigen
    $ mkdir build && cd build
    $ cmake ..
    $ sudo make install
    

    安装成功后在 /usr/local/include 下生成 eigen3 文件夹,为eigen相关头文件。

  6. 下载安装protobuf,版本查看方法如下:

    (1)打开 $TENSORFLOW/tensorflow/tensorflow/workspace.bzl;

    (2)搜索 ‘protobuf’;

    (3)从 strip_prefix = “protobuf-x.x.x”, 得知protobuf版本号。

    从网址中找到对应版本:https://github.com/protocolbuffers/protobuf/releases

    点击 Source code(tar.gz) 下载,解压到 $TENSORFLOW 下。

    $ cd $TENSORFLOW/protobuf
    $ ./autogen.sh
    $ ./configure --prefix=/usr/protobuf
    $ sudo make
    $ sudo make install
    $ sudo ln -s /usr/protobuf/bin/protoc /usr/local/bin/protoc
    

    测试

    $ protoc --version
    libprotoc x.x.x
    
  7. 写个demo测试效果 test.cpp

    #include <iostream>
    #include "tensorflow/cc/client/client_session.h"
    #include "tensorflow/cc/ops/standard_ops.h"
    #include "tensorflow/core/framework/tensor.h"
    
    using namespace tensorflow;
    using namespace tensorflow::ops;
    
    int main()
    {
      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));
      std::cout << "tensorflow session run ok" << std::endl;
      // Expect outputs[0] == [19; -3]
      std::cout << outputs[0].matrix<float>() << std::endl;
    
      return 0;
    }
    
  8. 把动态库复制到 $LD_LIBRARY_PATH 能访问到的地方

    $ sudo mkdir /usr/local/lib/tensorflow
    $ sudo cp $TENSORFLOW/tensorflow/bazel-bin/tensorflow/*.so* /usr/local/lib/tensorflow/
    $ sudo gedit ~/.bashrc
    

    在最后添加两行:

    #tensorflow
    export LD_LIBRARY_PATH=/usr/local/lib/tensorflow:$LD_LIBRARY_PATH
    

    更新变量:

    $ source ~/.bashrc
    
  9. 新建 run.sh:

    #!/bin/bash
    g++ test.cpp -o test -l tensorflow_cc -l tensorflow_framework \
    -L /usr/local/lib/tensorflow \
    -I $TENSORFLOW/tensorflow \
    -I $TENSORFLOW/tensorflow/bazel-bin \
    -I $TENSORFLOW/tensorflow/bazel-bin/tensorflow/include \
    -I $TENSORFLOW/protobuf/src \
    -I $TENSORFLOW/eigen3 \
    

    编译和运行demo:

    $ ./run.sh
    $ ./test
    
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Ubuntu 18.04,你可以使用以下步骤在C++中使用TensorFlow库: 1. 安装依赖项: ``` sudo apt-get update sudo apt-get install build-essential curl sudo apt-get install libcurl3-dev libfreetype6-dev libpng-dev libzmq3-dev pkg-config sudo apt-get install python3-dev python3-pip python3-wheel python3-setuptools ``` 2. 安装TensorFlow C++库: - 通过pip安装TensorFlow C++库: ``` pip3 install tensorflow ``` - 或者,从源代码构建TensorFlow C++库: - 克隆TensorFlow仓库: ``` git clone https://github.com/tensorflow/tensorflow.git cd tensorflow ``` - 配置构建选项并构建TensorFlow C++库: ``` ./configure bazel build --config=opt //tensorflow:libtensorflow_cc.so ``` - 在.bashrc或.bash_profile文件中设置LD_LIBRARY_PATH环境变量: ``` export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/tensorflow/bazel-bin/tensorflow/ ``` 3. 编写C++代码并链接TensorFlow库: - 创建一个C++文件,比如example.cpp,并添加以下代码: ```cpp #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(); auto A = Const(root, { {3.f, 2.f}, {-1.f, 0.f} }); auto b = Const(root, { {3.f, 5.f} }); auto product = MatMul(root.WithOpName("product"), A, b, MatMul::TransposeB(true)); std::vector<Tensor> outputs; ClientSession session(root); TF_CHECK_OK(session.Run({product}, &outputs)); for (const auto& tensor : outputs) { std::cout << tensor.matrix<float>() << std::endl; } return 0; } ``` - 编译C++代码: ``` g++ -std=c++11 -I/path/to/tensorflow -L/path/to/tensorflow/bazel-bin/tensorflow example.cpp -ltensorflow_cc -o example ``` - 运行生成的可执行文件: ``` ./example ``` 这样,你就可以在Ubuntu 18.04上使用C++编写和运行TensorFlow代码了。请确保将`/path/to/tensorflow`替换为你实际的TensorFlow安装路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值