I have built a custom tflite model which got converted from Keras hdf5 model. But when using the converted model, while making prediction the application is crashing in android, but when I am using the mobile net tflite model which is downloaded from internet works fine with the application. What changes do I have to make? Whether the problem is in the model conversion or in the application?

I have tried using the inbuilt function by tflite package that supports mobile net and various other nets that are given by tflite, I changed to custom build model prediction function and kept the converted file model. In the first case, it worked, but in the second case, it did not.

Future prediction(File image) async{
  _recognitions= null;
  var recognitions = await Tflite.runModelOnImage(
  path: image.path,   // required
  imageMean: 0.0,   // defaults to 117.0
  imageStd: 255.0,  // defaults to 1.0
  numResults: 2,    // defaults to 5
  threshold: 0.2,   // defaults to 0.1
  asynch: true      // defaults to true
);
  // var recognitions = await Tflite.detectObjectOnImage(
  //   path: image.path,
  //   model: "SSDMobileNet",
  //   imageMean: 127.5,
  //   threshold: 0.4,
  //   numResultsPerClass: 2,
  //   asynch: true
  // );
  print(recognitions);
  setState(() {
    _recognitions=recognitions;
  });
}

I have added 2 models in assets:

  assets:
   - images/webdoctor.png
   - assets/detect.tflite
   - assets/labelmap.txt
   - assets/labels.txt
   - assets/modelPneumonia.tflite

Expected results would be the working of custom model without app crashing.


Solution 1: G.Kaviraj

I ran into a similar problem and it was a memory leak that crashed the app. I solved it by doing the following:

  1. Add the following to close any resource

    @override
    void dispose() {
      // you can add to close tflite if error is caused by tflite
      // tflite.close();
      controller?.dispose(); // this is to dispose camera controller if you are using live detection
      super.dispose();
    }
    
  2. Run the Command: flutter clean

  3. Rebuild your project

P.S. It would be nice if more details on the error from the debug console was given.


Solution 2: Chandupa Lasith

Keeping extra lines on labels.txt or labelmap.txt would be an issue. Please make sure you don't keep new lines.

DO:

Don't: