Convert Mask R-CNN model to TFLite with Tensorflow 2.3

Wathek LOUED
4 min readOct 1, 2020

Mask R-CNN is one of the important models in the object detection world. It was published in 2018 and it has multiple implementations based on Pytorch (detectron2) and Tensorflow (object detection).

In this quick tutorial, we will explore how we can export Mask R-CNN to tflite so that it can be used on mobile devices such as Android smartphones. We are going to use leekunhee/Mask_RCNN version of Mask R-CNN which is a fork of the greatest implementation done by matterport/Mask_RCNN. The latter implementation is based on Keras and Tensorflow 1.13. While the fork done by leekunhee is a porting to Tensorflow 2.0.

We will start by describing the minor changes we have done to keep everything updated and compatible with Tensorfow 2.3 and after that, we will explain the steps to export the model to tflite and make it ready for mobile devices deployment.

Migrate functions to Tensorflow 2.3.0

Moving to Tensorflow 2.3.0 implies using the new set of functions provided by the new API. Most of the work was done by leekunhee, but we noticed that few of the legacy code remains and that we can update it.

The main file that defines the model is located at mrcnn/ Here is the list of the changed things:

At the beginning of the file, we can see this line tf.compat.v1.disable_eager_execution() which is, actually, not needed. Eager execution is one of the greatest features that TF enabled by default starting from version 2.0.

In that same file, we can see the use of some functions coming from tf.compat.v1 such as tf.compat.v1.where so we replaced it with tf.where.

These are the small changes that we did on the initial model. Let’s go to the TFLite conversion part.

Convert Keras Model to TFLite

Tensorflow supports converting Keras models very easily. According to the documentation, all you have to do is to use tf.lite.TFLiteConverter.from_keras_model and given the model, it will convert it to TFLite.

We used Netron which is an amazing tool that helped us in debugging the model and knowing where to look and what to change.

So since we have a Keras Model, we tried to follow the documentation of Tensorflow. We recommend you to open samples/demo.ipynb notebook and to run it first as it is.