0:00 Now before we talk about deploying the
0:02 model to Google cloud
0:04 I want to cover image data generator API.
0:07 In part 3 of the video, we built the
0:10 model part 2 and 3. Actually we used the
0:14 data augmentation layer in Keras to
0:17 train our model.
0:19 Now I received some command in that
0:21 video saying that why don't you use
0:23 image data generator and based on your
0:26 feedback and demand I am going to cover
0:28 image data generator API in this video.
0:31 So what we are doing in this session is
0:33 actually we are building, we are training
0:35 the model once again, which we did in
0:38 part two and three. Same thing but using
0:41 image data generator API. Now this API
0:44 allows you to do data augmentation
0:47 and data loading. It is quite convenient.
0:50 So we'll see how it works in detail and
0:53 in the next video we will do Google
0:56 cloud deployment.
0:59 So again just to summarize, I have this
1:01 notebook open where we use
1:04 data augmentation and resize rescale
1:06 explicitly as the layers
1:09 in our model. Now I will do the same
1:11 thing
1:12 using
1:13 more convenient approach and if you
1:15 Google
1:16 tensorflow image data generator say
1:22 image data generator, you find a
1:24 documentation where you can create this
1:28 class object where you can specify
1:30 all your augmentation as an argument. For
1:33 example, you want to increase brightness.
1:36 You know you can specify this brightness
1:38 range horizon to flip vertical flip
1:40 rescale.
1:42 You can do so many things.
1:45 So let me just go ahead and create
1:48 a variable
1:50 of image data generator so I've already
1:52 imported it. I'm going to create a
1:55 variable of image data generator and I
1:57 will specify different parameters for
1:59 example, the first parameter will be
2:01 rescaling.
2:02 So I want to rescale.
2:06 I would say rescale
2:11 every image has RGB value from 0 to 255
2:15 and I want to divide that by 255. In our
2:18 previous notebook what we were doing was
2:21 using this
2:22 this specific layer for rescaling and
2:25 now you're doing the same thing
2:27 here
2:28 using this particular argument.
2:32 The second one I will do is horizontal
2:35 flip.
2:36 I will say this
2:38 and
2:39 a rotation range.
2:41 I will use it.
2:43 Let's say 10 you know by 10 degree I
2:45 want to rotate my
2:47 images.
2:49 You can use more transformation, but I
2:51 just want to keep things simple. That's
2:53 why just using
2:55 these menu transformation and then
2:57 the second thing, once your
3:00 generator is created- so let me assign a
3:02 variable to it.
3:04 Now you need to
3:06 load the images directory from the
3:10 directory on the disk.
3:11 So here there is a method called flow
3:13 from directory.
3:15 The flow from directory it's a very very
3:18 convenient method and let me just show
3:20 you how you can use it. So train data set
3:24 train data generator
3:26 flow from directory.
3:28 Here you specify directory path.
3:31 Now
3:33 our directory if you look at it
3:37 we have just single directory with three
3:39 classes.
3:41 We need to think about
3:42 train splitting the images into
3:45 training and test and validation data
3:47 set as well. Now one thing, I don't like
3:49 about tensorflow API is
3:52 this particular method
3:54 allows you to
3:55 divide your data set into
3:58 training and validation data set. So let
4:01 me see where is it subset.
4:05 Yeah.
4:06 So you can specify
4:08 subset and you can say my subset is
4:10 either training and validation but I
4:12 need test as well. So if
4:14 tensorflow guys are watching this video
4:16 please add
4:18 taste as well. You know here because
4:20 otherwise it becomes inconvenient. So
4:22 since the support is not there, I'm going
4:25 to use a tool called
4:29 split folders. So I have already
4:31 installed the tool on my computer.
4:34 So let me just show you i previously ran
4:37 it.
4:38 So you just do
4:41 pip install split folders and that will
4:44 install this tool and
4:46 I will show you what I'm doing with
4:48 this tool. So here
4:50 when you run this command
4:52 you can say
4:54 split my plant village folder into train
4:57 test and validation. I want to split this
5:00 particular folder
5:01 this folder into three data sets train
5:05 test and
5:06 validation and the ratio that I want to
5:08 keep is seventy percent is training
5:12 one person ten percent is validation
5:15 twenty percent this test
5:17 and my output folder name is data set. So
5:20 when I run this command
5:22 it will become very clear to you what
5:24 this is doing. So here's it created a new
5:27 folder called data set.
5:29 Now see beautiful it has test train
5:32 validation, three different folders and
5:34 every folder has now these individual
5:36 classes. Now if you look at the images in
5:38 this folder there are 200 items
5:41 in test
5:43 because
5:45 there are total thousand items initially.
5:48 If you look at my original directory
5:50 here
5:52 early blight has thousand correct
5:55 and in my test I said twenty percent so
5:58 twenty percent of thousand is how much
6:02 two hundred, correct? So here if you go to
6:05 test see 200 images
6:08 then if you do
6:10 training
6:12 700 because 70 we said 70 percent are
6:16 training images and 1070 percent is 700
6:21 so now I have
6:24 folders split into these three
6:26 categories and I can just say
6:29 this folder is data set train.
6:32 So that will be my training generator. So
6:35 let me assign a
6:37 variable to this training generator
6:41 and I will specify couple of more
6:43 parameters. So my target size is what
6:46 each of my images is 256 by 256,
6:50 correct?
6:52 And just to
6:55 because I know I'm going to use this a
6:57 lot
6:58 so just storing it in a
7:01 makes sense.
7:03 Then the batch size will be 32
7:05 32 base size is pretty much standard
7:09 and the
7:11 class mode is sparse.
7:14 Why is pass because the labels I want
7:16 the labels to be 0 1 and 2.
7:21 See here
7:22 class mode.
7:24 So class mode can be
7:27 categorical if it is category curl it
7:30 will generate
7:32 one hot encoded
7:35 I don't want hot encoded vector. I want
7:38 simple sparse vector you know
7:41 0 1 2
7:43 and I'll just show you how this works
7:46 Let me do one thing.
7:48 I will mention save to directory as well
7:51 because I want to show you how the
7:54 augmented images look like.
7:57 I will create a new directory
7:59 called augmented images.
8:04 So here let me just
8:06 make a new directory- augmented images. So
8:09 you can see
8:10 here. See augmented images
8:13 and
8:15 it will save the augmented images into
8:17 that directory. Now you found 15006
8:20 images and it created a training
8:22 generator.
8:24 Now if you don't know
8:25 how generator works
8:27 in Python
8:29 I would suggest that you go to youtube's
8:32 click code basics
8:34 generator click on this video code
8:35 basics generator. You'll get an
8:37 understanding on how generator works. But
8:39 just to give you a brief idea,
8:42 Generator on fly it is like training
8:44 data set. You know tf data set that we
8:46 studied before, it will dynamically load
8:49 images from directory it will do
8:52 transformation because it's image data
8:53 generator. All this transformation and it
8:56 will return those images in a for loop.
8:58 So generator is something you can use it
9:00 in a
9:02 for loop.
9:04 Let's just run that so for
9:09 you can do for something in
9:12 train generator
9:17 now that something is basically image
9:20 batch
9:21 and label back just like tf data set.
9:24 So you will get
9:26 this image batch will be 32 images.
9:28 This will be 32 labels which will tell
9:31 you whether the plant is healthy or late
9:33 blight or lip light you know one of the
9:35 three values 0 1 2
9:38 and
9:39 this loop will just keep on going so you
9:41 need to
9:42 just break because
9:44 you know it's a generator it just keeps
9:45 on generating. So I will
9:48 maybe
9:50 you know first
9:52 print a shape
9:54 just to show you.
9:55 So every image batch 32 images
9:58 x and y is for each image is 256 256 3
10:02 is for RGB. Now when you run this you
10:05 will notice one thing that in augmented
10:08 images it created see 32 images total
10:11 and these are augmented. So some of them
10:12 are rotated you see this one is probably
10:15 vertical horizontal flip.
10:18 this is also horizontal flip I think
10:21 so that rotated there is a flip
10:24 and
10:25 so on.
10:29 Okay so rescale
10:31 rotation range, horizontal flip. You can
10:33 specify more augmentation
10:35 as per your convenience
10:38 but each image basically let's see if I
10:40 have to print each image
10:44 it will be a numpy array three
10:46 dimensional.. three dimensional array.
10:48 And see this is the most important thing.
10:50 Now each value is
10:52 between zero and one it is not between 0
10:55 and 255
10:56 because of this argument.
10:59 Now to do the same thing here in our
11:01 previous notebook.
11:03 Remember we had to
11:05 do this
11:08 but now
11:10 that thing is taken care of.
11:13 And when you run it again by the way see
11:14 this image directory will keep on
11:16 growing. Now you have 64 items so make.
11:19 Sure, I just
11:21 had this directory just to show you, but
11:23 ideally you should not have it. You
11:25 should delete it.
11:26 O,kay it will just work on fly on fly. It
11:30 will just keep on generating it. So I
11:32 will control execute this again. ctrl
11:34 enter if you have that directory and if
11:36 you have that specified here the
11:38 directory will keep on growing which
11:40 might be bad for your hard disk.
11:43 All right.
11:45 Now I will do the same thing for
11:47 validation
11:49 data set. The directory name is different
11:52 here.
11:54 And you will do the same thing for
11:57 test data generator as well and again
12:00 the directory name is different. So just
12:03 a simple copy paste I created three
12:05 generator train validation and
12:08 test.
12:27 Now you can directly build a model. So
12:30 I'm just going to copy paste some code
12:31 because we are using the
12:33 same model architecture convolutional
12:35 neural network, network architecture that
12:37 we used in a previous notebook.
12:40 The only difference here is I have put
12:41 some input layer here
12:43 and remaining things are
12:46 kind of same
12:48 and you control
12:49 execute. You will
12:52 print
12:53 model summary.
12:58 Okay because all parameters are
13:00 trainable, because we are not using any
13:01 transfer learning here.
13:03 So you can see the model summary,
13:06 model compile
13:08 same thing. I'm just doing copy paste
13:10 from the previous notebook
13:13 and then
13:14 you do
13:15 model.fit. Now here something changes.
13:18 So in model dot fit
13:20 you will specify your train generator
13:27 and in step
13:29 per epoch.
13:31 So you just specified step per epoc
13:33 otherwise train generator just keeps on
13:36 generating new images
13:39 and usually the guideline is tap per
13:42 epoch is basically how many batches you
13:44 are running.
13:45 So one batch size is what 32 okay 32 is
13:49 one best size and total number of images
13:52 let's say in train data set is x
13:55 So x divided by 32 is the value that you
13:57 want to put here so how many images are
14:00 there
14:01 n our train data set. Well,
14:03 1506.
14:08 So now
14:10 we have total
14:12 47 steps
14:14 for training.
14:17 Then the batch size is 32
14:21 validation data
14:23 comes through
14:25 validation
14:27 generator
14:29 and then val there are validation steps.
14:31 By the way validation steps
14:34 would be
14:35 same thing
14:37 y divided by 32 where y is number of
14:40 images in validation data set.
14:42 Validation has two one five images
14:50 okay around
14:51 six steps. I want here
14:54 and then verbose is one and i'm going to
14:56 try 20 epochs. I tried this previously.
14:58 I'm not going to lie but this is
15:00 something
15:02 you
15:03 decide
15:04 based on some experimentation.
15:06 Okay.
15:08 So
15:09 then you will run
15:10 your
15:11 training and I'm not gonna run it right?
15:14 Now because it's it's gonna take time so
15:16 i'm just going to show you the
15:18 the notebook which I ran before
15:20 you know which I used it before.
15:23 So I ran this
15:25 and I got
15:28 around
15:29 97 percent
15:31 accuracy and my validation accuracy is
15:33 95 and
15:35 you want to always test your model on
15:38 test data set before deploying it
15:40 because test data set is something model
15:42 has not seen so far.
15:44 When you run that you get 97 accuracy
15:47 which is pretty good
15:48 and then you will plot
15:51 you know your usual. The same thing
15:55 that we did in the in the part two and
15:56 three, which is plotting training and
15:59 validation accuracy that that code
16:00 doesn't change. So I'm not going to go
16:02 over it again you know
16:04 and then you can run prediction on
16:07 sample images
16:09 again. It's a generator. So previously in
16:11 a data set we used to do something like
16:13 take but here you just
16:15 use it live in a for loop. It's an
16:17 iterator generator, okay?
16:19 And you ran the prediction
16:21 the all these functions are same with
16:23 little change that's why I'm not going
16:24 over it but I ran it on couple of images
16:28 and I found that okay most of the
16:30 predictions were true if the actual
16:32 image is orally applied my predicted
16:34 image was also early blight and so on.
16:37 And then I saved the model in h5 format.
16:40 Okay. Now what is h5 format.
16:43 So here
16:47 again in Google if you say Tensorflow
16:52 save model
16:53 you will find
16:55 save model documentation and this is
16:57 this format called hdf file. Now this is
17:00 the old format right now they they use
17:03 usual you know they save it to directly
17:06 but what I like about this format is
17:09 the entire model gets saved into one
17:11 file.
17:13 Previously
17:14 when
17:15 we saved our models
17:17 you all know
17:18 that our models are saved like this in
17:20 it was like directory this is my one
17:22 saved model. This is my second. But every
17:24 model is a directory
17:26 but what I just did is I saved it in
17:29 this one file called potatoes.i
17:32 and that's the file I will be using
17:37 deploy to Google Cloud. In Google cloud I
17:40 will deploy this file
17:42 and we will make prediction
17:45 using the
17:46 Google Cloud function that is running on
17:49 that in the cloud.
17:51 All right! So I hope you like this video
17:53 if you are loving this series so far
17:55 please share it with your friends and
17:57 always practice along with me.
18:01 Only practice can help you if you just
18:03 watch the video, it's not gonna be useful.
18:05 So as I'm
18:07 coding on youtube, you make a pause you
18:10 write code, you practice with me and that
18:13 way you can learn effectively. In the
18:15 next video we'll deploy this h5 format
18:19 model to Google Cloud.