android - 在 flutter 中使用 tflite 处理自定义模型时应用程序崩溃。我如何解决它?

标签 android flutter keras prediction

我构建了一个从 Keras hdf5 模型转换而来的自定义 tflite 模型。但是当使用转换后的模型时,在进行预测时应用程序在 android 中崩溃,但是当我使用从互联网下载的移动网络 tflite 模型时,该应用程序可以正常工作。我必须做出哪些改变?是模型转换的问题还是应用的问题?

我尝试使用支持移动网络和 tflite 提供的各种其他网络的 tflite 包的内置功能,我更改为自定义构建模型预测功能并保留转换后的文件模型。在第一种情况下,它起作用了,但在第二种情况下,它没有起作用。

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

我在 Assets 中添加了 2 个模型:

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

预期结果是自定义模型在没有应用程序崩溃的情况下正常工作。

最佳答案

我遇到了类似的问题,是内存泄漏导致应用程序崩溃。我通过执行以下操作解决了它:

  1. 添加以下内容以关闭任何资源

    @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. 运行命令:flutter clean

  3. 重建你的项目

附言如果给出有关调试控制台错误的更多详细信息,那就太好了。

关于android - 在 flutter 中使用 tflite 处理自定义模型时应用程序崩溃。我如何解决它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57648400/

相关文章:

python - 序列化 `tf.Module` 的推荐方法是什么?

android - TranslateAnimation - 动画启动时图像消失

java - Java Android 应用程序中的文本编码问题

android-studio - Flutter doctor 错误 - 找不到 Android sdkmanager 工具。 window

android - 无法创建新的 flutter 项目

python - 如何将训练好的模型转换为函数?

python - 如何在tensorflow 2.4.1中设置优化器

android - AmbiguousViewMatcherException Espresso

android - "Application stopped working"启用 Proguard 后

flutter - 如何在 Flutter App 上获取编译/构建日期?