Read Story
Avail Here
Visit Here
Akanksha Gupta

Image Classification Model in keras from Scratch

In the previous tutorial, we learned what is transfer learning and mobilenet. how we load and train the pre-trained model with our problem.

In this tutorial, we are going to build an Image Classification model from scratch using Keras in the backend without leveraging pre-trained weights or a pre-made Keras Application model. This implementation is done on dag vs cat. if you are a beginner in ML so you do not need to worry this blog will help you to under about creating a model and train that model. okay, so Let’s start

You should install the dependencies first

Install and setup anaconda for Ubuntu:-

Installation:

  1. In your browser, download the Anaconda installer for Linux.
  2. 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

  3. Scroll to the bottom of the license terms and enter “Yes” to agree.
  4. 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.
  5. The installer prompts “Do you wish the installer to initialize Anaconda3 by running conda init?” We recommend “yes”.
  6. 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 :

 

You can collect datasets from here:-

You can choose small size datasets because it may be taking too much time to train the model so I am using 500 images of each cat and dog to train my model so that it will take much less time to train the model :

The folder structure must be like this

 

Import the dependent packages.

 

Initialize the variables

 

Load the datasets

We are now loading the datasets from the given path and process the images like change the images from color to gray-scale and resize the images after that we will pass through the model :

 

Create the model with different-2 layers.

Sequential model is appropriate for a plain stack of layers where each layer has exactly one input tensor and one output tensor. we are using to create a Sequential model incrementally via the add() method:

 

Compile the model

Once the model is created, we can config the model with losses and metrics with the model. compile(). we are using adam optimizer here :

 

Train the model

We have created a model now we have to train our model:

 

Save the model

Training is done therefore, we can save the model and their weights for further use :

 

See the model architecture.

When building a new Sequential architecture, it’s useful to incrementally stack layers withadd() and frequently print model summaries. For instance, this enables you to monitor how a stack of Conv2D and MaxPooling2D layers are downsampling image feature maps:

Once you have a model train your model, evaluate it, and run and we trained our model so it’s time to evaluate the model :

 

Load the images for testing.

Before the test, the image it needs to pre-process the image according to model requirement like it should be grey-scale image and size must be 100×100 therefore, we are defining a function to load and pre-process the image

 

Predict the result.

We are going to print the prediction result.

Congratulations you build an image classification model in Keras from scratch.

 

Conversion of the model in JSON format

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 project directory.

 

 

 

I Hope This Blog Will Help You To Make an Understanding to build a model and train that model from scratch. Feel Free To Comment If Any Problem Or For Any Suggestions. for more blogs like this please check out here. Thank You

 

Reference

 

. . .

Comment

Add Your Comment

Be the first to comment.

css.php