自定义机器学习代码在android工作室导入



我正在android中创建一个假新闻检测项目。我已经训练了模型,并将其转换为tensorflow lite。我试图将其导入到资产文件夹中的android工作室,但它显示了消息";这不是有效的tensorflow lite模型文件";。在此处输入图像描述

有人能帮我把它部署到android工作室并生成输入/输出吗

注意:我已经尝试导出到firebase自定义ML,我已经运行了这个代码,没有得到任何错误

CustomModelDownloadConditions conditions = new CustomModelDownloadConditions.Builder().requireWifi().build();
FirebaseModelDownloader.getInstance().getModel("NewsCheckModel",
DownloadType.LOCAL_MODEL_UPDATE_IN_BACKGROUND, conditions)
.addOnSuccessListener(new OnSuccessListener<CustomModel>() {
@Override
public void onSuccess(CustomModel customModel) {
File modelFile = customModel.getFile();
if (modelFile != null) {
Interpreter interpreter = new Interpreter(modelFile);
}
}
});

我不知道下一步该怎么走。.

如果有人可以使用资产文件夹或firebase的方式以任何方式提供帮助。我将非常感谢你

在试验资产目录情况时,您能确保没有启用如下压缩选项吗?

android {
// ...
aaptOptions {
noCompress "tflite"  // Your model's file extension: "tflite", "lite", etc.
}
}

请参阅本指南页中的更多详细信息。

你的帖子没有太多信息。如果知道:

  • 你能在android上部署它吗
  • 模型是否初始化
  • 如果是,你能进行推理吗
  • 如果没有,你得到的例外是什么

过去,我也遇到过同样的问题。以下是一些基本步骤:

  1. 将Firebase添加到您的项目中2a。请确保使用了正确的依赖项:它们最近更新了,所以最好遵循以下指南:此处

基本上:

dependencies {
// Import the BoM for the Firebase platform
implementation platform('com.google.firebase:firebase-bom:28.0.1')
// Declare the dependency for the Firebase ML model downloader library
// When using the BoM, you don't specify versions in Firebase library dependencies
implementation 'com.google.firebase:firebase-ml-modeldownloader'
// Also declare the dependency for the TensorFlow Lite library and specify its version
implementation 'org.tensorflow:tensorflow-lite:2.3.0' 
}

2b。添加到清单:

<uses-permission android:name="android.permission.INTERNET" />

3a。仔细检查转换模型的方式。幸运的是,你可以很容易地转换和部署到firebase:

3b。将模型转换为TensorFlow Lite并上传到云存储

source = ml.TFLiteGCSModelSource.from_saved_model('./model_directory')

3c。创建模型对象

tflite_format = ml.TFLiteFormat(model_source=source)
model = ml.Model(
display_name="example_model",  # This is the name you use from your app to load the model.
tags=["examples"],             # Optional tags for easier management.
model_format=tflite_format)

3d。将模型添加到您的Firebase项目并发布

new_model = ml.create_model(model)
ml.publish_model(new_model.model_id)

3a之二:如果你的模型已经在。tflite:

source = ml.TFLiteGCSModelSource.from_tflite_model_file('example.tflite')

导游在这里

关于输入/输出,我不能说太多,因为这取决于你的模型。此外,还要小心输入和输出的数据类型。请注意您的型号所需的类型(INT32、UINT8、FLOAT32……(

另一件重要的事情是在模型中嵌入一些元数据:当您打开.tflite文件时,AndroidStudio会使用该元数据来显示信息。

IMO很可能是您的问题发生在模型转换过程中或缺乏元数据。

最新更新