1
00:00:00,360 --> 00:00:07,440
Hi and welcome back in this lesson, we'll take a look at using PyTorch to implement a Siamese network

2
00:00:07,440 --> 00:00:11,940
that can compare faces, so it's a very cool project, so let's get started.

3
00:00:12,630 --> 00:00:17,730
So open notebook too late, which I've already done here, and we can begin to listen.

4
00:00:18,360 --> 00:00:25,530
So this lesson has a lot of small building blocks, so I'll try to go for them as detailed as I can.

5
00:00:25,620 --> 00:00:31,710
So firstly, we're going to import our modules and then we're going to import our data and some utility

6
00:00:31,710 --> 00:00:32,430
functions.

7
00:00:32,730 --> 00:00:35,570
So let's just run this first block of code at Hustle.

8
00:00:35,640 --> 00:00:38,250
Follow the inputs that we'll be using.

9
00:00:40,170 --> 00:00:43,080
Next, we're going to create some utility functions here.

10
00:00:43,200 --> 00:00:45,570
One is an IM show just to show the image.

11
00:00:46,000 --> 00:00:47,810
Another one is to show the plot.

12
00:00:47,820 --> 00:00:53,400
That's a plot for doing training to represent, to show a training loss and accuracy loss.

13
00:00:53,910 --> 00:00:55,690
Next will download our data.

14
00:00:55,770 --> 00:01:00,090
This is a source of the data here, so you can just click this link if you want to check out the source.

15
00:01:00,260 --> 00:01:06,090
The AT&amp;T database of faces and you can expand this and read all about it here.

16
00:01:06,480 --> 00:01:13,860
So the images 1982 by a hundred and twelve pixels and it's a grayscale image, so we can just take a

17
00:01:13,860 --> 00:01:15,480
look at that after.

18
00:01:15,570 --> 00:01:17,750
So let's look at our data here.

19
00:01:22,910 --> 00:01:25,400
OK, that was pretty quick, just took about five seconds.

20
00:01:26,030 --> 00:01:32,780
Next, we create a config class here, so it is still a training directory, training tested directory

21
00:01:33,380 --> 00:01:38,390
of that size and the number of epochs with the training for which is 100, which is quite a lot.

22
00:01:39,350 --> 00:01:41,990
Now we have a Siamese network dataset.

23
00:01:41,990 --> 00:01:47,660
This is what creates our image pairs here, so we just need 50 percent of the images to be in the same

24
00:01:47,660 --> 00:01:48,320
class.

25
00:01:48,650 --> 00:01:51,500
So this creates the image pins right there.

26
00:01:53,000 --> 00:01:57,620
Next, we set out image folders here using image for that right now.

27
00:01:58,340 --> 00:02:04,580
Next, we create or transform sort of transforms will be using or just resizing it to one hundred by

28
00:02:04,580 --> 00:02:06,650
100 and converting it to 10.

29
00:02:06,650 --> 00:02:09,470
So and no one getting here.

30
00:02:10,040 --> 00:02:11,420
So let's go.

31
00:02:12,260 --> 00:02:17,450
So now we can create our data loaders here from the same dataset that we've created right here that

32
00:02:17,450 --> 00:02:19,670
transforms will be created as well.

33
00:02:20,360 --> 00:02:26,930
So we shuffle them, said the number of workers to it, although that might be a bit high for collab.

34
00:02:26,940 --> 00:02:30,650
So maybe we can try for, although it should default to forfeit.

35
00:02:30,650 --> 00:02:33,790
Can't get it to believe that's how it should working with Potlatch.

36
00:02:34,520 --> 00:02:42,950
And then we have a terrible data here so we can take a look at the plots we use to watch visions, utils

37
00:02:42,950 --> 00:02:49,220
and make grid to just create a simple plot of some of the some of the sample images so you can see them

38
00:02:49,220 --> 00:02:49,550
there.

39
00:02:50,210 --> 00:02:52,760
These are all three sides to 100 by 100.

40
00:02:55,340 --> 00:02:58,100
Now we can start building out Siamese network.

41
00:02:58,670 --> 00:03:00,560
So let's take a look at this network.

42
00:03:00,920 --> 00:03:07,340
Basically, we use something called reflection, but to this pads the tensor using the reflection of

43
00:03:07,340 --> 00:03:08,480
the input boundary.

44
00:03:08,930 --> 00:03:14,630
Then we have a convoluted velu, then a batch norm, then another reflection pattern.

45
00:03:15,230 --> 00:03:18,490
Then we have another con's would descend with eight filters.

46
00:03:18,500 --> 00:03:23,120
Initially, it was four kernel sizes poetry and then we have.

47
00:03:23,240 --> 00:03:27,860
So we have tree convalesce here of increasing amount.

48
00:03:27,950 --> 00:03:31,400
And so we actually know it's about the same hand to see it.

49
00:03:32,090 --> 00:03:33,820
And then we have our sequentially.

50
00:03:34,010 --> 00:03:36,860
This produces our embedding.

51
00:03:36,980 --> 00:03:43,040
So the embedding, the final embedding is a one dimensional vector that's five hundreds of one by 500.

52
00:03:43,730 --> 00:03:45,380
And then we create this loop here.

53
00:03:45,590 --> 00:03:48,440
Well, listen, not this loop is going to create a forward pass here.

54
00:03:48,770 --> 00:03:50,330
So this is called forward once.

55
00:03:50,750 --> 00:03:55,280
And the reason we have forward ones is because we use it to create output one output two, which is

56
00:03:55,280 --> 00:03:58,610
basically the two same system networks.

57
00:03:58,880 --> 00:04:00,200
So let's run this.

58
00:04:01,160 --> 00:04:06,860
Next, we can define our contrast of most function or class two effectively, and then the function

59
00:04:06,860 --> 00:04:07,580
isn't so different.

60
00:04:07,580 --> 00:04:10,370
That creates two that gives us the contrast of loss.

61
00:04:11,300 --> 00:04:17,270
Now we create our training data loader and then set about contrast to the loss criterion, as well as

62
00:04:17,270 --> 00:04:19,880
a optimizer which pleasing Adam.

63
00:04:20,900 --> 00:04:25,790
And once that's done, we can actually start training in the training loop is actually quite simple

64
00:04:25,790 --> 00:04:32,540
compared to the usual PyTorch training grids, training plot loops that you see.

65
00:04:33,590 --> 00:04:34,270
You can see this.

66
00:04:34,540 --> 00:04:36,440
This is here where we run everything.

67
00:04:37,010 --> 00:04:44,150
So we send over two images here, two batches to the network here, and then we just get the loss out

68
00:04:44,150 --> 00:04:47,260
of it and an update gradients as well.

69
00:04:47,270 --> 00:04:52,220
And then we just plot print some results every 10 iterations.

70
00:04:52,430 --> 00:04:54,020
So let's load this.

71
00:04:55,130 --> 00:04:59,840
We'll run this, I should say, and you can see we're training for 100 epochs and it's running quite

72
00:04:59,840 --> 00:05:00,350
fast.

73
00:05:00,950 --> 00:05:03,200
So let's just wait for this to be complete.

74
00:05:23,840 --> 00:05:27,560
All right, so we're finished training and we can see a nice training plot here.

75
00:05:27,980 --> 00:05:33,200
And you can see loss went down to quite low point zero zero six and that's a reasonably good score.

76
00:05:33,770 --> 00:05:38,660
Now let's see how this network performs when we actually compare some fierce, fierce images.

77
00:05:39,140 --> 00:05:43,700
So we'll load some of the testing set here, and then it will create that.

78
00:05:43,700 --> 00:05:48,350
We created the same dataset again right here, and then we create the test data loader.

79
00:05:48,920 --> 00:05:52,790
We just pass to get a batch and then we just get zero here.

80
00:05:53,330 --> 00:05:56,570
So we just put that batch into it here to compare.

81
00:05:56,600 --> 00:06:02,990
So it concatenated, and then we just get the pairwise distance out of it, and then we may use torture

82
00:06:02,990 --> 00:06:08,030
machines to make the plot, the grid, the visual display.

83
00:06:08,690 --> 00:06:10,610
And we just use everything here.

84
00:06:10,610 --> 00:06:14,570
So we get particularly Euclidean distance to similarity score in it.

85
00:06:15,140 --> 00:06:19,280
So let's run that and see how it performance is a pretty cool idea.

86
00:06:20,090 --> 00:06:24,050
So let's take a look at this so you can see the first one.

87
00:06:24,620 --> 00:06:30,930
We get a high dissimilarity score of three point zero six and there are two different people.

88
00:06:31,470 --> 00:06:35,360
However, in the next one, we get a lower dissimilarity school under the same person.

89
00:06:35,570 --> 00:06:36,740
It's quite cool, isn't it?

90
00:06:37,340 --> 00:06:40,930
Next expected high school the similar.

91
00:06:41,780 --> 00:06:43,040
This one is in the similar.

92
00:06:43,040 --> 00:06:48,140
And again, even though they do appear to look similar, they are different people, obviously, and

93
00:06:48,140 --> 00:06:49,460
that work is picking up on that.

94
00:06:50,390 --> 00:06:52,910
Again, good good results.

95
00:06:53,750 --> 00:06:54,340
Good result.

96
00:06:54,350 --> 00:06:55,880
Again, no mistake.

97
00:06:55,890 --> 00:07:01,610
So far, it's all pretty good results that we're comparing this guy with a multitude of guys, including

98
00:07:01,610 --> 00:07:06,710
himself at different angles, because you can see definitely is a different angle for this and this,

99
00:07:06,710 --> 00:07:10,040
and he gets a very low dissimilarity score.

100
00:07:10,640 --> 00:07:11,720
So that's it.

101
00:07:11,930 --> 00:07:15,380
That concludes this lesson and hope you enjoyed it.

102
00:07:16,220 --> 00:07:17,450
Thanks for watching.

103
00:07:17,450 --> 00:07:17,750
But.
