TensorFlow模型迁移到Android,官方demo

xiaoxiao2025-09-26  226

 TensorFlow模型如何在移动端运行成为一种趋势,加之MobileNet等轻量级网络的产生,加速了移动端APP嵌入目标检测模型的发展趋势.最近做了tensorflow 模型在按着手机上的移植,效果不错,所以大概记录一下过程.因此本节从环境搭建-编译安装tf开始,一步步开始将tf模型部署到安卓平台,最后在安卓手机界面就能看到TF Detect, TF Classify, TF Stylize, TF Speech四个app程序,并实时进行目标检测.在最后我们将前一节训练的图表检测模型部署到安卓平台.

(本人使用ubuntu 16.04 LTS系统,GeForce GTX 1080 2块显卡,python3环境,本次安装先确保机器可以翻墙!!!!)

1,https://developer.android.com/studio/下载安卓开发工具andorid-studio.下载完后将其解压到/usr/local目录,执行sh ./bin/studio.sh启动即可.

2,在根目录执行git clone https://github.com/tensorflow/tensorflow.git下载tf

3,编译设置:打开android studion,选择Open an existing Android Studio project.或者在打开的主界面file-open选择刚刚下载的tf目录下的tensorflow/examples/android目录,然后就能打开该目录,打开build.gradle文件找到并将nativeBuildSystem变量设置为none,例如:def nativeBuildSystem = 'none',添加如下代码到build.gradle

allprojects {     repositories {         jcenter()     } }   dependencies {     compile 'org.tensorflow:tensorflow-android:+' } 意思是添加tf到安卓上的app.这样安卓上就会出现自带的四个app.在所有allprojects下的repositories中添加:maven { url "http://maven.aliyun.com/nexus/content/groups/public/" },google()

添加完成后的如:

4,编译:然后点击菜单build-rebuild开始编译运行.然后点击run-run android就行.如果报错就是一些需要的工具包没装,没事,点击界面右上角的train_again就会自己去下载安装,就是时间稍微长点,右下角报错显示没有装什么工具就点击让他自己安装就行.

5,编译完成后手机连接电脑,点击run-run-app就能看到手机链接的电脑(手机如何链接电脑不会的话可以baidu一下,很容易的)即可.

然后在手机界面就会出现4个新的app

 

 至此安装完成.下一步将把上次训练的检测模型部署到手机.

build.gradle文件配置

// This file provides basic support for building the TensorFlow demo // in Android Studio with Gradle. // // Note that Bazel is still used by default to compile the native libs, // and should be installed at the location noted below. This build file // automates the process of calling out to it and copying the compiled // libraries back into the appropriate directory. // // Alternatively, experimental support for Makefile builds is provided by // setting nativeBuildSystem below to 'makefile'. This will allow building the demo // on Windows machines, but note that full equivalence with the Bazel // build is not yet guaranteed. See comments below for caveats and tips // for speeding up the build, such as enabling ccache. // NOTE: Running a make build will cause subsequent Bazel builds to *fail* // unless the contrib/makefile/downloads/ and gen/ dirs are deleted afterwards. // The cmake build only creates libtensorflow_demo.so. In this situation, // libtensorflow_inference.so will be acquired via the tensorflow.aar dependency. // It is necessary to customize Gradle's build directory, as otherwise // it will conflict with the BUILD file used by Bazel on case-insensitive OSs. project.buildDir = 'gradleBuild' getProject().setBuildDir('gradleBuild') buildscript { repositories { google() jcenter() maven { url 'https://dl.google.com/dl/android/maven2' } } dependencies { classpath 'com.android.tools.build:gradle:3.2.1' classpath 'org.apache.httpcomponents:httpclient:4.5.4' } } allprojects { repositories { jcenter() google() maven { url "https://jitpack.io" } } } // set to 'bazel', 'cmake', 'makefile', 'none' //def nativeBuildSystem = 'bazel' def nativeBuildSystem = 'none' // Controls output directory in APK and CPU type for Bazel builds. // NOTE: Does not affect the Makefile build target API (yet), which currently // assumes armeabi-v7a. If building with make, changing this will require // editing the Makefile as well. // The CMake build has only been tested with armeabi-v7a; others may not work. def cpuType = 'armeabi-v7a' // Output directory in the local directory for packaging into the APK. def nativeOutDir = 'libs/' + cpuType // Default to building with Bazel and override with make if requested. def nativeBuildRule = 'buildNativeBazel' def demoLibPath = '../../../bazel-bin/tensorflow/examples/android/libtensorflow_demo.so' def inferenceLibPath = '../../../bazel-bin/tensorflow/contrib/android/libtensorflow_inference.so' // Override for Makefile builds. if (nativeBuildSystem == 'makefile') { nativeBuildRule = 'buildNativeMake' demoLibPath = '../../../tensorflow/contrib/makefile/gen/lib/android_' + cpuType + '/libtensorflow_demo.so' inferenceLibPath = '../../../tensorflow/contrib/makefile/gen/lib/android_' + cpuType + '/libtensorflow_inference.so' } // If building with Bazel, this is the location of the bazel binary. // NOTE: Bazel does not yet support building for Android on Windows, // so in this case the Makefile build must be used as described above. def bazelLocation = '/usr/local/bin/bazel' // import DownloadModels task project.ext.ASSET_DIR = projectDir.toString() + '/assets' project.ext.TMP_DIR = project.buildDir.toString() + '/downloads' apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion '28.0.3' if (nativeBuildSystem == 'cmake') { defaultConfig { applicationId = 'org.tensorflow.demo' minSdkVersion 21 targetSdkVersion 23 ndk { abiFilters "${cpuType}" } externalNativeBuild { cmake { arguments '-DANDROID_TOOLCHAIN=gcc', '-DANDROID_STL=gnustl_static' } } } externalNativeBuild { cmake { path './jni/CMakeLists.txt' } } } lintOptions { abortOnError false } sourceSets { main { if (nativeBuildSystem == 'bazel' || nativeBuildSystem == 'makefile') { // TensorFlow Java API sources. java { srcDir '../../java/src/main/java' exclude '**/examples/**' } // Android TensorFlow wrappers, etc. java { srcDir '../../contrib/android/java' } } // Android demo app sources. java { srcDir 'src' } manifest.srcFile 'AndroidManifest.xml' resources.srcDirs = ['src'] aidl.srcDirs = ['src'] renderscript.srcDirs = ['src'] res.srcDirs = ['res'] assets.srcDirs = [project.ext.ASSET_DIR] jniLibs.srcDirs = ['libs'] } debug.setRoot('build-types/debug') release.setRoot('build-types/release') } defaultConfig { minSdkVersion 21 } } task buildNativeBazel(type: Exec) { workingDir '../../..' commandLine bazelLocation, 'build', '-c', 'opt', \ 'tensorflow/examples/android:tensorflow_native_libs', \ '--crosstool_top=//external:android/crosstool', \ '--cpu=' + cpuType, \ '--host_crosstool_top=@bazel_tools//tools/cpp:toolchain' } task buildNativeMake(type: Exec) { environment "NDK_ROOT", android.ndkDirectory // Tip: install ccache and uncomment the following to speed up // builds significantly. // environment "CC_PREFIX", 'ccache' workingDir '../../..' commandLine 'tensorflow/contrib/makefile/build_all_android.sh', \ '-s', \ 'tensorflow/contrib/makefile/sub_makefiles/android/Makefile.in', \ '-t', \ 'libtensorflow_inference.so libtensorflow_demo.so all' \ , '-a', cpuType \ //, '-T' // Uncomment to skip protobuf and speed up subsequent builds. } task copyNativeLibs(type: Copy) { from demoLibPath from inferenceLibPath into nativeOutDir duplicatesStrategy = 'include' dependsOn nativeBuildRule fileMode 0644 } tasks.whenTaskAdded { task -> if (nativeBuildSystem == 'bazel' || nativeBuildSystem == 'makefile') { if (task.name == 'assembleDebug') { task.dependsOn 'copyNativeLibs' } if (task.name == 'assembleRelease') { task.dependsOn 'copyNativeLibs' } } } // Download default models; if you wish to use your own models then // place them in the "assets" directory and comment out this line. apply from: "download-models.gradle" dependencies { if (nativeBuildSystem == 'cmake' || nativeBuildSystem == 'none') { compile 'org.tensorflow:tensorflow-android:+' } } #Sat Nov 18 15:06:47 CET 2017 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip

grade-warpper.properties文件配置 ---------------------  作者:LH-心心  来源:  原文:https://blog.csdn.net/yjfncu/article/details/81737104  版权声明:本文为博主原创文章,转载请附上博文链接!

转载请注明原文地址: https://www.6miu.com/read-5036922.html

最新回复(0)