flutter-tflite icon indicating copy to clipboard operation
flutter-tflite copied to clipboard

IsolateInterpreter late initialization error.

Open Anthrapper opened this issue 1 year ago • 8 comments

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.

Anthrapper avatar Aug 03 '23 08:08 Anthrapper

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.

luiscib3r avatar Aug 12 '23 02:08 luiscib3r

The issue persist it seems, will it be due to

 diffusionInterpreter = Interpreter.fromFile(
      difFile,
      options: options,
    );

Anthrapper avatar Aug 19 '23 09:08 Anthrapper

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.

BlackFireAlex avatar Oct 20 '23 15:10 BlackFireAlex

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);
  }

rafaelmaia8384 avatar Nov 06 '23 13:11 rafaelmaia8384

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

unifractal avatar Jan 18 '24 14:01 unifractal

I am having this issue as well.

markatlarge avatar Jan 21 '24 20:01 markatlarge

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!

hedi-ghodhbane avatar Feb 05 '24 16:02 hedi-ghodhbane

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.

ranulfo avatar May 15 '24 23:05 ranulfo