Search code examples
androidfirebase-mlkit

Cannot load custom model from ML Kit: FirebaseMLException: The load task failed


I am following the Codelab: https://codelabs.developers.google.com/codelabs/mlkit-android-custom-model/#0 and it works as it is, but as soon as I remove the provided local model to force using the cloud model, I get an obscure error.

What I do different to the codelab is:

//        manager.registerLocalModelSource(localModelSource)
    manager.registerCloudModelSource(cloudSource)

    return FirebaseModelOptions.Builder()
            .setCloudModelName(HOSTED_MODEL_NAME)
//                .setLocalModelName("asset")
            .build()

Commenting out the "local" model configuration, so I force the project to use the hosted ML Kit model.

However, I get the following error when running my model:

E/MainActivity: com.google.firebase.ml.common.FirebaseMLException: The load task failed
    com.google.android.gms.tasks.RuntimeExecutionException: com.google.firebase.ml.common.FirebaseMLException: The load task failed
        at com.google.android.gms.tasks.zzu.getResult(Unknown Source:17)
        at work.beltran.mlkitcustommodelcodelab.MainActivity$onCreate$$inlined$use$lambda$1.then(MainActivity.kt:61)
        at com.google.android.gms.tasks.zzd.run(Unknown Source:26)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:172)
        at android.app.ActivityThread.main(ActivityThread.java:6590)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: com.google.firebase.ml.common.FirebaseMLException: The load task failed
        at com.google.android.gms.internal.firebase_ml.zzgy.zzf(Unknown Source:75)
        at com.google.android.gms.internal.firebase_ml.zzgu.call(Unknown Source:8)
        at com.google.android.gms.internal.firebase_ml.zzgp.zza(Unknown Source:0)
        at com.google.android.gms.internal.firebase_ml.zzgq.run(Unknown Source:4)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:172)
        at android.os.HandlerThread.run(HandlerThread.java:65)
     Caused by: com.google.firebase.ml.common.FirebaseMLException: Cannot load custom model
        at com.google.android.gms.internal.firebase_ml.zzhn.zzey(Unknown Source:148)
        at com.google.android.gms.internal.firebase_ml.zzha.zzfb(Unknown Source:75)
        at com.google.android.gms.internal.firebase_ml.zzha.call(Unknown Source:0)
        at com.google.android.gms.internal.firebase_ml.zzgp.zza(Unknown Source:0) 
        at com.google.android.gms.internal.firebase_ml.zzgq.run(Unknown Source:4) 
        at android.os.Handler.handleCallback(Handler.java:790) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:172) 
        at android.os.HandlerThread.run(HandlerThread.java:65) 

My project can be found here: https://github.com/miquelbeltran/codelab-mlkit-android-custom-model/tree/4aa336d9efca6eafcd01acc9417ad194aa12233e

Some stuff I tried:

  • My project has internet permission and Firebase Analytics works, as I can see events on the DebugView.
  • The local model is the same as the hosted model in ML Kit.
  • The model name is correct.

Running on a rooted emulator, navigating to the app data folder, I can find my models folder named like this:

/data/data/work.beltran.mlkitcustommodelcodelab/no_backup/com.google.firebase.ml.custom.models/W0RFRkFVTFRd+MTo2ODI2MzU4OTg3OTY6YW5kcm9pZDozMGYwZDA3MmQ5NDBjODEx/mobilenet

However the folder is empty

My only guess: Firebase decides to not to download the model for some reason, can't figure out why.


Solution

  • I had to add the SHA certificate fingerprints to make it work.

    If you are missing that step, the ML model will not be downloaded.

    Follow the instructions here to generate the SHA for your certificates: https://developers.google.com/android/guides/client-auth

    And introduce the output on your device project settings.