LoginSignup
3
1

More than 5 years have passed since last update.

[WIP]Firebase MLKit beta custom model使ってみた

Posted at

ドキュメント

tfliteを用意する

これ見ながらMobileNetのをコンバート
http://blog.gclue.com/?p=836

コード

    let conditions = ModelDownloadConditions(wiFiRequired: true, idleRequired: true)
    let cloudModelSource = CloudModelSource(
      modelName: "mobilenet_v1_1_0_224",
      enableModelUpdates: true,
      initialConditions: conditions,
      updateConditions: conditions
    )
    let registrationSuccessful = ModelManager.modelManager().register(cloudModelSource)

    guard let modelPath = Bundle.main.path(
      forResource: "mobilenet_v1_1_0_224_local",
      ofType: "tflite"
      ) else {
        // Invalid model path
        return
    }
    let localModelSource = LocalModelSource(modelName: "mobilenet_v1_1_0_224_local",
                                            path: modelPath)
    let registrationSuccessful2 = ModelManager.modelManager().register(localModelSource)

    print(registrationSuccessful)
    let options = ModelOptions(
      cloudModelName: "mobilenet_v1_1_0_224",
      localModelName: "mobilenet_v1_1_0_224_local"
    )
    let interpreter = ModelInterpreter(options: options)
    let ioOptions = ModelInputOutputOptions()
    do {
      try ioOptions.setInputFormat(index: 0, type: .uInt8, dimensions: [1, 640, 480, 3])
      try ioOptions.setOutputFormat(index: 0, type: .float32, dimensions: [1, 1000])
    } catch let error as NSError {
      print("Failed to set input or output format with error: \(error.localizedDescription)")
    }

    let input = ModelInputs()
    do {
      let image = UIImage(named: "hoge.jpg")!
      var data: Data  = UIImageJPEGRepresentation(image, 1.0)!
      // Store input data in `data`
      // ...
      try input.addInput(data)
      // Repeat as necessary for each input index
    } catch let error as NSError {
      print("Failed to add input: \(error.localizedDescription)")
    }

    interpreter.run(inputs: input, options: ioOptions) { outputs, error in
      print(error)
      guard error == nil, let outputs = outputs else { return }
      // Process outputs
      // ...
      let probabilities = try? outputs.output(index: 0)
      print(probabilities)
    }

多分こんな感じ

GTMSessionFetcherで落ちる

なんかおかしいのでinvokeOnCallbackQueueのコールバックのスレッドをメインスレッドに書き換える。
dispatch_group_async(_callbackGroup)ここ GTMSessionFetcher.mの2356行目

リモートモデル

取得出来なかった。

com.firebase.ml
Failed to load cloud model.

モデル名が駄目なんだかな…

ローカルモデル

nnapi error: unable to open library libneuralnetworks.so

上手くいったら追記する

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1