android缓冲区大小错误上的Tensorflowlite

Posted

技术标签:

【中文标题】android缓冲区大小错误上的Tensorflowlite【英文标题】:Tensorflowlite on android buffer size error 【发布时间】:2021-09-30 07:59:20 【问题描述】:

我正在尝试构建一个图像分类器 android 应用程序。我已经使用 keras 构建了我的模型。 模型如下:

model.add(MobileNetV2(include_top=False, weights='imagenet',input_shape=(224, 224, 3)))
model.add(GlobalAveragePooling2D())
model.add(Dropout(0.5))
model.add(Dense(3, activation='softmax'))

model.layers[0].trainable = False     
model.compile(optimizer='adam',  loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()

输出:

Model: "sequential_3"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
mobilenetv2_1.00_224 (Functi (None, 7, 7, 1280)        2257984   
_________________________________________________________________
global_average_pooling2d_2 ( (None, 1280)              0         
_________________________________________________________________
dropout_2 (Dropout)          (None, 1280)              0         
_________________________________________________________________
dense_1 (Dense)              (None, 3)                 3843      
=================================================================
Total params: 2,261,827
Trainable params: 3,843
Non-trainable params: 2,257,984

训练后我使用转换模型

model = tf.keras.models.load_model('model.h5')
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
open(f"myModel.tflite", "wb").write(tflite_model)

对于android,代码如下:

        make_prediction.setOnClickListener(View.OnClickListener 
            var resized = Bitmap.createScaledBitmap(bitmap, 224, 224, true)
            val model = MyModel.newInstance(this)
            var tbuffer = TensorImage.fromBitmap(resized)
            var byteBuffer = tbuffer.buffer

// Creates inputs for reference.
            val inputFeature0 = TensorBuffer.createFixedSize(intArrayOf(1, 224, 224, 3), DataType.FLOAT32)
            inputFeature0.loadBuffer(byteBuffer)

// Runs model inference and gets result.
            val outputs = model.process(inputFeature0)
            val outputFeature0 = outputs.outputFeature0AsTensorBuffer

            var max = getMax(outputFeature0.floatArray)

            text_view.setText(labels[max])

// Releases model resources if no longer used.
            model.close()
        )

但每当我尝试运行我的应用程序时,它都会关闭,并且我在 logcat 中收到此错误。

java.lang.IllegalArgumentException: The size of byte buffer and the shape do not match.

如果我将图像的输入形状从 224 更改为 300,并在 300 输入形状上训练我的模型并插入 android,我会收到 anthor 错误。

java.lang.IllegalArgumentException: Cannot convert between a TensorFlowLite buffer with 1080000 bytes and a Java Buffer with 150528 bytes

任何形式的帮助都将不胜感激。

【问题讨论】:

为什么要创建 TensorBuffer.createFixedSize? var byteBuffer 可以从模型中处理。像 val outputs = model.process(byteBuffer) 。检查它,如果它不工作就回来 【参考方案1】:

像这样使用它:

make_prediction.setOnClickListener(View.OnClickListener 
            var resized = Bitmap.createScaledBitmap(bitmap, 224, 224, true)
            val model = MyModel.newInstance(this)
            var tImage = TensorImage(DataType.FLOAT32)
            var tensorImage = tImage.load(resized)
            var byteBuffer = tensorImage.buffer

// Creates inputs for reference.
            //val inputFeature0 = TensorBuffer.createFixedSize(intArrayOf(1, 224, 224, 3), DataType.FLOAT32)
            //inputFeature0.loadBuffer(byteBuffer)

// Runs model inference and gets result.
            val outputs = model.process(byteBuffer)
            val outputFeature0 = outputs.outputFeature0AsTensorBuffer

            var max = getMax(outputFeature0.floatArray)

            text_view.setText(labels[max])

// Releases model resources if no longer used.
            model.close()
        )

然后用调试器检查问题是否仍然存在或

val outputFeature0 = outputs.outputFeature0AsTensorBuffer 导致另一个。

如果您需要更多帮助,请联系我

【讨论】:

以上是关于android缓冲区大小错误上的Tensorflowlite的主要内容,如果未能解决你的问题,请参考以下文章

我可以在 Android 上拥有两个屏幕大小的乒乓帧缓冲区吗?

android音池堆大小溢出

错误记录Android Studio Logcat 报错 ( read: unexpected EOF! )

TcpWindowSize 与 Windows 上的套接字缓冲区大小

如何获取/设置默认帧缓冲区的宽度和高度?

Android Studio logcat 历史记录/缓冲区大小