Bagisto Hosting
On this page

Table of Content

Transfer learning with mobilenet and run on Browser in TensorFlowJs

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

 

directory

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

toplayer

 

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.
  • The directory must be set to the path where your ‘n’ classes of folders are present.
  • The target_size is the size of your input images, every image will be resized to this size.
  • color_mode: if the image is either black and white or grayscale set “grayscale” or if the image has three color channels, set “RGB”.
  • batch_size: No. of images to be yielded from the generator per batch.
  • class_mode: Set “binary” if you have only two classes to predict, if not set to“categorical”, in case if you’re developing an Autoencoder system, both input and the output would probably be the same image, for this case set to “input”.
  • shuffle: Set True if you want to shuffle the order of the image that is being yielded, else set False.


 

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


full layer

 

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.



prediction

 

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
  • Git clone https://github.com/tensorflow/tfjs-examples.git (clone this project and follow the README to run this project ) -> goto the mobilenet folder -> goto the dist folder -> copy here the folder where the JSON model was saved
  • Note: to avoid the error of the Functional layer we have to replace the “Functional” keyword with “Model” in the model.json file.
  • Open the index.js file and replace the MOBILENET_MODEL_PATH value from your folder path where the model is.
  • Also, you have to create an imagenet_classes.js file like this
  • imagenetclasses
  • Change the topk=3 ( because we are using three classes as I mentioned before ) value in index.js
  • Now you can run the project by “npm run watch” and browse your image which you wanna test and see the prediction.

Here is my output

output

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