user 007
user 007

Reputation: 911

build and run tensorflow lite demo with gradle

so recently according to this comment tensorflow lite now supports the mobilenet_ssd for object detection. Which is great.. I managed to build and run the demo with bazel but originaly I wanted to do that with Android Studio. Unfortunately I couldn't do it.

Here is the error that I'm getting :

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

Reading through the comments it seems I'm not the only one confused about this. Is there a solution for this ? or there is no gradle support at the moment for this particular update ?

Any information that could clarify this problem is much appreciated since I'm still new to the AI world.

Upvotes: 2

Views: 3496

Answers (1)

user2585501
user2585501

Reputation: 606

Here are instructions for building and running the following (22 Aug 2018) TensorFlow Lite Android examples in both Bazel (Method 1) and Gradle (Method 2);


How to get the TensorFlow Lite Android examples to run [tensorflow/tensorflow/contrib/lite/examples/android];

(e.g. object detection/ssd models; detect.tflite[/mobilenet_ssd.tflite]/coco_labels_list.txt)

Instructions based on; https://medium.com/tensorflow/training-and-serving-a-realtime-mobile-object-detector-in-30-minutes-with-cloud-tpus-b78971cf1193

Method 1 (Bazel)

  • git clone https://github.com/tensorflow/tensorflow
  • cd tensorflow
  • OPTIONAL: git checkout master / 938a3b77797164db736a1006a7656326240baa59
  • gedit WORKSPACE, and add references to android_sdk_repository and 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)
    
  • [This prevents the following error:

    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)]
    
  • [Note android-ndk-r14b is required for Bazel according to https://medium.com/tensorflow/training-and-serving-a-realtime-mobile-object-detector-in-30-minutes-with-cloud-tpus-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
  • Run example app (tflDetect) on Android phone (search - tflDetect)
  • [give permissions to app when requested]

Method 2 (Gradle)

  • git clone https://github.com/tensorflow/tensorflow
  • cd tensorflow
  • OPTIONAL: git checkout master / 938a3b77797164db736a1006a7656326240baa59
  • OPTIONAL: extract tensorflow/contrib/lite/examples/android folder from tensorflow
  • Open the tensorflow/contrib/lite/examples/android directory in Android Studio project.
  • [Install all the Gradle extensions it requests.]
  • modify app/build.gradle;

    • remove (comment out) this; jackOptions { enabled true }
    • change compile 'org.tensorflow:tensorflow-lite:0.0.0-nightly' to compile 'org.tensorflow:tensorflow-lite:1.10.0' [latest working revision] (or compile 'org.tensorflow:tensorflow-lite:+'
  • [This prevents the following error:

    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]
    
  • Gradle Sync

  • Build
  • Run
  • [give permissions to app when requested]
  • Run example app (tflDetect) on Android phone (search - tflDetect)

Note if an error is thrown upon run, e.g.;

    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?

Then try one of the following;

  • Restart the phone, and re-run the application
  • Build - Rebuild project, and re-run the application

[EDIT: To get optional object tracking to work, libtensorflow_demo.so is required to be installed;

  • Assume the TensorFlow Lite Android examples with Gradle (Method 2) instructions above have already been completed
  • Execute the TensorFlow Lite Android examples with Bazel (Method 1) instructions above - this will install a working version of the Android examples with libtensorflow_demo.so
  • libtensorflow_demo.so now needs to be extracted from the installed apk on the Android device
  • Open Android studio - View - Tool Windows - Device File Explorer
  • Ensure Android device is selected
  • /data/app/org.tensorflow.lite.demo/lib/arm - Right click libtensorflow_demo.so - Save as - save to temporary folder on hard drive
  • create folder tensorflow/contrib/lite/examples/android/app/src/main/jniLibs
  • create 4 subfolders (jniLibs/arm64-v8a, jniLibs/armeabi-v7a, jniLibs/x86, jniLibs/x86_64)
  • place libtensorflow_demo.so in all subfolders
  • Open tensorflow/contrib/lite/examples/android in Android Studio
  • Build again using Gradle
  • Run ]

How to get the TensorFlow Lite Java Demo to run [tensorflow/tensorflow/contrib/lite/java/demo]

(e.g. classification models; mobilenet_quant_v1_224.tflite/labels_mobilenet_quant_v1_224.txt)

Instructions based on; https://www.tensorflow.org/mobile/tflite/demo_android

Method 1 (Bazel)

See https://www.tensorflow.org/mobile/tflite/demo_android / https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/java/demo/README.md (not tested)

Method 2 (Gradle)

  • git clone https://github.com/tensorflow/tensorflow
  • cd tensorflow
  • OPTIONAL: git checkout master / 938a3b77797164db736a1006a7656326240baa59
  • OPTIONAL: extract tensorflow/contrib/lite/java/demo folder from tensorflow
  • Open the tensorflow/contrib/lite/java/demo directory in Android Studio project.
  • [Install all the Gradle extensions it requests.]
  • edit app - build.gradle;
    • change androidTestCompile('androidx.test.espresso:espresso-core:3.1.0-alpha3' to androidTestCompile('com.android.support.test.espresso:espresso-core:3.0.2'
    • change compile 'org.tensorflow:tensorflow-lite:0.0.0-nightly' to compile 'org.tensorflow:tensorflow-lite:1.10.0' [latest working revision] (or compile 'org.tensorflow:tensorflow-lite:+'
  • [This prevents the following error:

    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]
    
  • [When requested select; 'add Maven repository and sync project']

  • Gradle Sync
  • Build
  • Run
  • [Run demo app (tflitecamerademo) on Android phone (search - tflitecamerademo)]
  • [give permissions to app when requested]

Upvotes: 2

Related Questions