Machine Learning - MobileNet
Tags: machinelearning, beginners, computerscience
Published: 2020-04-07 14:32:57

---
title: Machine Learning - MobileNet
published: true
description: Machine Learning studies
cover_image: https://i.ibb.co/gyDsXzP/0-hzqhs-KFIZNWJKhvq.jpg
tags: machinelearning, Beginners, Computer Science
series: Making Of Terminator
---
Hey there,
Lets begin with a story.
<i>The real-life story of Colonel Harland Sanders (KFC) who was disappointed umpteen times in his life and still made his dream come true late in his life is really inspiring.
He is a seventh grade dropped out who tried many ventures in life but tasted bitter every time. He started selling chicken at his age of 40 but his dream of a restaurant was turned down many times due to conflicts and wars.
Later he attempted to franchise his restaurant. His recipe got rejected 1,009 times before the final approval. And soon the secret recipe, “Kentucky Fried Chicken” became a huge hit worldwide. KFC was expanded globally and the company was sold for 2 million dollars and his face is still celebrated in the logos.</i>

<blockquote>Have you stopped your attempts to a venture just because you were rejected or failed a few times? Can you even accept a failure of 1009 times? This story inspires everyone to try hard and believe in yourself until you see success despite how many times you have failed.</blockquote>


<h1>Main Content From Here</h1>

As we mentioned before, there have been very successful models that have performed very well on the ImageNet competition, such as 
<ul><li>AlexNet</li><li> Inception</li><li> Resonant</li></ul>
These neural networks tend to be very deep and contain thousands and even millions of parameters. The large number of parameters allows the networks to learn more complex patterns and therefore achieve higher accuracies. However, having such a large number of parameters also means that the model will take a very long time to train, make predictions, use a lot of memory, and are very computationally expensive. 

In this lesson, we will use a state of the art convolutional neural network called MobileNet. 

<blockquote>MobileNet, uses a very efficient neural network architecture that minimizes the amount of memory and computational resources needed while maintaining a high level of accuracy. </blockquote>

This makes MobileNet ideal to work on mobile devices that have limited memory and computational resources. MobileNet was developed by Google and was trained on the ImageNet dataset. 


![details](https://i.ibb.co/R0JzbxS/table.png)

Since it's been trained on ImageNet, it has 1,000 output classes far more than the two we have in our cats and dogs dataset. To perform transfer learning, we'll download the features of MobileNet without the classification layer. 

![details](https://i.ibb.co/WtXhMWc/code.png)

In TensorFlow, the downloaded features can be used as regular KerasLayers with a particular input size. Since MobileNet was trained on ImageNet, we have to match the same input size that was used during training. In our case, MobileNet was trained on RGB images of a fixed size of 224 by 224 pixels. TensorFlow has a repository of pretrained models called TensorFlow hub. 

![details](https://i.ibb.co/GTfNthp/tensor-flow-hub.png)

TensorFlow hub also has pretrained models where the last classification layer has been stripped out. Here, you can see a version of MobileNet version two. 

Using TensorFlow hub from code is really simple, you can just refer to the URL and then embed the pre-trained model as a KerasLayer in a sequential model. 

![details](https://i.ibb.co/3vycgdn/usint-tensorflow-hub.png)

Then, we simply add our classification layer to the end of our model. Since our cat and dog dataset has only two classes, we'll add a dense layer with two outputs and a softmax activation function to our sequential model. 



This dense layer will be our new classifier and it needs to be trained. 

![details](https://i.ibb.co/St64p8Z/classifier.png)


We can train our sequential model just as we did previously using the fit method.




![details](https://i.ibb.co/Jz36XhN/full-code.png)

Adios. Hasta luego.