Hi and welcome back in this lesson.
Lesson three seven.
We'll take a look at how we can train our very own Siamese network and care us.
So open notebook 27 and we'll get started.
So let's take a look at this notebook.
So firstly, what we're going to do in this project, we're going to firstly, Ludo Amnesty to set create
appeared Typekit of set with the amnesty to create a model, define our contrastive laws and then train
the model, then visualize the results so we can actually compare different data data images in the
amnesty dataset and get the results.
So we'll be using contrastive loss in this network.
So let's begin.
So firstly, let's load some data and then we'll set our parameters law on here.
So let's first import the libraries here, and then let's look at Amnesty.
The set, which was done several times before.
And now let's to find some hyperemesis said epochs in this network to train for ten who set the bad
sights at 16 and a margin for contrast, of course, being one.
OK, we'll discuss margin later on.
But for no, this remember module is set to one.
Now let's define the training and validation data sets here.
So we just split this so we keep 50 percent of all of our training data in the validation set here next.
What we do, we create image pairs, so let's make a function here that goes through all of the dataset,
the entire him.
This data set and creates Piers.
Yeah.
So by fitting into extreme and light training, we have Pismo for training data and labels when it appears
is negative or positive.
Similarly, we have it for validation.
That's how we will evaluate our network after on this dataset, as well as a test one as well so we
can use all of those during training.
So you can see we get to Pierce ship here, if you wanted to take a look at it.
We have 62000 pairs and each pier contains two images and each image has a shape of 28 by 28.
So you can see that's what the two means here.
So we know instead of having one, like we previously did with the embassy, the that we have to because
it's two images.
So let's split the training and training pairs here.
So we have X one and X two and X and X-File one, an ex-felon two and as well for the tests, we have
X test one next test.
So let's visualize some of these pills and they label.
So use a visualization function here just to quickly plot some of the images here, along with their
labels.
So you can see level one.
Level one means that there are similar level zero means or this dissimilar.
And likewise, you can see positive and negative, positive and negative.
And we can similarly investigate our coalition peers as well should be the same type of thing.
We have positive peer and negative peer positive and negative fear.
And likewise, we can do it for test peers as well.
So now we're ready to define the model, so we'll create a Euclidean distance function here that simply
takes some vectors and then gets the Euclidean distance between them.
And then now will define the network design here.
So you can see this is a very simple, simple sienna.
We just have four convolution filters at five by five kilos.
We use a 10 inch function instead of to reload, and then we just pass it in.
Here, we use average pooling instead of max pooling.
And then we just flatten everything here.
Then we have that's normalization layer, and then we output to just a 10 vector, one by 10 vector.
And that's our embedding network right there.
So we have inputs, inputs, one inputs.
Two, we create the networks here told one until two we call it, and then we just create everything
here by bringing it all together and we couple final.
This is what I mean by bringing it all together.
We have a final dance one out, which tells you whether to pay a similar or dissimilar.
So let's render.
Now we can define our contrastive loss and begin to trim.
So let's define our loss here.
We use a margin equal one.
And like I said, I would explain margin for you later.
Margin is basically the integer that defines a baseline for distance, for which peers should be classified
as dissimilar.
So what does that mean?
Basically, it means that margin is simply a margin.
It has to be the distance for which a peer has to be dissimilar so that we say it's different.
So if it exceeds one, it's different.
If it's not less than one the margin, then it's similar.
And this function here returns a contrast to the loss that we calculate here.
So let's run that.
Now we're ready to compile a model.
We use it R-Miss. Prop Optimizer and the metrics we'll explore during training and investigate during
training would be accuracy, accuracy of what it's similar to the similar.
And now we're ready to test fit a Siamese model to Dippin data that we used before.
So you can see no, we use said extra in one extra and two.
That's why we split data into two.
And similarly, we have the X-File and Vivo and as well as two levels here, we have labels for treen
as well right there.
So now let's go ahead and train.
This network, which doesn't take that long, takes roughly a minute to put your book so we can just
wait for this to complete.
OK, so well as trends, because it's going to take quite a while.
I can just move on to showing you the previous results I got from running this notebook.
So we can create a function here called Visualize Results and what this does.
This plots a given metric from the history history being the history of the object that was returned
from Model Fit metric being the metric to plot, which basically just specifies what metric we here
we want to plot, meaning that we want to put accuracy or loss and we just input the title and then
we just input the history that we collected during the training procedure.
So you can see here for the model accuracy that is what it's seeing.
It's similar or dissimilar.
We reached some very high values, very close to ninety five, ninety six percent.
That's pretty good.
Similarly, you can see different loss has gone down for both train and Vo is even more for the VO and
better accuracy for the VO, which is quite good.
And now you can s run this here to evaluate your model.
This basically inputs on the X test data here these two two sets, as well as some labels for them so
we can compare whether they're this is similar to this or this is dissimilar to that.
And then we just get the accuracy here, and we get 98 percent accuracy, that's actually pretty good
on the testing is set even better than on the validated asset, which looked like it was topping out
at maybe ninety six and seven percent.
No, you can visualize predictions here with the Visualize function.
So we just get some pins here, piers to test labels and we just saw Tree as well.
And we just get our predictions here from the same year at work.
So we're passing X test here.
This is appears for the test data, the test labels and tests that test to true.
And let's just get the results here so you can take a look at these results.
You can see true meaning a similar kind of predictive score was point ninety nine.
Remember, I did see an initial one.
A highest similarity score means that they were different.
Well, in this case, actually, this is a different representation here.
So in this case, when it's two point ninety nine, that means that actually it is similar for this
model.
They don't PyTorch model that.
Actually, it's reversed.
So just pay attention to that and you can see if one two seven five the score is quite low.
And now, similarly, when it's two tours, it's quite high.
So this Sammies network is working very, very good, actually.
So while it takes about roughly 15 minutes to train the network, I might be training on chip and CPU.
Let's check so that can be why it's so slow.
Hope it's not.
Yes, it is on CPU.
So hopefully you set this the GPU on like I did, and you have a much faster training experience.
So that's it for this lesson will now continue with our pie towards Sammy's network, where we we will
explore this similarity instead.
Sort of the amnestied asset, which is even cooler.
So I'll see you in the next lesson.
Thank you.