JVM exception occurred: Cannot copy to a TensorFlowLite tensor (x) with 6488064 bytes from a Java Buffer with 2162688 bytes. java.lang.IllegalArgumentException

I am trying to use tflite in python but can’t get past this point:

def pred(self, x):
            input = ByteBuffer.wrap(x.tobytes())
            output = TensorBuffer.createFixedSize(self.output_shape, self.output_type)
            *self.interpreter.run(input, output.getBuffer().rewind())*  <- this line throws the error
            return np.reshape(np.array(output.getFloatArray()), self.output_shape)

jnius.jnius.JavaException: JVM exception occurred: Cannot copy from a TensorFlowLite tensor (Identity) with 2162688 bytes to a Java Buffer with 4 bytes. java.lang.IllegalArgumentException

If I change it to:

def pred(self, x):        
            *input = ByteBuffer.allocateDirect(2162688)*
            output = TensorBuffer.createFixedSize(self.output_shape, self.output_type)
            self.interpreter.run(input, output.getBuffer().rewind())
            return np.reshape(np.array(output.getFloatArray()), self.output_shape)

I get:
JVM exception occurred: Cannot copy to a TensorFlowLite tensor (x) with 6488064 bytes from a Java Buffer with 2162688 bytes. java.lang.IllegalArgumentException

I’d really appreciate if somebody could give me a hint what this is about.

1 Like

Hi @asdfgh12321

This error shows clearly that the input buffer is incorrect. If you make it
input = ByteBuffer.allocateDirect(6488064)
then probably it will proceed with success.
If this is an image are you sure you are creating the input buffer with all 3 channels of the image?

Regards

1 Like