[SOLVED] TFlite Model that runs on a List of tensor Images


This Content is from Stack Overflow. Question asked by Firas SOLTANI

So I’m stuck trying to feed this model input data, it’s supposed to take an array of TensorImages, but I’m stuck doing it, if someone could help me with it I would really appreciate it, I can’t figure it out.

fun getCount(interpreter: Interpreter): Int {
    var images = getImages()
    images = preprocessImages(images as ArrayList<TensorImage>) as ArrayList<TensorImage>
    var listImages = mutableListOf<TensorBuffer>()
    for(i in 0 until images.size){
    var imgBatches = mutableListOf<List<TensorBuffer>>()
    imgBatches.add(0,listImages as List<TensorBuffer>)

    var inputBuffer = TensorBuffer.createFixedSize(interpreter.getInputTensor(0).shape(),interpreter.getInputTensor(0).dataType())
    var outputBuffer = TensorBuffer.createDynamic(interpreter.getOutputTensor(0).dataType())

    Log.d("Output", outputBuffer.toString())
    return 0


You can’t pass a Kotlin Mutable list to inputBuffer.loadArray()
It needs to be one Array. The quickest fix is to pass the images one by one in a loop. I also suggest you look into ImageProcessor to prepare your input, it will help you avoid further bugs. I don’t know what the format of the image is and what the output is supposed to be but assuming it’s a bitmap:

fun getCount(interpreter:Interpreter): Int {

val imageProcessor = ImageProcessor.Builder()


var images = getImages()

for (img in images) {

    val tensorImage = imageProcessor.process(TensorImage.fromBitmap(img))
    val inputBuffer = tensorImage.tensorBuffer

    val pred = interpreter.process(buffer)
    val outputBuffer = pred.outputFeature0AsTensorBuffer

This Question was asked in StackOverflow by Firas SOLTANI and Answered by nayriz It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?