Issue
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.
@RequiresApi(Build.VERSION_CODES.P)
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){
listImages.add(i,images[i].tensorBuffer)
}
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())
inputBuffer.loadArray(imgBatches,interpreter.getInputTensor(0).shape())
interpreter.run(inputBuffer,outputBuffer)
interpreter.close()
Log.d("Output", outputBuffer.toString())
return 0
}
Solution
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:
@RequiresApi(Build.VERSION_CODES.P)
fun getCount(interpreter:Interpreter): Int {val imageProcessor = ImageProcessor.Builder() .add() ... .add() .build() 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.