Coders Packet

Weed-Identification using Deep Learning in Python

By Yashraj Tambe

Weeds are a major roadblocks to farmers which tend to reduce their yield. Here we aim to detect those weeds using Deep Learning

Weed identification can be done with Simple Neural Networks but that might be just too time-consuming.So here we will be using CNN that works faster & in a more better way on images.

Dataset: -

The dataset is present here.

Dependencies: -

This project would require TensorFlow, Keras, OpenCV, Numpy, Scikit-Learn. The libraries can be installed with the following commands:-

pip install tensorflow
pip install opencv-python
pip install scikit-learn
pip install numpy

Note:Keras now comes with TensorFlow package so no seperate installation is needed

Project creation:-

First we will need to have the directory structure similar to the one mentioned below:-

/content/drive
 |
 |-My Drive
 |    |
 |    |-AI
 |      |
 |      |-Your folder name
 |      |       |
 |      |       |-plant-seedlings-classification
 |      |       |        |
 |      |       |        |-train  
 |      |       |        |   |
 |      |       |        |   |-Blackweed
 |      |       |        |        |
 |      |       |        |        |-image0.jpg
 |      |       |        |        |-image1.jpg
 |      |       |        |        |- ....
 |      |       |        |   |- ....
 |      |       |        |   |-Sugar Beet
 |      |       |        |        |
 |      |       |        |        |-image0.jpg
 |      |       |        |        |-image1.jpg
 |      |       |        |        |- ....
 |      |       |        |-test
 |      |       |        |   |
 |      |       |        |   |-image.jpg
 |      |       |        |   |- ...

Once we're in a similar directory structure comes the next part of improting libraries

import cv2#Imports OpenCV
import numpy as np#Import Numpy
import os#Import OS
from tensorflow import keras#We're using keras library from tensorflow

Then we go for image fetching & resizing the image to a size of our choice but since Colab provides a limited resource will try to keep an image size of 128,128 by using

X_train = []#creating an empty list for images
y_train = []#creating an empty list for labels                                                                                                                                                                                                                                          
import cv2
for i in os.listdir('/content/drive/My Drive/AI/Project/4th/Convolutional Neural Network/plant-seedlings-classification/train'):#This changes as per the directory structure & it fetches the labels
    print(i)
    if (os.path.isdir('/content/drive/My Drive/AI/Project/4th/Convolutional Neural Network/plant-seedlings-classification/train/' + i)):#This validates the presences of images
        for j in os.listdir('/content/drive/My Drive/AI/Project/4th/Convolutional Neural Network/plant-seedlings-classification/train/' + i):#This loops through images
            try:
                dummy = cv2.imread('/content/drive/My Drive/AI/Project/4th/Convolutional Neural Network/plant-seedlings-classification/train/' + i + '/' + j)#here we read the image
                dummy = cv2.resize(dummy,(128,128))#here we resize the image
                X_train.append(dummy)#We fill this empty list with the new resized images
                y_train.append(i)#Here we fill this empty list with the labels
            except Exception as e:
                print(e)

We convert the list to NumPy array's so as to feed it to our model which is done using np.array() command & then we normalize by dividing the obtained array by diving the array by 255 since 8 bit image representations have pixel values ranging from 0 to 255.

Then we go for One hot encoding the labels since this is a multiclass classification problem using the following: -

from tensorflow.keras.utils import to_categorical
from sklearn.preprocessing import Label Encoder
lenc = LabelEncoder()
y_train2 = lenc.fit_transform(y_train1)
y_train1 = tf.keras.utils.to_categorical(y_train2)

Then we train our model on the image by building the model & then using model.fit() after compiling the model. Within few epochs we get an accuracy close to 94%.

Note:The code is implemented in the file "Weed-identification.ipynb" file & click on Run All or Run.

Download Complete Code

Comments

  • Hrushikesh Rokade :

    Can u please provide the directory

  • Reply to this comment



    Download Packet

    Reviews Report

    Submitted by Yashraj Tambe (yashraj)

    Download packets of source code on Coders Packet