Tensorflow編譯

MacOS平臺編譯

  1. clone tensorflow repositories
git clone https://github.com/tensorflow/tensorflow.git tensorflow_src
  1. download all build dependencies
./tensorflow/lite/tools/make/download_dependencies.sh
  1. build tensorflow-lite
cmake ../tensorflow_src/tensorflow/lite
cmake --build .

libtensorflow-lite.a需要的額外庫路徑tensorflow_src/_deps/xxx-build/xxx.a

Ubuntu平臺編譯

  1. install necessary library
sudo apt-get install build-essential
sudo apt-get install zlib1g-dev
sudo apt install libgles2-mesa-dev 
  1. download all build dependencies
./tensorflow/lite/tools/make/download_dependencies.sh
  1. build tensorflow-lite
./tensorflow/lite/tools/make/build_lib.sh

Ubuntu環(huán)境編譯動態(tài)庫腳本

#!/bin/sh
set -e
#set -x

export TENSORFLOW_VER=r2.4
export TENSORFLOW_DIR=`pwd`/tensorflow_${TENSORFLOW_VER}

git clone -b ${TENSORFLOW_VER} https://github.com/tensorflow/tensorflow.git ${TENSORFLOW_DIR}

cd ${TENSORFLOW_DIR}


# install Bazel 3.1.0
wget https://github.com/bazelbuild/bazel/releases/download/3.1.0/bazel-3.1.0-installer-linux-x86_64.sh
chmod 755 bazel-3.1.0-installer-linux-x86_64.sh
sudo ./bazel-3.1.0-installer-linux-x86_64.sh

# clean up bazel cache, just in case.
bazel clean

echo "----------------------------------------------------"
echo " (configure) press ENTER-KEY several times.         "
echo "----------------------------------------------------"
./configure

# ---------------
#  Makefile build
# ---------------

# download all the build dependencies.
./tensorflow/lite/tools/make/download_dependencies.sh 2>&1 | tee -a log_download_dependencies.txt

# build TensorFlow Lite library (libtensorflow-lite.a)
./tensorflow/lite/tools/make/build_lib.sh EXTRA_CXXFLAGS="-march=native" 2>&1 | tee -a log_build_libtflite_make.txt


# ---------------
#  Bazel build
# ---------------
# build with Bazel (libtensorflowlite.so)
bazel build -s -c opt //tensorflow/lite:libtensorflowlite.so 2>&1 | tee -a log_build_libtflite_bazel.txt

# build GPU Delegate library (libdelegate.so)
bazel build -s -c opt --copt="-DMESA_EGL_NO_X11_HEADERS" --copt="-DEGL_NO_X11" tensorflow/lite/delegates/gpu:libtensorflowlite_gpu_delegate.so 2>&1 | tee -a log_build_delegate.txt

echo "----------------------------------------------------"
echo " build success."
echo "----------------------------------------------------"

cd ${TENSORFLOW_DIR}
#ls -l tensorflow/lite/tools/make/gen/linux_x86_64/lib/
ls -l bazel-bin/tensorflow/lite/
ls -l bazel-bin/tensorflow/lite/delegates/gpu/

遇到問題

virtual memory exhausted: Cannot allocate memory
解決:內(nèi)存太小,用swap擴展內(nèi)存的方法

[root@Byrd byrd]# free -m
             total       used       free     shared    buffers     cached
Mem:           512        108        403          0          0         28
-/+ buffers/cache:         79        432
Swap:            0          0          0
[root@Byrd ~]# mkdir /opt/images/
[root@Byrd ~]# rm -rf /opt/images/swap
[root@Byrd ~]# dd if=/dev/zero of=/opt/images/swap bs=1024 count=2048000
2048000+0 records in
2048000+0 records out
2097152000 bytes (2.1 GB) copied, 82.7509 s, 25.3 MB/s
[root@Byrd ~]# mkswap /opt/images/swap
mkswap: /opt/images/swap: warning: don't erase bootbits sectors
        on whole disk. Use -f to force.
Setting up swapspace version 1, size = 2047996 KiB
no label, UUID=59daeabb-d0c5-46b6-bf52-465e6b05eb0b
[root@hz mnt]# swapon /opt/images/swap
[root@hz mnt]# free -m
             total       used       free     shared    buffers     cached
Mem:           488        481          7          0          6        417
-/+ buffers/cache:         57        431
Swap:          999          0        999

使用完畢后可以關(guān)掉swap:

[root@hz mnt]# swapoff swap
[root@hz mnt]# rm -f /opt/images/swap

參考鏈接

https://tensorflow.google.cn/lite/guide/build_cmake

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容