[英]Flutter Firebase Machine learning how to use remote labels
我第一次尝试在 flutter 上使用 firebase 机器学习,我已经成功地在自定义模型部分上传了 .tflite 文件,然后很好地使用了远程模型,我唯一不明白的问题是我是否更新了模型并想添加更多标签 我理想情况下也使用 remote.txt 文件,但我如何将其上传到机器学习部分,因为我只能找到说明如何使用模型文件本身的文档。
FirebaseModelDownloader.instance
.getModel(
"Breed-Detector",
FirebaseModelDownloadType.localModel,
FirebaseModelDownloadConditions(
iosAllowsCellularAccess: true,
iosAllowsBackgroundDownloading: false,
androidChargingRequired: false,
androidWifiRequired: false,
androidDeviceIdleRequired: false,
))
.then((customModel) async {
final localModelPath = customModel.file;
// ...
Tflite.close();
String res;
res = (await Tflite.loadModel(
model: localModelPath.toString(), labels: "assets/labels.txt"))!;
那么我如何将远程 labels.txt 与远程模型一起使用而不是使用远程模型并且必须在本地存储标签
我设法为此使用了 Firebase 存储,它错过了版本控制的想法......
这是我的解决方案首先创建一个存储桶并在那里更新您的文件: https ://firebase.google.com/docs/storage/flutter/start
我使用了以下规则:
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read;
allow write: if request.auth != null;
}
}
}
然后在 flutter 应用程序中:
late File labelsFile;
Future<File> getFirebaseFile(String fileName) async {
Directory appDocDir = await getApplicationDocumentsDirectory();
File labelsFile = File('${appDocDir.path}/$fileName');
if (!labelsFile.existsSync()) {
await downloadFirebaseFile(labelsFile, fileName);
}
return labelsFile;
}
Future<void> downloadFirebaseFile(File file, String fileName) async {
try {
await FirebaseStorage.instance.ref(fileName).writeToFile(file);
} on FirebaseException catch (e) {
print("Error loading labelsFile $e");
}
}
然后您可以在 localModelPath 实例化之后立即使用它:
labelsFile = await getFirebaseFile("labels.txt");
// init inference
Tflite.loadModel(
numThreads: numThreads,
isAsset: false, //<-- this is important
outputRawScores: outputRawScores,
inputType: inputType,
model: localModelPath.path,
label: labelsFile.path,
);
在 fetchLabels 方法中:
Future<List<String>> fetchLabelList() async {
List<String> labelList = [];
labelsFile = await getFirebaseFile("labels.txt");
await labelsFile.readAsString().then((q) {
for (String i in const LineSplitter().convert(q)) {
labelList.add(i);
}
});
return labelList;
}
如果标签的文件在本地不存在,将被下载,如果它改变版本,它不会被更新......我不知道如何处理这个......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.