android 准备Tensor分配时意外失败:tensorflow/lite/内核/reforme.cc:85 num_input_elements!= num_output_elements(1200!= 0)

m0rkklqb  于 2023-08-01  发布在  Android
关注(0)|答案(1)|浏览(145)

我有一个tflite模型,并且TFLite模型的输入签名是'shape_signature':array([-1,12000,1].我用形状为[1,1200,1]的随机数据进行了测试,模型运行没有任何错误。
预测形状也是(1,1200,1)
https://colab.research.google.com/gist/pjpratik/bd48804cc8d40239812079b5a249aac3/60367.ipynb#scrollTo=9qc4EpLTUw0v
现在我想用android来做这个
我在android上尝试过这个,但我得到了这个错误

private fun applyModel() {
        val inputFloatArray = Array(1) { Array(inputAudioData.size) { FloatArray(1) } } //1,1200,1

        val outputFloatArray = inputFloatArray //Attempt 1
        val outputFloatArray = FloatArray(1200) //Attempt 2
        val outputFloatArray = FloatArray(1) //Attempt 3
        val outputFloatArray = Array(1) { Array(inputAudioData.size) { FloatArray(1) } } //Attempt 4

        Log.d("tflite", "Model input data: ${inputFloatArray.toString()}")

        tflite!!.run(inputFloatArray, outputFloatArray)

        Log.d("tflite", "Model output data: ${outputFloatArray.toString()}")
    }

个字符

c3frrgcw

c3frrgcw1#

这看起来更像是一种调试,您需要检查您的模型期望的是哪种类型的输入,以及产生的数据类型。
您可以尝试分块数据,然后尝试处理部分数据,下面展示了我如何在dtln dtln_aec_128_1.tflite上应用
您可以遵循以下实现,并尝试在数据集上使用它
https://github.com/breizhn/DTLN-aec/tree/main/pretrained_models

private fun applyModel(data: ShortArray): ShortArray {
        val chunkSize = 257 // can be depending on your model test, can be 1200
        val outputData = ShortArray(chunkSize)

        val inputArray = Array(1) { Array(1) { FloatArray(chunkSize) } }

        for (i in 0 until chunkSize)
            inputArray[0][0][i] = data[i].toFloat()

        val outputArray = Array(1) { Array(1) { FloatArray(chunkSize) } }

        tflite!!.run(inputArray, outputArray)

        Log.d("tflite output", "Model direct output ${outputArray[0][0].joinToString(" ")}")

        val outBuffer = FloatArray(chunkSize)

        for (i in 0 until chunkSize)
            outBuffer[i] = (outputArray[0][0][i]).toFloat()

        for (i in 0 until chunkSize)
            outputData[i] = outBuffer[i].toInt()
        .toShort()

        return outputData
    }

字符串
要加载模型,您可以这样做

@Throws(IOException::class)
    private fun loadModelFile(activity: Activity): MappedByteBuffer? {
        val fileDescriptor: AssetFileDescriptor = activity.assets.openFd("dtln_aec_128_1.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, declared length)
    }


app/build.gradle

implementation 'org.tensorflow:tensorflow-lite:2.12.0'


在buildTypes{}下面

aaptOptions {
        noCompress "dtln_aec_128_1.tflite"
    }


并复制到资源文件夹

相关问题