tensorflow / decision-forests

A collection of state-of-the-art algorithms for the training, serving and interpretation of Decision Forest models in Keras.
Apache License 2.0
660 stars 110 forks source link

Encountered unresolved custom op: SimpleMLCreateModelResource. Failed to allocate tensors #171

Closed Maiadaa closed 1 year ago

Maiadaa commented 1 year ago

I have my keras.RandomForestModel for regression converted to tensorflow lite. I have added the model.tflite file in the assets folder of my android application. However, when running I had the following error, saying that my tflite model has unresolved custom operators.

I found an answer on stack overflow saying that (@rstz at https://stackoverflow.com/questions/75273668/error-while-convering-tensor-flow-decision-tree-model-into-tflite-model):

Full Disclosure: I'm one of the authors of Tensorflow Decision Forests.

Decision Forests are not yet compatible with TFLite, so the conversion is unfortunately not possible. Feel free to tell the team about your uses case on the TF-DF Github repo to help prioritize this feature.

CAN YOU PLEASE prioritize the conversion compatibility of Decision Forests with TFLite?

The code: MainActivity.kt

(https://i.stack.imgur.com/J1xjt.png)

 fun getModelByteBuffer() : MappedByteBuffer {
        val assetManager: AssetManager = this.getAssets()
        val fileDescriptor = assetManager.openFd("new__tflite_ model.tflite")
        val inputStream = FileInputStream(fileDescriptor.fileDescriptor)
        val fileChannel = inputStream.channel
        val startOffset = fileDescriptor.startOffset
        val declaredLength = fileDescriptor.declaredLength
        return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength)
    }

(https://i.stack.imgur.com/qWeyd.png) The error is in the highlighted line.


 fun test (): String {
        val tfliteModel = getModelByteBuffer()
        **val tflite = Interpreter(tfliteModel, Interpreter.Options())**
        var inputData: ByteBuffer = ByteBuffer.allocateDirect(
            1 // 1 dimension
                    * 6 //6 attributes/columns
                    * 1 //1 row
                    * 4 //4 bytes per number as the number is float
        )
        inputData.order(ByteOrder.nativeOrder())
        floatArrayOf(1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f).forEach {
            inputData.putFloat(it)
        }
        val labelProbArray: Array<FloatArray> = Array(1) { FloatArray(1) }
        tflite.run(inputData, labelProbArray)
        return tflite.toString()
    }

The error: (https://i.stack.imgur.com/vagJC.png)

Unexpected failure when preparing tensor allocations: Encountered unresolved custom op: SimpleMLCreateModelResource.
E/MethodChannel#com.julow.barometer/method(20951): See instructions: https://www.tensorflow.org/lite/guide/ops_custom 
E/MethodChannel#com.julow.barometer/method(20951): Node number 1 (SimpleMLCreateModelResource) failed to prepare.
E/MethodChannel#com.julow.barometer/method(20951): Encountered unresolved custom op: SimpleMLCreateModelResource.
E/MethodChannel#com.julow.barometer/method(20951): See instructions: https://www.tensorflow.org/lite/guide/ops_custom 
E/MethodChannel#com.julow.barometer/method(20951): Node number 1 (SimpleMLCreateModelResource) failed to prepare.
E/MethodChannel#com.julow.barometer/method(20951):  at org.tensorflow.lite.NativeInterpreterWrapper.allocateTensors(Native Method)
E/MethodChannel#com.julow.barometer/method(20951):  at org.tensorflow.lite.NativeInterpreterWrapper.init(NativeInterpreterWrapper.java:126)
E/MethodChannel#com.julow.barometer/method(20951):  at org.tensorflow.lite.NativeInterpreterWrapper.<init>(NativeInterpreterWrapper.java:73)
E/MethodChannel#com.julow.barometer/method(20951):  at org.tensorflow.lite.NativeInterpreterWrapperExperimental.<init>(NativeInterpreterWrapperExperimental.java:36)
E/MethodChannel#com.julow.barometer/method(20951):  at org.tensorflow.lite.Interpreter.<init>(Interpreter.java:227)
E/MethodChannel#com.julow.barometer/method(20951):  at com.example.gp_application.MainActivity.configureFlutterEngine$test(MainActivity.kt:75)
E/MethodChannel#com.julow.barometer/method(20951):  at com.example.gp_application.MainActivity.configureFlutterEngine$lambda-0(MainActivity.kt:101)
E/MethodChannel#com.julow.barometer/method(20951):  at com.example.gp_application.MainActivity.$r8$lambda$UzV2ABFNs3Iomo_rzyDnnwqJ3BU(Unknown Source:0)
E/MethodChannel#com.julow.barometer/method(20951):  at com.example.gp_application.MainActivity$$ExternalSyntheticLambda0.onMethodCall(Unknown Source:2)
E/MethodChannel#com.julow.barometer/method(20951):  at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:258)
E/MethodChannel#com.julow.barometer/method(20951):  at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
E/MethodChannel#com.julow.barometer/method(20951):  at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:322)
E/MethodChannel#com.julow.barometer/method(20951):  at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
E/MethodChannel#com.julow.barometer/method(20951):  at android.os.Handler.handleCallback(Handler.java:942)
E/MethodChannel#com.julow.barometer/method(20951):  at android.os.Handler.dispatchMessage(Handler.java:99)
E/MethodChannel#com.julow.barometer/method(20951):  at android.os.Looper.loopOnce(Looper.java:226)
E/MethodChannel#com.julow.barometer/method(20951):  at android.os.Looper.loop(Looper.java:313)
E/MethodChannel#com.julow.barometer/method(20951):  at android.app.ActivityThread.main(ActivityThread.java:8757)
E/MethodChannel#com.julow.barometer/method(20951):  at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#com.julow.barometer/method(20951):  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
E/MethodChannel#com.julow.barometer/method(20951):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
I/flutter (20951): PlatformException(error, Internal error: Unexpected failure when preparing tensor allocations: Encountered unresolved custom op: SimpleMLCreateModelResource.
I/flutter (20951): See instructions: https://www.tensorflow.org/lite/guide/ops_custom 
I/flutter (20951): Node number 1 (SimpleMLCreateModelResource) failed to prepare.
I/flutter (20951): Encountered unresolved custom op: SimpleMLCreateModelResource.
I/flutter (20951): See instructions: https://www.tensorflow.org/lite/guide/ops_custom 
I/flutter (20951): Node number 1 (SimpleMLCreateModelResource) failed to prepare., null, java.lang.IllegalStateException: Internal error: Unexpected failure when preparing tensor allocations: Encountered unresolved custom op: SimpleMLCreateModelResource.
I/flutter (20951): See instructions: https://www.tensorflow.org/lite/guide/ops_custom 
I/flutter (20951): Node number 1 (SimpleMLCreateModelResource) failed to prepare.
I/flutter (20951): Encountered unresolved custom op: SimpleMLCreateModelResource.
I/flutter (20951): See instructions: https://www.tensorflow.org/lite/guide/ops_custom 
I/flutter (20951): Node number 1 (SimpleMLCreateModelResource) failed to prepare.
I/flutter (20951):  at org.tensorflow.lite.NativeInterpreterW
W/System  (20951): A resource failed to call close. 
rstz commented 1 year ago

Hi, thank you for reporting this. I'll leave this open until we have a TFLite implementation, but know that there is no timeline for this. Feel free to use emojis to express your interest in a TFLite implementation.

For some use cases, it might be useful to look into serving TF-DF models with Yggdrasil Decision Forests (YDF). YDF and TF-DF models are cross-compatible. Since YDF is a pure C++-library, it even run on small devices such as Raspberry PI.

rstz commented 1 year ago

Closing this as a duplicate of https://github.com/tensorflow/decision-forests/issues/5

nxprxf commented 11 months ago

@Maiadaa May I know how do you convert keras.RandomForestModel to tflite model? Do you manually add custom operator for SimpleMLCreateModelResource? Meanwhile, I'm not sure whether someone can explain whether it's feasible to implement SimpleMLCreateModelResource in TFLite?