Tf-Ops Not Working in Android Kitkat 4.4 (API 19)

240 Views Asked by At

I'm trying to deploy a yolov3 model on Android Application and found that to implement this model I have to use the TF-Ops to convert the model into tflite by adding this snippet code

converter.target_spec.supported_ops = [
   tf.lite.OpsSet.TFLITE_BUILTINS,
   tf.lite.OpsSet.SELECT_TF_OPS ]

Since the tf-ops are enabled while converting the model, so I have to use tf-select-ops dependency in my Android project, so I added the library like this

implementation 'org.tensorflow:tensorflow-lite-select-tf-ops:0.0.0-nightly'

The application working correctly on my Android 10 but it give an error in Android 4.4, here is the log

04-14 09:18:33.912 8903-8903/com.example.posm W/System.err: java.lang.IllegalArgumentException: Internal error: Failed to run on the given Interpreter: Regular TensorFlow ops are not supported by this interpreter. Make sure you apply/link the Flex delegate before inference.
04-14 09:18:33.912 8903-8903/com.example.posm W/System.err: Node number 65 (FlexSize) failed to prepare.
04-14 09:18:33.912 8903-8903/com.example.posm W/System.err: Regular TensorFlow ops are not supported by this interpreter. Make sure you apply/link the Flex delegate before inference.
04-14 09:18:33.912 8903-8903/com.example.posm W/System.err: Node number 65 (FlexSize) failed to prepare.
04-14 09:18:33.912 8903-8903/com.example.posm W/System.err: Regular TensorFlow ops are not supported by this interpreter. Make sure you apply/link the Flex delegate before infer
04-14 09:18:33.942 8903-8903/com.example.posm W/System.err:     at org.tensorflow.lite.NativeInterpreterWrapper.run(Native Method)
04-14 09:18:33.942 8903-8903/com.example.posm W/System.err:     at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:204)
04-14 09:18:33.942 8903-8903/com.example.posm W/System.err:     at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:378)
04-14 09:18:33.942 8903-8903/com.example.posm W/System.err:     at com.example.posm.yolo.Yolo.predict(Yolo.java:71)
04-14 09:18:33.942 8903-8903/com.example.posm W/System.err:     at com.example.posm.POSM.predict(POSM.java:45)
04-14 09:18:33.942 8903-8903/com.example.posm W/System.err:     at com.example.posm.MainActivity.run_posm(MainActivity.java:110)
04-14 09:18:33.942 8903-8903/com.example.posm W/System.err:     at com.example.posm.MainActivity.lambda$onCreate$1$MainActivity(MainActivity.java:75)
04-14 09:18:33.942 8903-8903/com.example.posm W/System.err:     at com.example.posm.-$$Lambda$MainActivity$w2a9VvlIKLLPHcyHZBUHuFlJhQA.onClick(lambda)
04-14 09:18:33.942 8903-8903/com.example.posm W/System.err:     at android.view.View.performClick(View.java:4640)
04-14 09:18:33.942 8903-8903/com.example.posm W/System.err:     at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
04-14 09:18:33.952 8903-8903/com.example.posm W/System.err:     at android.view.View$PerformClick.run(View.java:19431)
04-14 09:18:33.952 8903-8903/com.example.posm W/System.err:     at android.os.Handler.handleCallback(Handler.java:733)
04-14 09:18:33.952 8903-8903/com.example.posm W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
04-14 09:18:33.952 8903-8903/com.example.posm W/System.err:     at android.os.Looper.loop(Looper.java:146)
04-14 09:18:33.952 8903-8903/com.example.posm W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5598)
04-14 09:18:33.952 8903-8903/com.example.posm W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
04-14 09:18:33.952 8903-8903/com.example.posm W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515)
04-14 09:18:33.952 8903-8903/com.example.posm W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
04-14 09:18:33.952 8903-8903/com.example.posm W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
04-14 09:18:33.952 8903-8903/com.example.posm W/System.err:     at dalvik.system.NativeStart.main(Native Method)
04-14 09:18:33.952 8903-8903/com.example.posm E/ERROR: Exception java.lang.IllegalArgumentException: Internal error: Failed to run on the given Interpreter: Regular TensorFlow ops are not supported by this interpreter. Make sure you apply/link the Flex delegate before inference.
    Node number 65 (FlexSize) failed to prepare.
    
    Regular TensorFlow ops are not supported by this interpreter. Make sure you apply/link the Flex delegate before inference.
    Node number 65 (FlexSize) failed to prepare.
    
    Regular TensorFlow ops are not supported by this interpreter. Make sure you apply/link the Flex delegate before infer

I don't really know why this happened to my Android 4.4. Based on my experience, this happened when I run the Android project without tf-select-ops dependency, then after adding the tf-select-ops dependency in my project the application working correctly in my Android 10. Is it a compatibility issue with older android APIs?

1

There are 1 best solutions below

0
Jae sung Chung On

Please make sure that the following method invocation should be done before the TFLite interpreter runs the tf-select-ops enabled model.

FlexDelegate.initTensorFlowForTesting();