Hi and welcome back in this lesson, we'll take a look at using PyTorch to implement a Siamese network
that can compare faces, so it's a very cool project, so let's get started.
So open notebook too late, which I've already done here, and we can begin to listen.
So this lesson has a lot of small building blocks, so I'll try to go for them as detailed as I can.
So firstly, we're going to import our modules and then we're going to import our data and some utility
functions.
So let's just run this first block of code at Hustle.
Follow the inputs that we'll be using.
Next, we're going to create some utility functions here.
One is an IM show just to show the image.
Another one is to show the plot.
That's a plot for doing training to represent, to show a training loss and accuracy loss.
Next will download our data.
This is a source of the data here, so you can just click this link if you want to check out the source.
The AT&T database of faces and you can expand this and read all about it here.
So the images 1982 by a hundred and twelve pixels and it's a grayscale image, so we can just take a
look at that after.
So let's look at our data here.
OK, that was pretty quick, just took about five seconds.
Next, we create a config class here, so it is still a training directory, training tested directory
of that size and the number of epochs with the training for which is 100, which is quite a lot.
Now we have a Siamese network dataset.
This is what creates our image pairs here, so we just need 50 percent of the images to be in the same
class.
So this creates the image pins right there.
Next, we set out image folders here using image for that right now.
Next, we create or transform sort of transforms will be using or just resizing it to one hundred by
100 and converting it to 10.
So and no one getting here.
So let's go.
So now we can create our data loaders here from the same dataset that we've created right here that
transforms will be created as well.
So we shuffle them, said the number of workers to it, although that might be a bit high for collab.
So maybe we can try for, although it should default to forfeit.
Can't get it to believe that's how it should working with Potlatch.
And then we have a terrible data here so we can take a look at the plots we use to watch visions, utils
and make grid to just create a simple plot of some of the some of the sample images so you can see them
there.
These are all three sides to 100 by 100.
Now we can start building out Siamese network.
So let's take a look at this network.
Basically, we use something called reflection, but to this pads the tensor using the reflection of
the input boundary.
Then we have a convoluted velu, then a batch norm, then another reflection pattern.
Then we have another con's would descend with eight filters.
Initially, it was four kernel sizes poetry and then we have.
So we have tree convalesce here of increasing amount.
And so we actually know it's about the same hand to see it.
And then we have our sequentially.
This produces our embedding.
So the embedding, the final embedding is a one dimensional vector that's five hundreds of one by 500.
And then we create this loop here.
Well, listen, not this loop is going to create a forward pass here.
So this is called forward once.
And the reason we have forward ones is because we use it to create output one output two, which is
basically the two same system networks.
So let's run this.
Next, we can define our contrast of most function or class two effectively, and then the function
isn't so different.
That creates two that gives us the contrast of loss.
Now we create our training data loader and then set about contrast to the loss criterion, as well as
a optimizer which pleasing Adam.
And once that's done, we can actually start training in the training loop is actually quite simple
compared to the usual PyTorch training grids, training plot loops that you see.
You can see this.
This is here where we run everything.
So we send over two images here, two batches to the network here, and then we just get the loss out
of it and an update gradients as well.
And then we just plot print some results every 10 iterations.
So let's load this.
We'll run this, I should say, and you can see we're training for 100 epochs and it's running quite
fast.
So let's just wait for this to be complete.
All right, so we're finished training and we can see a nice training plot here.
And you can see loss went down to quite low point zero zero six and that's a reasonably good score.
Now let's see how this network performs when we actually compare some fierce, fierce images.
So we'll load some of the testing set here, and then it will create that.
We created the same dataset again right here, and then we create the test data loader.
We just pass to get a batch and then we just get zero here.
So we just put that batch into it here to compare.
So it concatenated, and then we just get the pairwise distance out of it, and then we may use torture
machines to make the plot, the grid, the visual display.
And we just use everything here.
So we get particularly Euclidean distance to similarity score in it.
So let's run that and see how it performance is a pretty cool idea.
So let's take a look at this so you can see the first one.
We get a high dissimilarity score of three point zero six and there are two different people.
However, in the next one, we get a lower dissimilarity school under the same person.
It's quite cool, isn't it?
Next expected high school the similar.
This one is in the similar.
And again, even though they do appear to look similar, they are different people, obviously, and
that work is picking up on that.
Again, good good results.
Good result.
Again, no mistake.
So far, it's all pretty good results that we're comparing this guy with a multitude of guys, including
himself at different angles, because you can see definitely is a different angle for this and this,
and he gets a very low dissimilarity score.
So that's it.
That concludes this lesson and hope you enjoyed it.
Thanks for watching.
But.