1
00:00:00,300 --> 00:00:06,420
In our previous section, we've built a deep learning model based on convolutional neural networks to

2
00:00:06,420 --> 00:00:10,710
help detect the presence of malaria in blood cells.

3
00:00:10,830 --> 00:00:18,810
Nonetheless, in the real world, we are not always going to be using our models on a lab notebook like

4
00:00:18,810 --> 00:00:19,440
this.

5
00:00:19,800 --> 00:00:24,300
Hence, we need to be able to save this model so it could be used externally.

6
00:00:24,630 --> 00:00:34,050
In the section we'll learn how to save and load the model and also do the same process with Google Drive.

7
00:00:34,080 --> 00:00:37,650
That is, we'll be able to save our model in our Google Drive.

8
00:00:37,650 --> 00:00:42,960
And then later on, when we want to use this model, we'll just load it from our Google drive.

9
00:00:42,990 --> 00:00:43,680
That's it.

10
00:00:43,710 --> 00:00:48,930
Don't forget to subscribe and hit the notification button so you don't miss amazing content like this.

11
00:00:49,260 --> 00:00:58,110
We've built this very performant model, though we could improve on it, but then once we close this,

12
00:00:58,200 --> 00:01:02,280
we do not save this model's current state.

13
00:01:02,280 --> 00:01:11,100
And so if we have to come next time, the model will have randomly initialized weights, which will

14
00:01:11,100 --> 00:01:19,230
be different from the weight we've got now after training on this data set and other issues in case

15
00:01:19,230 --> 00:01:27,030
we want to use this model in another scenario or in another environment like, say, on a browser or

16
00:01:27,030 --> 00:01:28,650
on a mobile phone.

17
00:01:30,410 --> 00:01:34,520
We'll need to find a way to export this model from your.

18
00:01:34,730 --> 00:01:39,020
And so TensorFlow allows us to save our model.

19
00:01:39,170 --> 00:01:46,200
Now we'll have to differentiate between a model's configuration and a model's weights.

20
00:01:46,280 --> 00:01:53,080
So a model like this, let's suppose we have a model which is defined as such.

21
00:01:53,090 --> 00:01:59,450
We have the input which will pass into account layer, and then we have batch normalization, we have

22
00:01:59,450 --> 00:02:05,780
pooling for subsampling and then we flatten and after flattening we pass to a dense layer and we have

23
00:02:05,780 --> 00:02:06,330
our output.

24
00:02:06,350 --> 00:02:09,240
So suppose we have this small model.

25
00:02:09,260 --> 00:02:19,430
Now all the parameters for the creation of this model are known as the model's configuration and the

26
00:02:19,430 --> 00:02:20,960
model configuration.

27
00:02:20,960 --> 00:02:29,480
Women have it that the model, for example, like in this case here, the model starts with a curve

28
00:02:29,510 --> 00:02:33,620
layer with six filters, canal size, three batch norm and all this.

29
00:02:33,620 --> 00:02:37,100
So these are our model's configurations.

30
00:02:37,400 --> 00:02:42,230
But this model's configurations are different from the model's weights.

31
00:02:42,230 --> 00:02:45,350
The model's weights are those filters.

32
00:02:46,110 --> 00:02:55,890
We have for example in the case of the conf two D, so we have the model weights and the models configuration.

33
00:02:56,690 --> 00:03:04,610
And upon summarizing the model, we see clear that we have this connectivity and then we have this number

34
00:03:04,610 --> 00:03:05,600
of parameters.

35
00:03:06,080 --> 00:03:13,670
And so whenever we want to save a model, we have to take into consideration this configuration and

36
00:03:13,670 --> 00:03:19,040
the weights, because for this same configuration, we could have different weights.

37
00:03:20,180 --> 00:03:23,370
And so there are actually two main options.

38
00:03:23,390 --> 00:03:32,750
The first option is to save the full model, that is to save the model configuration and the model weights.

39
00:03:32,900 --> 00:03:37,760
Another option will be to save only the model weights.

40
00:03:37,910 --> 00:03:40,130
So we could save all of the model weights.

41
00:03:40,580 --> 00:03:49,940
Now, this option is used when, for example, we don't want to or we don't even know this model configuration

42
00:03:49,940 --> 00:03:50,760
upfront.

43
00:03:50,780 --> 00:03:57,320
So we have used this year we've defined the model's configuration with trained it, we've got a new

44
00:03:57,320 --> 00:04:01,140
weights and this the current model state.

45
00:04:01,160 --> 00:04:09,650
But if we take this to another environment where we don't have this configuration, then if we've saved

46
00:04:09,650 --> 00:04:17,210
this model's configuration and weights, all we need to do is just to load this configuration and weights

47
00:04:17,210 --> 00:04:20,270
which have been packaged as the full model.

48
00:04:20,720 --> 00:04:28,130
Now, in another case where we are able to get the configuration and all we need is just the weights,

49
00:04:28,130 --> 00:04:35,300
then we'll just save the weights and then reload these weights since we already have the configuration.

50
00:04:35,450 --> 00:04:40,190
Either ways we will always need the configuration and the weights.

51
00:04:40,670 --> 00:04:46,790
Nonetheless, it's important to note that the most important part of this is actually the weights since

52
00:04:47,060 --> 00:04:51,080
working with a randomized or randomly initialized weights.

53
00:04:51,680 --> 00:04:58,490
After we've trained, our model isn't very useful and sometimes we may take many days to train this

54
00:04:58,490 --> 00:04:58,850
model.

55
00:04:58,850 --> 00:05:05,810
So imagine you train your model for like ten days and then you want to reuse that model and the weights

56
00:05:05,810 --> 00:05:07,550
have been randomly initialized.

57
00:05:07,790 --> 00:05:13,190
You find that those ten days have been wasted both time wise and monetary wise.

58
00:05:13,190 --> 00:05:19,100
So you have to ensure that you save your weights properly such that you could reuse them.

59
00:05:19,100 --> 00:05:24,950
And then the great thing with TensorFlow is you could also continue training from the state.

60
00:05:24,950 --> 00:05:32,960
So this means that at this point where we've gotten this model's performance, here's where we have

61
00:05:32,960 --> 00:05:34,040
94%.

62
00:05:34,040 --> 00:05:39,800
We could keep training from years so that we could get even to say 99%.

63
00:05:40,760 --> 00:05:45,080
So you have to ensure that your saving is done properly.

64
00:05:45,290 --> 00:05:47,020
Now let's get into that.

65
00:05:47,030 --> 00:05:54,020
But before getting to that one last point, also note that with the first method here, with this first

66
00:05:54,020 --> 00:06:01,610
method, apart from this model configurations, we also have information like the Matrix.

67
00:06:01,610 --> 00:06:10,010
So the matrix you use, like the accuracy, the loss, you use the optimizer, so the optimizer information

68
00:06:10,010 --> 00:06:11,360
you use and all that.

69
00:06:11,360 --> 00:06:15,190
So this kind of hyper parameter information has been saved here.

70
00:06:15,200 --> 00:06:20,260
So next time all you need to do is just to load your model and then make use of it.

71
00:06:20,270 --> 00:06:23,060
Whereas here all you're saving is just the weights.

72
00:06:24,560 --> 00:06:31,730
That said, let's see if our model, this case we have in that model, that safe actually then that

73
00:06:31,730 --> 00:06:33,500
model are safe and we give it a name.

74
00:06:33,500 --> 00:06:37,850
So we say the net saved model, for example.

75
00:06:37,850 --> 00:06:38,530
That's it.

76
00:06:38,540 --> 00:06:45,050
Now we have this little net saved model or we run that cell and we check this out here.

77
00:06:45,050 --> 00:06:46,610
So check out those files.

78
00:06:46,610 --> 00:06:47,930
And what do you see?

79
00:06:47,930 --> 00:06:50,390
You have this net saved model folder.

80
00:06:50,390 --> 00:06:54,470
In this folder you have the assets, which in this case is empty.

81
00:06:54,500 --> 00:06:56,240
You have the variables.

82
00:06:57,610 --> 00:06:59,710
Which actually contains the weights.

83
00:06:59,710 --> 00:07:04,810
So you could download this from yours, we could download this and then upload it next time.

84
00:07:04,810 --> 00:07:06,910
So from here we see you click on download.

85
00:07:06,910 --> 00:07:07,780
That's fine.

86
00:07:07,900 --> 00:07:10,730
We have the variables which contains the weights.

87
00:07:10,750 --> 00:07:11,560
That's it.

88
00:07:11,620 --> 00:07:20,680
And then we have this saved model that put about file here, which actually contains our configuration.

89
00:07:20,680 --> 00:07:25,650
We've had our configuration saved and our weights saved.

90
00:07:25,660 --> 00:07:28,120
Now let's load this.

91
00:07:28,120 --> 00:07:31,180
So we've saved this and now we can now load it.

92
00:07:31,720 --> 00:07:36,610
Note that you could always download this, so you could download the weights right here, download this.

93
00:07:36,610 --> 00:07:43,930
So let's click on download, get download all this and then next time all you need to do is just to

94
00:07:43,960 --> 00:07:44,740
load it.

95
00:07:44,950 --> 00:07:46,900
Now let's go ahead and load this.

96
00:07:47,650 --> 00:07:49,270
The loading is quite simple.

97
00:07:49,270 --> 00:07:54,640
Yeah, we will define a new model net load that we have loaded.

98
00:07:54,640 --> 00:08:01,510
Model equals tier dot cross dot models, dot load model.

99
00:08:01,510 --> 00:08:02,260
There we go.

100
00:08:02,260 --> 00:08:05,050
And now we specify this exact same name.

101
00:08:05,050 --> 00:08:08,340
We have the net saved model.

102
00:08:08,350 --> 00:08:16,600
Now what we do is we're going to do the net load at model and then summary.

103
00:08:16,600 --> 00:08:17,410
So that's it.

104
00:08:17,920 --> 00:08:19,990
We're going to run that and.

105
00:08:20,830 --> 00:08:24,580
We get this arrow here, which is unusual.

106
00:08:25,390 --> 00:08:27,520
Changing this name actually makes this work.

107
00:08:27,520 --> 00:08:30,550
So, Lynnette, and then your.

108
00:08:30,940 --> 00:08:35,530
So we have this little net and let's run that again.

109
00:08:35,530 --> 00:08:42,030
We save that and then we load this and we have our model right here.

110
00:08:42,040 --> 00:08:50,470
So this means if you have to come back to this notebook, all you need is to load this model which has

111
00:08:50,470 --> 00:08:53,710
been saved right here in this little neck folder.

112
00:08:54,610 --> 00:09:01,360
And so just like with this, we will replace this little net model by net load that model.

113
00:09:01,360 --> 00:09:02,650
So let's load this.

114
00:09:03,010 --> 00:09:05,920
Let's use this loaded model and do some predictions.

115
00:09:06,550 --> 00:09:07,240
There we go.

116
00:09:07,240 --> 00:09:08,320
Yes, we'll get you.

117
00:09:08,320 --> 00:09:12,280
You, you p p p here we have an error.

118
00:09:12,280 --> 00:09:14,650
You, you, you, you and p p.

119
00:09:14,650 --> 00:09:20,590
So it's kind of similar to what we have with the original model.

120
00:09:20,590 --> 00:09:21,220
From here.

121
00:09:21,220 --> 00:09:23,200
We could also evaluate this model.

122
00:09:23,200 --> 00:09:28,420
We have learned load that model, we evaluate that and let's see what we get.

123
00:09:28,960 --> 00:09:35,860
Recall previously we had 95.16%, so now we expect to have something around that value.

124
00:09:36,730 --> 00:09:37,390
There we go.

125
00:09:37,390 --> 00:09:40,810
We have exactly the same output as previously.

126
00:09:41,590 --> 00:09:48,700
Now we are going to look at how to load and save with the HD five format.

127
00:09:49,000 --> 00:09:56,560
Now, the PDF format is a lightweight version of this TensorFlow model saving method.

128
00:09:57,160 --> 00:10:00,950
Here there's only this slight difference.

129
00:10:00,970 --> 00:10:09,700
All we need to do is to say include this file extension so we have your HD hd f five and then we save

130
00:10:09,700 --> 00:10:10,160
that.

131
00:10:10,180 --> 00:10:14,530
Now you check this out, you should have the HD five appearance.

132
00:10:14,530 --> 00:10:20,980
So here we have the net hd F five and then you could see it's weight like 52 re megabytes.

133
00:10:20,980 --> 00:10:24,520
S Now let's load this model to load it.

134
00:10:25,330 --> 00:10:32,230
What we have here is the same code we had previously, and then here we specify h, d, f five.

135
00:10:32,230 --> 00:10:36,670
So there we go, we run that and we have exactly the same summary.

136
00:10:37,300 --> 00:10:38,080
So that's it.

137
00:10:38,590 --> 00:10:45,340
Now we're yet to work with custom layers that you have to note that and the case where you build custom

138
00:10:45,340 --> 00:10:52,210
layers, then those configurations aren't start when you're dealing with this HD file format.

139
00:10:52,210 --> 00:11:01,360
And so that's why generally it's preferable for you to use this first format in which we presented that

140
00:11:01,360 --> 00:11:01,640
set.

141
00:11:01,660 --> 00:11:05,450
We're done with this first method where we save the configurations and the weights.

142
00:11:05,470 --> 00:11:09,120
Now let's look at this next method where we save only the weights.

143
00:11:09,160 --> 00:11:14,980
So in this case, for example, where we're having this notebook, what we could do is simply just save

144
00:11:14,980 --> 00:11:19,720
the weights, given that we already have the model's configuration defined in here.

145
00:11:19,720 --> 00:11:24,760
So let's get straight away into looking at the save Weights method, which comes with TensorFlow.

146
00:11:25,120 --> 00:11:26,500
So take that off.

147
00:11:26,980 --> 00:11:32,740
And then right here we have the net model, which we defined already, and then we save this weights

148
00:11:32,740 --> 00:11:33,970
as the net weight.

149
00:11:34,810 --> 00:11:38,110
So here we've saved this weights, let's put it in the folder.

150
00:11:38,110 --> 00:11:40,480
So we have the weights folder.

151
00:11:40,480 --> 00:11:43,960
We run that again so we could see clearly our weights.

152
00:11:45,370 --> 00:11:46,210
Click on that.

153
00:11:46,210 --> 00:11:47,050
And there we go.

154
00:11:47,050 --> 00:11:52,090
We have our weights and this weights, we have the check pointing, we have the weights.

155
00:11:52,120 --> 00:11:56,140
Now notice how this weights here that's in this variables.

156
00:11:56,170 --> 00:11:57,670
If you click on this variables.

157
00:11:58,240 --> 00:12:04,090
Okay, so notice how there is some similarity between what we had here and this.

158
00:12:04,090 --> 00:12:06,850
Notice how this is the same as this.

159
00:12:06,850 --> 00:12:09,130
And then this index here is the same as this.

160
00:12:09,130 --> 00:12:14,110
Here we call we said that this variables contain the weights and then we have this check points.

161
00:12:14,470 --> 00:12:17,260
Subsequently, we're going to look at checkpoints and with TensorFlow.

162
00:12:17,260 --> 00:12:25,720
So for now, just know that this is how we save the weights and then upon defining your model, so you

163
00:12:25,720 --> 00:12:32,650
define your model, you can now load just this weights and not the whole model, but loading the weights.

164
00:12:32,650 --> 00:12:40,030
You saying that you don't want the optimization or the optimizer configuration, you don't want the

165
00:12:40,030 --> 00:12:43,660
metrics and you don't want the last configurations.

166
00:12:43,660 --> 00:12:46,570
So that said, let's look at how to load these weights.

167
00:12:46,780 --> 00:12:48,760
Now here is all we need to load the weights.

168
00:12:48,760 --> 00:12:52,630
We have the learned model, the load weights, and then we load this weights.

169
00:12:52,780 --> 00:12:53,590
Let's do this.

170
00:12:53,590 --> 00:12:56,920
So you see clearly that this loading actually works.

171
00:12:56,920 --> 00:13:02,410
So the first thing we do is we are going to re initialize our model, so we will rerun this.

172
00:13:02,410 --> 00:13:10,000
So we run this will compile our model and then we run this evaluation right here.

173
00:13:10,000 --> 00:13:11,440
So we evaluate the model.

174
00:13:11,440 --> 00:13:17,020
And so you see that when the models which are randomly initialized, we have very poor results.

175
00:13:17,500 --> 00:13:18,400
So there we go.

176
00:13:18,400 --> 00:13:20,290
After random initialization, we have.

177
00:13:20,450 --> 00:13:21,110
This.

178
00:13:21,140 --> 00:13:28,580
Now, what we'll do is we'll take that model and then we load the weights.

179
00:13:28,580 --> 00:13:38,270
So we're going to load the weights and then pass in our weights, slash the net weights, and that's

180
00:13:38,270 --> 00:13:38,970
fine.

181
00:13:38,990 --> 00:13:44,510
Let's run this again and then get a new model's performance.

182
00:13:44,780 --> 00:13:45,530
There we go.

183
00:13:45,530 --> 00:13:55,550
We see that our model now gets back to the 94.16% accuracy we had initially after doing the training.

184
00:13:55,580 --> 00:14:02,520
At this point, we've been able to load and save our model right here on Google Lab.

185
00:14:02,540 --> 00:14:09,260
But as we know, at the end of the session or after closing my notebook, all this information will

186
00:14:09,260 --> 00:14:09,820
be lost.

187
00:14:09,830 --> 00:14:13,700
So let's see how to save this information in Google Drive.

188
00:14:13,730 --> 00:14:17,480
We'll start by imparting this drive.

189
00:14:17,480 --> 00:14:22,550
So we have from Google Dot Drive from Google Lab.

190
00:14:22,550 --> 00:14:24,680
We're going to import the drive.

191
00:14:24,710 --> 00:14:25,490
Let's run that.

192
00:14:26,180 --> 00:14:29,570
And then the next thing we'll do is to mount this drive.

193
00:14:29,570 --> 00:14:34,820
So we have drive that mount and then we specify the location.

194
00:14:34,820 --> 00:14:43,700
So we have your drive and running that you will be asked to put in an authorization code right here

195
00:14:43,700 --> 00:14:45,320
to get this authorization code.

196
00:14:45,380 --> 00:14:48,860
Suffice it to click on this right, this link given to us here.

197
00:14:48,860 --> 00:14:53,120
So we click on that link and then once this pops up, we have this.

198
00:14:53,120 --> 00:14:55,640
You select your account.

199
00:14:55,940 --> 00:14:59,660
Once you select that account, you now go to connection.

200
00:14:59,660 --> 00:15:02,600
So you've connected and then you copy your code.

201
00:15:02,600 --> 00:15:04,250
So your code is copy it.

202
00:15:04,250 --> 00:15:08,570
Now you put it in your and then you press enter one.

203
00:15:08,570 --> 00:15:09,560
That's fine.

204
00:15:11,000 --> 00:15:14,480
You see we have your mounted at content slash drive.

205
00:15:14,480 --> 00:15:18,380
So mount in this location you could see clearly from here.

206
00:15:18,500 --> 00:15:25,520
And this tells us we are in this directory content and in this directory content we've created this

207
00:15:25,730 --> 00:15:27,440
directory drive.

208
00:15:27,530 --> 00:15:35,990
Now we click this open and then from this I can get access to my own Google Drive.

209
00:15:36,230 --> 00:15:43,280
If now I want to copy this little net folder into my Google drive so that next time I could just load

210
00:15:43,280 --> 00:15:44,840
it from my Google drive.

211
00:15:45,290 --> 00:15:50,020
I'll make use of this CP command right here.

212
00:15:50,060 --> 00:15:57,770
So what we'll have is CP some option and then we have the source and the destination.

213
00:15:57,770 --> 00:16:04,880
So here we are going to specify or we are going to use this are so recursive, so we're going to use

214
00:16:04,880 --> 00:16:07,160
this to copy directory recursively.

215
00:16:07,160 --> 00:16:16,760
And that said, we run the command, we have this here and then we specify this folders directory here.

216
00:16:16,760 --> 00:16:20,090
So we have content and then net.

217
00:16:20,090 --> 00:16:25,280
So we're copying this net and then to what destination to my drive.

218
00:16:25,280 --> 00:16:35,690
So we specify my drive, we have my drive and then in here I have Loonette all I just hit Loonette collab.

219
00:16:35,690 --> 00:16:36,530
So that's it.

220
00:16:36,710 --> 00:16:42,680
From here I'm going to run this my drive and then I'll search for the net collab.

221
00:16:42,680 --> 00:16:44,180
So that's what I have now.

222
00:16:44,180 --> 00:16:52,460
I have this Loonette collab right here and then our next step will be to copy from the my drive to the

223
00:16:52,460 --> 00:16:59,750
Google Collab Search that next time in case where we have not, for example, saved this year, we'll

224
00:16:59,750 --> 00:17:05,620
be able to just quickly get that information from the drive onto the Google collab.

225
00:17:05,640 --> 00:17:09,800
Also know that this is really used to with data sets.

226
00:17:09,800 --> 00:17:16,160
So what we can have here is data set and our collab and then we could transfer that data set to our

227
00:17:16,160 --> 00:17:18,740
drive and vice versa.

228
00:17:19,010 --> 00:17:21,380
Now let's do the same thing.

229
00:17:21,380 --> 00:17:28,220
So here we are going to copy this back, but this time around we're going to copy this into Loonette

230
00:17:28,220 --> 00:17:33,080
collab, so we're going to create a new photo, the Net collab and then take this information.

231
00:17:33,080 --> 00:17:37,640
So this time around we're copying from our drive and to Loonette collab.

232
00:17:37,640 --> 00:17:38,660
So that's it.

233
00:17:38,660 --> 00:17:46,010
And then Loonette collab, we run that and let's click on this click again and guess what we see we

234
00:17:46,010 --> 00:17:48,080
have our Loonette collab right here.

235
00:17:48,650 --> 00:17:52,940
Thank you very much for following up to this point and see you next time.
