How to teach an AI to help farmers
Teaching the computer to recognize given plants -O-
How?
It's way easier than you think, we all know that AI can do most of the job you wants, if I want to be more specific Deep learning
can do most of the job we want
Now you may think what is that most of the job
means? 🤔
- Self-Driving Cars
- News Aggregation and Fraud News Detection
- Natural Language Processing
- Virtual Assistants
- Entertainment
- Visual Recognition
- Fraud Detection
- Healthcare
- Personalizations
- Detecting Developmental Delay in Children
- Colorisation of Black and White images
- Adding sounds to silent movies
- Automatic Machine Translation
- Automatic Handwriting Generation
- Automatic Game Playing
- Language Translations
- Pixel Restoration
- Photo Descriptions
- Demographic and Election Predictions
- Deep Dreaming
These things are considered to be the top 20 applications that AI(deep learning) can do in 2022
which is mind-blowing compared to like just 10 years ago most of the stuff mentioned here is impossible to understand by computers
So said that most of us think Then it's going to be done only by scientists right? or Ph.D. professors right?
No and No🙅♂️
You don't have to be a Ph.D. student to build this
Well I'm not a Ph.D. student, I'm actually a 18 year's old college student who is currently doing a BSc computer science
degree in India (which has nothing to do with Machine Learning or the AI field actually!😅)
And we are going to get a state of the art result
on this problem at least for now (the machine learning and deep learning field is always going to grow
always learning😇) by just understanding the fundamentals of Deep learning
It's again not that complicated then you think
once you get the fundamentals down its going to be really a fun ride 🥳
We are literally teaching Our Computer
to learn
Said that,
Well it does contains math
But how much math is it? It contains only three branches in math
- Calculus
- matrix multiplications
- Linear Algebra
I don't know about you But I hated math classes in our schools
But the catch is math is an art
Just like in this picture if you first saw this it's definitely going to trip your mind(considering that you have never seen this picture before)
Because you're not going to see this kind of pose posted by your friends or even you
It's the same when it comes to math
If you want to create anything math is going to be your friendly neighborhood buddy🐎
This is the only book that made me rethink Maths
which is written by Paul Lockhart
A Mathematician’s Lament
If you had time just consider checking this out it's really a great book that going to change most of our thought about maths And in bonus, it contains only 25 pages 🥳
Ok, let's finally get into the main topic that we are going to cover Can we teach an AI to understand how plants look?
The answer is
Yes, We Can!
Let's do this 👀
First, let's get our data from Kaggle
Which is
here it contains as that name describes Agricultural crops if I wanted to be specific ['maize', 'jute', 'wheat', 'sugarcane', 'rice']
these four classes(plants) pictures
Our problem is To teach an AI model to understand the plants and predict which one is mazie and which one is wheat and so on..
Getting our data ready🍃
If you saw my blogs then you know it SPLITTING
our data into Train and Validation sets
Code :
train_dir = "/content/kag2/"
valid_dir = "/content/crop_images"
train_data = tf.keras.preprocessing.image_dataset_from_directory(train_dir,label_mode="categorical",image_size=(224,224))
valid_data = tf.keras.preprocessing.image_dataset_from_directory(valid_dir,label_mode="categorical",image_size=(224,224),shuffle=False)
Now we have data let's build our AI
Creating MARK AI🤖
We are going to leverage TransferLearning Model if you don't know about it🤔 I just made a recent blog post if you wanted to check it out here :-> common click me it's going to be fun😙
Said that...let's create our mark_ai🤖
Code:
base_mark = tf.keras.applications.EfficientNetB0(include_top=False)
base_mark.trainable=False
input = tf.keras.layers.Input(shape=(224,224,3))
x = base_mark(input)
pool = tf.keras.layers.GlobalAveragePooling2D()(x)
output = tf.keras.layers.Dense(len(class_names),activation="softmax")(pool)
mark = tf.keras.Model(input,output)
mark.compile(loss="categorical_crossentropy",optimizer="adam",metrics="accuracy")
mark.summary()
Output :
Now we have a model let's give our training data and validation data to train our model
Code:
mark_history = mark.fit(train_data,epochs=5,steps_per_epoch=len(train_data),
validation_data=valid_data,validation_steps=int(0.15*len(valid_data)))
Output :
!Wow 🤯 our Mark AI got an accuracy of 96% which is in humans term(10 out of 9 images are going to be correct)
Which is really really great and the power of transfer learning 😎
Let's measure our model's performance🧐
Evaluation of our AI
Let's look at the loss and accuracy of our model
loss: how bad is our model prediction compared to our original data we always need less loss
Accuracy: how accurate is our model with the prediction compared to our original datawe always need more accuracy
LOSS :
Accuracy :
As we can see the loss is decreasing as we expected and accuracy is increasing(as shown in the model training output)
Confusion_Matrix
Let's find out where our model got confused
As we can see our Mark AI got confused mostly in this area's
Mark_1 AI
Let's create another model(Mark_1) and improve our first_model(Mark) result by just adding a single dense layer
Code :
base_mark = tf.keras.applications.EfficientNetB0(include_top=False)
base_mark.trainable=False
input = tf.keras.layers.Input(shape=(224,224,3))
x = base_mark(input)
pool = tf.keras.layers.GlobalAveragePooling2D()(x)
dense_layer = tf.keras.layers.Dense(500,activation="relu")(pool)#newly added line
output = tf.keras.layers.Dense(len(class_names),activation="softmax")(dense_layer)
mark_1 = tf.keras.Model(input,output)
mark_1.compile(loss="categorical_crossentropy",optimizer="adam",metrics="accuracy")
mark_1.summary()
Output :
As we can see our newly added layer let's look at the result
Code :
mark_1_history = mark_1.fit(train_data,epochs=5,steps_per_epoch=len(train_data),
validation_data=valid_data,validation_steps=int(0.15*len(valid_data)))
Output :
Wowie, Damn😱 our model has an accuracy of 1.0 which is as you guessed it 100%
By just adding a single line to our Mark AI model we got an accuracy that is considered of state of the art
result😎
But let's not stop here let's see the evaluation and predicts with the Mark_1 AI
Confusion_matrix
As we can see no error here it predicted every plant right
Let's visualize and predict some plants that have never been seen before
prediction
Here our model is saying that it's rice and it is 51% sure
Well it is indeed rice
Let's look at another example
Again here our model is saying that it's wheat and it is 100% sure
And we can go on for a while by doing this...
App version
And If you wanted to check this out on your own I created an app with a help of HuggingFace and you can check this out here:->-> click me I ( ̄︶ ̄)↗
🎯
If you are using a PC while viewing this app you can directly open google in the next tab and drag plant's images from there and drop it on the drop section of the app
🎯
Well, there you go ... we build a model which can classify our plants easily 😎
Well Thank-you very much for your time (ಥ _ ಥ)
Bye (^▽^)┛
The code is here : @clickme
My linkdin profile : you_know_what_to_do