Hello, everyone, and welcome to this new session in which we are going to treat data augmentation.
Previously, we had seen how to load our dataset from this dataset directory right here, and then we
trained a learned model which performs very well on the training data but didn't perform as well on
the validation data.
And then we're able to evaluate this model on different evaluation metrics like the accuracy, top key
accuracy and the confession metrics.
In this session, we are going to focus on data augmentation.
That is, we're going to see the effect of augmenting our data artificially without actually getting
to add an element in this dataset right here and then seeing how this affects our model performance
in the session.
We'll see how to augment our data like those data right here and see how this technique of data augmentation
helps in making the model even more performance.
Now, we have looked at data augmentation previously, but if there's one thing you have to note about
that, augmentation is simply the fact that it promotes diversity in your dataset.
And so if you have data like this one year, let's open this.
We consider this original data.
So this our original data.
This is the data we actually gather and then we have this brightness here.
So we modify this data is all this image is brightness, and we obtain this other data point and then
we modify from this, we rotate this and we obtain this other data point.
You see that it's exact same image which has been modified.
And so now the model doesn't only get used to seeing this image right here, but now you could see this
one, this one.
And so that augmentation is this method or this technique for promoting robustness in models, hence
fighting overfitting as now the model can see different versions of certain data points.
So let's close this up and get back to the code here.
Let's get back up here.
We have the augmentation.
We're going to simply get back to this year.
We had that augmentation.
We looked at this already in the previous section so you could get back and try to understand exactly
how this is carried out.
Previously, we had seen that we could carry out the augmentation by using these kinds of TensorFlow
image methods like this one.
You see, you could rotate the image, you could flip left, right, you could adjust the saturation,
or you could use cross layers.
You are going to use cross layers.
And then also we saw how to use our argumentation library, which is this amazing library which permits
us carry out their augmentation on different tasks, not only classification very easily.
So you could check on that video.
Now, that said, let's simply copy this out here, then we paste it here.
So we have this.
We're going to we have this augmented layers.
We have random rotation, random flip, random contrast.
Let's actually get back here and this layers and then add the random contrast.
So we have random contrast.
Okay, So we have that and you can feel free to get back to the documentation on the TensorFlow Keras
layers.
Let's get to cross layers and then check out this different augmentation strategies.
Here you can see random brightness contrast, crop flip height, rotation, translation, random width,
random zoom, and you could check out your documentations in case you have any doubts.
So you could just have this here and you see how to use each and every one of these augmentation strategies.
Now, getting back to the code, we have this three year.
Now what was generally done is you could actually run one and then test this all test how it helps in
making the model perform better.
And then you could add the other and see whether it helps and so on and so forth.
So it's kind of like it's not a fixed kind of method where you just have some fixed methods or some
fixed augmentation strategies, which we just place in this other sequentially.
And then it will always work magically.
Generally, you will have to test this out, different tests, different strategies out and then see
which one works well for the data you're working with.
Now we have this set.
Let's run this cell right here.
We have this is not defined.
Let's let's get back here and be sure that this we run this cell.
Let's run the cell.
Normally, that should be fine.
Get back and we run this one here and that's fine.
So we have that.
And then now at the level of this dataset preparation, we're going to include this, We're going to
do the mapping.
So we have we have our augment layers, not that we're not going to do this for the validation dataset,
we're going to only do this for the training data set.
We have this augmented layers and then we're going to specify the number of parallel calls.
So we have non parallel calls and then this has gotten via the auto tune.
So we have that automatically.
Then before we move on, we're going to copy this out here and then simply paste this here.
So we create this augmented layer which takes this and then has it outputs the augmented image and the
level.
We're not going to have this here.
So let's just take this off.
We have just this image.
So that's it.
We have this augmented layers and then we define this function as augmented layer.
So right here, let's take this one off and run this again.
With this, we're now set to train.
So let's go ahead and retrain.
Our model train is now complete and we could see that the model doesn't perform as well.
As used to do without augmentation.
So here we did augmentation.
The model performs even poorer as compared to when there was no augmentation.
Let's run this here.
So you could see I could compare this with the previous results we got or the previous evaluation.
You see, we go from 75% to 54%, and then you will go from 90% to 83%.
To understand the reason why we have this drop in performance, let's look at this visualization of
our data set.
We'll see that after carrying out the rotation, we have images which are rotated at very unusual angles.
So you see like this image here is unusual.
This angle to looks very unusual as compared to the kind of data we would have in our test or validation
set.
So we have to ensure that when carrying out this random operation, we limit the angle at which we could
carry out this rotation.
So that said, if we have a face like this or let's say we have an input image like this, we should
limit this rotation such that this image cannot be rotated at, say, 180 degrees.
So let's have this here.
So you see that better.
We have this, this and this.
So we do not want these kinds of rotations.
What we want is rotations where we have the face a bit tilted like this and that's it.
So we want this kind of rotations, but not this type.
So to solve this problem, what we're going to do is we're going to get back to the documentation that's
random rotation.
We check out this factor right here.
We see that the value we put your.
Takes us from -20% of two pi pyres 180 degrees pi and radians convert this two degrees we have pi which
is 180 degrees.
So we have two times 180 which is 360.
So when you say 0.2 or -0.2, what you're in fact saying is -20% of 360.
So when you have let's get back to the code.
So when you have your let's add this cell, you're when you have your 0.25, what you have in is 0.25
times 360 running this, you see you go in 90 degrees.
So this means that if you had an image which was already somehow tilted, this image, we end up in
a very unusual position where the face will look something like this.
And so what we'll do here is we are going to limit this rotation.
So we're going to we're going to go from 0.025, for example, to this.
So let's limit that and we'll go from -0.025, meaning that we have your 0.025, that will be nine degrees.
So limiting this to nine degrees and then going from -0.025 to 0.025 simply means that if you have this
axis here and then you have the face like this, let's put the mouth in this.
Then after rotation you can only go 90 degrees this direction or 90 degrees in this, or rather nine
degrees in this direction.
So you have a limit.
So you can only pick a random value between negative nine degrees and nine degrees in this other direction.
So you can only go this way 90 degrees or this way, 90 degrees.
So that said, the extreme will have a face like this.
That's after rotation.
So we'll go from this blue to this red.
Or you could also get something like this.
So this is what we are going to get after rotation, unlike with the case of 90 degrees, where if you
have a face, which is already let's let's change this, let's delete this.
If we have a face which is already, say, tilted like this, we have a face tilted like this after
rotating 90 degrees.
What you have now let's let's have your this 90 degrees, what you have now will be a face tilted this
way.
And this is in a very usual position when taking an image.
So when taking a photo.
So the image is not validation or test.
This set wouldn't look like this.
And so that's why we actually limited this year.
So let's get back to our code now and we run this let's run this training data and then let's visualize
our data set.
There we go.
As you can see, you do not have images which are upside down as we had before.
And that's it.
So we have this now.
We now go ahead and retrain our model and see what we get.
After training for over 20 epochs, your results, we get you see that we go up to 78%.
So this is the highest we get.
And when we run the evaluation, you see here we have this.
When we run the evaluation, what we get is 77.8% for the accuracy and the top case 91%.
So we've improved compared to what we had before or what we had before the data augmentation.
Now we are going to use another data augmentation strategy, which is the cut mix.
Now, the cut mix isn't like this other than augmentation strategies, where we just modify a single
image with a cut mix, as we have seen in the previous sections, as we actually combine two images.
So what we're going to do here is we're going to simply copy out this code and then put it out in this
code base right here.
Now, if you haven't or if you're new to the cut, mix the documentation.
You could check out the previous sections where we treat this in detail.
So let's get your and there we go.
We're going to apply Cut, mix and see the effect it has after training our model.
So we have that augmentation in here.
Let's let's add the cell and have your cut mix, augmentation, cut cell, and let's paste all that
part of the code.
We also paste out this part where we have the training data set one and our training data set to then
we create this mixed dataset.
So from here we have augment augments layer.
Let's take this off and here we have AUGMENT layer.
So we carry out augmentation for this two separately.
And then once we have this two year, we're going to shuffle the shuffling already.
So we just do the mapping, we do the mapping, and that's what we're saying.
Once we have this two year, we're going to combine this into this one mixed data set.
So that's it, that's have that.
And then from year now, we build our training data set.
So let's take this year, let's take this up.
There we go.
This is a preparation and then we now going to command this part so we could command this one.
And we have a validation data set.
That's fine.
Everything looks fine and that's okay.
So we have this set.
Let's run this now for our cut mix.
You could always try to mix up all the cut out augmentation strategies and see how it better or how
it ameliorates your model performance.
So let's have this.
Let's run this.
There we go.
There's our cut mix here, and we combine the two data sets and then once the combined, we now apply
the cut mix augmentation.
Here we have this size not defined.
Let's actually we should have okay, this should be our configuration.
So we should have this configuration.
We run this cell now run that again.
We want to cut mix and we run the cells and now everything should be fine.
So there we go.
We have all this fine, and then we get to validation, run that training data set and validation data
sets.
So that's it.
Let's go ahead and retrain our model and see what we get after training for 20 epochs, we notice that
unlike previously where the training was went to about 99%, while the validation was about 77%.
Here the training is about 80%.
And the validation let's curl this way.
The validation is about all the highest we have here is 78%.
So this shows us clearly that the model isn't overfitting because the training and the validation data
set both are evolving in a similar manner.
So let's scroll down here and look at this.
Curves you see here before, look at the accuracy plot.
Before what we had was something like this.
We had the training and yeah, we had one.
And then the validation was like this.
But now we have these two curves which are evolving in a similar manner, though sometimes we have those
kinds of peaks anyway.
Clearly, our model isn't overfitting, so what we'll do is we're going to train for more epochs.
So that set, let's go ahead and retrain this model for more epochs.
So we're going to modify the number of epochs.
So yeah, we just have to or what we can do simply is we just keep training.
So we train for 20 more epochs.
So let's run this and then we'll wait for 20 more epochs.
Train now complete the other results we get.
As you could see, the validation accuracy starts to stagnate around this.
And then after evaluation we obtain an accuracy of 79.71%.
But what's interesting to note here is the fact that our training accuracy is still having this value
of 86.25%.
Unlike previously, when where our model was overfitting, we had this accuracy of about 99%.
So that's it.
We have that we've evaluated.
We see these values and then test on this values.
Here we see that we have 14 out of 16 images predicted correctly.
And here's our conversion metrics right here.
See that clearly this model performs best or performs better than all the other previous models.