Hello, everyone, and welcome to this new and exciting session.
In this session, we shall be building a system which permits us automatically detect whether an input,
which in this case is an image, is that of an angry, happy or sad person.
So this person may be sad.
As you could see here.
The person may be angry.
I could see here all the person may be happy.
So, in fact, we want to be able to have this kind of input, pass it into our system, which we want
to build, and then automatically infer that this is a happy person.
And so to train the system will be making use of this data set, which has been made available to us
by Mohammad Hanan Asghar.
And since this dataset is available on Kaggle, just anyone could have access to it.
We are now going to take a closer look at this data set.
As you could see here, we have this test and train directories.
You open up the train, open up the test.
You see you have this three different folders angry, happy, sad here for the train.
You have Angry, happy, sad too.
So what we do is generally when trying to create this kind of dataset for classification problems,
what you want to do is make sure that you put each or you put the different images in separate directories.
So all images represent an angry people are in this directory, happy in this directory and sad in this
directory.
Now this way it becomes easy for us to build our model, taking this dataset as input.
Also, on the previous sessions, we explained the reason why it's very important for us to split our
data set in this kind of manner such that while you use part of the data set that the training to actually
train or build your model, you're going to use the auto part to evaluate this model.
So although in this case we've just been given this data, which is very unlike what you have in real
world scenarios, where you will be asked or tasked to build your own dataset like this one thing or
one very important thing you have to note is the fact that if, for example, your building is kind
of data set where say, we want to monitor the usage of an app or better still monitor how the users
of an app react to a certain feature, then it's important to gather data that reflects or is very similar
to what the data are, rather what the model will be seen during inference.
And so this means that if you have this kind of data or if your model is going to be seeing this kind
of data during the inference or when it's going to make use of the model to predict whether there's
a happy image or not, then training on this kind of data isn't a good idea, although this is images
or these are images of happy people.
So you have to ensure that the data you train on is representative of the data or the kinds of inputs.
The model will be seen when it's going to be deployed and left to predict or make predictions from these
kinds of images.
Then, if you have noticed, not all classes we have here have the same number of files.
So here you have a happy a thousand angry 500 sad five 757 for the train is 1525 year 3000.
So sad to to five five.
So this shows us that when solving your real world problems it may happen that some class or it's easier
to gather data from a particular class as compared to the other.
So your maybe the other of this dataset found it easier to gather happy images as compared to angry
images.
Another very important point to note here is the fact that the kind of problem we're trying to solve
is that of multiclass classification.
So unlike previously where we had a model, we had some data which went past in, we would say whether
this is a parasitic or non parasitic input cell, whereas here what our model outputs isn't just either
one class or the other.
A model which outputs a given class out of several options.
Now, those options could be three, four, five, six, seven or even 1000 different possibilities.
Now all the different possibilities are termed classes.
So in this case we have three classes.
The first class is that of the angry person, the next class, happy person, the other class, a third
person.
Now, to be able to download this data, the very first thing we want to do is create this new API token
right here.
So you get to your profile, you click on account and you will have this year, click here and save
this Kaggle, that JSON file.
So we'll get that saved.
Then getting back to the lab notebook, we simply copy this in your okay.
And then we should have this here.
Now, before moving on, it's important to note that you have to sign up for a Kaggle account, and
then the next step will be to install this Kaggle package.
So Pip, install Kaggle, run that sell.
Then we create this directory.
So we make this directory and then we copy this JSON file in this directory.
So again, we run the cell.
Then from here we give the user full rights to read and write into this cargo file right here.
We run the cell, then get back to Kaggle where we are going to copy this API command.
Now, after copying this API command, we're going to paste this just here.
So here we have Kaggle data sets download.
So we're going to download this data, set this human emotions data set.
Let's run the cell.
Our dataset has been downloaded.
Now we're now left to unzip this downloaded file.
Let's click this here.
You see, we have human emotions data set that zip.
We now unzip this and store in this dataset folder.
So you're going to run the cell.
You'll see that it's going to create this folder, this set.
And in this data set you have the emotions data set, which we saw in Kaggle.
Basically, this is the data set right here.
From this point, we'll go on to generate a tensorflow data set based off the images which are found
in the directories.
So we're going to make use of this image data set from directory method, which is in the Keras utils
package right here.
So here we have this description.
So here we have this different arguments directory.
Now the directory let's, let's copy this.
So we see clearly this in the code.
We copy and paste this out here.
This directory is going to be simply one of the directories here.
So your first thing we'll do is we'll create our training data set.
So that's our trained data set, which is a tensorflow data set.
We've seen this already.
We're going to specify the directory.
So let's call this train directory.
And we'll simply copy this path.
So let's copy this path and then paste it out your copy that path, paste it.
Now we have our validation validation directory.
We're going to do the same thing.
So basically, again, we're going to copy this here, copy and paste.
Now, here we're going to use this as our training data set and we're going to use this as our validation
data set.
So here's our train user validation and that's it.
So with this, let's run this cell.
That's it.
We get back here, we change this to train directory.
Now, here the levels are inferred, and this means that the labels will be generated from the directory
structure.
So if you could look at this here, this example lets you have this main directory and you have the
directory structure.
That's why at the beginning we made mention of the fact that it's important to maintain this kind of
directory structure when dealing with classification problems.
So because we have this kind of directory structure where we have a class, we have images, we have
a class, and then it's images like we could see here we have this in the train, we have the class,
we have the images, we have this class and this images, this class and these images.
We are able to automatically create this kind of data set.
And that's the role of this inferred right here.
Then the next one we have is a label mode.
Now, for the label mode, we have several modes here, the default, we have the INT, we have categorical,
we have binary or none.
Now let's explain what these different modes mean for the integer mode.
We simply meaning that when we design all the way we design our data set is such that we have an image
and we have a label.
So let's say we have this image of this person.
The person is happy and we have three different possibilities.
Either the person is angry, happy or sad.
So here what we're going to have is angry, angry.
We're going to give it a value of zero happy, a value of one side, a value of two.
So here are three different possibilities.
And so when designing this data set or when creating this data set, we'll create a sorry that we have
the image and this integer which reflects what that the emotion in that image.
So here in our case, in the case of happy would simply have an output of one.
Now, that said, either we create our dataset in this way or we use a one hard representation of this
labels.
So what this means is instead of having zero, we'll create a vector of size three, which has three
different positions where if we have an angry input, So if the person is angry, we'll put a one in
this position and zero zero here, If the person is happy, we'll put a zero.
You're a one and a zero.
And if the person is sad, we'll put a zero.
You're a zero and a one.
And the way this encoding works is that where we have zero, you see the position is one for the one
representation where we have a one happy person, we put a one under first position.
Well, we have a person who put a tool, We have a tool, and so we put a one at the second position.
And then with this, there'll be some differences in the designing of the loss function, which we are
going to see in subsequent sessions.
Now we have this binary for the binary is like the previous example of the previous project we worked
in where we have two classes.
So where we have two classes, we just have the level mode to be binary.
In our case, we may pick out the integer or the categorical.
Then from here we have the class names.
Now, if you decided to infer directly from the class directory, then it's important to make sure that
this class names match the subdirectories.
So you should ensure that if you're, for example, you have this class names.
Let's put out the class names here.
So you will have class names.
And let's say we want to have angry.
We have angry.
We have happy.
We have sad.
So yours are class names.
Now, if we don't put this exactly as it is here, we should have an error.
So let's run this.
We have class names there.
We get back here and instead of having this year, we're going to specify our class names.
So here we have class names.
There we go.
So we have that.
The next is a color mode.
RGV The batch size by default is 32.
The image size here, the default is 256.
By 256.
We could always modify this.
Shuffling by default is true.
So by default we're going to shuffle our data set to we do not need to explicitly do this shuffling,
so we just have to specify this to be true and that that's done now for reproducibility, we could give
a seed such that we always have the same shuffling.
Then we have this validation split.
So you're in a case where let's say we have just one directory, let's say we just have the dataset
directory, we do not have this test.
We may want to split this directory into the training and validation.
So here we could have your, let's say 0.2 and automatically we're going to have a split of this data
into the and validation data sets.
Now, once you have the split, since you're creating the trained data set, you specify that this is
training.
So training is actually in the documentation.
Here you have your validation split.
So an optional float between zero and one, which is logical since we split in our data set.
And then we'll specify either training or validation.
So let's get back here.
So in this case it will be training the interpolation by linear follow links.
False crop two aspect ratio false.
So these are the default values.
We are going to use this, take this off.
And in this case, in the case where we would have had validation, we just have your validation.
So anyway, we're not going to use this.
Let's take this off.
That said, by default, the validation split is known.
So optional float, there's no we're not going to do any validation.
So that said, let's have this off and let's run this and see what our data set are trained data set
is about.
So here we have 6000 799 files belonging to three classes.
Now let's go ahead and modify this.
Let's add that is that let's run this now and see what we get.
You see, already we have this error because there's no match in the class namespace didn't match the
names of the subdirectories of the target directory.
So I expected this but instead received this.
So let's get back.
Modify it here.
There we go.
That's fine.
We run this here and that's fine.
So that's our training data.
We could do the same for our validation data.
So validation.
Let's see Val and your string.
Okay, so your stranger is Val, and we have the Val directory.
We have the Val directory.
Let's ensure that you call this Val.
Val directory.
We get back and that's it.
Here is inferred level mode int class name same class names.
RGV 32 image size 256 Now let's change this and have configuration and batch size.
Okay, so that's our batch size.
Here we have this image size.
So size, take this.
Off base and out.
You have your size.
Okay, so we have this configurations and then we could create this year configuration.
There we go.
We have the batch size, which will take a value of 32.
We have the image size, which would take a value of 256.
So we have that.
Let's run this.
We get back here just configurations.
Let's copy this here.
And that's fine.
We have our 2278 files belonging to three different classes for our validation data set.
From here, we're going to do we're going to look at our data set.
So let's say for the of our image and label in VAL legislative for for for I in Val data set.
Let's take one let's take one batch.
We're going to print out I so we're on that scroll down.
See we have the images we're interested in looking at the labels for now.
So let's go down scroll up to this year.
Okay.
You see the labels we have 22001 is the label is between zero and one because we have three different
classes.
Now, let's modify this.
Instead of having the label more two inch, let's know this is in already, or let's change it to categorical,
categorical and this year categorical.
Run that, run this.
And okay, so we'll see what we get.
So instead of having two here, instead of having two two, what we have is this one hard representation
here where the everywhere is zero except at the second position.
So here, instead of having zero, we have everywhere zero except at the zero position.
So that's it.
Now we shall go on to visualize this there.
So here we have this figure and then we specify the figure size.
So we have your fixed size and we have this say 12 by 12.
Okay, So we have that.
And then four images and labels in our trained data set.
We have this in our train data set.
You could always change this and put, for example, the validation dataset.
So we have that now trained data set.
We take just one, then we create a subplot.
Your PLT subplot is going to be 4x4, then I plus one.
Okay, so we have that and then we'll do an IM show.
So we show we plot out the image.
Basically this image right here and then we select a given image.
So we have that and then we divide all those pixels values by 255.
So we have that.
And then next one next step is to plot out the title.
So now we plot now the images, then we then go ahead to plot out the different titles.
Then here we have the outputs that the labels basically select the label, so we select the particular
label just as we selected the image.
And then we have let's because, because the labels will be a 100 presentation, then we have to take
the max.
Now, if you're new to this, you could check out the previous sections where we talk about these kinds
of methods here.
So we have the max of this and we have an axis.
Let's specify this axis zero.
That's fine.
I think here we should also just do the plotting, so we should take off the axis.
Sorry.
So axis and off.
So let's run this.
So you see what we get before we have that.
Anyway, here we have non pi, so let's convert this to non pi.
Run that again.
You have this output.
There we go.
You see that we have the image and its class above.
Now to convert this into some words, let's make use of the class names.
So we have class names.
Class names and then you're we have this we run this again.
We now get the different images and your labels.
At this point, our dataset is now ready for training.
We just have to include this pre fetching here for a more efficient usage of our data.
We saw this kind of refreshing or we explained what perfection was all about and some previous sessions.
So here we have the prevention.
We're going to include the batch because you're already, we've included a badge size previously.
So in this data set loading right here, we specify the batch size, so our data is already batched.
Now we have that, we run the solve for the training and then we simply redo this for the validation.
So this is our validation, data validation.
And now we're ready for building our model.
But before going on to build our model, we shall copy out this code here from the previous session
and paste it out here.
So we have this resized skill layers, which we're going to include in the model.
Now recall that we could do this resizing like we have our data set here.
So supposing you have your data set, you could do the resizing and reskilling your this reskilling
and this a resize into the required size before passing this data into the model.
So this could be done before passing into the model such that you could train your model on this resized
and reskilled data.
Now another method will be to pass in your data here.
So let's suppose that we have let's take this off.
We suppose that year we have in this middle we have the resizing and rescaling unit, so we resize and
reskill before passing into the model.
Now, instead of doing this, what we could do is we could pass the data into the model directly and
then carry out the resizing and reskilling in the model as a layer in the model.
Now, we've seen this already, but we'll just explain here again that doing this is great for deployment
because when you have to deploy this kind of system, you no longer want to resize it again.
So your all you do now is just pass in this image and then the model on its own.
There's the model which has been deployed takes care of resizing and reskilling.
Unlike in this kind of system where when you deploy this model, you have to do the resizing and reskilling
on your own.
So this means that if you deploy this model on some cloud solution and then you're calling it from a
JavaScript client, for example, or say some Android client, then you would have to carry out this
resizing and rescaling in JavaScript, unlike here, where you just pass in the image and the model
takes charge of resizing and rescaling.
So that said, let's take this off, get back to our code here.
We're going to have we're going to run this simply and then we start with building our model.
Here we have this error resize not defined.
So simply we have Keras layers here.
So we should just as we did here to the resizing.
So we have resize and that we go run that cell again and then get back to.
At this point our dataset is now ready for training.
We just have to include this pre fetching here for a more efficient usage of our data.
We saw those kinds of prevention or we explained what prevention was all about and some previous sessions.
So here we have the prevention.
We're now going to include the batching because you're already, we've included a batch size previously.
So in this data set loading right here, we specify the batch size.
So our data is already batched.
Now we have that we run the saw for the training and then we simply redo this for the validation.
So this is our validation, data validation, and now we're ready for building our model.
But before going on to build our model, we shall copy out this code here from the previous session
and paste it out here.
So we have this resized three skill layers, which we're going to include in the model.
Now recall that we could do this resizing like we have our dataset here.
So supposing you have your data set, you could do the resizing and rescaling your this reskilling and
this resize into the required size before passing this data into the model.
So this could be done before passing to the model such that you could train your model on this resized
and rescaled data.
Now another method will be to pass in your data your So let's suppose that we have let's take this off.
We suppose that here we have in this model we have the resizing and rescaling unit.
So we resize and reskill before passing into the model.
Now, instead of doing this, what we could do is we could pass the data into the model directly and
then carry out the resizing and reskilling in the model as a layer in the model.
Now, we've seen this already, but we'll just explain your again that doing this is great for deployment
because when you have to deploy this kind of system, you no longer want to resize it again.
So your all you do now is just pass in this image and then the model on its own does the model which
has been deployed takes care of resizing and reskilling.
Unlike in this kind of system where when you deploy this model, you have to do the resizing and reskilling
on your own.
So this means that if you deploy this model on some cloud solution and then you're calling it from a
JavaScript client, for example, or say some Android client, then you would have to carry out this
resizing and rescaling in JavaScript, unlike here, where you just pass in the image and the model
takes charge of resizing.
And reskilling.
So that said, let's take this off.
Get back to our code.
Here we're going to have we're going to run this simply.
And then we start with building our model.
Here we have this error resize not defined.
So simply, we have cross layers here.
So we should just as we did here, do the resizing.
So we have resize and there we go, run that cell again and we're now ready for modeling.