0
$\begingroup$

I am using Tensorflow.Lite.Support function for Inference of a model that takes two input and gives output in the form of Image. The first input is an RGB image whereas the second image is a single-channel image. When I run the application for inference I get the error that is:

Cannot convert between a TensorFlowLite buffer with 1392640 bytes and a Java Buffer with 4177920 bytes.

I have attached my code snippet below:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        try {
            tflite = new Interpreter(loadModelFile(MainActivity.this, "converted_model.tflite"));


            Bitmap Image = getBitmapFromAsset(this,"001.png");
            Bitmap Mask = getBitmapFromAsset(this,"tmp_mask.png");

            int imageTensorIndex = 0;
            int[] imageShape = tflite.getInputTensor(imageTensorIndex).shape(); // {1, height, width, 3}
            int imageSizeY = imageShape[1];
            int imageSizeX = imageShape[2];
            DataType imageDataType = tflite.getInputTensor(imageTensorIndex).dataType();

            TensorImage inputImageBuffer = new TensorImage(imageDataType);
            inputImageBuffer.load(Image);

            int imageTensorIndex1 = 1;
            int[] imageShape1 = tflite.getInputTensor(imageTensorIndex1).shape(); // {1, height, width, 3}
            int imageSizeY1 = imageShape1[1];
            int imageSizeX1 = imageShape1[2];
            DataType imageDataType1 = tflite.getInputTensor(imageTensorIndex1).dataType();

            TensorImage inputImageBuffer1 = new TensorImage(imageDataType1);
            inputImageBuffer1.load(Mask);


            int OutputTensorIndex = 0;
            int[] OutputShape =
                    tflite.getOutputTensor(OutputTensorIndex).shape(); // {1, NUM_CLASSES}
            DataType OutputDataType = tflite.getOutputTensor(OutputTensorIndex).dataType();

            TensorBuffer outputBuffer = TensorBuffer.createFixedSize(OutputShape, OutputDataType);

            Object[] Inputs = {inputImageBuffer.getBuffer(),inputImageBuffer1.getBuffer()};

            Map<Integer, Object> outputs = new HashMap<>();
            outputs.put(0,outputBuffer);
            tflite.runForMultipleInputsOutputs(Inputs, outputs);

            Toast.makeText(this,"Working",Toast.LENGTH_LONG).show();

        } catch (IOException e) {
            Toast.makeText(this,"Failed",Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.