0

I am using this collab to create my tensorflow model. I am adding this code in the collab to export the model as a tflite file:

saved_model_dir = '/created_model'
model.save('/created_model')
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
tflite_model = converter.convert()
from google.colab import files
open("converted_model.tflite", "wb").write(tflite_model)
files.download('converted_model.tflite')

After importing the model in AndroidStudio. I am trying to run the model using bitmap but I got this error:

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

This is the code I used in android side

val model = ConvertedModel.newInstance(requireContext())
val inputFeature0 = TensorBuffer.createFixedSize(intArrayOf(1, 180, 180, 3), DataType.FLOAT32)
//get the bitmap file
val bitmap = ImageDecoder.decodeBitmap(ImageDecoder.createSource(requireContext().contentResolver, data?.data!!))
// get the byte buffer from bitmap file

val stream = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream)
val byteArray = stream.toByteArray()
// This line when the error happened
inputFeature0.loadBuffer(ByteBuffer.wrap(byteArray))
val outputs = model.process(inputFeature0)
val outputFeature0 = outputs.outputFeature0AsTensorBuffer
model.close()

Any helps or leads would be appreciated. Thank you!

mangkool
  • 316
  • 2
  • 18

2 Answers2

0

PNG format is compressed type but the model takes a (1 x 180 x 180 x 3) size long uncompressed float32 image.

Please check out the model input/output specification again and find a way to convert the given PNG formatted image to the model's input image format.

Jae sung Chung
  • 835
  • 1
  • 6
  • 7
0

If the size of the image you input is not the same as inputfeature0, you should resize the image first to match that of the input feature:

val resize = Bitmap.createScaledBitmap(*input image*, 180, 180, true)

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

var tensorImage = TensorImage(DataType.FLOAT32)
tensorImage.load(resize)

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

ouflak
  • 2,458
  • 10
  • 44
  • 49