1. 程式人生 > >Ubuntu X86編譯tensorflow C++動態連結庫

Ubuntu X86編譯tensorflow C++動態連結庫

  以下方法在x86上親測通過,在Nvidia TX 系列第三步會出錯。但是會生成動態連結庫,有興趣的可以試試能不能用,我測試是可以用的。
環境(16.04LTS cuda8.0 cudnn6.0.10 tf1.3 python2 )

1.安裝依賴項

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update && sudo apt-get -y dist-upgrade
sudo apt-get install oracle-java8-installer -y
sudo apt-get install zip unzip autoconf automake libtool curl zlib1g-dev
maven -y sudo apt-get install python-numpy python-pip python-dev python-wheel //--------For Python 2.7-------- sudo apt-get install python-pip python-numpy swig python-dev sudo pip install wheel //--------For Python 3.3+-------- sudo apt-get install python3-pip python3-numpy swig python3-dev sudo pip3 install wheel

2.安裝Bazel

Bazel 安裝版本問題:
個人試過很多個版本,0.6.1 0.4.5 0.5.2。最後選擇0.5.2。安裝的時候0.6.1問題最多,時間久遠問題記不清了。
bazel下載
https://github.com/bazelbuild/bazel/releases
這段時間,翻不出去。自己下載很慢很慢。找在學校的同學下載的。把本地的傳到CSDN上了,供需要的下載
http://download.csdn.net/download/ycdhqzhiai/10130322
安裝:解壓後很簡單(別直接解壓,mkdir先)
./compile.sh
安裝得一會,完成後將生成的二級制檔案複製到bin中
cp output/bazel /usr/local/bin/


(大汗!!!!!!bazel可以搞一天。。。。。)

3.編譯tensorflow

cd tensorflow
git checkout v1.3.0

修改tensorflow/BUILD檔案,在末尾行新增

#Added build rule
cc_binary(
    name = "libtensorflow_all.so",
    linkshared = 1,
    linkopts = ["-Wl,--version-script=tensorflow/tf_version_script.lds"], # Remove this line if you are using MacOS
    deps = [
        "//tensorflow/core:framework_internal",
        "//tensorflow/core:tensorflow",
        "//tensorflow/cc:cc_ops",
        "//tensorflow/cc:client_session",
        "//tensorflow/cc:scope",
        "//tensorflow/c:c_api",
    ],
)

./configure

編譯選擇

You have bazel 0.5.2- installed.
Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python
Do you wish to build TensorFlow with MKL support? [y/N] N
No MKL support will be enabled for TensorFlow
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]:
Do you wish to use jemalloc as the malloc implementation? [Y/n] Y
jemalloc enabled
Do you wish to build TensorFlow with Google Cloud Platform support? [y/N] N
No Google Cloud Platform support will be enabled for TensorFlow
Do you wish to build TensorFlow with Hadoop File System support? [y/N] N
No Hadoop File System support will be enabled for TensorFlow
Do you wish to build TensorFlow with the XLA just-in-time compiler (experimental)? [y/N] N
No XLA support will be enabled for TensorFlow
Do you wish to build TensorFlow with VERBS support? [y/N] N
No VERBS support will be enabled for TensorFlow
Do you wish to build TensorFlow with OpenCL support? [y/N] N
No OpenCL support will be enabled for TensorFlow
Do you wish to build TensorFlow with CUDA support? [y/N] N
No CUDA support will be enabled for TensorFlow
Do you wish to build TensorFlow with MPI support? [y/N] N
MPI support will not be enabled for TensorFlow
Configuration finished

生成動態連結庫

bazel build tensorflow:libtensorflow_all.so
cp bazel-bin/tensorflow/libtensorflow_all.so /usr/local/lib/

4.編譯 Protobuf Eigen

此時所在目錄應為tensorflow原始碼根目錄下。

mkdir /tmp/proto
tensorflow/contrib/makefile/download_dependencies.sh
cd tensorflow/contrib/makefile/downloads/protobuf/
./autogen.sh
./configure --prefix=/tmp/proto/
make
make install

mkdir /tmp/eigen
cd ../eigen
mkdir build_dir
cd build_dir
cmake -DCMAKE_INSTALL_PREFIX=/tmp/eigen/ ../
make install
cd ../../../../../..

5新增include檔案

sudo mkdir -p /usr/local/include/google/tensorflow/tensorflow
cp -r bazel-genfiles/* /usr/local/include/google/tensorflow
cp -r tensorflow/cc /usr/local/include/google/tensorflow/tensorflow
cp -r tensorflow/core /usr/local/include/google/tensorflow/tensorflow
cp -r third_party  /usr/local/include/google/tensorflow
cp -r /tmp/proto/include/*  /usr/local/include/google/tensorflow
cp -r /tmp/eigen/include/eigen3/*  /usr/local/include/google/tensorflow

至此,tensorflow編譯動態連結庫就差不多了。.so檔案在/usr/loca/lib下。include在/usr/local/include/google/tensorflow/下,有了.so和.h趕緊測試吧。(親測呼叫tensorflow C++API 提取影象特徵,與python提取特徵僅僅資料精度有差別)。

=========我是分割線==========
前面提到的TX系列使用.so問題。我在編譯的時候第三步會失敗。先貼報錯資訊吧
TX1上報錯

/home/nvidia/tensorflow/tensorflow/BUILD:458:1: error loading package ‘tensorflow/c’: Encountered error while reading extension file ‘protobuf.bzl’: no such package ‘@protobuf//’: java.io.IOException: Error downloading [https://github.com/google/protobuf/archive/0b059a3d8a8f8aa40dde7bea55edca4ec5dfea66.tar.gz, http://mirror.bazel.build/github.com/google/protobuf/archive/0b059a3d8a8f8aa40dde7bea55edca4ec5dfea66.tar.gz] to /home/nvidia/.cache/bazel/_bazel_nvidia/d2751a49dacf4cb14a513ec663770624/external/protobuf/0b059a3d8a8f8aa40dde7bea55edca4ec5dfea66.tar.gz: Checksum was e5fdeee6b28cf6c38d61243adff06628baa434a22b5ebb7432d2a7fbabbdb13d but wanted 6d43b9d223ce09e5d4ce8b0060cb8a7513577a35a64c7e3dad10f0703bf3ad93 and referenced by ‘//tensorflow:libtensorflow_all.so’.

TX2和TX1上共同出現的錯誤:
ERROR: /home/nvidia/tensorflow/tensorflow/core/kernels/BUILD:2157:1: C++ compilation of rule ‘//tensorflow/core/kernels:matrix_solve_ls_op’ failed: gcc failed: error executing command /usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -B/usr/bin -B/usr/bin -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -g0 -O2 ‘-D_FORTIFY_SOURCE=1’ -DNDEBUG … (remaining 106 argument(s) skipped): com.google.devtools.build.lib.shell.BadExitStatusException: Process exited with status 4.
gcc: internal compiler error: Killed (program cc1plus)

這個錯誤搞了半天依舊沒有解決。如果有大神知道是什麼問題希望能夠留言指導下,拜謝!!!

但是此時如果cd bazel-bin/tensorflow 裡面可以看見已經生成了libtensorflow_all.so,只是標頭檔案沒有生成。
菜鳥解決不了問題只能採用最笨的方法。
將X86_64平臺下的標頭檔案(google資料夾)整個拷貝到TX2 /usr/local/include/下
將生成的.so檔案拷到/usr/local/lib下。
這樣.so檔案和.h檔案不都有了嘛!!!!!!
後來測試提取圖片特徵,依然能夠編譯通過。而且列印的特徵值也沒問題。。。。。。