larq/compute-engine

Failed import 'org.tensorflow.lite.DataType' on Android project

amkmpro opened this issue · 8 comments

Hello, thank you for excellent framework.
I want to run LCE on Android. I do those steps from tutorial (this), but after add below code:
implementation ('org.tensorflow:tensorflow-lite-support:0.1.0') { exclude module: 'tensorflow-lite' }
classifier.java (under lib_support ) doesn't import 'org.tensorflow.lite.DataType'.
what do I do?
thanks in advance.

I could not reproduce this issue. I took a fresh clone of the TensorFlow examples repository, then followed the steps up to the point where this exclude is added. Then I tried to build the project and I don't see any issues when I run Make Project. My git diff at this point looks like this:

diff --git a/lite/examples/image_classification/android/build.gradle b/lite/examples/image_classification/android/build.gradle
index c0602307..5ca4a13c 100644
--- a/lite/examples/image_classification/android/build.gradle
+++ b/lite/examples/image_classification/android/build.gradle
@@ -18,6 +18,7 @@ allprojects {
     repositories {
         google()
         mavenCentral()
+        mavenLocal()
         maven {
             name 'ossrh-snapshot'
             url 'https://oss.sonatype.org/content/repositories/snapshots'
diff --git a/lite/examples/image_classification/android/lib_support/build.gradle b/lite/examples/image_classification/android/lib_support/build.gradle
index 87cb34ad..3d27b5d0 100644
--- a/lite/examples/image_classification/android/lib_support/build.gradle
+++ b/lite/examples/image_classification/android/lib_support/build.gradle
@@ -38,11 +38,14 @@ dependencies {
     implementation project(":models")
     implementation 'androidx.appcompat:appcompat:1.1.0'
 
+    implementation 'org.larq:lce-lite:0.1.000'
     // Build off of nightly TensorFlow Lite
-    implementation 'org.tensorflow:tensorflow-lite:0.0.0-nightly-SNAPSHOT'
+    // implementation 'org.tensorflow:tensorflow-lite:0.0.0-nightly-SNAPSHOT'
     implementation 'org.tensorflow:tensorflow-lite-gpu:0.0.0-nightly-SNAPSHOT'
 
-    implementation 'org.tensorflow:tensorflow-lite-support:0.3.0'
+    implementation ('org.tensorflow:tensorflow-lite-support:0.3.0') {
+        exclude module: 'tensorflow-lite'
+    }
 
     // Use local TensorFlow library
     // implementation 'org.tensorflow:tensorflow-lite-local:0.0.0'

When exactly do you see this message, during the project build or at runtime?

Thank you for replay.
I founded that I used org.tensorflow:tensorflow-lite-support:0.1.0 and there isn't org.tensorflow.lite.DataType in version: 0.1.0, hence I changed it to org.tensorflow:tensorflow-lite-support:0.3.0 and I run Make Project without error. When I run it on device, it closed rapidly.
May you run it?

Indeed, for me it also crashes. Did you do all the next steps in the tutorial as well (specifying the model, adding it to the assets folder, etc.)? And what error did you get (you can see it in the logcat tab)? For me it errors with:

java.lang.NoSuchMethodError: No virtual method getNumThreads()I in class Lorg/tensorflow/lite/InterpreterImpl$Options; or its super classes (declaration of 'org.tensorflow.lite.InterpreterImpl$Options' appears in /data/app/~~3FKaP-HEhZueK6nrT2DUBQ==/org.tensorflow.lite.examples.classification-09a2my7j7KeKrfH4KmjP9Q==/base.apk)

I could resolve this issue by using an older version of the LCE package (0.6.2 instead of 0.7.0) and by changing org.tensorflow:tensorflow-lite-support:0.3.0 back to org.tensorflow:tensorflow-lite-support:0.1.0.

This is an acceptable work-around I think, but we need to fix this properly for the long-term. I guess the LCE .aar release script needs some modifications to be compatible with the 0.3.0 version of tensorflow-lite-support. I'll update here when that is solved, however that will not be on our high-priority list.

Unfortunately, I have same error. But your solution doesn't work for me, because I mentioned before, when I use org.tensorflow:tensorflow-lite-support:0.1.0 in classifier.java (under lib_support ) doesn't import 'org.tensorflow.lite.DataType'.

If you still get that some error, you most likely didn't downgrade the LCE package properly. As I said above, you'll need an older version of the LCE package (0.6.2 instead of 0.7.0). Then re-run the mvn install command (I gave it a new version number and also updated that in the gradle file, but not sure if that's needed).

Thank you again for your reply.
I used org.tensorflow:tensorflow-lite-support:0.3.0 and LCE package 0.6.2. But, there are many duplication classes when I run Make Project.

Duplicate class org.tensorflow.lite.DataType found in modules jetified-lce-lite-v0.6.2-runtime (lce-lite-v0.6.2.aar) and jetified-tensorflow-lite-api-2.7.0-runtime (org.tensorflow:tensorflow-lite-api:2.7.0)
Duplicate class org.tensorflow.lite.DataType$1 found in modules jetified-lce-lite-v0.6.2-runtime (lce-lite-v0.6.2.aar) and jetified-tensorflow-lite-api-2.7.0-runtime (org.tensorflow:tensorflow-lite-api:2.7.0)
Duplicate class org.tensorflow.lite.Delegate found in modules jetified-lce-lite-v0.6.2-runtime (lce-lite-v0.6.2.aar) and jetified-tensorflow-lite-api-2.7.0-runtime (org.tensorflow:tensorflow-lite-api:2.7.0)
Duplicate class org.tensorflow.lite.Tensor found in modules jetified-lce-lite-v0.6.2-runtime (lce-lite-v0.6.2.aar) and jetified-tensorflow-lite-api-2.7.0-runtime (org.tensorflow:tensorflow-lite-api:2.7.0)
Duplicate class org.tensorflow.lite.Tensor$QuantizationParams found in modules jetified-lce-lite-v0.6.2-runtime (lce-lite-v0.6.2.aar) and jetified-tensorflow-lite-api-2.7.0-runtime (org.tensorflow:tensorflow-lite-api:2.7.0)
Duplicate class org.tensorflow.lite.annotations.UsedByReflection found in modules jetified-lce-lite-v0.6.2-runtime (lce-lite-v0.6.2.aar) and jetified-tensorflow-lite-api-2.7.0-runtime (org.tensorflow:tensorflow-lite-api:2.7.0)

You'll have to make both changes, not one of the two:

I could resolve this issue by using an older version of the LCE package (0.6.2 instead of 0.7.0) and by changing org.tensorflow:tensorflow-lite-support:0.3.0 back to org.tensorflow:tensorflow-lite-support:0.1.0.

Or at least, that's what I did to make it work.

Thank you, it ran finally.

org.tensorflow:tensorflow-lite-support:0.1.0
LCE package:0.6.2
Gradle:4.2.0