After upgrade my android os to version 12, my code stopped work, I use flutter plugin tflite_flutter 0.9.0 and I launch tflite in isolation.

It throws this error:

TfLiteGpuDelegate Invoke: GpuDelegate must run on the same thread where it was initialized.
E/tflite (20055): Node number 64 (TfLiteGpuDelegateV2) failed to invoke.
E/flutter (20055): [ERROR:flutter/runtime/dart_isolate.cc(1111)] Unhandled exception:
E/flutter (20055): Bad state: failed precondition
E/flutter (20055): #0 checkState (package:quiver/check.dart:74:5)
E/flutter (20055): #1 Interpreter.invoke (package:tflite_flutter/src/interpreter.dart:150:5)
E/flutter (20055): #2 Interpreter.runForMultipleInputs (package:tflite_flutter/src/interpreter.dart:190:5)
E/flutter (20055): #3 Classifier.predict (package:myai/ai/tflite/mobilenet/object/classifier.dart:179:19)
E/flutter (20055): #4 IsolateHelper.entryPoint (package:myai/ai/helper/isolate_helper.dart:51:30)

My code:

final gpuDelegateV2 = GpuDelegateV2(
options: GpuDelegateOptionsV2(
isPrecisionLossAllowed:true,
inferencePreference:TfLiteGpuInferenceUsage.preferenceSustainSpeed,
inferencePriority1:TfLiteGpuInferencePriority.minMemoryUsage,
inferencePriority2:TfLiteGpuInferencePriority.auto,
inferencePriority3:TfLiteGpuInferencePriority.auto,
)
);
var interpreterOptions = InterpreterOptions()..addDelegate(gpuDelegateV2);
_interpreter = interpreter ??
await Interpreter.fromAsset(
MODEL_FILE_NAME,
options: interpreterOptions
);

Anyone faced such problem? Any suggestions how to fix it?


Solution 1: Yonatan S

Is it possible that the error you encounter is Can not open OpenCL library on this device - dlopen failed: library "libOpenCL.so" not found? Version 12 requires modules to declare dependencies in the manifest. When targeting Sdk version S you need to add the TFLite dependencies to your manifest. Read the follow relevant comment taken from here.

Note: You can now target Android S+ with targetSdkVersion="S" in your manifest, or targetSdkVersion "S" in your Gradle defaultConfig (API level TBD). In this case, you should merge the contents of AndroidManifestGpu.xml into your Android application's manifest. Without this change, the GPU delegate cannot access OpenCL libraries for acceleration. AGP 4.2.0 or above is required for this to work.