How to load .tflite model as File

Viewed 201

I am running a YOLOv4Tiny model in my Flutter app. I am using the tflite_flutter_plugin and tflite_flutter_helper packages modified with TexMexMax's code (https://github.com/TexMexMax/object_detection_flutter) to get the packages to work with YOLO. When I load my model as an asset the model runs fine, however there is a memory leak, and it crashes after it is loaded and unloaded 12 times. This post provides a potential fix (loading the model from a file): https://github.com/am15h/tflite_flutter_plugin/issues/170

However I am having trouble loading my model as a file. My model is of type Float32, which when I alter the documentation (from uint8list which doesn't give an error, but doesn't load the model) to account for this I get the error Float32List' can't be assigned to a variable of type 'List<int>

Edit: it appears Uint8List is of type List<int> and Float32List is type List<double>. writeAsBytes only accepts List<int>. How do I reconcile these in the following context?

My load function is below:

  void loadModel({Interpreter? interpreter}) async {
    try {
      Future<File> getFile(String fileName) async {
        final appDir = await getTemporaryDirectory();
        final appPath = appDir.path;
        final fileOnDevice = File('$appPath/$fileName');
        final rawAssetFile = await rootBundle.load(fileName);
        // final List<int> rawBytes = rawAssetFile.buffer.asUint8List(); // no error here
        final List<int> rawBytes2 = rawAssetFile.buffer.asFloat32List(); // error here!!!

        // await fileOnDevice.writeAsBytes(rawBytes, flush: true);
        await fileOnDevice.writeAsBytes(rawBytes2, flush: true);
        return fileOnDevice;
      }

      final dataFile = await getFile(MODEL_FILE_NAME);
      _interpreter = interpreter ??
          Interpreter.fromFile(
            dataFile,
            options: InterpreterOptions()..threads = numThreads,
          );


      var outputTensors = _interpreter!.getOutputTensors();
      print("the length of the ouput Tensors is ${outputTensors.length}");
      _outputShapes = [];
      _outputTypes = [];
      outputTensors.forEach((tensor) {
        //print(tensor.toString());
        _outputShapes!.add(tensor.shape);
        _outputTypes!.add(tensor.type);
      });
      print('YOLOv4Tiny model Loaded!');
    } catch (e) {
      print("Error while creating interpreter: $e");
    }
  }

0 Answers
Related