The size of byte buffer and the shape do not match (previously not answered)

Viewed 838

I am trying to create a app that uses tensorflow model. My app crashes when inputFeature0.loadBuffer(byteBuffer) gets executed. ( got to know by commenting the lines)

var img=Bitmap.createScaledBitmap(bmp,229,229,true)
val model = SkinDiseasesDetectionFitSizeFinal24120210504.newInstance(this)
val inputFeature0 = TensorBuffer.createFixedSize(intArrayOf(1, 229, 229, 3), DataType.FLOAT32)
var tensorimage=TensorImage.fromBitmap(img)
var byteBuffer=tensorimage.buffer
inputFeature0.loadBuffer(byteBuffer)

This is the error

FATAL EXCEPTION: main
Process: com.azsky.skincancerdetection, PID: 31954
java.lang.IllegalArgumentException: The size of byte buffer and the shape do not match.

Can someone help me out in this?

2 Answers

I had same problem. I don't know why does this occurs but it is maybe because of DataType.FLOAT32. I printed both buffers and found the total size was different.

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

My inputFeature0.buffer was 4-times bigger that byteBuffer. Found it by dividing the both buffers. The solution i got from someplace it was

You should multiply your buffer with 4.

According to their explanation my buffer should be like this

buffer = image.width x image.height x colorChanels x 4

But i don't know how to change the total buffer instead is double the width and height to have the same effect.

Try using 458 x 458 image size.

Related