Unable to get TFlite model working because of wrong inputs

Viewed 15

I have a model called RepNet I want to run on TFlite for Kotlin, the interpreter provides the shape and DataType of the data, but I simply could not get the data to work with the interpreter, I have problems getting it to the correct shape. The data shape is this : The shape

But I simply can't get my list of Images to match the correct shape, I'll include my code down below if someone could help me make the right transformations to get it working and thank you.

class TestActivity2 : AppCompatActivity() {
    @RequiresApi(Build.VERSION_CODES.P)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test2)
        //val file = File("/assets/repnet.tflite")
        //Log.d("Path", file.path)
        var file = loadModelFile(assets,"repnet2.5.tflite")
        var interpreter = Interpreter(file!!)

        var counts = getCount(interpreter)
        interpreter.close()
        Log.d("Counts", counts.toString())
    }
@RequiresApi(Build.VERSION_CODES.P)
private fun getImages(): MutableList<TensorImage> {

    val mmd = MediaMetadataRetriever()
    val path = "/sdcard/bicycle.mp4"
    mmd.setDataSource(path)
    var duration = mmd.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_FRAME_COUNT)

    var inputImages = mutableListOf<TensorImage>()

    Log.d("Duration",duration.toString())
    if (duration != null) {
        for (i in 0 until 50) {
            var bitmap: Bitmap?
            Log.d("Frame", i.toString())
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                    bitmap = mmd.getFrameAtIndex(i)
                    if (bitmap != null) {
                        bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true)
                        inputImages.add(TensorImage.fromBitmap(bitmap))
                    }
                }
            } catch (e: Exception) {
                println("Could not convert image to BitMap")
                e.printStackTrace()
            }

        }
    }
    return inputImages
}

    fun preprocessImages(images: List<TensorImage>): List<TensorImage> {
        val imageMean = 127.5F
        val imageStd = 127.5F
        val imageSize = 224
        val imageProcessor = ImageProcessor.Builder()
            .add(CastOp(DataType.FLOAT32))
            .add(NormalizeOp(imageMean,imageStd))
            .add(ResizeOp(imageSize,imageSize,ResizeOp.ResizeMethod.BILINEAR))
            .build()
        val preprocessedImages = mutableListOf<TensorImage>()
        for (image in images) {
            val processedImage = imageProcessor.process(image)
            preprocessedImages.add(processedImage)
        }
        return preprocessedImages
    }


    @RequiresApi(Build.VERSION_CODES.P)
    fun getCount(interpreter: Interpreter): Int {
        var images = getImages()
        images = preprocessImages(images as List<TensorImage>).toMutableList()
        val batchImages = mutableListOf<TensorBuffer>()
        for(img in images) {
            batchImages.add(img.tensorBuffer)
        }

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


        interpreter.run(images[0].tensorBuffer,outputBuffer.buffer)

        Log.d("Result", outputBuffer.floatArray[0].toString())
        Log.d("Output", outputBuffer.toString())
        return 0
    }

    @Throws(IOException::class)
    private fun loadModelFile(assets: AssetManager, modelFilename: String): MappedByteBuffer? {
        val fileDescriptor = assets.openFd(modelFilename)
        val inputStream = FileInputStream(fileDescriptor.fileDescriptor)
        val fileChannel: FileChannel = inputStream.getChannel()
        val startOffset = fileDescriptor.startOffset
        val declaredLength = fileDescriptor.declaredLength
        return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength)
    }
}
0 Answers
Related