美文网首页
Tensorflow编译

Tensorflow编译

作者: samychen | 来源:发表于2023-06-21 09:40 被阅读0次

    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环境编译动态库脚本

    #!/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
    解决:内存太小,用swap扩展内存的方法

    [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
    

    使用完毕后可以关掉swap:

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

    参考链接

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

    相关文章

      网友评论

          本文标题:Tensorflow编译

          本文链接:https://www.haomeiwen.com/subject/yikyedtx.html