Search code examples
androidtensorflowbuild.gradletensorflow-liteaar

tensorflow-lite-select-tf-ops and tensorflow-lite AAR built from source tflite model cannot find org.tensorflow.lite.Interpreter


I followed tensorflow android guide to generate tensorflow-lite.aar and tensorflow-lite-select-tf-ops.aar from source.

https://www.tensorflow.org/lite/guide/build_android

My current app works with,

dependencies {
    ...
    implementation 'org.tensorflow:tensorflow-lite:2.3.0'
    implementation 'org.tensorflow:tensorflow-lite-gpu:2.3.0'
    implementation 'org.tensorflow:tensorflow-lite-select-tf-ops:2.3.0'
}

I am looking to reduce size of tensorflow-lite-select-tf-ops.aar to decrease app apk size.

I am following build_aar.sh process to reduce size with my tflite model. The command used,

bash tensorflow/lite/tools/build_aar.sh --input_models=custom_model.tflite --target_archs=arm64-v8a

This generates tensorflow-lite.aar and tensorflow-lite-select-tf-ops.aar in bazel-bin/tmp folder for custom_model.tflite. The tflite model is FP16 quantized.

Implementation with newly generated aar files,

dependencies {
    ...
//    implementation 'org.tensorflow:tensorflow-lite-select-tf-ops:2.3.0'
//    implementation 'org.tensorflow:tensorflow-lite:2.3.0'
    implementation 'org.tensorflow:tensorflow-lite-gpu:2.3.0'

    implementation files('libs/tensorflow-lite.aar')
//    implementation files('libs/tensorflow-lite-gpu.aar')
    implementation files('libs/tensorflow-lite-select-tf-ops.aar')
}

But when they are included in android app compilation error occurs.

C:\folder\myapp\app\src\main\java\com\example\mapp\tflite\YoloV4Classifier.java:19: error: cannot find symbol
import org.tensorflow.lite.Interpreter;
                          ^
  symbol:   class Interpreter
  location: package org.tensorflow.lite

This problem goes away when tensorflow-lite.aar built with bazel build -c opt --fat_apk_cpu=arm64-v8a --host_crosstool_top=@bazel_tools//tools/cpp:toolchain tensorflow/lite/java:tensorflow-lite. But that introduces UnsatisfiedLinkError: dlopen failed: cannot locate symbol with previous tensorflow-lite-select-tf-ops.aar and newly built tensorflow-lite.aar with above command.

If I put both previous tensorflow-lite.aar generated from custom_model.tflite and new tensorflow-lite.aar generated with bazel build -c opt --fat_apk_cpu=arm64-v8a --host_crosstool_top=@bazel_tools//tools/cpp:toolchain tensorflow/lite/java:tensorflow-lite then there is build error,

More than one file was found with OS independent path 'lib/arm64-v8a/libtensorflowlite_jni.so'

I followed non input tflite model based command to generate tensorflow-lite.aar, tensorflow-lite-gpu.aar, tensorflow-lite-select-tf-ops.aar from source, but app size becomes very large. I have tried other ways but each one brings up a new type of error.


Solution

  • The issues were fixed as mentioned below,

    https://github.com/tensorflow/tensorflow/issues/45153

    https://github.com/tensorflow/tensorflow/issues/46989

    https://github.com/tensorflow/tensorflow/issues/45488