美文网首页
编译Android/IOS版本的tensorflow-lite

编译Android/IOS版本的tensorflow-lite

作者: 晓函 | 来源:发表于2023-04-11 11:21 被阅读0次

    我的环境:
    mac intel_x84_64,
    osx 13.3
    python3.8.2
    xcode14.3(最后切换到了13.3)
    android sdk 30.0.3
    android ndk 21
    tensorflow 2.12源码
    bazel 5.3.0

    配置环境

    编译配置

    这一步的目的是为了执行.configure文件,调用bazel编译ios版tflite framwork

    首先安装android sdk这个大家都知道了,
    然后还要下载ndk,
    手动下载:https://github.com/android/ndk/wiki/Unsupported-Downloads
    【推荐】进入Android Studio - SDK Manage自动下载

    image.png

    我就是手动下载ndk,编译一直失败,自动下载ndk就成功了。

    我把sdk放到了/Users/coorell/Develop/Android/sdk
    把ndk都放到了/Users/coorell/Develop/Android/sdk/ndk/r20b

    下载源码并配置

    https://github.com/tensorflow/tensorflow
    下载源码到本地,cd到该目录,并配置

    ./configure
    

    配置过程会自动安装bazel5.3.0,如果需要手动安装,请看https://www.jianshu.com/p/1a30a2549539

    开始配置,输入回车和y/n等

    You have bazel 5.3.0 installed.
    Please specify the location of python. [Default is /Users/coorell/.pyenv/versions/3.8.2/bin/python3]:直接回车,表示默认
    
    
    Found possible Python library paths:
      /Users/coorell/.pyenv/versions/3.8.2/lib/python3.8/site-packages
    Please input the desired Python library path to use.  Default is [/Users/coorell/.pyenv/versions/3.8.2/lib/python3.8/site-packages]:直接回车,表示默认
    
    Do you wish to build TensorFlow with ROCm support? [y/N]: n
    No ROCm 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 download a fresh release of clang? (Experimental) [y/N]: n
    Clang will not be downloaded.
    
    Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -Wno-sign-compare]:直接回车,表示默认
    
    
    Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: y
    Searching for NDK and SDK installations.
    
    Please specify the home path of the Android NDK to use. [Default is /Users/coorell/library/Android/Sdk/ndk-bundle]: /Users/coorell/Develop/Android/ndk/r20b
    
    
    Please specify the (min) Android NDK API level to use. [Available levels: ['16', '17', '18', '19', '21', '22', '23', '24', '26', '27', '28', '29']] [Default is 26]: 21
    
    
    Please specify the home path of the Android SDK to use. [Default is /Users/coorell/library/Android/Sdk]: /Users/coorell/Develop/Android/sdk
    
    
    Please specify the Android SDK API level to use. [Available levels: ['28', '28-2', '29', '30', '31']] [Default is 31]: 30
    
    
    Please specify an Android build tools version to use. [Available versions: ['28.0.3', '29.0.2', '31.0.0']] [Default is 31.0.0]: 30.0.2
    
    
    Do you wish to build TensorFlow with iOS support? [y/N]: y
    iOS support will be enabled for TensorFlow.
    
    

    编译尝试

    bazel build -c opt --fat_apk_cpu=x86,x86_64,arm64-v8a,armeabi-v7a \
      --host_crosstool_top=@bazel_tools//tools/cpp:toolchain \
      //tensorflow/lite/java:tensorflow-lite
    
    编译过程
    编译完成

    如果编译失败可以重启一下电脑试试.
    编译成功输出 aar 路径 :
    tensorflow源码/bazel-bin/tensorflow/lite/java/tensorflow-lite.aar

    编译成功,则表示bazel完全安装好了,接下来具体看配置android和ios

    ====Android====

    安卓直接使用tflite会出现如下提示

    [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Invalid argument(s): Failed to load dynamic library 'libtensorflowlite_c.so': dlopen failed: library "libtensorflowlite_c.so" not found
    
    

    我们只需要一个脚本,就可以自动下载so依赖文件到项目目录<projectdirectory>/android/app/src/main/jniLibs下

    #!/usr/bin/env bash
    
    cd "$(dirname "$(readlink -f "$0")")"
    
    # Available versions
    # 2.5, 2.4.1
    
    TF_VERSION=2.5
    URL="https://github.com/am15h/tflite_flutter_plugin/releases/download/"
    TAG="tf_$TF_VERSION"
    
    #download to <projectdirectory>/android/app/src/main/jniLibs
    ANDROID_DIR="android/app/src/main/jniLibs/"
    ANDROID_LIB="libtensorflowlite_c.so"
    
    ARM_DELEGATE="libtensorflowlite_c_arm_delegate.so"
    ARM_64_DELEGATE="libtensorflowlite_c_arm64_delegate.so"
    ARM="libtensorflowlite_c_arm.so"
    ARM_64="libtensorflowlite_c_arm64.so"
    X86="libtensorflowlite_c_x86_delegate.so"
    X86_64="libtensorflowlite_c_x86_64_delegate.so"
    
    delegate=0
    
    while getopts "d" OPTION
    do
        case $OPTION in
            d)  delegate=1;;
        esac
    done
    
    
    download () {
        wget "${URL}${TAG}/$1" -O "$1"
        mkdir -p "${ANDROID_DIR}$2/"
        mv $1 "${ANDROID_DIR}$2/${ANDROID_LIB}"
    }
    
    if [ ${delegate} -eq 1 ]
    then
    
    download ${ARM_DELEGATE} "armeabi-v7a"
    download ${ARM_64_DELEGATE} "arm64-v8a"
    
    else
    
    download ${ARM} "armeabi-v7a"
    download ${ARM_64} "arm64-v8a"
    
    fi
    
    download ${X86} "x86"
    download ${X86_64} "x86_64"
    

    把这上面的代码放到项目根目录,install.sh文件,打开命令行窗口,运行

    sh install.sh
    
    sh install.sh结果 自动下载完成

    安卓好简单吧,自动化脚本,这样app就可以直接运行tflite推理拉。

    ====IOS====

    配置环境

    如果没安装过xcode,可以

    xcode-select --install
    sudo xcodebuild -license accept
    

    我以前已经安装过了,并且更新系统后升级到了xcode14.3,

    不过编译了n多次,都提示错误

    ERROR: /private/var/tmp/_bazel_thao/26d40dc75f2c247e7283b353a9ab184f/external/local_config_cc/BUILD:48:19: in cc_toolchain_suite rule @local_config_cc//:toolchain: cc_toolchain_suite '@local_config_cc//:toolchain' does not contain a toolchain for cpu 'ios_arm64'
    
    toolchain error

    折腾了3天,最后用xcodes安装了旧版本xcode13.3才安装成功

    #安装xcodes,管理多版本xcode
    brew install --cask xcodes
    
    image.png
    #查看当前默认xcode版本
    xcode-select -p
    #切换13.3.1版本
    sudo xcode-select -s /Applications/Xcode-13.3.1.app/Contents/Developer
    
    xcode-select

    编译:

    bazel build --config=ios_fat -c opt --cxxopt=--std=c++17 \
      //tensorflow/lite/ios:TensorFlowLiteC_framework
    

    如果是给ios模拟器使用,则需要根据你mac的cpu来,我的是intel x86_64的,用以下命令

    bazel build --cpu=x86_64 -c opt --cxxopt=--std=c++17 \
      //tensorflow/lite/ios:TensorFlowLiteC_framework
    
    终于编译成功

    生成的文件在
    tensorflow源码/bazel-bin/tensorflow/lite/ios/TensorFlowLiteC_framework.zip

    回顾

    折腾了3天,编译成功后的结果里,又提示默认选择了xcode14.3

    /bazel_tools/tools/osx/xcode_configure.bzl:243:14: No default Xcode version is set with 'xcode-select'; picking ':version14_3_0_14E222b'
    

    难道是因为xcode是升级过来的,某些东西安装不完整,安装13.3的时候全部都重新安装上了?换成14.3是否可以?答案是no,我通过xcode-select -p切换到14.3版本,编译还是会失败,提示之前的错误'@local_config_cc//:toolchain' does not contain a toolchain for cpu 'ios_arm64'

    如果失败,需要清理一下,重新配置环境后再尝试

    bazel clean --expunge
    

    效果

    image.png

    相关文章

      网友评论

          本文标题:编译Android/IOS版本的tensorflow-lite

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