java.lang.illegalargumentexception: cannot copy to a tensorflowlite tensor (serving_default_input:0) with 2408448 bytes from a java buffer with 150528 bytes.

Hi, I’m new to Android studio and TF and I’m having an issue. I convert my .pth file to .tflite file and imported into Android Studio. When I run my app, Logcat shows there is something wrong.

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.skinapp/com.example.skinapp.MainActivity}: java.lang.IllegalArgumentException: Cannot copy to a TensorFlowLite tensor (serving_default_input:0) with 2408448 bytes from a Java Buffer with 8064 bytes.

Following is my while code of MainActivity.kt.

package com.example.aifer   import android.content.ActivityNotFoundException import android.content.Intent import android.graphics.Bitmap import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.provider.MediaStore import android.widget.* import com.example.skinapp.ml.ModelMeta import org.tensorflow.lite.support.image.TensorImage import java.io.IOException import android.graphics.drawable.BitmapDrawable import com.example.skinapp.R import kotlin.math.roundToInt   class MainActivity : AppCompatActivity() {     override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         setContentView(R.layout.activity_main)         findViewById<Button>(R.id.btn_photo).setOnClickListener {              //Create an Intent object for image acquisition              val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)             //Use try-catch to avoid exceptions, and if they occur, display Toast             try {                 startActivityForResult(intent, 0) //Send Intent             } catch (e: ActivityNotFoundException) {                 Toast.makeText(                     this,                     "error", Toast.LENGTH_SHORT                 ).show()             }         }          findViewById<Button>(R.id.btn_album).setOnClickListener {             //Create an Intent object for image acquisition             val intent =                 Intent(Intent.ACTION_GET_CONTENT).setType("image/*")             //Use try-catch to avoid exceptions, and if they occur, display Toast             try {                 startActivityForResult(intent, 1) //發送 Intent             } catch (e: ActivityNotFoundException) {                 Toast.makeText(                     this,                     "error", Toast.LENGTH_SHORT                 ).show()             }         }     }      // receive results     override fun onActivityResult(requestCode: Int,                                   resultCode: Int, data: Intent?) {         super.onActivityResult(requestCode, resultCode, data)         //Identify returned objects and execution results         if (requestCode == 0 && resultCode == RESULT_OK) {             val image = data?.extras?.get("data") ?: return //Get information             val bitmap = image as Bitmap //Convert data to Bitmap             val imageView = findViewById<ImageView>(R.id.imageView)             imageView.setImageBitmap(bitmap) //Using Bitmap to set images             imageView.rotation = 90f //Make the ImageView rotate 90 degrees clockwise             recognizeImage(bitmap) //Use Bitmap for identification          }         if (requestCode == 1 && resultCode == RESULT_OK) {             val uri = data!!.data             val imageView = findViewById<ImageView>(R.id.imageView)             imageView.setImageURI(uri)             imageView.rotation = 0f             val drawable = imageView.drawable as BitmapDrawable //Obtain data from imageView and convert it into Bitmap             val bitmap = drawable.bitmap             recognizeImage(bitmap) //Use Bitmap for identification         }     }      // Recognize images     private fun recognizeImage(bitmap: Bitmap) {         try {             // Loads my custom model             val model = ModelMeta.newInstance(this)              // Creates inputs for reference.             val tensorImage = TensorImage.fromBitmap(bitmap)              // Runs model inference and gets result.             val outputs = model.process(tensorImage)                 .probabilityAsCategoryList.apply {                     sortByDescending { it.score } // Sort from high to low                 }              //Obtain identification results and credibility             val result = arrayListOf<String>()             for (output in outputs) {                 val label = output.label                 val score: Int = (output.score * 100).roundToInt()                 result.add("The probability that the disease is $label is $score %")             }              //Display results in ListView             val listView = findViewById<ListView>(R.id.listView)             listView.adapter = ArrayAdapter(this,                 android.R.layout.simple_list_item_1,                 result             )         } catch (e: IOException) {             e.printStackTrace()         }     } } 

I’d really appreciate if somebody could give me a hint.

1 Like

Hi @user253, This error might be due to input data mismatch between the size of the input tensor expected by your TensorFlow Lite model and the size of the input data passed to the java buffer.
Also make sure that you have passed the correct data type of the input tensor expected by your TensorFlow Lite model. Thank You.