'Android 12(Api Level 31): TfLiteGpuDelegate Invoke: GpuDelegate must run on the same thread where it was initialized

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?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source