自定义模型[MLKit]-FirebaseMLException:执行Firebase ML tastks时发生内部错误

时间:2019-08-06 08:21:36

标签: android kotlin local firebase-mlkit

我想在使用MLKit的Android中使用自定义ML模型,而不能使用本地或远程模型。因为我有FirebaseMLException,所以我将重点放在本地。

我试图逐步遵循official documentation

我相信错误是在输入/输出格式中,但是我无法弄清楚它是什么。使143类用整数标记。这是我构建输入/输出的方法:

inputOutputOptions = FirebaseModelInputOutputOptions.Builder()
            .setInputFormat(
                0,
                FirebaseModelDataType.FLOAT32,
                intArrayOf(
                    DIM_BATCH_SIZE,
                    DIM_IMG_SIZE_X,
                    DIM_IMG_SIZE_Y,
                    DIM_PIXEL_SIZE
                )
            )
            .setOutputFormat(
                0,
                FirebaseModelDataType.INT32,
                intArrayOf(1, 143)
            )
            .build()

在孔堆栈轨迹下方找到:

W/System.err: com.google.firebase.ml.common.FirebaseMLException: Internal error has occurred when executing Firebase ML tasks
W/System.err:     at com.google.firebase.ml.common.internal.zze.zza(com.google.firebase:firebase-ml-common@@20.0.1:38)
        at com.google.firebase.ml.common.internal.zzh.run(Unknown Source:4)
W/System.err:     at android.os.Handler.handleCallback(Handler.java:873)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:99)
        at com.google.android.gms.internal.firebase_ml.zzf.dispatchMessage(com.google.firebase:firebase-ml-common@@20.0.1:6)
W/System.err:     at android.os.Looper.loop(Looper.java:193)
        at android.os.HandlerThread.run(HandlerThread.java:65)
W/System.err: Caused by: java.lang.IllegalArgumentException: Cannot convert between a TensorFlowLite tensor with type FLOAT32 and a Java object of type [[I (which is compatible with the TensorFlowLite type INT32).
W/System.err:     at org.tensorflow.lite.Tensor.throwExceptionIfTypeIsIncompatible(Tensor.java:233)
W/System.err:     at org.tensorflow.lite.Tensor.copyTo(Tensor.java:116)
        at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:157)
W/System.err:     at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:250)
        at com.google.android.gms.internal.firebase_ml.zzpz.runForMultipleInputsOutputs(com.google.firebase:firebase-ml-model-interpreter@@20.0.1:4)
W/System.err:     at com.google.android.gms.internal.firebase_ml.zzpu.zza(com.google.firebase:firebase-ml-model-interpreter@@20.0.1:85)
W/System.err:     at com.google.android.gms.internal.firebase_ml.zzpu.zza(com.google.firebase:firebase-ml-model-interpreter@@20.0.1:145)
        at com.google.firebase.ml.common.internal.zzi.zza(com.google.firebase:firebase-ml-common@@20.0.1:33)
        at com.google.firebase.ml.common.internal.zzk.call(Unknown Source:8)
W/System.err:     at com.google.firebase.ml.common.internal.zze.zza(com.google.firebase:firebase-ml-common@@20.0.1:32)
        ... 6 more

我不知道如何解释这一行:

Cannot convert between a TensorFlowLite tensor with type FLOAT32 and a Java object of type [[I (which is compatible with the TensorFlowLite type INT32)

1 个答案:

答案 0 :(得分:0)

我发现问题出在哪里,在面孔MLKit中等待一系列概率作为输出,这意味着float范围内的一组[0.0, 1.0]值。这意味着该代码段(包含错误的行):

.setOutputFormat(
    0,
    FirebaseModelDataType.INT32, // Wrong line
    intArrayOf(1, 143)
)

应该用这个替换:

.setOutputFormat(
    0,
    FirebaseModelDataType.FLOAT32, // <== Expected array type is a float
    intArrayOf(1, 143)
)