Search code examples
androidtensorflow-liteobject-detection-apikotlin-android-extensionsmobilenet

what should be the input and output forom of variables for float object detection model


https://storage.googleapis.com/download.tensorflow.org/models/tflite/mobilenet_ssd_tflite_v1.zip

I am making an android object detection app with gpu delegate support. The above link is for tensorflow lite object detection float model. There is no documentation available for this. I want to know the input and output form of the variables for this tflite models so that i can feed it to the interpreter for gpu delegation. Thanks in advance!


Solution

  • I use colaboratory. So I use below code to determine inputs and outputs:

    import tensorflow as tf
    interpreter = tf.lite.Interpreter('mobilenet_ssd.tflite')
    print(interpreter.get_input_details())
    print(interpreter.get_output_details())
    

    So unzip the folder, find the file and load it with above code. I did that with above code and the result was:

    [{'name': 'Preprocessor/sub', 'index': 165, 'shape': array([ 1, 300, 300, 3], dtype=int32), 'shape_signature': array([ 1, 300, 300, 3], dtype=int32), 'dtype': , 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}]

    [{'name': 'concat', 'index': 172, 'shape': array([ 1, 1917, 4], dtype=int32), 'shape_signature': array([ 1, 1917, 4], dtype=int32), 'dtype': , 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'concat_1', 'index': 173, 'shape': array([ 1, 1917, 91], dtype=int32), 'shape_signature': array([ 1, 1917, 91], dtype=int32), 'dtype': , 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}]

    Also inside android you can do:

    // Initialize interpreter
    @Throws(IOException::class)
    private suspend fun initializeInterpreter(app: Application) = withContext(Dispatchers.IO) {
        // Load the TF Lite model from asset folder and initialize TF Lite Interpreter without NNAPI enabled.
        val assetManager = app.assets
        val model = loadModelFile(assetManager, "mobilenet_ssd.tflite")
        val options = Interpreter.Options()
        options.setUseNNAPI(false)
        interpreter = Interpreter(model, options)
        // Reads type and shape of input and output tensors, respectively.
        val imageTensorIndex = 0
        val inputShape: IntArray =
            interpreter.getInputTensor(imageTensorIndex).shape() // {1, length}
        Log.e("INPUT_TENSOR_WHOLE", Arrays.toString(inputShape))
        val imageDataType: DataType =
            interpreter.getInputTensor(imageTensorIndex).dataType()
        Log.e("INPUT_DATA_TYPE", imageDataType.toString())
    
        //modelInputSize indicates how many bytes of memory we should allocate to store the input for our TensorFlow Lite model.
        //FLOAT_TYPE_SIZE indicates how many bytes our input data type will require. We use float32, so it is 4 bytes.
        //PIXEL_SIZE indicates how many color channels there are in each pixel. Our input image is a colored image, so we have 3 color channel.
        inputImageWidth = inputShape[1]
        inputImageHeight = inputShape[2]
        modelInputSize = FLOAT_TYPE_SIZE * inputImageWidth *
                inputImageHeight * PIXEL_SIZE
    
        val probabilityTensorIndex = 0
        outputShape =
            interpreter.getOutputTensor(probabilityTensorIndex).shape()// {1, NUM_CLASSES}
        Log.e("OUTPUT_TENSOR_SHAPE", outputShape.contentToString())
        val probabilityDataType: DataType =
            interpreter.getOutputTensor(probabilityTensorIndex).dataType()
        Log.e("OUTPUT_DATA_TYPE", probabilityDataType.toString())
        isInitialized = true
        Log.e(TAG, "Initialized TFLite interpreter.")
    
    
        // Inputs outputs
        /*val inputTensorModel: Int = interpreter.getInputIndex("input_1")
        Log.e("INPUT_TENSOR", inputTensorModel.toString())*/
    
    }
    
    @Throws(IOException::class)
    private fun loadModelFile(assetManager: AssetManager, filename: String): MappedByteBuffer {
        val fileDescriptor = assetManager.openFd(filename)
        val inputStream = FileInputStream(fileDescriptor.fileDescriptor)
        val fileChannel = inputStream.channel
        val startOffset = fileDescriptor.startOffset
        val declaredLength = fileDescriptor.declaredLength
        return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength)
    }
    

    If you need any help tag me.