2

I am making my first ML integrated android app and I am trying to add this ocr model into my app. But I am facing this error

Caused by: java.lang.IllegalArgumentException: The size of byte buffer and the shape do not match.
    at org.tensorflow.lite.support.common.SupportPreconditions.checkArgument(SupportPreconditions.java:104)
    at org.tensorflow.lite.support.tensorbuffer.TensorBuffer.loadBuffer(TensorBuffer.java:296)
    at org.tensorflow.lite.support.tensorbuffer.TensorBuffer.loadBuffer(TensorBuffer.java:323)
    at com.security.ml.MainActivity.onDetect(MainActivity.kt:36)
    at java.lang.reflect.Method.invoke(Native Method) 
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:441) 
    at android.view.View.performClick(View.java:7585) 
    at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119) 
    at android.view.View.performClickInternal(View.java:7541) 
    at android.view.View.access$3900(View.java:842) 
    at android.view.View$PerformClick.run(View.java:28875) 
    at android.os.Handler.handleCallback(Handler.java:938) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:255) 
    at android.app.ActivityThread.main(ActivityThread.java:8212) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:632) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1049) 

This is how I tried to integrate it

        val model = Linear.newInstance(this)
    val imageview = findViewById<ImageView>(R.id.imageview)
    var bitmap = imageview.getDrawable().toBitmap()
    bitmap = Bitmap.createScaledBitmap(bitmap, 32, 32, true)

    val inputFeature0 = TensorBuffer.createFixedSize(intArrayOf(1, 32, 32, 1), DataType.FLOAT32)

    val tensorImage = TensorImage(DataType.FLOAT32)
    tensorImage.load(bitmap)
    val byteBuffer = tensorImage.buffer

    inputFeature0.loadBuffer(byteBuffer)

    val outputs = model.process(inputFeature0)
    val outputFeature0 = outputs.outputFeature0AsTensorBuffer

    model.close()

    Toast.makeText(this, outputFeature0.floatArray[0].toString(),Toast.LENGTH_LONG).show()

I saw many other same questions and tried those solution, I tried to change the size of tensorBuffer by 4 times, I tried to pass the byteBuffer directly into model.process() funtion. But none of them worked. What can be the problem, please Help.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
Amandeep
  • 31
  • 2
  • 4

2 Answers2

0

The bitmap you are creating is most likely from a image that has three channels RGB so your buffer will be 32 x 32 x 3 = 3072

bitmap = Bitmap.createScaledBitmap(bitmap, 32, 32, true)

but you need to send Gray-Scale images to your model as it indicates [1,32,32,1] it only needs single Chanel so buffer should be 32 x 32 x 1 = 1024

val inputFeature0 = TensorBuffer.createFixedSize(intArrayOf(1, 32, 32, 1), DataType.FLOAT32)

Debug

There is a way to check the buffer.

Log.d("shape", byteBuffer.toString())
Log.d("shape", inputFeature0.buffer.toString())

And check the output of the buffer to find any difference in it.

M Tayyab Asghar
  • 322
  • 2
  • 11
0

Don't forget to match the data type to float(32):

TensorImage ti = new TensorImage(DataType.FLOAT32);
ti.load(bitmapPred);

Based on my experience, it works and I use a Bitmap for my input. Sorry if that's different for you.

source: https://stackoverflow.com/a/69257283/

General Grievance
  • 4,555
  • 31
  • 31
  • 45