In my previous article about TensorFlow I’ve shown you, how to write an app on Android using TensorFlow API. In this one, I’m gonna show you how to create a model that was used in that example.

To create a model we can go with one of these two approaches:

Create the whole model from scratch, train it, evaluate it and freeze it. Retrain already created model with our own data.

The second one is much easier to do so we’re gonna start from that.

MobileNet

Yes, there are models out there which enable us to use them for our purposes. MobileNet is one of them and it’s optimized for mobile devices in contrast to Inception, which basically we can say that is optimized for accuracy.

Scheme of the Inception model

Simply put, the larger the model is, the bigger size it has. If we’re gonna solve some very complex problem, in a sense, we are forced to create a large graph which is able to solve it. And we want our model to be super accurate, right?On the one hand “hell, yeah!”, but on the other one its size has to be small enough to use it on mobile devices.

As you can see, the Inception model is more accurate than the MobileNet, but what we can’t see in the above table is that the MobileNet is much lighter.

For example, the model that I’ve trained for Hot Or Not example was trained on over 300 pictures. Using MobileNet the retrained model has about 13MB but with Inception over 87MB — so it’s a meaningful difference.

Retraining the model

All we need to do for retraining the model is to run 2 commands. To be able to do that we need 2 things:

TensorFlow installed. Directory with our images.

Ok, so let’s look at the TensorFlow Hub GitHub repo where we can find a script called retrain.py. What do you think it’s doing 🙃?