Cannot convert between a TensorFlowLite tensor with type UINT8 and a Java object

ぃ、小莉子 提交于 2020-05-30 06:47:07

问题


I am using MLKiT for loading custom tensoflow model While reading the model gets following error

java.lang.IllegalArgumentException: Cannot convert between a TensorFlowLite tensor with type UINT8 and a Java object of type [[[[F (which is compatible with the TensorFlowLite type FLOAT32).

I am using below code for object detection using tlflite file

    private fun bitmapToInputArray(bitmap: Bitmap): Array<Array<Array<FloatArray>>> {
            var bitmap = bitmap
            bitmap = Bitmap.createScaledBitmap(bitmap, 224, 224, true)

            val batchNum = 0
            val input = Array(1) { Array(224) { Array(224) { FloatArray(3) } } }
            for (x in 0..223) {
                for (y in 0..223) {
                    val pixel = bitmap.getPixel(x, y)
                    // Normalize channel values to [-1.0, 1.0]. This requirement varies by
                    // model. For example, some models might require values to be normalized
                    // to the range [0.0, 1.0] instead.
                    input[batchNum][x][y][0] = (Color.red(pixel) - 127) / 128.0f
                    input[batchNum][x][y][1] = (Color.green(pixel) - 127) / 128.0f
                    input[batchNum][x][y][2] = (Color.blue(pixel) - 127) / 128.0f
                }
            }
            return input
        }

private fun setImageData(input: Array<Array<Array<FloatArray>>>) {
        var inputs: FirebaseModelInputs? = null
        try {
            inputs = FirebaseModelInputs.Builder()
                .add(input)  // add() as many input arrays as your model requires
                .build()
        } catch (e: FirebaseMLException) {
            e.printStackTrace()
        }

        firebaseInterpreter!!.run(inputs!!, inputOutputOptions!!)
            .addOnSuccessListener(
                OnSuccessListener<FirebaseModelOutputs> {
                    // ...
                    Log.d("Final",it.toString());
                })
            .addOnFailureListener(
                object : OnFailureListener {
                    override fun onFailure(p0: Exception) {
                        // Task failed with an exception
                        // ..

                    }
                })
    }

回答1:


Your model expects a quantized image. You can prepare it thus:

val input = Array(1) { Array(224) { Array(224) { ByteArray(3) } } }
        for (x in 0..223) {
            for (y in 0..223) {
                val pixel = bitmap.getPixel(x, y)
                input[batchNum][x][y][0] = Color.red(pixel)
                input[batchNum][x][y][1] = Color.green(pixel)
                input[batchNum][x][y][2] = Color.blue(pixel)
            }
        }

Note that it's often easier to pass a ByteBuffer to tflite, instead of a multidimensional array.



来源:https://stackoverflow.com/questions/58487740/cannot-convert-between-a-tensorflowlite-tensor-with-type-uint8-and-a-java-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!