1
00:00:00,080 --> 00:00:06,080
Hello, everyone, and welcome to this new section in which we'll delve deep into implementing data

2
00:00:06,080 --> 00:00:08,630
augmentation with TensorFlow two.

3
00:00:08,660 --> 00:00:16,880
The first method we'll use in implementing data augmentation will be using this TensorFlow image model,

4
00:00:16,880 --> 00:00:19,910
which is made of this different functions.

5
00:00:19,940 --> 00:00:25,070
Now, this is just a few of those functions and you could check out the rest in the documentation.

6
00:00:25,070 --> 00:00:31,850
So we'll be able to do stuff like adjusting brightness, contrast, gamma adjustments, saturation adjustment,

7
00:00:31,850 --> 00:00:35,630
flipping left, right, flipping up, down and rotation.

8
00:00:35,630 --> 00:00:43,970
Using this TF image is known to be a more flexible way of implementing data augmentation as we could

9
00:00:43,970 --> 00:00:49,100
alter an input image with all this different functions given to us.

10
00:00:49,130 --> 00:00:56,390
The next method we'll use will implement in this course is by working with the Keras layers.

11
00:00:56,390 --> 00:01:04,140
And although we're limited by the number of data augmentation layers made available to us, this method

12
00:01:04,140 --> 00:01:10,110
permits us to carry out data augmentation more efficiently and hence speeds up the training process.

13
00:01:10,110 --> 00:01:16,890
Now, to find a balance between these two methods, we could implement our own custom keras layers.

14
00:01:16,890 --> 00:01:19,980
And that's exactly what we're going to do in this section.

15
00:01:19,980 --> 00:01:23,970
We'll now look at how to implement data augmentation with TensorFlow.

16
00:01:23,970 --> 00:01:27,870
So we're going to get into TensorFlow and images.

17
00:01:27,870 --> 00:01:30,630
So here we have TF.

18
00:01:31,170 --> 00:01:32,700
Here is layers.

19
00:01:32,700 --> 00:01:37,200
We actually in Keras closes up and TF dot image actually.

20
00:01:37,200 --> 00:01:43,740
So we have the image with all these methods which we could use in data augmentation.

21
00:01:43,740 --> 00:01:51,420
Recall that in order to do data augmentation, we're basically modifying the images while keeping the

22
00:01:51,420 --> 00:01:52,830
labels fixed.

23
00:01:53,070 --> 00:01:57,630
So that said, as you could see here we have this several methods.

24
00:01:57,660 --> 00:01:59,730
Let's check out on this overview.

25
00:01:59,730 --> 00:02:02,880
And we should be able to get this list of methods.

26
00:02:02,880 --> 00:02:07,710
So here we have the list of methods and we have the categories.

27
00:02:07,710 --> 00:02:10,290
So here we have image adjustments.

28
00:02:10,320 --> 00:02:13,980
Working with bounding boxes here is for object detection.

29
00:02:13,980 --> 00:02:22,410
Where you have to get into this here is cropping, flipping and decoding and encoding.

30
00:02:22,410 --> 00:02:24,900
So this is what we have now.

31
00:02:24,900 --> 00:02:29,370
If we get back to the top, we will have this adjustments.

32
00:02:30,840 --> 00:02:37,410
Um, okay, so we have this resize which we've seen already and then we have this adjustments, we could

33
00:02:37,410 --> 00:02:44,700
adjust the brightness, contrast, gamma hue, jpeg quality, saturation, brightness, contrast, hue,

34
00:02:45,270 --> 00:02:48,330
saturation and per image standardization.

35
00:02:48,330 --> 00:02:55,050
Now notice that these ones here are random, so we are adjusting our image randomly.

36
00:02:55,050 --> 00:03:01,770
As you can see, we have random brightness, whereas here we actually adjusted this brightness with

37
00:03:02,840 --> 00:03:04,580
some fixed parameters.

38
00:03:04,580 --> 00:03:10,700
So if we click on this adjust brightness right here, you see we have this image and we have this fixed

39
00:03:10,700 --> 00:03:12,350
delta, which we choose.

40
00:03:12,350 --> 00:03:14,870
Whereas if it's random, let's get back.

41
00:03:14,870 --> 00:03:18,350
If it's random, that will be fixed randomly.

42
00:03:18,350 --> 00:03:20,930
So we click on this random brightness.

43
00:03:20,960 --> 00:03:27,650
You see we are just giving a max delta because we just all we need to pass in here is a range and then

44
00:03:27,950 --> 00:03:31,940
we are going to randomly pick a delta in that range.

45
00:03:32,540 --> 00:03:36,230
So that's it for this image adjustments.

46
00:03:36,230 --> 00:03:42,680
We also have this cropping so we could crop out some parts of the image, we could crop out the central

47
00:03:42,680 --> 00:03:44,420
crop, we could crop and resize.

48
00:03:44,420 --> 00:03:55,880
So this means if we have this image, so we have this image and it's 224 by 224 and then we want to

49
00:03:55,880 --> 00:03:59,690
do a crop, we could do a center crop like this.

50
00:03:59,720 --> 00:04:03,020
We could take this center crop.

51
00:04:03,020 --> 00:04:04,010
Sorry for that.

52
00:04:04,010 --> 00:04:10,010
We have this center crop and we have this new image right here, which is this center.

53
00:04:10,010 --> 00:04:18,560
Now, after getting this image, let's suppose we have now this smaller image of shape, say a 150 by

54
00:04:18,560 --> 00:04:19,790
150.

55
00:04:19,790 --> 00:04:28,340
And then what we could do now is to resize this so that we get this shape which we need to pass into

56
00:04:28,340 --> 00:04:30,290
the model so we could resize this.

57
00:04:30,290 --> 00:04:37,850
Hence the reason why we're using the crop or crop by resize crop and resize right here.

58
00:04:38,480 --> 00:04:43,670
And you could as well just crop and then do the resize manually.

59
00:04:43,760 --> 00:04:45,020
So that's it.

60
00:04:45,380 --> 00:04:52,540
We have flipping, rotating, so flipping we have an image, we flip it left, right, up, down or

61
00:04:52,550 --> 00:04:53,870
random randomly.

62
00:04:53,870 --> 00:04:55,430
So it's basically the same thing.

63
00:04:55,430 --> 00:04:59,660
But this time around we're doing the randomly rotation and.

64
00:04:59,830 --> 00:05:01,990
And the transposition of the image.

65
00:05:03,100 --> 00:05:10,030
So as you could see, TensorFlow gives us all these methods which we could use in modifying our images

66
00:05:10,030 --> 00:05:13,480
and hence augmenting our data.

67
00:05:13,960 --> 00:05:17,800
So here we will define this method, visualize.

68
00:05:17,800 --> 00:05:19,570
So you could see this clearly.

69
00:05:19,990 --> 00:05:24,290
We have the original image and then we have the augmented image.

70
00:05:24,310 --> 00:05:28,300
Now in here we have subplot.

71
00:05:29,200 --> 00:05:36,520
We want just one line, two columns, and this will occupy the first position in that in that two column

72
00:05:36,520 --> 00:05:37,240
space.

73
00:05:37,240 --> 00:05:43,690
So here now we have this image show which takes in the original and then we repeat this.

74
00:05:43,690 --> 00:05:52,480
So here we have this second position and then we have the augmented augmented.

75
00:05:53,620 --> 00:05:55,960
Okay, So that's our visualized method.

76
00:05:55,990 --> 00:06:00,170
Now what we'll do is we are going to get the original image.

77
00:06:00,170 --> 00:06:08,300
So let's say we have this original image which is equal, an element which we take from our data set.

78
00:06:08,300 --> 00:06:10,880
So we have train data set.

79
00:06:10,880 --> 00:06:13,370
And then let's just pick one element.

80
00:06:13,370 --> 00:06:19,850
So we have this, and then we take just one element from our data set.

81
00:06:19,880 --> 00:06:21,050
Now that's it.

82
00:06:21,050 --> 00:06:23,090
And this should output the label.

83
00:06:23,090 --> 00:06:26,360
So we should have the label and there we go.

84
00:06:26,360 --> 00:06:32,600
So we have the original image here, and then we now work with the augmented image.

85
00:06:32,600 --> 00:06:40,700
We get this augmented image and to obtain this augmented image, we are going to modify this original

86
00:06:40,700 --> 00:06:41,300
image.

87
00:06:41,300 --> 00:06:46,610
To do this modification, we are going to make use of this methods which we've seen already.

88
00:06:46,610 --> 00:06:49,310
So here, let's pick out this flip left, right.

89
00:06:49,310 --> 00:06:52,580
We'll click on this flip left, right and see what we get.

90
00:06:52,580 --> 00:06:54,740
So all we need to do is just pass the image.

91
00:06:54,740 --> 00:06:56,870
So let's copy this.

92
00:06:56,870 --> 00:07:02,480
And then what we have here is our flip left, right and our image which has been passed.

93
00:07:03,050 --> 00:07:03,740
So that's fine.

94
00:07:03,740 --> 00:07:06,620
We have original image.

95
00:07:06,620 --> 00:07:07,430
So that's it.

96
00:07:07,460 --> 00:07:14,510
Now we have this augmented image, we run that and then we could go ahead, visualize this augmentation.

97
00:07:14,510 --> 00:07:21,050
So here we have original image and augmented image.

98
00:07:21,230 --> 00:07:24,200
Okay, so we run this and see what we get.

99
00:07:24,200 --> 00:07:24,890
So that's it.

100
00:07:24,890 --> 00:07:31,670
You see that now in your data or in your data set, you will not only get this image right here, you

101
00:07:31,670 --> 00:07:36,560
will not only get this one, but you would get this and this.

102
00:07:36,560 --> 00:07:40,490
So this means our data set now will be multiplied by two.

103
00:07:41,450 --> 00:07:44,090
Let's check on other augmentation strategies.

104
00:07:44,090 --> 00:07:45,380
Let's get back.

105
00:07:46,460 --> 00:07:47,930
Um, here we go.

106
00:07:47,930 --> 00:07:52,400
We have flip up down this similar to what we've seen already.

107
00:07:52,400 --> 00:07:58,820
Okay, let's check out let's do this random flip up down and then let's click on this and then rotation

108
00:07:58,820 --> 00:08:00,050
by 90 degrees.

109
00:08:00,050 --> 00:08:05,000
So here we have this image similar and then they could specify the seed.

110
00:08:05,000 --> 00:08:09,380
So here we have the random flip up down, let's run that.

111
00:08:09,380 --> 00:08:18,410
And then here we have random flip up, down.

112
00:08:20,960 --> 00:08:23,450
Okay, so we run that again.

113
00:08:23,450 --> 00:08:26,450
We have our mental image and then we visualize.

114
00:08:27,170 --> 00:08:33,950
You see that it happens, that it's exactly the same image which is outputted, Let's run this again.

115
00:08:36,190 --> 00:08:38,020
And now all we get is insert this.

116
00:08:38,020 --> 00:08:41,770
So notice how the difference between these two images.

117
00:08:42,040 --> 00:08:42,880
So that's it.

118
00:08:42,880 --> 00:08:45,460
The next will be this route 90 degrees.

119
00:08:45,460 --> 00:08:46,570
You could click on that.

120
00:08:46,570 --> 00:08:51,280
You could always feel free to click and then understand what it actually means.

121
00:08:51,280 --> 00:08:56,320
Like, yeah, we told this rotate image counterclockwise by 90 degrees.

122
00:08:56,350 --> 00:08:58,770
Now let's take this off.

123
00:08:58,780 --> 00:09:06,040
We know clockwise is this direction, so rotating counterclockwise means we'll be rotating it in this

124
00:09:06,040 --> 00:09:08,380
direction by 90 degrees.

125
00:09:09,010 --> 00:09:09,970
So that's it.

126
00:09:09,970 --> 00:09:11,380
Let's take this off.

127
00:09:11,380 --> 00:09:13,750
And then we have Route 90.

128
00:09:13,900 --> 00:09:16,600
We run that and there we go.

129
00:09:17,500 --> 00:09:17,950
Okay.

130
00:09:17,950 --> 00:09:23,770
So as you can see, we've rotated this by 90 degrees and the anti-clockwise direction.

131
00:09:23,950 --> 00:09:29,170
We can also try out this adjust brightness and random saturation.

132
00:09:29,170 --> 00:09:31,900
So let's get back to this notebook.

133
00:09:31,900 --> 00:09:41,380
We have your adjust adjust brightness and we run this visualize.

134
00:09:44,170 --> 00:09:47,050
We have this error missing positional argument.

135
00:09:47,050 --> 00:09:51,430
So let's get back to this and understand how it's used.

136
00:09:51,460 --> 00:09:55,120
We check and adjust brightness and there we go.

137
00:09:55,150 --> 00:09:59,350
We need to pass in a delta, so we have to pass in this delta.

138
00:09:59,680 --> 00:10:05,380
Now, this delta should be in the range negative one one.

139
00:10:05,380 --> 00:10:11,980
And as you could see when you add this delta, it basically just adds up to each and every pixel we

140
00:10:11,980 --> 00:10:12,210
have.

141
00:10:12,220 --> 00:10:17,320
So this one turns to 1.1, this 11 turns to 11.1 and so on and so forth.

142
00:10:17,440 --> 00:10:23,560
And we told you that this delta is a scalar and it's an amount to add to the pixel values.

143
00:10:23,560 --> 00:10:26,380
So let's go ahead and add this delta.

144
00:10:27,130 --> 00:10:28,000
Let's have that.

145
00:10:28,000 --> 00:10:33,910
We add the delta and we have 0.1.

146
00:10:33,910 --> 00:10:36,730
So we run that again and this should be fine.

147
00:10:36,760 --> 00:10:38,110
Okay, so that's fine.

148
00:10:38,110 --> 00:10:41,680
We now visualize and that's what we get.

149
00:10:41,710 --> 00:10:44,600
You will notice that there's some difference with this.

150
00:10:44,600 --> 00:10:50,060
Like this one appears brighter than this and let's go ahead and increase this.

151
00:10:50,060 --> 00:10:53,360
Let's take 0.8 and then have that.

152
00:10:53,570 --> 00:10:53,960
Okay.

153
00:10:53,960 --> 00:10:58,040
So you see, we are able to modify this brightness.

154
00:10:58,970 --> 00:11:06,870
We could also include the random saturation, random saturation, and we have the original image.

155
00:11:06,890 --> 00:11:12,190
Let's get back to the documentation for random saturation and see what we need to pass in.

156
00:11:12,200 --> 00:11:13,100
So there we go.

157
00:11:13,100 --> 00:11:16,760
We have this image we pass in the lower and the upper limit.

158
00:11:16,790 --> 00:11:21,020
So here, um, let's find this lower upper limit.

159
00:11:21,380 --> 00:11:27,350
We told you we're going to get an error if the upper is less than the lower, which is logical.

160
00:11:27,350 --> 00:11:29,270
Lower should be less than the upper.

161
00:11:29,270 --> 00:11:31,160
And then if the lower is less than zero.

162
00:11:31,160 --> 00:11:34,900
So we have to ensure that we're dealing with values greater than or equal to zero.

163
00:11:34,910 --> 00:11:43,040
So getting back here, we have the saturation that and then we have lower, um, let's say two and then

164
00:11:43,040 --> 00:11:49,970
upper let's say 12, take this off, run again and visualize.

165
00:11:51,350 --> 00:11:52,340
Okay, so that's fine.

166
00:11:52,340 --> 00:11:56,660
So you see, we've added some random saturation and this is what we get.

167
00:11:56,990 --> 00:11:58,940
We could obviously reduce this.

168
00:11:58,970 --> 00:12:05,420
We could make this fall in the range of zero one and here is what we get.

169
00:12:05,630 --> 00:12:06,470
Okay.

170
00:12:07,310 --> 00:12:08,960
We now check out on cropping.

171
00:12:08,960 --> 00:12:12,200
Let's check out this central crop here.

172
00:12:12,200 --> 00:12:18,830
We have central crop, central crop and the original image.

173
00:12:19,790 --> 00:12:20,840
So that's what we have.

174
00:12:20,840 --> 00:12:25,850
Let's make sure we understand exactly how it's meant to be used.

175
00:12:25,850 --> 00:12:29,390
So here we have this and then there's this central fraction.

176
00:12:29,390 --> 00:12:32,030
So you have to explain how this works.

177
00:12:32,030 --> 00:12:41,120
Now, where X is the central 50% of the image and it is a float which lies between 0 and 1.

178
00:12:41,120 --> 00:12:48,110
As you could see here, 0.5 has been peaked, means that we're going to pick 50% of the image, but

179
00:12:48,650 --> 00:12:54,680
this portion of the image we're going to pick has to be centralized as it has to be surrounding the

180
00:12:54,680 --> 00:12:55,430
center.

181
00:12:55,430 --> 00:12:58,850
So that said, we will understand how that works.

182
00:12:58,850 --> 00:13:05,480
We can now put that, let's say, 0.3, run that and see what we get.

183
00:13:05,960 --> 00:13:09,030
Okay, So you see, we get this portion right here.

184
00:13:09,030 --> 00:13:13,050
Now, what if we expand this to like, say, 0.8?

185
00:13:13,080 --> 00:13:15,810
We should we expect to have some black regions now.

186
00:13:17,390 --> 00:13:18,230
Run that.

187
00:13:19,330 --> 00:13:23,080
And you see we have some black regions, so it's actually centralized.

188
00:13:23,920 --> 00:13:25,150
Let's take this off.

189
00:13:25,300 --> 00:13:26,980
You see, it's actually centralized.

190
00:13:26,980 --> 00:13:34,240
And we the reason why we have something like this is actually carrying out like this and something like

191
00:13:34,240 --> 00:13:34,930
this.

192
00:13:36,120 --> 00:13:39,720
So this is what we get in here.

193
00:13:40,470 --> 00:13:41,490
Something like this.

194
00:13:41,580 --> 00:13:46,020
Okay, so this is what we get when we do this central crop.

195
00:13:46,840 --> 00:13:52,870
Now, the way we're going to integrate this augmentation in our data pipeline is going to be similar

196
00:13:52,870 --> 00:13:55,260
to the way we did with the resize rescale.

197
00:13:55,270 --> 00:14:04,690
So just like we use this map method, we are going to reuse this same method for our augmentation here.

198
00:14:04,900 --> 00:14:12,670
Um, let's suppose we are going to, let's add this code and then define our AUGMENT method.

199
00:14:14,200 --> 00:14:22,660
So we have this, let's put this down and okay, so here we, let's just have here augment.

200
00:14:22,690 --> 00:14:26,740
We have this augment method, image label.

201
00:14:26,740 --> 00:14:35,020
And then what we'll do is we'll take the image and then for that image we're going to have, um, the

202
00:14:35,020 --> 00:14:39,280
rotate 90 degrees of that image.

203
00:14:39,760 --> 00:14:51,760
Then next we are going to have adjust saturation and then we'll pass in the image and specify the saturation

204
00:14:51,760 --> 00:14:52,560
factor.

205
00:14:52,570 --> 00:14:56,980
So here we have our saturation factor equal, say 0.3.

206
00:14:57,010 --> 00:15:05,050
Now you could always feel free to visualize this so you don't get to fully augment your data.

207
00:15:05,080 --> 00:15:11,470
That said, here we could have this saturate always and copy that and we just flip left, right.

208
00:15:11,470 --> 00:15:13,300
Let's come back to this.

209
00:15:13,330 --> 00:15:16,420
We have this adjust saturation.

210
00:15:16,690 --> 00:15:20,860
We could check that out and let's have it here.

211
00:15:22,090 --> 00:15:23,410
Okay, we have that.

212
00:15:23,560 --> 00:15:28,690
So we run this and visualize to see exactly what it's going to look like.

213
00:15:29,260 --> 00:15:33,340
We modify that image to original image and then run it again.

214
00:15:34,360 --> 00:15:36,080
Okay, so this is what we get.

215
00:15:36,100 --> 00:15:37,150
Now, that's fine.

216
00:15:37,150 --> 00:15:40,360
We can also include the flip left, right.

217
00:15:40,360 --> 00:15:48,250
So here we have image, um, tf dot image, flip left, right.

218
00:15:48,250 --> 00:15:55,300
And then what we do is we have, we can return the image and the label, so that's fine.

219
00:15:56,080 --> 00:16:00,820
Then we could also just simply include this resize, rescaling, this augment.

220
00:16:00,820 --> 00:16:11,470
So here we have uh, image and the label equal resize rescale, which takes in the image and the level.

221
00:16:11,470 --> 00:16:15,130
So we resize and rescale before doing the augmentation.

222
00:16:15,130 --> 00:16:16,150
So that's it.

223
00:16:16,180 --> 00:16:19,180
Now we have this augment method defined.

224
00:16:19,210 --> 00:16:21,790
We could now instead of doing this here.

225
00:16:21,790 --> 00:16:24,220
So here we have augment.

226
00:16:26,230 --> 00:16:30,730
Now another thing we want to do before doing the map is actually shuffling.

227
00:16:30,730 --> 00:16:33,860
So we'll modify the order in which we're doing things.

228
00:16:33,860 --> 00:16:41,840
Now here we have this train data set, we could have that and then we have the train data set right

229
00:16:41,840 --> 00:16:42,470
here.

230
00:16:43,910 --> 00:16:45,260
Dot Shuffle.

231
00:16:46,380 --> 00:16:49,480
Dot match and dot prefetch.

232
00:16:49,500 --> 00:16:51,000
So yeah, that's what we have.

233
00:16:51,000 --> 00:16:53,700
And then we just include the mapping right here.

234
00:16:53,700 --> 00:16:59,040
So instead of doing the mapping before the shuffling, we'll just do it after the shuffling.

235
00:16:59,040 --> 00:17:00,180
And there we go.

236
00:17:00,180 --> 00:17:04,710
Let's now have this, basically this with AUGMENT.

237
00:17:04,710 --> 00:17:08,100
So we just have that and then there we go.

238
00:17:08,100 --> 00:17:09,600
We have this map.

239
00:17:09,720 --> 00:17:14,190
Okay, So we now have that for the training and then we can close this up.

240
00:17:15,090 --> 00:17:16,800
So that's fine.

241
00:17:17,580 --> 00:17:20,520
We repeat the same process for the validation and the testing.

242
00:17:20,520 --> 00:17:28,540
So here we're going to modify it just as we did with the training we have here, our validation and

243
00:17:28,560 --> 00:17:33,660
then we include this map right here, map, resize, rescale.

244
00:17:33,660 --> 00:17:37,680
Now notice how we are not augmenting the validation and test datasets.

245
00:17:37,680 --> 00:17:45,570
So here we have the validation and then we now do the same for the testing anyway, for the testing

246
00:17:45,670 --> 00:17:48,550
we had, we only did the we only use the map.

247
00:17:48,550 --> 00:17:51,490
That is, we only did the resize rescale.

248
00:17:51,490 --> 00:17:52,930
So that's fine.

249
00:17:52,930 --> 00:17:56,470
We will now take this off train validation.

250
00:17:56,650 --> 00:17:57,730
That's off.

251
00:17:58,810 --> 00:17:59,800
That's fine.

252
00:18:01,000 --> 00:18:08,410
Okay, so here we have our test data, we have our train data and then we have our validation data.

253
00:18:09,130 --> 00:18:10,900
At this point, everything is set up.

254
00:18:10,900 --> 00:18:13,630
We could now rerun ourselves.

255
00:18:13,630 --> 00:18:15,850
So we start out with training.

256
00:18:15,850 --> 00:18:17,020
So that's fine.

257
00:18:17,020 --> 00:18:19,060
We'll come back, get back to this.

258
00:18:19,090 --> 00:18:27,370
We have our data, we run all the cells and our model created.

259
00:18:27,370 --> 00:18:34,540
So yeah, we're going to work only with this sequential API and skip out all this other methods of creating

260
00:18:34,540 --> 00:18:35,380
models.

261
00:18:35,590 --> 00:18:40,780
Once we're done with that, we get right up to this level.

262
00:18:41,500 --> 00:18:44,710
Okay, so now we get into training.

263
00:18:45,250 --> 00:18:46,480
That's fine.

264
00:18:46,480 --> 00:18:51,490
I'll compile the model and we wouldn't use any callbacks for now.

265
00:18:51,520 --> 00:18:54,400
Take that off and start training.

266
00:18:55,180 --> 00:18:59,950
We're now done with training and we'll see the results right here.

267
00:18:59,950 --> 00:19:03,610
So as you could see, the model performs very poorly.

268
00:19:04,200 --> 00:19:13,200
And we'll try to understand why the model doesn't perform as well as it performed without the data augmentation.

269
00:19:13,920 --> 00:19:19,980
Now, if you can recall, though, what we did for the data augmentation was that we rotated the image

270
00:19:20,400 --> 00:19:22,980
by 90 degrees, anti-clockwise.

271
00:19:23,010 --> 00:19:29,790
We adjusted the image, the image is saturation, and then we flip the image left, right.

272
00:19:30,360 --> 00:19:36,810
But taking a look at the kind of data set we're dealing with, this saturation shouldn't be a great

273
00:19:36,810 --> 00:19:37,660
idea.

274
00:19:37,710 --> 00:19:44,490
And this is simply because if we look at each and every one of these, what differentiates a parasitized

275
00:19:44,490 --> 00:19:51,900
cell like this one and this uninfected cell is this patches we have right here.

276
00:19:51,900 --> 00:19:58,080
So you'll notice that with the parasitized cell we have, we generally have this kind of patches, whereas

277
00:19:58,080 --> 00:20:01,080
with the uninfected cell, we practically have no patch.

278
00:20:01,080 --> 00:20:08,290
And with this, whereas here you see, you could see this patches and so rotating or flipping left,

279
00:20:08,290 --> 00:20:11,120
right, wouldn't change much about that.

280
00:20:11,140 --> 00:20:13,330
So that is acceptable.

281
00:20:13,360 --> 00:20:21,790
Now the problem with modifying the contrast is when you modify the contrast, you tend to make the parasitized

282
00:20:21,790 --> 00:20:25,750
cells and the uninfected cells less differentiable.

283
00:20:25,750 --> 00:20:34,240
And so it isn't a good idea in this case to modify the contrast or saturation of the image.

284
00:20:34,720 --> 00:20:42,490
Let's take this example so we could clearly understand why this saturation data augmentation strategy

285
00:20:42,490 --> 00:20:43,780
isn't a good idea.

286
00:20:43,810 --> 00:20:48,910
So right here what we do is we just copy out this and then paste here.

287
00:20:48,940 --> 00:21:01,510
We take out just two elements from our data set, and then here we have this one, four and two I plus

288
00:21:01,510 --> 00:21:07,680
one, and we have the show From here we create another subplot.

289
00:21:08,430 --> 00:21:09,560
There we go.

290
00:21:09,570 --> 00:21:10,800
We have that.

291
00:21:10,800 --> 00:21:14,010
And this one now is a saturation.

292
00:21:14,010 --> 00:21:18,120
So TF image dot adjust saturation.

293
00:21:18,360 --> 00:21:19,200
That's it.

294
00:21:19,200 --> 00:21:21,930
And then we're passing the image and that's what we have.

295
00:21:21,960 --> 00:21:25,740
So here we have this first subplot and this other subplot.

296
00:21:25,770 --> 00:21:30,870
We have that two now, or we could run this and see what we get.

297
00:21:32,070 --> 00:21:33,600
I'm missing the positional argument.

298
00:21:33,600 --> 00:21:38,340
That is the 0.3, the the saturation rate.

299
00:21:38,340 --> 00:21:40,290
So here we have 0.3.

300
00:21:40,290 --> 00:21:42,420
We're using exactly what we used previously.

301
00:21:42,420 --> 00:21:49,050
So here we have that and this is what we have and this example we have both in uninfected cells.

302
00:21:49,050 --> 00:21:51,780
So we wouldn't actually see much difference.

303
00:21:51,810 --> 00:21:56,760
It's true, this is modified, this two is modified, but there isn't much difference.

304
00:21:56,760 --> 00:21:59,520
Let's take a parasitized cell.

305
00:21:59,520 --> 00:22:01,080
Let's rerun this again.

306
00:22:01,110 --> 00:22:02,730
Hopefully we get a parasitized cell.

307
00:22:02,760 --> 00:22:03,090
Okay.

308
00:22:03,090 --> 00:22:06,580
As you can see here, um, let's take this off.

309
00:22:06,820 --> 00:22:13,390
As you could see in this parasitized cell, what could clearly show the model that this is a parasite

310
00:22:13,390 --> 00:22:15,820
cell is this patch we have right here.

311
00:22:15,820 --> 00:22:19,810
But when we adjust the saturation, it turns into this.

312
00:22:19,810 --> 00:22:25,180
And you notice that this parasitized cell now looks more like an uninfected cell.

313
00:22:25,180 --> 00:22:29,590
So the model isn't able to differentiate between these two clearly.

314
00:22:29,590 --> 00:22:37,860
So we see clearly that using the adjust saturation data augmentation strategy isn't a great idea.

315
00:22:37,870 --> 00:22:41,040
Let's comment this part and that's fine.

316
00:22:41,050 --> 00:22:43,840
Now we're going to modify our strategy.

317
00:22:43,840 --> 00:22:49,180
So just right here, what we had previously, we were going to take this off.

318
00:22:49,180 --> 00:22:53,200
So we comment this and then we'll retrain our model.

319
00:22:53,710 --> 00:22:55,960
And so here we retrain our model.

320
00:22:55,960 --> 00:23:00,820
After modifying our data augmentation strategy, we run this again.

321
00:23:01,030 --> 00:23:04,750
And after training, as you could see, we get this results.

322
00:23:04,780 --> 00:23:06,670
Now we check out this loss.

323
00:23:06,700 --> 00:23:08,230
This is what we get for the loss.

324
00:23:08,230 --> 00:23:15,490
And if we look at the accuracy, we see that it looks more like what we had previously, though we still

325
00:23:15,490 --> 00:23:26,530
have that maximum validation accuracy of about 94% and the train accuracy of about 99% right here.

326
00:23:26,530 --> 00:23:33,940
And though the data augmentation strategy hasn't closed this gap, we had between the training and validation,

327
00:23:33,940 --> 00:23:41,500
accuracy would see in other sections how the augmentation will be very instrumental in helping reduce

328
00:23:41,500 --> 00:23:45,550
this gap between the training and the validation scores.

329
00:23:45,550 --> 00:23:51,820
So far, we've been applying data augmentation by using the image methods.

330
00:23:51,820 --> 00:23:59,770
Now the other way we could apply data augmentation is by directly making use of Keras layers.

331
00:23:59,770 --> 00:24:01,360
So you come right here.

332
00:24:01,390 --> 00:24:03,520
TF dot keras layers.

333
00:24:03,740 --> 00:24:04,400
You could see.

334
00:24:04,400 --> 00:24:14,210
And then you'd notice that among those layers we have this layers which permit us do modifications on

335
00:24:14,210 --> 00:24:22,640
images, like with this random contrast which permits us randomly adjust contrast during training.

336
00:24:22,760 --> 00:24:31,160
Here we have the definition of this layer, this random contrast layer, and we have this attribute

337
00:24:31,160 --> 00:24:32,120
right here.

338
00:24:32,960 --> 00:24:38,210
Now, apart from this random contrast, we have the random crop, we have the random flip, and you

339
00:24:38,210 --> 00:24:42,330
could always get all this definitions get back to, yeah, let's have this flip.

340
00:24:42,350 --> 00:24:44,990
Now the mode horizontal and vertical.

341
00:24:44,990 --> 00:24:47,990
So we're going to flip it both horizontally and vertically.

342
00:24:48,020 --> 00:24:51,170
We could also have other modes like horizontal.

343
00:24:51,170 --> 00:24:54,590
That's kind of a left right flip.

344
00:24:54,600 --> 00:24:57,140
We have vertical kind of up down flip.

345
00:24:57,140 --> 00:25:02,330
We have horizontal and vertical, which is a mixture of the two and the default is horizontal vertical

346
00:25:02,330 --> 00:25:03,900
as we've just seen right here.

347
00:25:05,370 --> 00:25:12,330
We also have this random rotation which you could see random translation, random zoom, so you could

348
00:25:12,330 --> 00:25:17,970
randomly zoom the data you're training on and you also have this resizing.

349
00:25:17,970 --> 00:25:22,140
So we looked at resizing previously, but not actually on the data augmentation.

350
00:25:22,140 --> 00:25:24,630
So we did resize using TF image.

351
00:25:24,630 --> 00:25:28,580
Now you could also do this resizing via this resizing layer.

352
00:25:28,590 --> 00:25:34,290
Now here you have the height and the width as we had similarly to what we had in the image.

353
00:25:34,620 --> 00:25:37,560
We could scroll down and check on others.

354
00:25:38,760 --> 00:25:40,630
We don't really find anyone here.

355
00:25:40,650 --> 00:25:43,410
We could scroll back up and that's it.

356
00:25:43,440 --> 00:25:49,230
Anyway, what's important to note is that you could create or you could implement data augmentation

357
00:25:49,230 --> 00:25:53,530
via two main ways using the TF image and using the Keras layers.

358
00:25:53,550 --> 00:26:00,120
Now we are going to look at how to implement this data augmentation strategy we had previously via the

359
00:26:00,120 --> 00:26:01,050
Keras layers.

360
00:26:01,050 --> 00:26:07,530
So here we had this rotation, so we had rotation and we had the flip left, right.

361
00:26:07,530 --> 00:26:09,300
So we'll get back to the documentation.

362
00:26:09,300 --> 00:26:10,760
And then what do we have here?

363
00:26:10,770 --> 00:26:18,210
We have this random flip right here and then we're going to select the mode to horizontal since we're

364
00:26:18,210 --> 00:26:19,500
doing left right flip.

365
00:26:19,500 --> 00:26:21,180
So we'll copy this out.

366
00:26:21,210 --> 00:26:23,450
We have that layer, random flip.

367
00:26:23,460 --> 00:26:25,440
Let's put this just below.

368
00:26:25,440 --> 00:26:27,180
So this is the augmentation.

369
00:26:27,210 --> 00:26:30,900
This is augmentation using TF image.

370
00:26:30,990 --> 00:26:38,640
So TF image augment and then this is augmentation using keras layers.

371
00:26:38,640 --> 00:26:43,350
So tf Keras layers augment.

372
00:26:43,650 --> 00:26:46,080
Okay, so here we have our augment.

373
00:26:46,230 --> 00:26:53,670
It takes the image and the label, but since we actually built in Keras layers, we are not going to

374
00:26:53,670 --> 00:26:54,600
take this this way.

375
00:26:54,600 --> 00:26:58,020
We're going to have augment our layers.

376
00:26:58,440 --> 00:27:03,750
It's just a way of differentiating between these two augment and augment layers, which is a sequential

377
00:27:03,750 --> 00:27:04,320
model.

378
00:27:04,320 --> 00:27:10,470
So we have the sequential all we're going to make use of the sequential API of the Keras dot sequential.

379
00:27:10,470 --> 00:27:17,820
And then just right in here we are going to specify all this different steps.

380
00:27:17,970 --> 00:27:22,260
And so we copied this out from the documentation.

381
00:27:22,260 --> 00:27:23,700
Let's just paste it out here.

382
00:27:23,700 --> 00:27:27,540
We have this random flip, so we're going to use this random flip right here.

383
00:27:27,570 --> 00:27:28,950
Let's take this.

384
00:27:28,950 --> 00:27:30,900
Let's import random flip from the layers.

385
00:27:30,900 --> 00:27:34,200
So we have your keras layers.

386
00:27:34,350 --> 00:27:35,370
There we go.

387
00:27:35,370 --> 00:27:37,200
We will import random flip.

388
00:27:37,200 --> 00:27:38,670
Now, we could have this.

389
00:27:38,700 --> 00:27:41,430
We have random flip.

390
00:27:41,460 --> 00:27:44,010
Okay, so we have the random flip imported.

391
00:27:44,010 --> 00:27:47,600
And then the other one is that rotation.

392
00:27:47,610 --> 00:27:48,450
Let's get back.

393
00:27:48,450 --> 00:27:51,810
And we have here, um, rotation.

394
00:27:51,810 --> 00:27:52,680
Let's check on that.

395
00:27:52,680 --> 00:27:57,210
We have, let's scroll up and let's just do a quick search.

396
00:27:57,210 --> 00:27:59,160
So here we have rotation.

397
00:27:59,160 --> 00:27:59,640
Okay?

398
00:27:59,640 --> 00:28:01,680
So here we have random rotation.

399
00:28:01,680 --> 00:28:06,510
And don't forget we rotate it 90 degrees in the anti-clockwise direction.

400
00:28:06,510 --> 00:28:13,800
So here we have to be careful to make sure we re-implement that same kind of rotation we had seen before.

401
00:28:13,800 --> 00:28:16,110
Now here we have this definition.

402
00:28:16,110 --> 00:28:23,730
We have a factor fill mode, reflect interpolation by linear, the seed for reproducibility fill value.

403
00:28:23,730 --> 00:28:24,630
And that's it.

404
00:28:24,660 --> 00:28:29,190
Now let's check down here and understand better this factor.

405
00:28:29,820 --> 00:28:36,960
Now this factor we have here is actually this tuple, and this tuple is going to represent our range.

406
00:28:36,960 --> 00:28:42,780
To better understand this range, you should note that it's like each and every one of these represents

407
00:28:42,810 --> 00:28:44,340
a fraction of two pi.

408
00:28:44,370 --> 00:28:45,900
Now what is pi?

409
00:28:45,930 --> 00:28:48,480
Pi equals 180 degrees.

410
00:28:48,480 --> 00:28:51,420
So two pi is 360 degrees.

411
00:28:51,420 --> 00:28:54,150
So we're looking for a fraction of 360 degrees.

412
00:28:54,180 --> 00:28:59,790
Now recall that we're going anticlockwise by 90 degrees.

413
00:28:59,790 --> 00:29:03,480
Like we're just trying to re do the exact same.

414
00:29:03,550 --> 00:29:05,560
Thing we have with the TF image.

415
00:29:05,560 --> 00:29:09,370
So here we are going to look for a way to get 90 degrees from this 360.

416
00:29:09,400 --> 00:29:11,600
And to do that we need to divide this by four.

417
00:29:11,620 --> 00:29:20,710
In other words, it means we're going to get 25% of this 360 and 25% of 360 is 90 degrees.

418
00:29:20,710 --> 00:29:24,040
So 90 degrees not percent.

419
00:29:24,040 --> 00:29:28,460
So that said, we want to get 25% and that's fine.

420
00:29:28,480 --> 00:29:33,790
Now, from what is given to us here, this is we represent as a single float.

421
00:29:33,790 --> 00:29:36,130
This value is used for both the upper and the lower bound.

422
00:29:36,130 --> 00:29:41,380
For instance, this results in an output in an output rotation by a random amount in this range.

423
00:29:42,010 --> 00:29:49,090
Um, a positive value means rotating counterclockwise, while a negative means rotating clockwise.

424
00:29:49,120 --> 00:29:52,990
Now we're interested in rotating counter clockwise or anti-clockwise.

425
00:29:52,990 --> 00:29:54,900
Thus we're actually interested in rotating.

426
00:29:54,910 --> 00:29:56,920
This way we call it was this.

427
00:29:56,920 --> 00:29:59,470
This is clockwise, so we're not interested in this.

428
00:29:59,500 --> 00:30:03,440
We're interested in this anti-clockwise 90 degrees rotation.

429
00:30:03,440 --> 00:30:09,990
So we take positive values and then we will try to ensure that it lies in that given range.

430
00:30:10,010 --> 00:30:16,700
Now, given that we are actually picking just random numbers and unlike the Route 90 method we are seeing,

431
00:30:16,700 --> 00:30:23,060
which was somehow fixed, yeah, what we do is we could just pick a range between, say, 90 degrees

432
00:30:23,060 --> 00:30:30,560
and say 90.1 degrees or any value very close to 90.

433
00:30:30,590 --> 00:30:33,470
So that said, recall is going to be positive.

434
00:30:33,470 --> 00:30:38,050
Unlike uh, if it was clockwise, it will be negative.

435
00:30:38,060 --> 00:30:46,100
So here we're going to pick the 0.25 because we want to get 25% of this 360.

436
00:30:46,100 --> 00:30:47,570
So we have 0.25.

437
00:30:47,600 --> 00:30:56,990
That's positive, 0.25 up to say 0.2, let's say 501.

438
00:30:56,990 --> 00:30:58,700
So let's pick out this range.

439
00:30:58,700 --> 00:31:00,230
So we'll work in this range.

440
00:31:00,230 --> 00:31:05,100
So that will be around this 90 degree that we actually looking for.

441
00:31:05,120 --> 00:31:15,650
Now that said, since we're taking 0.250.205201, what we will have here is a 25% of two pi, which

442
00:31:15,650 --> 00:31:16,460
is 90 degrees.

443
00:31:16,460 --> 00:31:23,720
So it's going to be translating it to 90 degrees and a value very close to 90 degrees.

444
00:31:23,720 --> 00:31:30,830
So it will be like 90 point a value very close or 0.00 whatever degrees.

445
00:31:30,830 --> 00:31:32,420
So it's going to be around this.

446
00:31:32,420 --> 00:31:35,680
So we pick this up and that's fine.

447
00:31:35,690 --> 00:31:40,880
Now we ensure it's anti-clockwise because we've picked it to be positive.

448
00:31:40,910 --> 00:31:47,330
Now the film mode and the fill values will just be the default values, even the interpolation tool.

449
00:31:47,360 --> 00:31:54,350
So that said, we get back to the code, let's copy this out right here, run the rotation, copy this

450
00:31:54,350 --> 00:31:56,150
out, get back to the code.

451
00:31:56,150 --> 00:32:01,220
Let's include random rotation here so we have random rotation.

452
00:32:01,340 --> 00:32:03,020
Okay, random rotation.

453
00:32:03,020 --> 00:32:03,980
That's fine.

454
00:32:04,520 --> 00:32:06,050
We could now run this.

455
00:32:06,620 --> 00:32:11,510
Let's run this cell and then get back to our method.

456
00:32:11,510 --> 00:32:16,100
Or this layer we're trying to construct right here.

457
00:32:16,100 --> 00:32:19,520
So here we have the random flip and then the random rotation.

458
00:32:19,640 --> 00:32:23,750
Now we started in this order method with the rotation.

459
00:32:23,750 --> 00:32:25,240
So here we're going to redo that.

460
00:32:25,250 --> 00:32:31,880
Now we have this random rotation, random rotation, okay?

461
00:32:31,880 --> 00:32:35,030
And then we have this factor.

462
00:32:35,030 --> 00:32:38,960
So we just do exactly what we just agreed to put.

463
00:32:38,960 --> 00:32:46,160
So we have the factor a tuple take 0.25 and 0.32501.

464
00:32:46,370 --> 00:32:47,000
Okay.

465
00:32:47,000 --> 00:32:48,020
Now we have that.

466
00:32:48,020 --> 00:32:49,880
We just for the rest we take the default value.

467
00:32:49,880 --> 00:32:51,290
So we don't need to put those values.

468
00:32:51,290 --> 00:32:52,250
So that's fine.

469
00:32:52,250 --> 00:32:54,560
We've done with this first layer.

470
00:32:54,590 --> 00:32:58,400
The next layer will be this random flip.

471
00:32:58,400 --> 00:33:05,270
So we just put this random flip, let's copy this, copy that and then paste it out here.

472
00:33:05,270 --> 00:33:06,800
Now we have our random flip.

473
00:33:06,800 --> 00:33:11,300
We've decided on the mode, Take that off and that's fine.

474
00:33:11,300 --> 00:33:14,390
Now this is horizontal, so let's have this as horizontal.

475
00:33:14,510 --> 00:33:20,030
Okay, so we have this random flip and that and there we go.

476
00:33:20,030 --> 00:33:23,300
We have our layer, our sequential layer.

477
00:33:23,720 --> 00:33:28,160
Okay, now let's take this off and that's fine.

478
00:33:28,160 --> 00:33:33,050
So we have this augmented layers or this augment layers which we run this time around.

479
00:33:33,080 --> 00:33:34,430
Horizontal is not defined.

480
00:33:34,460 --> 00:33:42,980
Now let's use the small case, uh, horizontal and run again.

481
00:33:44,030 --> 00:33:45,470
Horizontal, still not defined.

482
00:33:45,500 --> 00:33:50,240
Let's pass this as a string horizontal run again.

483
00:33:50,270 --> 00:33:52,940
Then we could redefine an augment method.

484
00:33:52,940 --> 00:34:01,160
Let's call this method, augment layer, and then this takes in the image image layer.

485
00:34:02,060 --> 00:34:03,290
Sorry, labels.

486
00:34:03,730 --> 00:34:10,960
And then what we'll do is we're going to return this augment layers which we just created, and then

487
00:34:10,960 --> 00:34:16,240
passing the image specify that training is equal to true if that's okay.

488
00:34:16,240 --> 00:34:17,710
And then we send out the labels.

489
00:34:17,710 --> 00:34:22,810
So here we're going to take this augment layers, that's we're going to take the image and pass into

490
00:34:22,810 --> 00:34:24,790
this layer right here.

491
00:34:26,020 --> 00:34:31,750
It's going to rotate the image and then into this other layer which is going to flip that image.

492
00:34:31,750 --> 00:34:37,630
So we get our output, which has been augmented and we have our labels, we run this, run this, and

493
00:34:37,630 --> 00:34:44,020
then we get to this here where we have our AUGMENT and we have augment layer.

494
00:34:44,020 --> 00:34:46,600
So we call the augment layer method.

495
00:34:46,600 --> 00:34:48,790
Right here we call this method.

496
00:34:48,790 --> 00:34:52,150
That's fine, we run that and everything is okay.

497
00:34:52,660 --> 00:34:58,180
Now after running this training, we have this error, so it shows that we have an error at the level

498
00:34:58,180 --> 00:34:59,640
of the shapes.

499
00:34:59,650 --> 00:35:09,440
Now let's get back the problem here is we forgot to do this resizing before the data augmentation so

500
00:35:09,440 --> 00:35:12,500
we could copy this out and then put out this resizing.

501
00:35:12,500 --> 00:35:18,260
Here we have the image and labels or label, let's say image and label.

502
00:35:18,260 --> 00:35:21,440
We have that and then we resize and rescale.

503
00:35:21,440 --> 00:35:24,110
After resizing and rescaling, we now do the augmentation.

504
00:35:24,110 --> 00:35:29,360
So we run this again and then we run this training.

505
00:35:30,110 --> 00:35:31,970
As you can see, all is well.

506
00:35:31,970 --> 00:35:37,730
We will now look at another way of creating of doing data augmentation.

507
00:35:37,910 --> 00:35:45,080
Before doing that, we should also note that instead of doing this, resize rescale using the TF image

508
00:35:45,080 --> 00:35:53,120
as we had done here, that is using this TF image resize, and then by dividing by 255 as we just did,

509
00:35:53,120 --> 00:35:57,260
we could also use the resizing and rescaling layers.

510
00:35:57,260 --> 00:36:01,040
So right here we're going to include those layers.

511
00:36:01,040 --> 00:36:07,090
Here we have resizing and then rescale.

512
00:36:08,080 --> 00:36:13,270
We run that and then just here we're going to include that resize rescale.

513
00:36:13,270 --> 00:36:17,830
So here we say resize, rescale layers.

514
00:36:18,250 --> 00:36:23,440
Okay, so now we have this, our sequential API and then we always start with resizing.

515
00:36:23,440 --> 00:36:25,480
So we have the resizing.

516
00:36:25,630 --> 00:36:29,350
Take this off and have this.

517
00:36:29,350 --> 00:36:31,870
Yeah, we're going to put our image size.

518
00:36:31,870 --> 00:36:35,590
We have M size M size.

519
00:36:35,740 --> 00:36:36,310
Okay.

520
00:36:36,310 --> 00:36:38,260
And then we do the rescaling.

521
00:36:38,260 --> 00:36:44,500
So we have rescaling take this off and we do the rescaling.

522
00:36:45,040 --> 00:36:51,880
So here we have this taken off one divided by 255 Okay.

523
00:36:51,880 --> 00:37:00,850
So now we have this layers which are responsible for resizing and rescaling, uh, resizing and rescaling.

524
00:37:00,850 --> 00:37:05,870
So we'll see how we'll replace this method in doing the resizing scale.

525
00:37:05,870 --> 00:37:08,720
So that said, let's get back to this augment.

526
00:37:08,750 --> 00:37:11,570
We had augment layer right here.

527
00:37:12,680 --> 00:37:19,430
After getting this image, we do resize, rescale and layer.

528
00:37:19,430 --> 00:37:24,110
So we do the resize scale on the image before doing the augmentation.

529
00:37:24,110 --> 00:37:29,870
Let's take this off now and this should be fine now since we've built this resize rescale layers, we

530
00:37:29,870 --> 00:37:37,730
could replace this here, resize rescale layers even for the test set and then we do same for this.

531
00:37:37,730 --> 00:37:40,550
This already fine augment layer as well.

532
00:37:40,550 --> 00:37:45,770
We've seen already for the validation we have resized rescale layers.

533
00:37:45,890 --> 00:37:46,550
Okay.

534
00:37:46,550 --> 00:37:47,330
So that's fine.

535
00:37:47,330 --> 00:37:51,890
We run this, run the validation run train and that's fine.

536
00:37:52,790 --> 00:37:57,130
We then train our model and see what we get.

537
00:37:57,140 --> 00:37:58,010
So that's it.

538
00:37:58,010 --> 00:37:59,030
Our model is training.

539
00:37:59,030 --> 00:38:00,470
Everything is working well.

540
00:38:00,470 --> 00:38:08,090
Now let's pause this and then check out this other interesting way of doing this data augmentation.

541
00:38:08,090 --> 00:38:15,890
So as of now, we've seen how to do data augmentation by using TF image and then by building this layers.

542
00:38:15,920 --> 00:38:19,100
Now we'll see the importance of building these layers.

543
00:38:19,940 --> 00:38:29,090
But before looking at this importance, it's important to note this, and that is the fact that when

544
00:38:29,090 --> 00:38:32,240
you're working with a TF image, you have more flexibility.

545
00:38:32,240 --> 00:38:38,750
As with those layers, you are kind of like restrained to what you could do with data augmentation.

546
00:38:38,750 --> 00:38:46,700
Though this layers already have many common data augmentation strategies like the rotation, the zoom,

547
00:38:46,700 --> 00:38:51,110
the crop, the flipping, and the others.

548
00:38:51,260 --> 00:38:56,810
Nonetheless, when you're working with a TF image, you will see that you have many more operations

549
00:38:56,810 --> 00:38:57,650
you could do.

550
00:38:57,650 --> 00:39:03,410
Like if you open up this TF image right here, you see you have many more methods and.

551
00:39:03,450 --> 00:39:05,470
You could do many more stuff with this.

552
00:39:05,490 --> 00:39:13,710
Anyway, getting back to our code, we have to note that the advantage of this layers is that you could

553
00:39:13,710 --> 00:39:17,070
embed this into the model itself.

554
00:39:17,100 --> 00:39:18,420
Now let's explain.

555
00:39:18,420 --> 00:39:21,050
Supposing you have let's take this off.

556
00:39:21,060 --> 00:39:26,220
Supposing you have your training pipeline here, so you have that TF data.

557
00:39:26,250 --> 00:39:31,560
You do the shuffling, you do the mapping, you do prefetching caching and everything you need to do,

558
00:39:31,680 --> 00:39:33,150
like with your training data.

559
00:39:33,150 --> 00:39:34,770
So you are using your data.

560
00:39:34,770 --> 00:39:41,520
And then next, after doing all this resizing and scaling and everything, you now pass this into your

561
00:39:41,520 --> 00:39:42,310
model.

562
00:39:42,330 --> 00:39:50,040
So you pass this into your model and then you train your model and have all your results.

563
00:39:50,070 --> 00:39:59,190
Now, when you want to test this model, you will actually still need to do resizing and rescaling.

564
00:39:59,190 --> 00:40:07,270
So you still need to ensure that you resize your image and then you also ensure that you rescale your

565
00:40:07,270 --> 00:40:09,790
image before passing to the model.

566
00:40:09,820 --> 00:40:16,360
Now, if you recall like, let's, let's go down, let's get down here, you would see under the testing

567
00:40:16,360 --> 00:40:21,040
section, let's look at this under the testing section.

568
00:40:22,200 --> 00:40:31,680
That will make use of this test data and this test data, as we have seen from here, was already resized

569
00:40:31,680 --> 00:40:32,460
and rescaled.

570
00:40:32,460 --> 00:40:38,140
So if you check here, you see we have done some resizing and rescaling right here.

571
00:40:38,160 --> 00:40:46,260
Now, what if you have to take this this your model, which you have trained into another setup and

572
00:40:46,260 --> 00:40:53,400
obviously in that new setup you will still need to resize and rescale because just like here, before

573
00:40:53,400 --> 00:40:57,150
testing your model, you had to resize and rescale.

574
00:40:57,360 --> 00:41:03,750
So what if we input this resizing and rescaling in the model?

575
00:41:03,750 --> 00:41:11,100
So if we embed this resize and rescale in the model, it means that no matter where we go with this

576
00:41:11,100 --> 00:41:18,480
model, we don't need to resize and rescale all we need to do now we need we will take this off or rather

577
00:41:18,480 --> 00:41:20,820
I will take this and put it in the model.

578
00:41:20,820 --> 00:41:26,590
So all we need to do now is just to pass in your data without the resizing or rescaling and the job

579
00:41:26,590 --> 00:41:27,370
is done.

580
00:41:27,970 --> 00:41:34,120
So now we're going to see how to do this with this augment or rather with the resize rescale layers

581
00:41:34,120 --> 00:41:35,080
which we've just seen.

582
00:41:35,080 --> 00:41:40,450
We also do with the AUGMENT, but the AUGMENT isn't very useful since in testing time we don't make

583
00:41:40,450 --> 00:41:45,450
use of the AUGMENT or what's more important here to note is the resizing and rescaling.

584
00:41:45,460 --> 00:41:50,320
Although if you can include this, augment layers into your model and make the training faster.

585
00:41:50,800 --> 00:41:53,950
Anyways, let's go ahead and see how this is done.

586
00:41:53,950 --> 00:42:00,100
So firstly, you have the layers which you've built, you've had the augment layers and then you've

587
00:42:00,100 --> 00:42:04,120
had the resize rescale layers which we have had here.

588
00:42:04,120 --> 00:42:12,160
So all we need to do is to get to this model right here and then after the input layer you just have

589
00:42:12,160 --> 00:42:16,540
the resize, rescale layers and that's it.

590
00:42:16,540 --> 00:42:21,310
And then you have augment, augment layers.

591
00:42:21,580 --> 00:42:26,110
So here you have resize rescale layers and augment layers and that's all.

592
00:42:26,110 --> 00:42:32,170
So with this you don't need to do any resizing or scaling on your input because this is going to be

593
00:42:32,170 --> 00:42:36,160
done or this our layers, which are now part of your model.

594
00:42:37,210 --> 00:42:40,930
And then here too, you don't really need to specify this image size.

595
00:42:40,930 --> 00:42:45,940
You could now have this as known and then you have that.

596
00:42:45,940 --> 00:42:52,600
So you could put in any image and then the resizing and rescaling will be done in that model so that

597
00:42:52,600 --> 00:42:56,050
you could put this model or use this model in any environment.

598
00:42:56,050 --> 00:43:00,430
And all you need to do is to pass in the image and you have your corresponding output.

599
00:43:00,460 --> 00:43:02,440
Now we've modified this model.

600
00:43:02,440 --> 00:43:08,620
We need to also modify the way we working with our testing and training and validation datasets.

601
00:43:08,620 --> 00:43:12,040
So let's get back to this right here.

602
00:43:12,040 --> 00:43:19,960
We have the down here, we have this augment, augment, and then we have this test data set.

603
00:43:19,960 --> 00:43:23,290
So with the test data set now, we do not need to do this again.

604
00:43:23,290 --> 00:43:28,150
So we just comment this because we don't need to resize and rescale anymore.

605
00:43:28,150 --> 00:43:35,290
Now with the validation you see, with the validation, we do not need to do this mapping anymore.

606
00:43:35,680 --> 00:43:36,880
You see, we take this off.

607
00:43:36,880 --> 00:43:38,800
We don't need to do this resizing and rescaling.

608
00:43:38,800 --> 00:43:42,040
And then with the training we'll get back to the training.

609
00:43:42,520 --> 00:43:44,740
Um, the training.

610
00:43:44,830 --> 00:43:48,820
We do not need to do this augment because we've included this already.

611
00:43:48,820 --> 00:43:53,560
So all we need to do now is just to shuffle, put this in batches and do the profession.

612
00:43:53,560 --> 00:43:54,490
So that's it.

613
00:43:54,490 --> 00:44:00,490
So we have this, okay, we can now rerun this and see how the training goes.

614
00:44:01,600 --> 00:44:02,980
We run this training.

615
00:44:03,490 --> 00:44:06,970
As you can see, we have this very long trace.

616
00:44:06,970 --> 00:44:11,560
And with all this with this message right here, which isn't very readable.

617
00:44:11,560 --> 00:44:19,060
So what we could do is generally when you're trying to get or understand an error and you don't seem

618
00:44:19,060 --> 00:44:23,620
to have an idea of what's going on, you could run the model eagerly.

619
00:44:23,620 --> 00:44:27,520
So we use the eager mode, so run eagerly equal True.

620
00:44:27,520 --> 00:44:34,600
And here you see when we run this again, you would have a shorter error message or more readable error

621
00:44:34,600 --> 00:44:35,200
message.

622
00:44:35,200 --> 00:44:38,590
So you check this out and you see you just have the shorter error message.

623
00:44:38,590 --> 00:44:45,550
It should be noted that TensorFlow operates in two main modes that is the graph mode and the eager mode.

624
00:44:45,550 --> 00:44:51,070
So as you could see here, when you don't specify this, you run the training in the graph mode.

625
00:44:51,070 --> 00:44:57,280
The graph mode is actually faster and more efficient way of training our model, whereas the eager mode

626
00:44:57,280 --> 00:45:00,730
is slower but more easily debuggable.

627
00:45:00,730 --> 00:45:06,490
So when you have errors like this, you advise to run in this eager mode and when everything is okay,

628
00:45:06,490 --> 00:45:09,940
you could take this off or set this simply to false.

629
00:45:09,940 --> 00:45:13,090
But by default, this is not is not true.

630
00:45:13,090 --> 00:45:16,780
So by default, we are not in eager mode while doing this training.

631
00:45:17,260 --> 00:45:21,310
Now that said, let's scroll down and check on our error.

632
00:45:21,930 --> 00:45:26,280
And what it reads is we cannot batch tensors with different shapes and components.

633
00:45:26,280 --> 00:45:31,260
Zero First element had the shape and the element one had this other shape.

634
00:45:31,260 --> 00:45:38,910
So we try to we understand from this that since we doing the rescaling in the model that is in here,

635
00:45:38,940 --> 00:45:44,670
we actually doing the rescaling, the resizing and rescaling in here we have this batching.

636
00:45:44,670 --> 00:45:45,990
That's what we had here.

637
00:45:45,990 --> 00:45:52,230
We had doing this batching on elements which do not have exactly the same shape.

638
00:45:52,560 --> 00:45:56,940
And so to avoid this, we will just fix this batch size to be equal one.

639
00:45:56,940 --> 00:46:01,680
So we will work with batches of one and work with batches of one.

640
00:46:01,680 --> 00:46:05,700
We run that and we run our model again.

641
00:46:05,820 --> 00:46:08,910
We run this and that's fine.

642
00:46:08,910 --> 00:46:13,080
So you notice that the training starts and everything now works well.

643
00:46:13,080 --> 00:46:18,450
Those are going to be slower since we're no more working with batches of 32, but now our batches of

644
00:46:18,450 --> 00:46:18,960
one.

645
00:46:19,080 --> 00:46:22,150
And also to ensure that this goes even faster.

646
00:46:22,150 --> 00:46:23,800
Let's take this off.

647
00:46:23,800 --> 00:46:29,140
So let's pause this and then let's restart training with the graph mode.

648
00:46:29,140 --> 00:46:31,390
So we run this and run this.

649
00:46:31,390 --> 00:46:34,900
And this should now be faster than what we had previously.

650
00:46:34,900 --> 00:46:36,490
Now training is going on.

651
00:46:36,490 --> 00:46:45,220
We could pause with the training and see how it's going to be very easy now with this model to run inference,

652
00:46:45,220 --> 00:46:46,090
to test this.

653
00:46:46,120 --> 00:46:51,430
We've had this cell here, the cell image which will run inference on.

654
00:46:51,430 --> 00:46:57,130
So we have the cell image and then we'll make use of OpenCV to read this image.

655
00:46:57,130 --> 00:47:06,490
So here we have our image equals Cv2 Imread M read this image.

656
00:47:06,490 --> 00:47:12,730
So and yeah, you could start by printing out image.

657
00:47:12,730 --> 00:47:14,320
So you would have that.

658
00:47:15,350 --> 00:47:16,300
Not defined.

659
00:47:16,310 --> 00:47:18,080
Let's import cv2.

660
00:47:18,960 --> 00:47:19,040
Um.

661
00:47:19,130 --> 00:47:20,000
Right here.

662
00:47:20,060 --> 00:47:22,460
Okay, so we go ahead and import cv2.

663
00:47:22,490 --> 00:47:26,240
We can close this and we could import that just here.

664
00:47:26,240 --> 00:47:27,890
So we import Cv2.

665
00:47:27,920 --> 00:47:28,910
That's OpenCV.

666
00:47:29,000 --> 00:47:33,740
We run that and then we now rerun the cell.

667
00:47:33,770 --> 00:47:36,890
OpenCV actually stands for open computer Vision.

668
00:47:36,890 --> 00:47:46,430
So this is a very popular image processing library, which normally you cannot do image processing or

669
00:47:46,430 --> 00:47:50,690
open or computer vision today without really working with this.

670
00:47:50,690 --> 00:47:54,620
So that's how with OpenCV you could read an image.

671
00:47:54,620 --> 00:47:57,560
So image read and then here we go.

672
00:47:57,560 --> 00:48:02,660
We have the let's print out the shape so you see that it matches up with what we expect.

673
00:48:03,050 --> 00:48:04,010
We have that.

674
00:48:04,010 --> 00:48:06,470
Okay, so here is the shape of our image.

675
00:48:06,470 --> 00:48:10,010
And now we are ready to let's add a batch.

676
00:48:10,010 --> 00:48:18,390
So here we're going to have image equal TF dot expand, add dreams, and then we have the image and

677
00:48:18,390 --> 00:48:21,660
then we specify the zeroth axis.

678
00:48:21,660 --> 00:48:25,860
So we want to add the batch dimension and then we print out this image.

679
00:48:25,860 --> 00:48:27,900
So here we have our image.

680
00:48:29,140 --> 00:48:30,070
Okay, that's fine.

681
00:48:30,070 --> 00:48:31,420
Let's print out the shape.

682
00:48:31,420 --> 00:48:32,530
So we should.

683
00:48:32,560 --> 00:48:33,730
We should see that.

684
00:48:34,390 --> 00:48:36,880
Okay, We have this here.

685
00:48:36,880 --> 00:48:40,990
And now you see that we're going to pass this into our model directly.

686
00:48:40,990 --> 00:48:50,980
So here we're just going to do, uh, net model dot predict image, and that's it.

687
00:48:50,980 --> 00:48:53,080
So all you need to do is just do this.

688
00:48:53,110 --> 00:48:58,640
We don't need to divide by 255, we don't need to resize and the model is going to understand.

689
00:48:58,660 --> 00:49:07,570
Now note that we are passing this image or this input, which is 265 by two, 62 by three, and our

690
00:49:07,570 --> 00:49:13,450
model is going to automatically resize this because we've included resizing in the model.

691
00:49:13,450 --> 00:49:16,870
So let's run this now and see what we get and that's fine.

692
00:49:16,870 --> 00:49:23,470
So it tells us that zero, that is, it's parasitic, but which is actually wrong.

693
00:49:23,470 --> 00:49:29,330
But this is normal because we have not really trained the model for a long period of time.

694
00:49:29,330 --> 00:49:35,030
And clearly from here, our results show that we should expect very poor results.

695
00:49:35,060 --> 00:49:36,380
Why is this disturbing?

696
00:49:36,380 --> 00:49:37,430
Let's scroll down.

697
00:49:37,430 --> 00:49:37,940
Okay.

698
00:49:37,940 --> 00:49:41,150
So let's scroll this way and then scroll back up.

699
00:49:41,150 --> 00:49:46,100
Okay, So you see that here we have this poor accuracy 48%.

700
00:49:46,160 --> 00:49:50,330
If we train for longer, we could get to the 99%, which we had already.

701
00:49:50,330 --> 00:50:00,140
So this is actually done to show how easy it's going to be for you to run inference on this model anytime

702
00:50:00,140 --> 00:50:00,950
you want to.

703
00:50:01,340 --> 00:50:05,720
In every in in any environment you find yourself.

704
00:50:06,510 --> 00:50:15,060
So far we've seen two methods of creating, of implementing data augmentation with TensorFlow within

705
00:50:15,060 --> 00:50:15,990
the TF image.

706
00:50:15,990 --> 00:50:17,640
And then we've seen the tf.keras.

707
00:50:17,670 --> 00:50:25,590
Now with the Tf.keras is actually interesting when you want your pre-processing that is, say resize

708
00:50:25,590 --> 00:50:32,370
and rescaling and data augmentation to be in the form of layers and the TF image is more flexible to

709
00:50:32,370 --> 00:50:33,180
work with.

710
00:50:33,300 --> 00:50:43,800
Now we are going to look at a way in which we could build a custom keras layer based on the TF image

711
00:50:43,800 --> 00:50:44,790
operations.

712
00:50:44,790 --> 00:50:53,610
So recall that when we're working with a TF image, we had this route 90 degrees, which was right here,

713
00:50:53,880 --> 00:51:01,140
and when we wanted to work with those layers, we had to play around with this to give us that same

714
00:51:01,140 --> 00:51:03,240
route, 90 degrees effect.

715
00:51:03,450 --> 00:51:11,200
Now what we will do is in order to create a layer which takes to create this route 90 degrees keras

716
00:51:11,200 --> 00:51:16,390
layer and not just use this random rotation because it's not exactly this.

717
00:51:16,420 --> 00:51:21,840
We are going to define a class which inherits from Keras layers.

718
00:51:21,850 --> 00:51:24,640
So right here we have this class.

719
00:51:25,830 --> 00:51:29,340
Which will call Route 90.

720
00:51:29,460 --> 00:51:31,110
Let's call it Route 90.

721
00:51:31,140 --> 00:51:36,450
And then it inherits from layer we have in it.

722
00:51:37,770 --> 00:51:39,090
There we go.

723
00:51:39,690 --> 00:51:40,920
Self.

724
00:51:42,360 --> 00:51:43,500
Super.

725
00:51:44,700 --> 00:51:45,280
There we go.

726
00:51:45,300 --> 00:51:46,380
Dot init.

727
00:51:47,730 --> 00:51:48,810
And that's fine.

728
00:51:48,810 --> 00:51:56,160
So yeah, we have this route 90 which inherits from layer and then we'll define the call method.

729
00:51:56,160 --> 00:52:02,760
So we'll define the operation which is going to be carried out in this route 90 layer which we're about

730
00:52:02,760 --> 00:52:03,450
to create.

731
00:52:03,480 --> 00:52:07,140
Yeah, we have self and then we have the input image.

732
00:52:07,140 --> 00:52:13,830
So what we simply want to do here is we want to have well, we want to return the TF dot image, the

733
00:52:13,830 --> 00:52:15,000
Route 90.

734
00:52:15,000 --> 00:52:17,010
So we're carrying out the same operation.

735
00:52:17,010 --> 00:52:19,560
But now we converted this into a layer.

736
00:52:19,560 --> 00:52:22,350
So here we take that and then we have image.

737
00:52:22,350 --> 00:52:23,460
So that's what we output.

738
00:52:23,680 --> 00:52:26,050
Now that sounds fine.

739
00:52:26,050 --> 00:52:29,110
So here we've had this Route 90, which we've created.

740
00:52:29,110 --> 00:52:36,480
We run this and then instead of just random rotation, we are going to use this route 90.

741
00:52:36,490 --> 00:52:38,380
So here we have Route 90.

742
00:52:38,740 --> 00:52:43,300
Take this off, not in its past and that's it.

743
00:52:43,480 --> 00:52:44,380
So that's it.

744
00:52:44,410 --> 00:52:47,980
We have our Route 90 now and we are not long.

745
00:52:47,980 --> 00:52:53,620
We are no longer using the the random rotation layer.

746
00:52:53,620 --> 00:52:54,490
So that's it.

747
00:52:54,520 --> 00:52:59,800
We get back and then run the cells and get to training.

748
00:53:00,100 --> 00:53:03,220
And as you can see, everything just works fine.

749
00:53:03,220 --> 00:53:06,640
And we'll see how to get the best of both worlds.

750
00:53:06,970 --> 00:53:16,720
And now we have the flexibility of TF image and the speed and portability of Keras layers.

751
00:53:16,720 --> 00:53:21,130
Before we move ahead, there is this point which needs to be clarified.

752
00:53:21,220 --> 00:53:28,600
So we've already seen that when we have a single sample like this one with data augmentation, we're

753
00:53:28,600 --> 00:53:30,700
able to produce different samples.

754
00:53:30,700 --> 00:53:34,870
In this case, we have the rotated version of this.

755
00:53:34,900 --> 00:53:40,270
In this case, we have another version in which we've modified the saturation.

756
00:53:40,270 --> 00:53:45,520
And this we have this version where we've flipped this horizontally.

757
00:53:46,060 --> 00:53:53,380
That said, in practice we are not going to feed or we're not going to create, let's say this three

758
00:53:53,380 --> 00:53:55,090
other extra samples.

759
00:53:55,090 --> 00:54:01,960
What we do is if we suppose we want to implement three augmentation strategies, that is rotation,

760
00:54:01,960 --> 00:54:08,860
saturation and flipping, for example, then for one epoch, let's have this here.

761
00:54:08,860 --> 00:54:15,370
For one epoch we may decide that we do not want to carry out rotation and then from here we may decide

762
00:54:15,370 --> 00:54:17,350
to adjust its saturation.

763
00:54:17,350 --> 00:54:25,000
So let's have from this we have its saturation adjusted and then in the last step we will decide that

764
00:54:25,000 --> 00:54:26,950
we do not want to flip.

765
00:54:26,950 --> 00:54:35,620
So at the end we have this input and then we have this as our output.

766
00:54:35,650 --> 00:54:38,620
Now in another epoch you will have this.

767
00:54:38,650 --> 00:54:41,920
Instead, you may decide that you want to rotate.

768
00:54:41,920 --> 00:54:45,040
So you may decide that you want to have something like this.

769
00:54:45,040 --> 00:54:49,120
Let's rotate this by 90 degrees again here.

770
00:54:49,120 --> 00:54:49,990
This is random.

771
00:54:49,990 --> 00:54:52,420
So we randomly rotating this.

772
00:54:52,900 --> 00:54:56,800
We decide at random whether we want to rotate this at 90 degrees or not.

773
00:54:56,800 --> 00:55:02,860
And then once we have this, we may decide that we do not want to adjust the saturation.

774
00:55:02,860 --> 00:55:06,040
And then finally here we have flipping.

775
00:55:06,040 --> 00:55:08,170
So let's flip this.

776
00:55:09,010 --> 00:55:10,510
Um, there we go.

777
00:55:10,510 --> 00:55:12,670
We flip this and we have this as output.

778
00:55:12,670 --> 00:55:16,600
So in this case, this is what we pass in the model.

779
00:55:16,600 --> 00:55:21,370
In another case, we may decide that we still want to rotate.

780
00:55:21,370 --> 00:55:23,950
So we may decide, I want to have this rotation.

781
00:55:24,400 --> 00:55:26,260
Um, then, um.

782
00:55:26,320 --> 00:55:36,010
Still no saturation, but here we may want to maintain this, so we may still have this.

783
00:55:36,040 --> 00:55:37,720
You see, now we do not flip this.

784
00:55:37,720 --> 00:55:42,100
And this is instead what this example or this model sees.

785
00:55:42,220 --> 00:55:45,880
Now, in another case, we may, we may have the input itself.

786
00:55:45,880 --> 00:55:47,260
That's this input.

787
00:55:47,650 --> 00:55:55,300
Um, it gets passed this no rotation gets passed this, no saturation gets passed this, no flipping.

788
00:55:55,300 --> 00:55:57,040
And this is what the model sees.

789
00:55:57,040 --> 00:56:08,380
So we can see that the model has now seen four different examples of this same input without us necessarily

790
00:56:08,380 --> 00:56:15,010
having to create this four different examples separately before starting to train.

791
00:56:15,010 --> 00:56:19,540
And so this helps us carry out the data augmentation much more efficiently.

792
00:56:19,540 --> 00:56:23,500
That said, when we get back to the code, you see that that's what we actually did.

793
00:56:23,740 --> 00:56:25,210
Um, yeah, we decided to.

794
00:56:25,270 --> 00:56:27,610
Or we may rotate or not.

795
00:56:27,640 --> 00:56:29,680
We may flip this or not.

796
00:56:29,680 --> 00:56:32,170
And if we are to flip, it's going to be horizontally.

797
00:56:32,170 --> 00:56:35,650
And then here we may modify the contrast or not.

798
00:56:35,650 --> 00:56:40,500
So these are all augmentations which are randomly carried out during training.

799
00:56:40,510 --> 00:56:47,230
Now for the other alternative, which is the TensorFlow image, we need to make sure that this rotation

800
00:56:47,230 --> 00:56:53,410
here is carried out in a way that sometimes we have the image rotated and some other times the image

801
00:56:53,410 --> 00:56:54,460
remains intact.

802
00:56:54,490 --> 00:56:56,320
Now get into documentation.

803
00:56:56,320 --> 00:57:01,360
You'll see here we have this Route 90, which takes in the image.

804
00:57:01,360 --> 00:57:03,120
And this K right here.

805
00:57:03,130 --> 00:57:09,580
Now this K is simply a scalar integer tensor and the number and it's actually the number of times the

806
00:57:09,580 --> 00:57:13,390
images, the image or the images are rotated by 90 degrees.

807
00:57:13,390 --> 00:57:19,510
And so here we're going to define this K, which is a random number which could either be a zero or

808
00:57:19,510 --> 00:57:20,320
a one.

809
00:57:20,320 --> 00:57:23,110
And that's it for this rotation.

810
00:57:23,110 --> 00:57:29,720
Now, getting back to documentation, you could check the same folder, adjust saturation.

811
00:57:30,440 --> 00:57:33,920
Yes, actually stateless random saturation.

812
00:57:33,920 --> 00:57:36,260
So you click on this as usual.

813
00:57:36,260 --> 00:57:45,020
We pass in the image and then we have this lower and upper bounds, which are those of the random saturation

814
00:57:45,020 --> 00:57:45,560
factor.

815
00:57:45,560 --> 00:57:48,680
So let's take this here.

816
00:57:48,860 --> 00:57:53,480
Basically this, we copy this out here.

817
00:57:53,480 --> 00:57:55,610
So instead of this, we would have this.

818
00:57:55,640 --> 00:57:57,710
Well, let's just let's just put this out here.

819
00:57:58,250 --> 00:58:02,870
Um, we have image which is equal this.

820
00:58:02,900 --> 00:58:11,420
Okay, so we're taking the image and then we could have, um, let's say 0.3 and then 0.5.

821
00:58:11,420 --> 00:58:12,710
Okay, so that's it.

822
00:58:12,740 --> 00:58:14,750
We are going to have this.

823
00:58:14,750 --> 00:58:19,070
And then now for the flipping left, right, we get back again here.

824
00:58:19,100 --> 00:58:21,860
Here you have random flip left, right.

825
00:58:21,860 --> 00:58:24,860
And then we copy this out, see?

826
00:58:24,860 --> 00:58:28,790
So we're going to randomly decide whether we're going to flip the image or not.

827
00:58:28,790 --> 00:58:30,620
Let's paste this out here.

828
00:58:31,220 --> 00:58:33,440
Um, instead of this, we have this now.

829
00:58:33,440 --> 00:58:34,490
There we go.

830
00:58:34,940 --> 00:58:36,410
Take this off.

831
00:58:36,410 --> 00:58:38,420
And that's it.

832
00:58:38,420 --> 00:58:48,530
So now we have this augmented method which randomly selects which augmentation strategies we are going

833
00:58:48,530 --> 00:58:51,500
to use for a given instance.

834
00:58:51,500 --> 00:58:55,580
And this permits us to carry out augmentation much more efficiently.

835
00:58:55,580 --> 00:58:57,560
Now, let's do the same here.

836
00:58:57,590 --> 00:59:02,360
Let's copy this and paste out right here.

837
00:59:02,360 --> 00:59:04,190
So here we have this.
