简体   繁体   English

Flutter Firebase 机器学习如何使用远程标签

[英]Flutter Firebase Machine learning how to use remote labels

I'm trying to use the firebase machine learning for the first time on flutter and Ive managed to upload the.tflite file in the custom model section easily enough and then use the remote model fine, the only issue I dont understand is if I update the model and want to add more labels I would ideally also use a remote.txt file but how would I upload that in the machine learning section as I can only find documentation showing how to use the model file itself.我第一次尝试在 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"))!;

so how would I use a remote labels.txt as well with the remote model rather than use a remote model and have to store the labels locally那么我如何将远程 labels.txt 与远程模型一起使用而不是使用远程模型并且必须在本地存储标签

I managed to use Firebase Storage for this, it miss versionning thought...我设法为此使用了 Firebase 存储,它错过了版本控制的想法......

Here is my solution First create a bucket and update your file there: https://firebase.google.com/docs/storage/flutter/start这是我的解决方案首先创建一个存储桶并在那里更新您的文件: https ://firebase.google.com/docs/storage/flutter/start

I used following rules:我使用了以下规则:

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read;
      allow write: if request.auth != null;
    }
  }
}

Then in flutter app:然后在 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");
  }
}

Then you can use it right after localModelPath instanciation:然后您可以在 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,
);

And in the fetchLabels method:在 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;
}

label's file will be downloaded if it does not exists localy, it just wont be updated if it changes version... Idk yet how to handle this...如果标签的文件在本地不存在,将被下载,如果它改变版本,它不会被更新......我不知道如何处理这个......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM