Build an AI based image classification (Healthy vs Infected plant) tool yourself

Share it with your senior IT friends and colleagues
Reading Time: 4 minutes

Build an AI based image classification tool yourself

Namaste, and welcome to Build it yourself !!

Gone are the days when it used to take days (if not months) to build an AI tool.

Today, you can make an AI tool for Image segmentation in less than 5 minutes.

Don’t believe me? Read on and see it yourself. 

Here is the Google Colab notebook we will be using to build it – https://github.com/tayaln/Image-Classification/blob/main/Infected_vs_Healthy_plant_image_classification.ipynb

You don’t have to write any code. The code is written for you. You just need to run the cells.

Kaggle Notebook is like Google Colab where you can execute a piece of code in the browser itself. 

This notebook is created by Jeremy Howard for his Fast.ai course. I have just customized it to classify Healthy plants vs Infected plants.

Here is the macro-level view of what we will be doing

  • We will use a pre-trained AI model – Resnet18
  • We will then train the model with a few images 
  • The model will classify images and also provide the probability of that prediction

Seems cool, right? Let’s begin.

The concept of taking an existing pre-trained model and then training it with our data is known as Transfer Learning.

Building AI tool for detecting “Infected vs. Healthy plant

Open the Google Colab notebook to get started

https://github.com/tayaln/Image-Classification/blob/main/Infected_vs_Healthy_plant_image_classification.ipynb

Run the first cell to install the DuckDuckGo search engine.

Why we are installing DuckDuckGo?

We need training data for our AI model to learn. As we are building a Healthy vs Infected plant image classification tool, we need a few images of a Healthy plant and a few images of an Infected plant

One way is to search for these images on Google ourselves, download them and then upload them for the model training.

But a better way is to automate this process by using the “DuckDuckGo” search engine. Our code will search the images based on our keyword and fetch their URL  

And hence we are installing it in this step

Step 1 – Install the dependencies

Let’s search for a photo of an infected plant and check if this system is working properly. We will get the URL of the image first.

And then see the image.

Run the next 3 cells to do so.

You should be able to see an image after running the 3rd cell

Now let’s do the same activity for healthy plant photos.  As you can see, we are searching with the keyword “healthy plant photos” in DuckDuckGo search engine

Once you see an image of a Healthy plant, we are sure that we are getting the right images from the search engine. You can ignore the warnings.

Let us search for some more images. In the next cell, we are using 2 more keywords for both infected and healthy plant

  • Leaves photo 
  • Side photo

You can be creative here and write your keywords to get more varied and accurate results

Step 2 – Training the model.

Collecting the data and then cleaning the data are 2 major activities of Data scientists/ AI professionals. A majority of their time is spent doing these activities. 

In the next cell, let’s check if there are failed downloads or not. If there are incomplete images, we have to remove them. 

Incorrect images will hamper our AI model’s learning. Running the cell will throw a number for failed downloads and unlink them.

In the next cell, let’s visualise our remaining images.

As we have our dataset ready, we are all set to train our model.

We will be using the Resnet18 AI model for classification. So, let’s train the model with our images by running the next cell.

It will display the time along with training and validation loss. You can see we have trained the model in just a few seconds.

And you know what? Our AI image classification model is ready.

Before we move on to make the prediction, just process the fact that you have trained an AI model in under 10 seconds.

How cool is that? And people say AI is a complex technology.

Step 3 – Prediction through the model

It’s time to see the result. 

We will provide our original Healthy plant image and see the prediction of the model. 

You can also see, the model also tells the probability of its prediction.

Indeed a Good job from Resnet18 

What we did just now is Transfer Learning. 

The process of taking an existing model and training it with our data is known as Transfer Learning. 

And Transfer Learning has made AI super simple. 

I will make a separate article on Transfer Learning later

But now it’s time for you to unleash your creativity and build your own Image classification model.

Happy coding!

Thanks for choosing to read it completely. I can try to simplify any AI concept for you.

In case you want me to simplify any AI topic, please let me know on LinkedIn – https://www.linkedin.com/in/nikhileshtayal/

Here is how I learned AI as a non-technical person in 4 months for free.

Let’s learn to build a basic AI/ML model in 4 minutes (Part 1)

Share it with your senior IT friends and colleagues
Nikhilesh Tayal
Nikhilesh Tayal
Articles: 53