Identifying Weeds using Image Classification

By Archana Bhatia, Computer Engineering, VESIT


In this article, we will be talking about the application of AI in the field of agriculture. Specifically, we’ll build a CNN model to classify the farm images to separate weeds from crop plants. We’ll start with why do we need such systems and go on to how to actually build one. 

The importance of weed identification in farms

Insects, diseases, and weeds are creating life miserable for farmers today. The impact of weeds may not be clearly visible as that of other pests, but many a time, they prove to be economically more harmful than insects. 

According to the Indian Council for Agricultural Research(ICAR), every year the country loses agricultural produce costing over 11 billion dollars, owing to weeds. 

So, the identification of weeds, whether to further remove it manually or using herbicides spray, becomes the foremost step in weed control.  

How AI can help in agriculture?

Automated weeding systems are more promising in crop production. Especially, for crops such as soybean, which are spread over millions of hectares, the automated weeding systems can outdo any other manual techniques.

The weeds can be distinguished from crop plants by exploiting the image classification and edge processing capabilities of AI. 

Our setup

This article provides a step-by-step guide for building a powerful image classification model for weed identification and similar applications. Following are some points you should know before diving deeper into the content:

  • The data used for the system is from the soybean cultivation field. The input images are classified into four classes: soybean, grass, soil, and broadleaf(weed); instead of just weed and non-weed.
  • It makes use of hybrid and advanced machine learning techniques like Convolutional Neural Networks(CNNs) 
  • A comparative analysis has been made for the two models used. The first is custom-built CNN, and the second is a classic model VGG16.

Continue reading to learn more about each of these points in detail.

Methodology followed

Fig 1. shows the overall flow of the system. It consists of eight main steps, right from data collection to finally ending up with identifying weeds from a set of images.

 Fig 1. Overall flow diagram of the system
  1. Data collection

Kaggle is the source of obtaining data. The dataset namely ‘Weed Detection In Soybean Crops’ consists of 400 UAV captured, weed containing images from soybean farm, which are converted into 15336 image segments. Follow this article to download such a huge dataset from Kaggle directly in your Google colab.  

Sample image from the dataset
  1. Data preparation

Data augmentation is a powerful tool in image processing systems. It avoids overfitting by creating a diversity of objects, also increasing the size of the dataset. 

However, since the size of this dataset is large enough, the augmentation part is skipped here.

The next step is to arrange the four classes in their respective labeled directories and splitting the data into 80% training part and the rest 20% testing part. 

  1. Building CNN models

Using Keras and Tensorflow modules of Python, layered architectures are built for two models: Conv2D model and VGG16 model. 

The images will be transformed into 150X150 size to be fed as input to the model.

Now let’s have a look at the most interesting part of the system. The layered architectures!

  1. Architecture of the first CNN model: Conv2D

The model is built with layers like Convolutional2D, MaxPooling2D, Flatten, and Dense; with activation functions like ReLU and Softmax being used.

Architecture of Conv2D model
  1. Architecture of the second CNN model: VGG16 model

VGG is an easy-to-implement convolution neural network and is widely used for image classification problems. It simply outperforms various previous generation models. 

Architecture of VGG16 model used


Let’s have a look at the results obtained after performing the training and testing of these models.

After 10 epochs, the initial and final metrics of the two models are shown below:

Training and validation metrics of the Conv2D model
 Training and validation metrics of the VGG16 model


The models are almost ready to be used in real-world applications!

The prediction can be done by creating a folder of random images from soybean farmland and feeding it to the trained model. The model will successfully classify them into one of the four classes: soil, soybean, grass, and broadleaf(weed). 

Prediction using an unknown set of images

So, this has brought us to the end of this article. Just a quick recap before you start your journey of building powerful CNN models!

  • Collect data from a trusted source
  • Process the data properly. If the number of images is not enough, go for data augmentation
  • Split the data into train-validation-test sets or simply into train-test sets
  • Start building your own model from scratch or build upon an existing CNN model
  • Decide the number of layers, number of filters, activation functions to be used, etc
  • Train your model
  • Test it
  • And use it for your predictive purposes.

Leave a Reply

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