以下是在 Bazel(方法 1)和 Gradle(方法 2)中构建和运行以下(2018 年 8 月 22 日)TensorFlow Lite Android 示例的说明;
如何让 TensorFlow Lite Android 示例运行 [tensorflow/tensorflow/contrib/lite/examples/android];
(例如对象检测/ssd 模型;detect.tflite[/mobilenet_ssd.tflite]/coco_labels_list.txt)
指令基于;https://medium.com/tensorflow/training-and-serving-a-realtime-mobile-object-detector-in-30-minutes-with-cloud-tpus-b78971cf1193
方法1(巴泽尔)
git clone https://github.com/tensorflow/tensorflow
cd tensorflow
- 可选:
git checkout master
/938a3b77797164db736a1006a7656326240baa59
gedit WORKSPACE
,并添加对android_sdk_repository和android_ndk_repository的引用;
android_sdk_repository(
name = "androidsdk",
api_level = 28,
build_tools_version = "28.0.1",
# Replace with path to Android SDK on your system
path = "/[INSERTCORRECTPATHHERE]/android-sdk-linux",
)
android_ndk_repository(
name="androidndk",
path="/[INSERTCORRECTPATHHERE]/android-ndk-r14b",
api_level=28)
[这可以防止以下错误:
ERROR: /.../tensorflow/contrib/lite/kernels/internal/BUILD:620:1: no such package '@androidndk//': The repository could not be resolved and referenced by '//tensorflow/contrib/lite/kernels/internal:cpu_check'
ERROR: Analysis of target '//tensorflow/contrib/lite/examples/android:tflite_demo' failed; build aborted: Analysis failed
FAILED: Build did NOT complete successfully (60 packages loaded)]
[注意根据https://medium.com/tensorflow/training-and-serving-a-realtime-mobile-object-detector-in-30-minutes-with-cloud-tpus,Bazel需要 android-ndk-r14b -b78971cf1193 ]
bazel build -c opt --config=android_arm --cxxopt='--std=c++11' //tensorflow/contrib/lite/examples/android:tflite_demo
adb install bazel-bin/tensorflow/contrib/lite/examples/android/tflite_demo.apk
- 在 Android 手机上运行示例应用程序 (tflDetect) (search - tflDetect)
- [在请求时授予应用程序权限]
方法 2(梯度)
注意是否在运行时抛出错误,例如;
Unknown failure (at android.os.Binder.execTransact(Binder.java:573))
Error while Installing APKs
...
Installation failed with message Invalid File: /.../app/build/intermediates/split-apk/debug/slices/slice_5.apk.
It is possible that this issue is resolved by uninstalling an existing version of the apk if it is present, and then re-installing.
WARNING: Uninstalling will remove the application data!
Do you want to uninstall the existing application?
然后尝试以下方法之一;
- 重启手机,重新运行应用程序
- Build - 重建项目,并重新运行应用程序
[编辑:要使可选的对象跟踪工作,libtensorflow_demo.so
需要安装;
- 假设上面的带有 Gradle(方法 2)说明的 TensorFlow Lite Android 示例已经完成
- 使用上面的 Bazel(方法 1)说明执行 TensorFlow Lite Android 示例 - 这将安装 Android 示例的工作版本
libtensorflow_demo.so
libtensorflow_demo.so
现在需要从Android设备上安装的apk中提取
- 打开 Android studio - 查看 - 工具窗口 - 设备文件资源管理器
- 确保选择 Android 设备
/data/app/org.tensorflow.lite.demo/lib/arm
- 右键单击libtensorflow_demo.so
- 另存为 - 保存到硬盘上的临时文件夹
- 创建文件夹
tensorflow/contrib/lite/examples/android/app/src/main/jniLibs
- 创建 4 个子文件夹 (
jniLibs/arm64-v8a
, jniLibs/armeabi-v7a
, jniLibs/x86
, jniLibs/x86_64
)
- 放在
libtensorflow_demo.so
所有子文件夹中
tensorflow/contrib/lite/examples/android
在 Android Studio 中打开
- 使用 Gradle 再次构建
- 跑 ]
如何让 TensorFlow Lite Java Demo 运行 [tensorflow/tensorflow/contrib/lite/java/demo]
(例如分类模型;mobilenet_quant_v1_224.tflite/labels_mobilenet_quant_v1_224.txt)
指令基于;https://www.tensorflow.org/mobile/tflite/demo_android
方法1(巴泽尔)
见https://www.tensorflow.org/mobile/tflite/demo_android / https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/java/demo/README.md(未测试)
方法 2(梯度)
git clone https://github.com/tensorflow/tensorflow
cd tensorflow
- 可选:
git checkout master
/938a3b77797164db736a1006a7656326240baa59
- 可选:从 tensorflow 中提取 tensorflow/contrib/lite/java/demo 文件夹
- 在 Android Studio 项目中打开 tensorflow/contrib/lite/java/demo 目录。
- [安装它请求的所有 Gradle 扩展。]
- 编辑应用程序 - build.gradle;
- 更改
androidTestCompile('androidx.test.espresso:espresso-core:3.1.0-alpha3'
为androidTestCompile('com.android.support.test.espresso:espresso-core:3.0.2'
- 更改
compile 'org.tensorflow:tensorflow-lite:0.0.0-nightly'
为compile 'org.tensorflow:tensorflow-lite:1.10.0'
[最新工作修订版](或compile 'org.tensorflow:tensorflow-lite:+'
[这可以防止以下错误:
08-22 05:03:19.470 24480-24480/org.tensorflow.lite.demo W/System.err: TensorFlowLite: failed to load native library: dlopen failed: cannot locate symbol "__android_log_vprint" referenced by "/data/app/org.tensorflow.lite.demo-2/lib/arm/libtensorflowlite_jni.so"...
08-22 02:48:55.728 29643-29643/org.tensorflow.lite.demo E/art: No implementation found for long org.tensorflow.lite.NativeInterpreterWrapper]
[当要求选择; '添加 Maven 存储库并同步项目']
- Gradle 同步
- 建造
- 跑
- [在Android手机上运行演示应用程序(tflitecamerademo)(搜索-tflitecamerademo)]
- [在请求时授予应用程序权限]