Agriculture Project From Scratch To Deployment Using Deep Learning Architecture
All you need to know how useful machine learning in real-world
Table Of Contents:
(i) Problem Statement
(ii) Motivation
(iii) Data Processing
(iv) Data Augmentation
(v) CNN from Scratch
(vi) InceptionV3
(vii) Deployment with Flask
(viii) Conclusion
(ix) Future Scope
(x) References
(i) Problem Statement :
Our problem statement is Classifying the given Cotton Plant Or Cotton Leaf is Diseased Or Fresh
(ii) Motivation :
India is the largest producer of cotton in the world. The United States Department of Agriculture (USDA) pegs India’s cotton production at 29 million bales in the 2019–20 season as against 26 million bales the previous year.
As a machine learning enthusiast, I want to apply my machine learning knowledge to increase the productivity of cotton crops in India.
If my model useful for one farmer also that means a lot to me, this idea drives me to do this project.
(iii) Data Processing :
To do this project I use Google Colab
We have Images of Cotton Plants and Cotton Leafs
The data divided into 4 classes
(i) Diseased cotton plant
(ii) Fresh cotton plant
(iii) Diseased cotton leaf
(iv) Fresh cotton leaf
I upload my dataset to google drive
In my dataset, we have 3 folders
train_, val_, test_
train_: it contains images of 4 classes, these images used to train the model
val_: it contains images of 4 classes, these images used for validating the trained model
test_: it contains images of 4 classes, these images used to test the trained model
(iii) Data Agumentation :
Data augmentation is a technique to artificially create new training data from existing training data. This is done by applying domain-specific techniques to examples from the training data that create new and different training examples.
Why data augmentation?
we don’t know which type of image we get from the user in production it may be titled or zoom in or stretched or flipped, so to efficiently classify any type of images also we need to do data augmentation on training classes.
I use the ImageDataGenerator module for data augmentation
I apply rotation,width_shift,height_shift,shear,zoom,flip on training class images
The flow_from_directory() method allows you to read the images directly from the directory and augment them while the neural network model is learning on the training data
We have 1951 images to train the model
I want input image size 224 x 224 that’s why I give traget_size (224,224)
The dataset contains color images, color images represent in RGB
train_agumented_set[0][0][0] returns 3dim array which contains [R,G,B] values
I write a function which returns the augmented images
We don’t apply any data augmentation on val_ class images because they are used to validate the trained model if we augmented them then our model overfit.
We just rescale val_ class images
val_agument = ImageDataGenerator(rescale = 1./255)
We have 324 images in the validation data set
Classes of model
0: Diseased cotton leaf
1:Diseased cotton plant
2: Fresh cotton leaf
3: Fresh cotton plant
(iv) Building CNN From Scratch :
Basic CNN have
Convolution layer, Maxpooing, Fully connected,Dropout , Batch normalization,Flatten layer ,Output(softmax/sigmoid)layer
Dropout + BatchNormaization work as regularization
Our input image size = (224,224)
Our Model architecture
con2D (kernal : 3 x 3 ,No_of_kernals= 32)
Maxpooling2D( 2 x 2)
con2D (kernal : 3 x 3 ,No_of_kernals= 64)
Maxpooling2D( 2 x 2)
con2D (kernal : 3 x 3 ,No_of_kernals= 128)
Maxpooling2D( 2 x 2)
con2D (kernal : 3 x 3 ,No_of_kernals= 256)
Maxpooling2D( 2 x 2)
Dropout layer (dropout rate =0.5)
Flatten layer
Dense layer (units=128, activation =relu)
Dropout layer(dropout rate =0.1)
Dense layer (units=256, activation =relu)
Dropout layer (dropout rate=0.25)
Dense layer ( units=no_of_classes(4) ,activation =softmax) ==> Output layer
Our problem statement belong to multiclass classification that’s why Output layer activation is Softmax
Libraries used: Keras with backend Tensorflow
Importing necessary modules
Steps for building a Convolution neural network in Keras
(i) Building model: Defining Architecture
(ii) Compile: Giving metadata(like loss, optimizer, etc)
(iii) Fit the model
step 1: Building Model
Step2: Compilation
I use optimizer =Adam(lr=0.0001), loss=’categorical_crossentropy’, metrics=‘accuracy’
step3: Fit the model
I fit the model for 50 epochs
At the end of the 50th epoch
training_data_set_accuracy = 0.88
Valdation_data_set_accuracy=0.85
Epoch vs Loss :
Epoch vs Accuracy
Model Evaluation:
To evaluate the trained model we use Validation data set class images
Predicting the class of validation data set images
In the validation dataset, we have 324 images
Using trained CNN model we predicting the class of individual image
Testing Trained Model Using Test Data Set Images
Before testing, we need to save the trained model
Loading saved model
I Took the new image from the test_ data set to test the trained model.
Test_ data set images are new to the model.
If the model predicts the image class correctly then our trained model considers as generalization model.
importing necessary modules
Loading an image from the test data set
I take an image of diseased cotton leaf
Converting the loaded image into an array
loaded image converted into 3dim array in the form of [R, G, B]
Now we need to normalize the [R, G, B] values
Predicting the class of loaded image
By this, we can conclude our trained model working as generalization model
(vi) Transfer Learning :
Transfer learning is the reuse of a pre-trained model on a new problem. It’s currently very popular in deep learning because it can train deep neural networks with comparatively little data.
Famous Pretained Models
(i) Resnet50 : 25,636,712 parameters
(ii) Resnet152V2 : 60,380,648 parameters
(iii) InceptionV3 : 23,851,784 parameters
I choose InceptionV3 because it has fewer parameters compared to other models, fewer parameters leads to less time to response
Inceptionv3 consists of two parts: (i) Feature extraction (ii) Convolutional neural network.
We use imagenet weights
We don’t train Extracted feature
We train only last layers
importing necessary modules
For InceptionV3 model default input image size = [224,224]
Our input image is a color image.
So we need to add + 3 to the input image_size
Loading inception model with imagenet weights, and excluding the last layers
We don’t want to train the complete model from scratch. So we don’t train our model for imagenet weights.
No of classes in the training data set
We have 4 classes
Now we need to Flatten the InceptionV3 model output
By Flatten the inceptionV3 model we get Bottleneck layer output
Now we need to add layers to the bottleneck layer output .
We can add any number of layers
I add only one dense layer(units=no_of_classes,activation=softmax)
We have 4 classes , so we need 4 units in dense layer and activation is softmax
We apply softmax activation on Bottle_neck_layer_output
Step1: Model building
Our model has total parameters =22,007,588
But only 204,804 parameters are trainable
Step2: Compilation
Step3: Fitting model
I fit the model for 50 epochs
At the end of 50th epoch
training accuracy = 0.93
validation accuracy = 0.94
Epoch vs Loss
Epoch vs Accuracy
Transfer Learned Model Evaluation
Evaluating the inceptiov3 model with a validation data set images
Predicting the class of validation data set images individually
we have 324 images in the validation data set
We predict each class of images individually
Testing Transfer Learning Model with test data set images
Before testing, we need to save the Transfer learning model
Loading saved model
I took an image from the test_ data set to test the trained transfer learning model.
I specifically took a diseased cotton leaf image
Now we need to test we are getting the same class or not
Loading image from the test data set
Now we need to convert the image into an array
Our input image is a color image
So array in the form of [R, G, B]
Now we need to normalize the [R, G, B] values
Predicting image class
Taken image is the diseased cotton leaf
(vii) Deployment with Flask :
(viii) Conclusion :
CNN model
parameters : 5,141,188
Epochs=50
Val_accuracy=0.85 and accuracy=0.87 at 50th epoch
InceptionV3 model
Total params: 22,007,588
Epochs =50
Training accuracy = 0.93 ,Validation accuracy = 0.94 at 50th epoch
It’s always better to use a Transfer Learning model because it can train deep neural networks with fewer data and it also gives good accuracy compared to CNN from scratch.
(ix) Future Scopes :
- Try more Transfer Learning Models
- Fine Tune the CNN model using Keras-tuner
(x) References:
Google (Some definitions)
Krish naik youtube channel (UI part)
Indian ai production youtube channel(Data part)