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