Transfer learning with mobilenet and run on Browser in TensorFlowJs

Updated 3 March 2021

Transfer learning with MobileNet

In this tutorial, you will learn how to classify images of cpu, ac, and shoe using transfer learning from a pre-trained model and we are going to use MobileNet to re-trained with our customize datasets. we don’t need to make a model from scratch we are going to load mobilenet model which is already trained on 1000 classes of datasets.

Before we start, You should know about Transfer learning and MobileNet. Therefore, here’s a quick intro

Transfer Learning,  The idea behind transfer learning is to use some pre-trained networks. therefore we do not need to train the network from scratch for our datasets some pre-trained networks are like VGG19, MobileNet , VGG16, and more.

MobileNet is based on the CNN architecture model for image classification and mobile vision. More models are available as well but Mobilenet is special because it consumes very little computation power to run also apply transfer learning. that’s why mobileNet is perfect for mobile devices also best for web browsers. MobileNet has 28 layers. standard MobileNet has 4.2 million parameters. Click here for more

Let’s begin step by step :

 

Step 1: you should install the dependencies first
Install and setup anaconda for Ubuntu:-
Installation:
  1. In your browser, download the Anaconda installer for Linux.

Open the terminal and run the command

.bash ~/Downloads/Anaconda32020.02Linuxx86_64.sh(your downloaded sh filename)

Note: If you did not download to your Downloads directory, replace ~/Downloads/ with the path to the file you downloaded

  1. Scroll to the bottom of the license terms and enter “Yes” to agree.
  2. The installer prompts you to click Enter to accept the default install location, CTRL-C to cancel the installation, or specify an alternate installation directory.
  3. The installer prompts “Do you wish the installer to initialize Anaconda3 by running conda init?” We recommend “yes”.
  4. The installer finishes and displays “Thank you for installing Anaconda!”
Setup :

Create environment: conda create –name myenv or conda create -n myenv

Activate env: conda activate myenv

Deactivate env: conda deactivate

Env list : conda env list

Install other dependencies :

 

Step 2: Collect the datasets of AC, CPU, and Shoe from google and save them in different-2 folders with a folder named AC, CPU, and Shoe under a single folder named as datasets. like this

 

Step 3: It’s time for coding :

 

Import the packages save as model.py

 

Load the Mobilenet model

 

Freeze the base layers and let’s add and train the top layer.

this is the layer that we added above

Now let’s load the training data into the ImageDataGenerator. Specify a path, and it automatically sends the data for training in batches, simplifying the code.flow_from_directory is a function of ImageDataGenerator_to read the images from a big NumPy array and folders containing images.

Calling flow_from_directory(main_directory, labels='inferred') will return a tf.data.Dataset that yields batches of images from the subdirectories 'AC','CPU' and 'Shoe' together with labels 0, 1, and 2 (0 correspondings to 'AC', 1 corresponding to 'CPU' and 2 correspondings to 'Shoe').

Here are the most used attributes along with the flow_from_directory()  method.

 

If you are training a much larger model and want to pre-trained weights so it is important to use a lower learning rate at this stage. or your model could over-fit very quickly.

Adam is a replacement optimization algorithm and it is usefull for stochastic gradient descent for training deep learning models.

 

Train the model with our datasets

 

Save the model and their weights as h5 file

 

After that, you can also See the model architecture

Load image for testing on the terminal and print the prediction value. before the test of the image, we have to pre-process the image according to the image size, which was we use at training time.

 

Okay so the model saved in the .h5 extension and we need a .json model for browser use so that we have to convert our h5 format model into a JSON formate model. therefore here is the code to convert the model file format you must run this command on the terminal inside the directory.

 

Step 4: It’s time to run or test the model on the browser

Here is my output

 

 

I hope this blog will help you to make understanding about transfer learning with mobileNet . Feel free to comment if any problem or for any suggestions. For more such blogs click here. Thank You

 

Reference Links :

 

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project




    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home