flutter-tflite
flutter-tflite copied to clipboard
IsolateInterpreter late initialization error.
I see there is no documentation provided for IsolateInterpreter
, which is very much helpful while performing computationally intensive operations. I tried IsolateInterpreter
in my application based on the syle transfer example.
I declared the interpreters like this ,
late Interpreter diffusionInterpreter;
late IsolateInterpreter dif;
and then initialized the interpreter using fromFile method
final difFile = await getFile(Configs.diffusionModelName);
final InterpreterOptions options = InterpreterOptions();
options.threads = 4;
diffusionInterpreter = Interpreter.fromFile(
difFile,
options: options,
);
dif = IsolateInterpreter(address: diffusionInterpreter.address);
and then running inference as
await dif. dif.runForMultipleInputs(
inputs
{
0: output,
},
);
But I am getting late Initialization error with this code for the IsolateInterpreter,
Unhandled Exception: LateInitializationError: Field '_sendPort@1656391573' has not been initialized.
I am able to solve this issue by adding a 1 second wait explicitly in the code after the initialization as below
await Future.delayed(const Duration(seconds: 1));
So why Interpreter.fromFile
or IsolateInterpreter(address: )
are not async methods? Which i assume would solve this issue,
note that the model trying to load is of size 800 mb.
You're right. The IsolateInterpreter constructor invokes the async method _init for initializing communication mechanisms. It might be more effective to employ an alternate method for initialization rather than relying on the basic constructor.
The issue persist it seems, will it be due to
diffusionInterpreter = Interpreter.fromFile(
difFile,
options: options,
);
I'm getting the same error :
E/flutter ( 5904): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: LateInitializationError: Field '_sendPort@523391573' has not been initialized.
when calling runForMultipleInputs
tfl.Interpreter interpreter =
await tfl.Interpreter.fromAsset('assets/models/yamnet.tflite');
final isolateInterpreter =
await tfl.IsolateInterpreter.create(address: interpreter.address);
this is my setup before calling it.
I'm getting the same error :
E/flutter ( 5904): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: LateInitializationError: Field '_sendPort@523391573' has not been initialized.
when calling runForMultipleInputs
tfl.Interpreter interpreter = await tfl.Interpreter.fromAsset('assets/models/yamnet.tflite'); final isolateInterpreter = await tfl.IsolateInterpreter.create(address: interpreter.address);
this is my setup before calling it.
Well, if I insert a delay before creating the isolate interpreter, it works:
Future<List<double>?> _getEmbeddingsFromFace(
imglib.Image convertedImage, Face face) async {
List? input = await _preProcess(convertedImage, face);
if (input == null) return null;
input = input.reshape([1, 112, 112, 3]);
List output = List.generate(1, (index) => List.filled(192, 0));
final isolateInterpreter =
await IsolateInterpreter.create(address: _interpreter!.address);
await Future.delayed(const Duration(seconds: 1));
await isolateInterpreter.run(input, output);
final result = output.reshape([192]);
print(result);
return List<double>.from(result);
}
This is still an issue. Adding a delay between initializing and running .run(...)
fixes it for me as well. One suggestion is to just expose the sendPort so that we can check whether it is initialized, or adding a "ready" state
I am having this issue as well.
Same issue, so I need to choose between trying a hack like ( delay ) or just block the UI for few seconds which is a super bad experience for the user!
I have the same problem, even when I put 'delayed' before 'run'. I tried using a 'singleton' class to make sure I was always getting the same instance, but that solution didn't work either.