tensorflow / flutter-tflite

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How can I load a tflite model using Isolate?

app-chain-hagio opened this issue · comments

Since the screen drawing becomes unstable during loading, I am thinking of loading the model using Isolate.

I got an error with the code below.
thanks.

[ERROR:flutter/runtime/dart_isolate.cc(1107)] Unhandled exception:
Invalid argument: <- _interpreter in Instance of 'Interpreter' (from package:tflite_flutter/src/interpreter.dart)

Future<dynamic> spawnAndReceive(File filename) async {
    final resultPort = ReceivePort();
    await Isolate.spawn(_readTensorModel, [resultPort.sendPort, filename]);
    return (await resultPort.first);
  }

void _readTensorModel(List<dynamic> args) async {
    SendPort responsePort = args[0];
    File fileName = args[1];

    var itp = Interpreter.fromFile(fileName);
    Isolate.exit(responsePort, itp);
  }

I changed Isolate.exit to

Isolate.exit(responsePort, itp.address);

int address = isolateTask.spawnAndReceive(dataFile);
interpreter = Interpreter.fromAddress(address);

and after this, I can get Instance using address.
it works, but, Does this make sense for processing distribution?