Firebase 要在android studio中导入的自定义机器学习代码
我正在android中创建一个假新闻检测项目。我已经训练了模型并转换成tensorflow lite。我试图将其导入到android studio的asset文件夹中,但显示消息“这不是有效的tensorflow lite模型文件”。 有人能帮我把它部署到android studio中并生成输入/输出吗 注意:我已经尝试导出到firebase自定义ML,我已经运行了这段代码,没有得到任何错误Firebase 要在android studio中导入的自定义机器学习代码,firebase,android-studio,tensorflow2.0,tensorflow-lite,Firebase,Android Studio,Tensorflow2.0,Tensorflow Lite,我正在android中创建一个假新闻检测项目。我已经训练了模型并转换成tensorflow lite。我试图将其导入到android studio的asset文件夹中,但显示消息“这不是有效的tensorflow lite模型文件”。 有人能帮我把它部署到android studio中并生成输入/输出吗 注意:我已经尝试导出到firebase自定义ML,我已经运行了这段代码,没有得到任何错误 CustomModelDownloadConditions conditions = new Cust
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);
}
}
});
CustomModelDownloadConditions=new CustomModelDownloadConditions.Builder().requireWifi().build();
FirebaseModelDownloader.getInstance().getModel(“NewsCheckModel”),
DownloadType.LOCAL_MODEL_UPDATE_IN_BACKGROUND,conditions)
.addOnSuccessListener(新的OnSuccessListener(){
@凌驾
成功时公共无效(CustomModel CustomModel){
File modelFile=customModel.getFile();
if(modelFile!=null){
解释器=新解释器(模型文件);
}
}
});
我不知道进一步的步骤。
。
如果有人可以使用asset文件夹或firebase way以任何方式提供帮助。我将非常感谢您您能否确保在对asset dir案例进行实验时未启用以下压缩选项
android {
// ...
aaptOptions {
noCompress "tflite" // Your model's file extension: "tflite", "lite", etc.
}
}
请在此页面查看更多详细信息。您的帖子上没有太多信息。了解以下情况将是有益的:
- 你能在android上部署它吗
- 模型是否初始化
- 如果是,你能进行推断吗
- 如果没有,您会得到什么例外情况
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" />
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…)
另一件重要的事情是在模型中嵌入一些元数据:
Android Studio在打开.tflite文件时使用该元数据显示信息
IMO很可能是您的问题在模型转换期间或缺少元数据。是的,我也参考了该指南,并启用了无压缩选项
source = ml.TFLiteGCSModelSource.from_tflite_model_file('example.tflite')