Hang tight while we fetch the video data and transcripts. This only takes a moment.
Connecting to YouTube player…
Fetching transcript data…
We’ll display the transcript, summary, and all view options as soon as everything loads.
Next steps
Loading transcript tools…
Deep learning project end to end | Potato Disease Classification - 3 : Model Building | codebasics | YouTubeToText
YouTube Transcript: Deep learning project end to end | Potato Disease Classification - 3 : Model Building
Skip watching entire videos - get the full transcript, search for keywords, and copy with one click.
Share:
Video Transcript
Video Summary
Summary
Core Theme
This content details the process of building, training, and evaluating a Convolutional Neural Network (CNN) for image classification, culminating in saving the trained model for future predictions and discussing strategies for versioning.
Mind Map
Click to expand
Click to explore the full interactive mind map • Zoom, pan, and navigate
In the last video we loaded our data and then
we did some pre-processing to make it
ready for model training.
In this video we are going to build a
convolutional neural network and train
that network on our trained data set
and then we'll measure the accuracy
using our test data set. If the accuracy
looks okay we will export that model to
a file on a disk so that it can be used
later on in our fast api based server
for making prediction. Let's get started!
Our data set is pre-processed and we are
ready to build the model. We are going to
use convolutional neural network which
is a famous type of neural network
architecture. If you are solving
you know image classification problem
and for that I made a video actually so
if you go to Youtube search for this
you'll find my video. I highly recommend
you watch this because there are a lot
of concepts that I'm
clearing in this video
and we are going to use this
architecture you know in our code today.
So when you have convolutional neural
network there are two parts. There are
convolutional layers so there are
convolutional and pulling layers so
pulling
there could be a number of layers
and then there is a dense layer where
you just flatten it so we are going to
follow this architecture and that
therefore I am saying you need to watch
this video if you haven't to understand
what we are doing in today's
session. So here I'm
now building
basically my model. So I'll say model is
equal to this and we will have
set of layers here. So the first layer
would be resize and rescale
whenever you feed an image first thing
will be you will resize it then you
scale it by dividing by 255,
then you do data augmentation so that
you do you know horizontal vertical
flip
random rotation to generate new samples?
The third layers is your convolutional
layer so here I will say Conv
2d.
Okay. What are the arguments
here? you can say
tensorflow conv2d layer you find a help
the first argument is number of
filters and kernel size. So this kernel
size is
basically the size of the filter so if
you looked at
this video you know I'm talking about
filters here in this video
and
the filter.. Let's see
this is that filter in that video so
this is three by three filter.
So
the second argument is what filter this
is. This is kernel size which means this
is the three by three what is filters
then?
Okay. Let's see.
So when you build a convolutional neural
network, I'm just
quickly skipping this ahead
you are
applying different filters. So let's say
one filter. Let's say you are doing
koalas image detection
one filter could be to detect eye, one
filter could be to detect nose
this way you detect
eye, nose, ear and if you have eye, nose
ear of koala you can say the image has
koala's hair too!
Similarly if you have hands and legs of
Koala you can say Koala the image has
Koala's body too!
And if you have koala's head and body it
means it can be a koala's image. So you
filled flatten that and you kind of
figure that out. So here you have one, two
three, four, five.. five layers
in our case we are going to use 32
layers. So here
how did I come with 32? Well this was
trial and error.
You need to have a lot of layers where
you can
detect
edges. you know the small features?
The second one is the actual filter size
and the beauty of convolutional neural
network is that
in this example I had to give specific
ionosphere
filter
there. Here
the neural network just figures it out
for you.
Then the activation layer the popular
activation layer for hidden layers is
always value because it's very fast to
compute
and the input shape here
is
256 by 256, right? So the 256 by
what is it's image size.
Okay. I'm going to maybe create a
variable using this because
that could be useful little later
and
basically I created this variable assign
it here pretty straightforward.
After convolutional layer you generally
have
pulling layer. So if you look at again
this video
you see convolutional plus value then
pulling conversion value you just keep
on repeating it
and pulling is
basically there are
different types of pulling. Here, this
image is showing you
the max pooling. Max pooling means you
have 4 by 4 pixel area you take the
maximum which is 8 eight
another four by four area what is
maximum nine. So nine
here. What is maximum-three? three three
two two
when you do this
you are preserving the features and you
are reducing the size of the image which
can be very helpful you know computation
wise. So that's exactly what we did here.
So here I'm saying I want a max pooling
layer of size 2d
and then
I before preparing this video by the way
I did some trial and error and i figured
maybe I need to stack
few max pooling and convert 2d layers.
Okay. This was trial and error you can
remove couple of layers and figure it
out. Again 30 to 60 to 64 all these
numbers are
a little bit based on that trial and
error
and then once you have stacked
the max pooling you know convert to 2d
layer max pooling layer. If you look at
this image once again
you need to now flatten it so whatever
data you get here you need to flatten it
so that it's an array of neurons and you
can have then a hidden layer here. You
know a dense layer and the final layer
is your final output classification
layer. So I'm going to
flatten it here
and then
let me add a dense layer of you know 64
neurons and then I'm going to add
okay let me say that my number of
classes is 3
and
my last layer will have three neurons
with soft max activation function.
So soft max activation function if you
don't know about it it will normalize
the probability of your classes
okay?
After that
you need to do model dot build. I mean
this is
just the API. API requires that okay
we're getting
images
must have either three or four
dimensions. Also looks like
the input shape that we gave here
is not just image it's
see what we're supplying is actually
there is channels.
And there is a batch size too.
That's the format of our input.
Okay?
Now.
Our
model architecture is ready I will just
do a quick summary we are not done
training yet by the way
so this summary just prints the quick
summary, you know. The parameters these
are the weights that you need to train
that's why it says
trainable parameter. You know you are
doing back propagation on all this
weight basically number of weights.
In deep learning we always first define
the neural network architecture so that
is this.
Then we do compile using optimizers and
all that like adam is a famous optimizer.
Then you define your loss function your
matrix
in each epoch.
what type of metric will you use to
track the
gradient descent basically? to you know..
So the accuracy is the metric that we
use to
kind of track the training
your training process.
And then finally this is the third step
which is model dot fit. Here
you actually
train your network
so in the epochs
you know we have 50 epochs.
We have batch size or batch size.
We have I will do verbose one so that it
just
prints lot of output and we can see
what's going on
and we have validation data as well. By
the way so this validation will be
used in
you know during the
each epoch it can help you track the
accuracy.
And by the way I will record all the
history of every epochs in
the history parameter so that we can do
some plot we can chart some plots later
on
so if this is
gonna run it's gonna take some time
based on what computer you have.
Sometimes people have
you know
gpus. So gpus makes this process faster
if you don't it might take time you can
try with less epochs in like 50s it's
taking time. 50 might be high so you can
try with less epoch but let me
explain you all these parameters that
you're seeing here. So in the first go
the loss the accuracy that you got on a
training data set was 51.
But the accuracy on validation data set
was 71 percent.
You can understand so it will train your
model first on
training data set it will measure the
accuracy and then it will kind of take
this is like running little test so it
will run little test using validation
data and it gets 71 accuracy. Okay. Then
it just keeps on running and you'll see
as we have more epochs the accuracy
keeps on improving see validation
accuracy
84 now we started with what 71.
Even the
training accuracy just keeps on
improving.
So right now I'm here at 45th epoch 46
47
and my
validation accuracy is already close to
one. See one it yeah it jumps up and down
but
it's pretty good actually
so now
I will run
test on my test data set. See, before you
deploy any model
you want to run a test
you want to figure out how well your
model is performing by trying it out on
a test data set. So that it's not biased.
Your model has not seen this data set.
This is the first time we are trying it
it can give us good understanding.
The accuracy comes to be 98 percent
which is
actually pretty good. And if you look at
this course parameter it's a python list
which has first parameters as loss.
The second parameter is accuracy.
Now I want to play with this history
parameter, okay?
You see history so we store history.
Let's see what this history is.
Actually,
it tells me
it's a tera's callbacks history and
immediately I'll go to my friend do you
know who is my friend? Well, Google
tensorflow
keras
callback history.
And it will give me some documentation
history has some parameters. So let me
try this parameter see what's going on
here.
Okay it's just telling me
you know that I had 50 epochs 54 steps
verbose was one and so on
it also has
history.history
So history object has another element
called history and if you look at
all the keys see you get four parameter
loss, accuracy validation loss and
validation accuracy.
Those are this parameter loss accuracy,
validation loss, validation accuracy and
we ran 50 epochs. So for each parameter
we have 50 value. See
one, two, three for loss we have 50
accuracy view 50
validation 50 and so on and that's
exactly what this is.
So if you look at
let's say accuracy let me look at
accuracy.
It's a python list 50 values so see if
you do lan
50 value correct and what are these
values? Let's check it
0.513.612
Okay
let's go here
0.513.612. So all these numbers during
epochs it will just record them in the
history and once we have those
see what we can do is this.
We can plot them in a nice matplotlib
chart you know so I will
first
get all these errors stored in different
variables and then
I will plot
the accuracy. So
let me
plot
training and validation accuracy. So this
is how you plot it.
I'm not going to go into details. This is
not matplotlib tutorial but I'm just
plotting training versus validation
accuracy and you can see that these are
epochs on the x axis.
As number of epochs so we started with
very less accuracy, one accuracy is like
perfect. So we started with point
five
you know
and it kept on jumping jumping jumping
and it went here. This graph shows that
even if you train for let's say 30 or 40
epochs is fine you don't need to go all
the way till 50 because you already
achieved
very high accuracy. And if you want to
plot another chart
next to it, let's say the
loss chart you know. Then see
the loss initially is high but it just
loss is basically an error. So an error
in back propagation keeps on reducing as
you proceed forward in your
epochs. So far it looks good. Now I want
to make a prediction so again we will go
through
test data setup. We'll run our for loop.
So we are taking just one batch. So this
will be 32 images and I will take the
first image. Okay, what is my first image?
My first image is this.
okay?
Let me just print it this is my first
image.
You see it's a tensor and if you want to
convert tensor to numpy
it's just a different format. Okay. I want
to now display this. So if you do as
type
and
you know this function it will show you
the actual image. So this is an actual
and if you print it
you see it's a three dimensional array
rgb.
Okay.
I will call it first image.
This is my first image, okay? If you do
you get second image, third image and so
on. This is my first image.
What is my first label?
My first label is
this.
Okay?
Now
I will say this
print
first image to predict
and then
just
that image
and
the first image
actual label
is
you know what?
Let me do this here.
So this is my first label
so actual label is one but I want to
know the class type. So maybe you can do
class names here and you get the
class type. So it will say okay
the actual label
is this.
This is the actual label I want to find
the predicted label, okay?
What is my predicted label?
My model is ready
I can call predict function
The predict function expects the image
batch.
Fine. I will give the image batch
and
whatever I get is the batch prediction
so this is this will be the prediction
for 32 images.
If I want the prediction for the first
image
I need to do this
badge prediction,
zero correct.
Oay so my image is potato early blight
and my prediction is this okay why is it
three dimensional array? one, two, three..
Well because of this
you see our model.
Or model architecture is
we have
three neurons and activation is soft max.
So soft max is just a probability so
there are like three probabilities
and
whatever is the highest probability that
is the class.
So you see e raised to minus 0 1 e
raised to minus 7 e raised to 18. So
this is the actual class.
Okay and if you want to
get like which is the maximum number,
there is a function called np dot arg
max.
This gives you the index.
So arc max here the answer will be
see if you have this array let me show
if you do np essentially we are doing np
dot argmax on this.
[Music]
Oh!
import
numpy
as
correct
the answer is zero. Meaning this the
item at the zeroth location is maximum.
Okay that will be your class? Now again
you want to convert it to class name so
you will say class names like this.
Okay you do one by one this is not
confusing by the way it's pretty
straightforward actually.
So
this one is a predicted labor
and you will be like okay why image is
changing well we are doing some
shuffling here. That's why image keeps on
changing.
see?
late blight late blight
only bled out so my actual label is
early blight my predicted label is also
early blight so my model is performing
awesome. see? We are trying all these
cases and it is exactly telling me what
image is this. You see this is the power
of convolutional neural network. Okay.
Nw what I'm going to do is this.
I will write a function and I'm not
going to go into detail.
I already wrote this here so this is
just a simple function
that is
taking model and image as an input and
telling you what is the predicted class
and what is the confidence. Hundred
percent confidence means say I am
the accuracy of prediction is hundred
percent, you know. So that's your
confidence score and if you look at this
we converted image into an image array
and then we created a batch out of it
after that we called predict function
and then we figured out predictor class.
This code is same as this code,
okay? So we we figured it out and we
found the confidence so we run this and
now I'm going to run my prediction
on the entire batch and I'm going to
print those
so
we'll run a for loop on the first batch
and
I don't want to
do prediction on all 32 images. Let me
just pick only nine images you know so I
will say for i in range nine
okay
I will first
show that particular image so you see.
But wait so it's not showing all the
images
and that's because
you need to Ithink have a subplot
then you know so see it shows all the
images.
Okay, I don't want all these axes so I
will look plt dot
access
off.
So now I don't see it. I also want to
increase the dimension so that each
image looks a little bigger. Now each
image is bigger because of this line
and
now
I want to put a title
what should be my title?
Well actual predicted label and
confidence
okay.
So
let's first
so I'm calling predicted function using
the model that we have and using the
image, okay.
It's giving me predicted class and
confidence and my actual class
is this. So I have predicted class actual
class and confidence and all these three
things one two three. I will print as a
plot title. So I will say plot title okay
what is my plot title?
I will use python's format string so if
you want to
display just the actual label this is
how you do it. If you use python format
string
then
you know
predicted class will be this.
So see actual class and predictor class
Okay that came now I want one more line
and I want to introduce
confidence as well.
Perfect! You see
my actual image is
early blight my prediction is only right
confidence is 100.
Confidence is pretty high in
most of see most of them is 99 this is
amazing folks. Amazing amazing
performance indeed!
Now I would like to save the model. To
save the model c we were here in this
training file right but I will create a
new directory
called models
and I will save my model here okay?
So here
I will show my model by model version so
let's say this model working is one
I can just say
model dot save
again I'm using python form string dot
dot slash models
y dot dot well
I'm in this directory if I want to go to
modules Ineed to go one step up. That's
why dot dot and then model so here then
here it will save the model okay?
again python format string
model version is one.
When I do that
here you will notice
it will see it created one.
See here it's saved model to here
and if you look at this file this is how
tensorflow models are saved. All
there are all these directories you
don't need to worry about what these are
you will be able to import model from
this one directory
and let's say one if you have one model
next time
what you might want to do is
as a data scientist you want to conduct
some experiments.
Maybe you remove data augmentation, maybe
you change model architecture a little
bit
and
you want to export let's say two or
three different versions of your model
that you can try out.
So you can
you know you can run it like you can say
okay model 2
and that will be my
second version and see it will save it
here right now they are same but maybe
you can make some changes here.
You know in your model architecture
or maybe change optimizer
and save different versions.
If you want to keep a running count
and kind of figure out.
Okay, if there is a two directory then
automatically increment the model
version
then you can do that using OS
library. See if you have import OS
and if I do OS dot list directories
if I do this what's gonna happen.
See? It is telling me
what directives we have
now.
This is returning me string how do you
convert string to an integer?
Well it's very simple. You use list
comprehension
you will say
okay
end i.
For i in this
and you get integer
if you want to
listen model zero
as version you can get that also
and
eventually we find a max of all this
and max of this is two and when you do
one you get your next model version. So
this is how you get your
next model version. So I'm just going to
say this is my model version
okay?
And
I want to save it like this
so now when I save this and when I run
it
up
it created version 3. if I run it again
it automatically creates version four
version four and so on.
So this way you can auto increment your
models.
So folks, that's all I had for this video.
In the next video we are going to build
a fast API
server
and also we will be using tensorflow
serving actually to
load all these models. So we'll cover all
of that but as an exercise
I want you to change this model for
tomato classification
so we tried potatoes, okay?
You need to do this exercise where you
are building this
model for this tomato classification and
there are many classes so it will
require some changes into your model
architecture. But that's an exercise a
sincere student is the one who works on
the exercise.
If you
work on exercise you will learn a lot of
useful things. All right, if you like this
video please give it a thumbs up and
share it with your friends so that they
can also benefit and I will see you in
the next video in the same tutorial
series. Thank you very much.
Click on any text or timestamp to jump to that moment in the video
Share:
Most transcripts ready in under 5 seconds
One-Click Copy125+ LanguagesSearch ContentJump to Timestamps
Paste YouTube URL
Enter any YouTube video link to get the full transcript
Transcript Extraction Form
Most transcripts ready in under 5 seconds
Get Our Chrome Extension
Get transcripts instantly without leaving YouTube. Install our Chrome extension for one-click access to any video's transcript directly on the watch page.