android - 使用Gradle构建并运行tensorflow lite演示

标签 android android-studio machine-learning tensorflow-lite

所以最近根据这个,Tensorflow Lite现在支持
用于对象检测的mobilenet_ssd。太好了..
我设法用bazel构建并运行了这个演示,但最初我想用android studio来实现。可惜我做不到。
下面是我得到的错误:

Error:Plugin with id 'com.android.application' not found.

从评论中看,我并不是唯一一个对此感到困惑的人。有解决办法吗?或者目前没有gradle支持这个特定的更新?
任何能澄清这个问题的信息都是非常感谢的,因为我对人工智能世界还很陌生。

最佳答案

以下是在Bazel(方法1)和Gradle(方法2)中构建和运行以下(2018年8月22日)Tensorflow Lite Android示例的说明;
https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/lite/examples/android(分类/检测/等)
https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/lite/java/demo(分类)
如何让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(Bazel)
git clone https://github.com/tensorflow/tensorflow
cd tensorflow
可选:git checkout master/938a3b77797164db736a1006a7656326240baa59
[这些说明基于https://github.com/tensorflow/tensorflow/commit/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-b78971cf1193],bazel需要android-ndk-r14b]
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手机上运行示例应用程序(tfldetec)(search-tfldetec)
[请求时授予应用程序权限]
方法2(梯度)
git clone https://github.com/tensorflow/tensorflow
cd tensorflow
可选:git checkout master/938a3b77797164db736a1006a7656326240baa59
[这些说明基于https://github.com/tensorflow/tensorflow/commit/938a3b77797164db736a1006a7656326240baa59]
可选:从tensorflow中提取tensorflow/contrib/lite/examples/android文件夹
打开android studio项目中的tensorflow/contrib/lite/examples/android目录。
[安装它要求的所有渐变扩展。]
修改app/build.gradle;
删除(注释掉);jackOptions { enabled true }
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]

梯度同步
建造
跑步
[请求时授予应用程序权限]
在android手机上运行示例应用程序(tfldetec)(search-tfldetec)
注意运行时是否抛出错误,例如;
    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?

然后尝试下列方法之一:
重新启动手机,然后重新运行应用程序
构建-重建项目,并重新运行应用程序
[编辑:
要使可选的对象跟踪工作,需要安装libtensorflow_demo.so
假设上面的tensorflow lite android示例和gradle(方法2)指令已经完成
执行上面的tensorflow lite android示例和bazel(方法1)说明-这将安装android示例的工作版本,其中libtensorflow_demo.so
libtensorflow_demo.so现在需要从安卓设备上安装的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-v8ajniLibs/armeabi-v7ajniLibs/x86jniLibs/x86_64libtensorflow_demo.so
tensorflow/contrib/lite/examples/android放入所有子文件夹
在android studio中打开git clone https://github.com/tensorflow/tensorflow
使用Gradle重新构建
运行]
如何让tensorflow lite java演示程序运行[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(Bazel)
https://www.tensorflow.org/mobile/tflite/demo_android/https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/java/demo/README.md(未测试)
方法2(梯度)
cd tensorflow
git checkout master
可选:938a3b77797164db736a1006a7656326240baa59/androidTestCompile('androidx.test.espresso:espresso-core:3.1.0-alpha3'
[这些说明基于https://github.com/tensorflow/tensorflow/commit/938a3b77797164db736a1006a7656326240baa59]
可选:从tensorflow中提取tensorflow/contrib/lite/java/demo文件夹
打开android studio项目中的tensorflow/contrib/lite/java/demo目录。
[安装它要求的所有渐变扩展。]
编辑app-build.gradle;
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存储库并同步项目']
梯度同步
建造
跑步
[在Android手机上运行演示应用程序(tflitecamerademo)(搜索-tflitecamerademo)]
[请求时授予应用程序权限]

关于android - 使用Gradle构建并运行tensorflow lite演示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50330184/

相关文章:

android - JS代码部署在Web上

java - 应用程序在调用谷歌地图 Activity 后终止

python - 从 keras 模型中提取特征到数据集中

python - 安装 tensorflow : Python - Tensorflow Version Mismatch Warnings

android - 在android库包中包含一个外部库

android - 如何为 Arduino ADK 编译 hello world 程序?

android - 调试不适用于 Android Studio 的 C++/ native 库模块(使用 Cmake)

android-studio - 如何在Android Studio中创建库项目以及使用该库项目的应用程序项目

python - 尝试使用 sk learn 绘制线性回归模型时遇到模糊的 RuntimeWarning

android - 使 RelativeLayout 可检查