首页 > 解决方案 > 字节缓冲区的大小和形状不匹配(以前没有回答)

问题描述

我正在尝试创建一个使用 tensorflow 模型的应用程序。inputFeature0.loadBuffer(byteBuffer)我的应用程序在执行时崩溃。(通过评论来了解)

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)

这是错误

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

有人可以帮我解决这个问题吗?

标签: tensorflowkotlin

解决方案


我有同样的问题。我不知道为什么会发生这种情况,但可能是因为DataType.FLOAT32. 我打印了两个缓冲区,发现总大小不同。

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

inputFeature0.buffer4-times更大byteBuffer。通过划分两个缓冲区找到它。我从某个地方得到的解决方案是

您应该将缓冲区乘以 4。

根据他们的解释,我的缓冲区应该是这样的

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

但是我不知道如何更改总缓冲区,而是将其加倍widthheight具有相同的效果。

尝试使用458x458图像大小。


推荐阅读