1
00:00:00,180 --> 00:00:06,750
Hi and welcome back in this lesson, we'll take a look at using cameras to train our one CNN model on

2
00:00:06,750 --> 00:00:12,310
the Catzavelos Dogs dataset, which is basically something you can apply to your own datasets as well.

3
00:00:12,330 --> 00:00:15,330
So open this notebook and we'll get started in this lesson.

4
00:00:15,990 --> 00:00:17,640
So there's a lot going on in this lesson.

5
00:00:17,640 --> 00:00:21,030
So I may be a bit a bit quick with some things, however.

6
00:00:21,330 --> 00:00:25,920
I'll try my best to be slow and explain these things line by line and block by block.

7
00:00:26,580 --> 00:00:31,320
So firstly, in this first section, we're loading all of the necessary functions we're going to be

8
00:00:31,320 --> 00:00:31,740
using.

9
00:00:32,220 --> 00:00:36,480
Take note that we're using the skills tree and test split function.

10
00:00:36,600 --> 00:00:40,890
So it's no that now that's downloadable images and unzip them.

11
00:00:40,920 --> 00:00:42,840
This takes about 30 seconds, roughly.

12
00:00:43,410 --> 00:00:46,800
So while that's how well that works, well, that runs.

13
00:00:46,800 --> 00:00:48,710
I'll talk about this.

14
00:00:48,720 --> 00:00:53,040
This is where we just define our image width and height and image size, which takes more to do this

15
00:00:53,040 --> 00:00:54,990
width and height as inputs.

16
00:00:55,710 --> 00:01:00,090
And then we just set the number of channels because it's a color image we're using.

17
00:01:00,090 --> 00:01:03,360
So it's 60 by 60 by trees, the dimensionality of this.

18
00:01:04,140 --> 00:01:06,480
And next, we'll wait for that to finish.

19
00:01:07,680 --> 00:01:08,490
Yeah, here we go.

20
00:01:08,910 --> 00:01:10,500
So next, forget the error below.

21
00:01:10,530 --> 00:01:12,000
Let's run this and get rid of it.

22
00:01:12,960 --> 00:01:18,110
What we're doing here, we're going to load our data and we're going to store the file name and its

23
00:01:18,120 --> 00:01:19,500
class in a data frame.

24
00:01:19,680 --> 00:01:26,250
And the reason for that for us, because Keros has an automatic function that allows you to pull information

25
00:01:26,250 --> 00:01:31,830
like this out of the data frames, which are which is convenient because data scientists like myself

26
00:01:31,830 --> 00:01:35,590
and I'm assuming many of you would be familiar with pandas.

27
00:01:35,670 --> 00:01:37,320
And this is a very, very good tool.

28
00:01:37,470 --> 00:01:39,930
So Keros integrates directly with pandas.

29
00:01:40,380 --> 00:01:45,450
So once you have your file name and your class here, it's a very good way to separate it, in my opinion.

30
00:01:46,140 --> 00:01:47,310
You can actually do that.

31
00:01:47,340 --> 00:01:54,960
So to do this or to build this, all we have to do is actually just get the file names for all of the

32
00:01:54,960 --> 00:01:56,070
images in the directory.

33
00:01:56,520 --> 00:02:03,690
Split them by the DOT because if it's but that's how the clusters are named, it's not always going

34
00:02:03,690 --> 00:02:04,240
to be this way.

35
00:02:04,260 --> 00:02:09,060
Just remember that this is specific to this dataset, which is let's actually look at the test data

36
00:02:09,240 --> 00:02:10,260
actually again.

37
00:02:10,260 --> 00:02:16,110
And remember, you won't see it, but the images are named something like, Let's do this here.

38
00:02:17,280 --> 00:02:20,190
So if there's a dog in the image, it's going to be dog dot.

39
00:02:20,730 --> 00:02:22,710
Some no jpg.

40
00:02:23,280 --> 00:02:28,860
So if you split on the dot here and if a category, if you get if you grab that first element and split

41
00:02:29,130 --> 00:02:34,860
the fishery, you will probably get you can get whether it's a cat or dog in that sense.

42
00:02:35,760 --> 00:02:43,440
And then you can just use pandas after the reframe here to create the use.

43
00:02:43,440 --> 00:02:44,070
Those are easier.

44
00:02:44,070 --> 00:02:49,020
So you get the file names here and the categories, and you just create this data frame out of this.

45
00:02:49,140 --> 00:02:50,430
So that's quite convenient.

46
00:02:50,940 --> 00:02:56,350
And then you can do things like value counts and plot and bob plot to get the codes that each category.

47
00:02:57,120 --> 00:03:00,790
And you can even view a sample image here using the lower image function.

48
00:03:00,820 --> 00:03:02,820
This is not related to the pandas part of it.

49
00:03:02,820 --> 00:03:06,720
This is just a way for us to get a random image is random.

50
00:03:06,720 --> 00:03:11,880
The choice in the file names here we get our sample image name because it's in this.

51
00:03:12,030 --> 00:03:13,170
This is our actual pet.

52
00:03:13,740 --> 00:03:16,380
And we just use my plot lib to show the image.

53
00:03:18,180 --> 00:03:22,350
So it's quite simple and you get a different random image every time you run this code.

54
00:03:23,280 --> 00:03:28,830
So now this will create a model which we've have done many times before, and this is the same model

55
00:03:28,830 --> 00:03:30,330
we used to pay to listen to.

56
00:03:30,630 --> 00:03:37,650
It's a very simple model 16 filters, 3D filters, 64 filters, three battery filters, really activation,

57
00:03:38,340 --> 00:03:39,660
max pooling here.

58
00:03:40,260 --> 00:03:47,010
Then we just have to fully connected layers, 256 nodes, 128 nodes up to finally two nodes, which

59
00:03:47,010 --> 00:03:48,560
are where we have to solve Max.

60
00:03:48,570 --> 00:03:54,570
This gives us the probability we use Adam as the optimizer with a very low learning rate that first

61
00:03:54,720 --> 00:03:59,580
point zero zero five, you're probably going to have to go that low, but that's just keep it that way

62
00:03:59,580 --> 00:04:00,060
for now.

63
00:04:00,750 --> 00:04:06,090
And then we just find our model has four hundred and sixty six thousand parameters, not that much.

64
00:04:06,750 --> 00:04:11,040
Now what we're going to do is we're going to create our data generators.

65
00:04:11,040 --> 00:04:16,410
So let's take a look at how we do that to firstly, we need to replace all the classrooms, the ones

66
00:04:16,410 --> 00:04:21,240
and zeros that we saw previously in the data frame with the cat or dog.

67
00:04:21,480 --> 00:04:22,480
So let's do that.

68
00:04:22,920 --> 00:04:24,030
Let's move our convenience.

69
00:04:24,030 --> 00:04:28,920
We could have left it at one and zero, but we'll change it to cat and dog because it's nicer to look

70
00:04:28,920 --> 00:04:31,200
out for us, for human to and to it.

71
00:04:31,200 --> 00:04:37,530
If we see the grass next, we'll use the scale and train test bed function under the F dataset here

72
00:04:37,980 --> 00:04:43,260
to create training data sets and validation data frames are in here and data frame here.

73
00:04:43,920 --> 00:04:48,960
And then we just reset the index and drop trou just to get back a nice data frame like this.

74
00:04:49,320 --> 00:04:52,170
So let's run this and then we go.

75
00:04:52,170 --> 00:04:58,050
So we have that one day, let's take a look at our validation dataset, which is going to be.

76
00:04:59,940 --> 00:05:02,130
Oops, didn't start properly.

77
00:05:06,830 --> 00:05:09,860
So there we go, so you can see those are the frames here.

78
00:05:10,610 --> 00:05:14,630
This this data frame contains all the image names with the validation data.

79
00:05:15,140 --> 00:05:17,360
This one contains all the ones with the training data.

80
00:05:18,050 --> 00:05:20,870
Next, this is what is cool about Keros.

81
00:05:21,380 --> 00:05:27,740
There is this flow from data frame function that exists in the train to generate the function that we

82
00:05:27,740 --> 00:05:29,840
create with the image data generated here.

83
00:05:30,380 --> 00:05:33,050
This is similar to PyTorch, where we have our transmissions.

84
00:05:33,050 --> 00:05:36,680
We can reskill the image here, so played at risk of transformation here.

85
00:05:37,040 --> 00:05:39,590
It's not exactly the same thing, but it's quite similar.

86
00:05:40,310 --> 00:05:46,640
However, this is the cool part here with trained data gender flow from data frame, we can set the

87
00:05:46,640 --> 00:05:48,830
part to the data frame.

88
00:05:49,070 --> 00:05:49,910
That's the train.

89
00:05:49,910 --> 00:05:54,560
The data frame here said the part with the images where they're located.

90
00:05:55,220 --> 00:06:01,010
Then we just put the column names here with the X column name is found in what the white labels name

91
00:06:01,010 --> 00:06:02,240
is going to be class.

92
00:06:02,870 --> 00:06:09,050
Then we have an image size, which we defined before class melodies categorical that size here and there

93
00:06:09,050 --> 00:06:09,350
we go.

94
00:06:09,770 --> 00:06:15,140
So and now we do the same for the validation data generated exactly the same thing, actually.

95
00:06:15,920 --> 00:06:19,250
The only difference here is that we're using a different data frame.

96
00:06:19,370 --> 00:06:21,170
Otherwise, everything else remains the same.

97
00:06:22,190 --> 00:06:24,230
And here's an example of how it works.

98
00:06:24,770 --> 00:06:26,930
So this is just an example, this generator here.

99
00:06:28,010 --> 00:06:29,120
So let's take a look at this.

100
00:06:29,120 --> 00:06:34,340
So example the f here is just this is just a random line from this.

101
00:06:34,340 --> 00:06:35,810
Here's one image, basically.

102
00:06:36,530 --> 00:06:42,220
And what we do is if you just run this here, it's found one image one in this class.

103
00:06:42,230 --> 00:06:46,580
That's because it's only one image was set here, but we can then preview that image.

104
00:06:47,030 --> 00:06:48,440
So how do we use it out there?

105
00:06:48,860 --> 00:06:54,470
Well, for example, generate the returns, put the image and label.

106
00:06:54,950 --> 00:06:57,470
So you can actually get the image out of it here.

107
00:06:57,500 --> 00:06:59,480
Images stored in a multidimensional array.

108
00:06:59,480 --> 00:07:01,220
So you just index and the first one.

109
00:07:01,880 --> 00:07:04,000
And you can display that random image.

110
00:07:04,010 --> 00:07:07,370
For that example, image had its first image in our training dataset.

111
00:07:07,970 --> 00:07:09,580
So that's how this works.

112
00:07:09,680 --> 00:07:16,880
So you can see now that all you have to do now in fitting the model is to model that fit set pointed

113
00:07:16,880 --> 00:07:22,130
to a train that they generate through the generator, which contains our training images set to part

114
00:07:22,130 --> 00:07:28,010
four, set to a validation data generator to be equal to the validation data part of it.

115
00:07:28,490 --> 00:07:32,630
Remember, these are the things we created above here validation generator.

116
00:07:33,320 --> 00:07:39,470
And this is just a function that points basically to where the data is using the pandas data frame we

117
00:07:39,470 --> 00:07:43,650
created and we can train.

118
00:07:43,670 --> 00:07:47,150
Also remember, we have to do the validation steps and steps we book.

119
00:07:47,600 --> 00:07:48,860
This is a validation site.

120
00:07:48,860 --> 00:07:55,070
Abide by the VAT status of the training administered data size divided by the body size.

121
00:07:55,070 --> 00:07:56,540
So let's run this.

122
00:07:57,500 --> 00:08:00,530
It's going to take a little while to train roughly about a minute for each epoch.

123
00:08:01,100 --> 00:08:03,780
So while the trends continue with the lesson.

124
00:08:04,760 --> 00:08:09,740
And since most of this code has been run before the outputs exist here, so I can we can.

125
00:08:09,890 --> 00:08:15,660
I can teach you from those so you can see this model is being trained to see immediately.

126
00:08:15,680 --> 00:08:23,420
It's a 55 percent accuracy, which is a good sign because in a binary classification problem, 50,

127
00:08:23,420 --> 00:08:27,410
if it starts at 50 percent and it continues at 50 percent, it means it's not learning.

128
00:08:27,410 --> 00:08:32,000
But now we can see it's learning, so it's getting better, even though it's not perfect yet.

129
00:08:32,000 --> 00:08:33,380
Obviously, it's just started.

130
00:08:34,010 --> 00:08:36,950
So let's continue with this lesson to the next block.

131
00:08:36,950 --> 00:08:41,320
Here's where we saved the model, and here's where we thought the results here.

132
00:08:41,330 --> 00:08:47,810
So you can see in the first graph we have treating this with the legend, isn't he actually not sure

133
00:08:47,820 --> 00:08:49,960
why it's not OK?

134
00:08:49,970 --> 00:08:51,230
And take a look at occlude?

135
00:08:51,230 --> 00:08:57,050
Yeah, we need to have another legend above, but that's OK, so we can see this as the training accuracy

136
00:08:57,170 --> 00:08:58,400
which is going up here.

137
00:08:58,880 --> 00:09:01,880
Validation accuracy is going around 80 percent.

138
00:09:01,950 --> 00:09:02,600
Not too bad.

139
00:09:03,620 --> 00:09:10,400
And similarly, we can see this is the validation loss here, which you can see it's sort of a Fitbit

140
00:09:10,400 --> 00:09:13,550
and training dataset because the validation loss is going up already.

141
00:09:14,120 --> 00:09:17,300
And that's because it's probably just a very small network anyway.

142
00:09:17,330 --> 00:09:19,960
Even the small networks tend to generalize better.

143
00:09:20,540 --> 00:09:26,210
They don't always work too well, especially if you will between them, because you can see the training

144
00:09:26,390 --> 00:09:28,580
loss this blue line went down.

145
00:09:29,940 --> 00:09:31,460
Now let's take a look at this.

146
00:09:31,850 --> 00:09:34,880
Let's get the predictions for validation images here.

147
00:09:35,420 --> 00:09:41,990
So we just use the validation data generated here, and we use models that predict generator and steps

148
00:09:41,990 --> 00:09:43,360
is what we have before.

149
00:09:43,550 --> 00:09:45,680
Five divided that's about size.

150
00:09:46,160 --> 00:09:50,450
We use ceiling just to make sure it's not to make sure it's an integer that point.

151
00:09:51,080 --> 00:09:53,930
So we get the model's predictions out of it here.

152
00:09:54,980 --> 00:09:57,920
So then we can actually concatenated with our data frame here.

153
00:09:58,640 --> 00:10:03,770
This could I'm not going to go into too much depth with this code, but just look at the output to see

154
00:10:03,770 --> 00:10:04,380
what it does.

155
00:10:04,380 --> 00:10:06,060
So know what a.

156
00:10:06,160 --> 00:10:11,770
Both in know, you can see we have to ground your class here in the middle column and then the predicted

157
00:10:11,770 --> 00:10:15,940
class for my model, so you can see for all of these, it got rough.

158
00:10:16,120 --> 00:10:17,410
No, it didn't get that much, right?

159
00:10:17,770 --> 00:10:20,050
This one, in particular, the dog, when it's really a cat.

160
00:10:20,560 --> 00:10:21,970
This is not a mistake.

161
00:10:22,000 --> 00:10:25,030
I believe as silly of these two here and this one here.

162
00:10:25,510 --> 00:10:26,860
So it got a few them wrong.

163
00:10:26,980 --> 00:10:31,750
So at 80 percent accuracy, that's what you would expect generally.

164
00:10:32,410 --> 00:10:37,510
So no, we can inference and a bunch of images here and get the probabilities out of it.

165
00:10:38,050 --> 00:10:39,550
So you can see this.

166
00:10:39,550 --> 00:10:44,320
Here's a cat image here when to cut the probability in the brackets.

167
00:10:45,070 --> 00:10:47,410
That's what the title is giving us.

168
00:10:47,410 --> 00:10:50,890
We're getting a category out of it category is predicted.

169
00:10:50,890 --> 00:10:52,090
Finally, but this rule?

170
00:10:52,540 --> 00:10:58,960
So we're printing category here into the image as long as well as the file name, so you can see the

171
00:10:58,960 --> 00:11:00,260
file name as a ground shirt.

172
00:11:00,280 --> 00:11:03,060
And this is a prediction ground truth prediction.

173
00:11:03,070 --> 00:11:03,840
Good so far.

174
00:11:03,850 --> 00:11:07,630
Ground truth prediction or good or these?

175
00:11:07,630 --> 00:11:10,510
This one isn't good, though it's a cat, but it predicted a dog.

176
00:11:11,650 --> 00:11:14,110
And let's see if there are any more mistakes.

177
00:11:16,210 --> 00:11:18,280
No, but this is a mistake.

178
00:11:18,280 --> 00:11:19,510
Very obvious mistake here.

179
00:11:19,510 --> 00:11:20,920
This is definitely not a cat.

180
00:11:22,150 --> 00:11:24,880
So is this one definitely not a cat as well?

181
00:11:26,020 --> 00:11:28,490
This one is definitely not a cat.

182
00:11:28,510 --> 00:11:30,160
It's actually that it's a cat.

183
00:11:30,870 --> 00:11:35,290
And yeah, so you can see at 80 percent accuracy was still making quite a few mistakes.

184
00:11:35,290 --> 00:11:38,620
But generally, this cat, this dog classifier works fairly well.

185
00:11:39,080 --> 00:11:42,100
What's with such a simple model that's actually pretty good?

186
00:11:42,670 --> 00:11:48,520
So now let's take a look at implementing checkpoints and Keros checkpoints are actually super simple

187
00:11:48,520 --> 00:11:49,090
to implement.

188
00:11:49,900 --> 00:11:52,660
You would have seen some of the examples in the slides previously.

189
00:11:53,110 --> 00:11:56,420
So all you do, basically, you just look at the checkpoint you want to use.

190
00:11:56,440 --> 00:11:57,820
We're going to use model checkpoints.

191
00:11:58,300 --> 00:12:05,630
This saves animal waits after every epoch and early stopping, which is another one where we do it.

192
00:12:05,860 --> 00:12:10,900
We use it to monitor the devaluation, loss or validation accuracy, whichever one you want to monitor.

193
00:12:11,440 --> 00:12:13,530
You can specify whatever parameter you want.

194
00:12:13,540 --> 00:12:19,840
Here we are looking at validation, loss both of them and basically we set the parameters here.

195
00:12:19,870 --> 00:12:24,580
This is the value that it's required to change before we stop.

196
00:12:24,970 --> 00:12:27,460
This is no the box we wait before we stop.

197
00:12:28,330 --> 00:12:32,810
This is to open settings and we can restore best weights to be equal to true.

198
00:12:32,830 --> 00:12:39,400
So once we stop it, it saves the model with the best weights, and that specified actually just saves

199
00:12:39,400 --> 00:12:41,020
them the best, which is the model.

200
00:12:41,440 --> 00:12:47,080
So whatever model it keeps, it returns at the end is the best model in this case here.

201
00:12:48,040 --> 00:12:53,320
Similarly, for model check pointing here, you can set a pattern for where you want the model to be

202
00:12:53,320 --> 00:12:53,830
saved.

203
00:12:54,400 --> 00:13:01,300
So I'll just take off this part because this path, as it exists in this directory and this is the saving

204
00:13:01,300 --> 00:13:02,860
it to the best start for here.

205
00:13:03,610 --> 00:13:04,970
Again, that's it.

206
00:13:06,190 --> 00:13:12,960
Also, let's take a look at the lending rate on the tool so we can just set the parameters here as well.

207
00:13:12,970 --> 00:13:15,640
I wouldn't go into details with this because we did have in the slides.

208
00:13:15,640 --> 00:13:23,140
So what we do know, we have those three checkpoints we created here model check point, early stopping

209
00:13:23,830 --> 00:13:25,570
and reduced lending rates.

210
00:13:26,110 --> 00:13:28,990
So we put all of those check points in an array here.

211
00:13:28,990 --> 00:13:30,730
So we have recorded callbacks.

212
00:13:31,300 --> 00:13:36,610
And then all we have to do when we're doing model of fitting tariffs is put callbacks equal to the callbacks

213
00:13:36,610 --> 00:13:37,480
that we set here.

214
00:13:38,050 --> 00:13:38,890
And that's it.

215
00:13:38,980 --> 00:13:44,380
And then you start creating your model, and all of us basically obeys all of those rules.

216
00:13:44,380 --> 00:13:50,200
And you can see here that in this example, it's stopped trading after the sixth iteration because at

217
00:13:50,200 --> 00:13:55,150
that point, violation was, I believe, stopped it and improve from point sixty five.

218
00:13:55,630 --> 00:13:59,680
So it implemented it, really stopping and return the model with the best suites here.

219
00:13:59,950 --> 00:14:05,410
So the model now has those best suites based on the sixty bucks here, which most likely would have

220
00:14:05,410 --> 00:14:06,670
been the last one either.

221
00:14:06,720 --> 00:14:09,100
Actually, this will be this one.

222
00:14:09,220 --> 00:14:13,930
The third one point six point seven four, which is the one with the lowest suites.

223
00:14:15,040 --> 00:14:16,540
So that's it.

224
00:14:16,540 --> 00:14:20,290
For this lesson, I notice a bit lengthy and it was a lot going on in this lesson.

225
00:14:21,430 --> 00:14:23,660
I know things are getting a bit more difficult.

226
00:14:23,680 --> 00:14:29,950
However, I'm hoping you can keep up, and if you don't, please feel free to message me on Udemy support

227
00:14:29,950 --> 00:14:32,440
forums and I'll try to get back to you as soon as possible.

228
00:14:33,070 --> 00:14:37,840
Thank you, and we'll stop there for now and then after we'll take a look at PyTorch lightning.

229
00:14:38,440 --> 00:14:38,740
OK.

230
00:14:39,220 --> 00:14:40,300
So stay tuned for that.

231
00:14:40,450 --> 00:14:40,810
Thank you.
