Bruno Justino Praciano
Bruno Justino Praciano

Reputation: 552

Error when load custom ML model in flutter

I can't load tensorflow lite model in flutter. I am using this library

https://github.com/shaqian/flutter_tflite

And when I tried to load the model, I received this error.

E/flutter ( 7258): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(error, Unsupported value: java.io.FileNotFoundException: flutter_assets/assets/output.tflite, 

I have followed all the steps in the documentation of the library. Is there another method to load this model in flutter without this library?

Upvotes: 2

Views: 2245

Answers (2)

nickname
nickname

Reputation: 11

make sure to declare the model assets in your pubspec.yaml file under:

flutter:
  assets:
    - assets/tflite/model.tflite
    - assets/tflite/labels.txt 

or else you will receive this error when trying to load the model in the code such as:

      String tfResponse;
      tfResponse = await Tflite.loadModel(
        model: "assets/tflite/model.tflite",
        labels: "assets/tflite/labels.txt",

      );
      print("Response: TF Model Load $tfResponse");
    } catch (e) {
      print('Failed to load model. Error: $e');
    } 

EDIT: I'm using this package https://pub.dev/packages/tflite

Upvotes: 0

Siddharth Jadhav
Siddharth Jadhav

Reputation: 524

If you have trained a Keras model(HDF5) ,convert it to Tensorflow model(.pb) and then convert it to Tflite Please refer to code below:

converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)

tflite_model = converter.convert()

open("converted_model.tflite", "wb").write(tflite_model)

Upvotes: 1

Related Questions