Cannot copy to a TensorFlowLite tensor (input_values:0) with 0 bytes from a Java Buffer with 49152 bytes

Viewed 124

Hi I want to feed an input to my .tflite model in android studio 7.2.1 with below code :

        val catBitmap = getBitmapFromAsset("bwr.jpg")// a 64*64 image 
        val output2 = TensorImage(DataType.FLOAT32)
        
        for (y in 0..63) {
            for (x in 0..63) {
                val px: Int = catBitmap.getPixel(x, y)

                // Get channel values from the pixel value.
                val r: Int = Color.red(px)
                val g: Int = Color.green(px)
                val b: Int = Color.blue(px)

                // Normalize channel values to [-1.0, 1.0]. This requirement depends
                // on the model. For example, some models might require values to be
                // normalized to the range [0.0, 1.0] instead.
                val rf = (r - 127) / 255.0f
                val gf = (g - 127) / 255.0f
                val bf = (b - 127) / 255.0f
                input.putFloat(rf)
                input.putFloat(gf)
                input.putFloat(bf)
            }
        }
        
        tflite.run(input, output2)

and this is my tflite model properties:

enter image description here

and it causes this error:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.tag22, PID: 15184
java.lang.IllegalArgumentException: Cannot copy to a TensorFlowLite tensor (input_values:0) with 0 bytes from a Java Buffer with 49152 bytes.

this is my python interpreter code by which I could successfully get predictions:

image_filename='img.jpeg'
input_data  = tf.compat.v1.gfile.FastGFile(image_filename, 
'rb').read()
cc=[input_data ]
input_data = np.array([input_data ])
interpreter.set_tensor(input_details[0]['index'], input_data)

can you please help me to fix this ? I do not know hot to feed input to my model in android.

0 Answers
Related