首页 > 解决方案 > 无法应用委托:Android 中的 TFlite 崩溃

问题描述

当我尝试使用tflite GPU. 我正在使用MoveNet这里的模型单击。我在 CPU 中运行这个模型没有错误。

这是我的解释器初始化代码。

            val compatList = CompatibilityList()


            val options = Interpreter.Options().apply{
                if(compatList.isDelegateSupportedOnThisDevice){
                    // if the device has a supported GPU, add the GPU delegate
                    val delegateOptions = compatList.bestOptionsForThisDevice
                    this.addDelegate(GpuDelegate(delegateOptions))
                } else {
                    // if the GPU is not supported, run on 4 threads
                    this.setNumThreads(8)
                }
            }
           interpreter = Interpreter(loadTFFile(),options)

毕业(模块):

implementation 'org.tensorflow:tensorflow-lite:2.3.0'
implementation 'org.tensorflow:tensorflow-lite-gpu:2.3.0'
implementation 'org.tensorflow:tensorflow-lite-support:0.1.0'

错误:

java.lang.IllegalArgumentException: Internal error: Failed to apply delegate: Following operations are not supported by GPU delegate:
    ARG_MAX: Operation is not supported.
    CAST: Operation is not supported.
    CONCATENATION: OP is supported, but tensor type isn't matched!
    DEQUANTIZE: 
    FLOOR_DIV: Operation is not supported.
    GATHER_ND: Operation is not supported.
    MUL: OP is supported, but tensor type isn't matched!
    PACK: Operation is not supported.
    RESHAPE: OP is supported, but tensor type isn't matched!
    SUB: OP is supported, but tensor type isn't.

我在 android 8.1 上测试这个

标签: androidtensorflow-lite

解决方案


我正在使用此代码来委托 tflite 模型,这是有效的。

tfliteModel = FileUtil.loadMappedFile(activity, this.getModelPath())
    tfliteOptions.setNumThreads(numThreads)
    tflite =
        try {
            val options = Interpreter.Options()
            options.addDelegate(NnApiDelegate())
            Interpreter(tfliteModel, options)
        } catch (e: Exception) {
            e.printStackTrace()
            try {
                val options = Interpreter.Options()
                options.addDelegate(GpuDelegate())
                Interpreter(tfliteModel, options)
            } catch (e: Exception) {
                val options = Interpreter.Options()
                try {
                    Interpreter(tfliteModel, options)
                } catch (e: Exception) {
                    e.printStackTrace()
                    null
                }
            }
        }

这是 kotlin 语言


推荐阅读