1
00:00:00,060 --> 00:00:02,560
Hi and welcome back in this lesson.

2
00:00:02,580 --> 00:00:03,720
Lesson three seven.

3
00:00:04,110 --> 00:00:08,730
We'll take a look at how we can train our very own Siamese network and care us.

4
00:00:09,210 --> 00:00:12,480
So open notebook 27 and we'll get started.

5
00:00:13,350 --> 00:00:14,940
So let's take a look at this notebook.

6
00:00:14,950 --> 00:00:22,170
So firstly, what we're going to do in this project, we're going to firstly, Ludo Amnesty to set create

7
00:00:22,170 --> 00:00:29,370
appeared Typekit of set with the amnesty to create a model, define our contrastive laws and then train

8
00:00:29,370 --> 00:00:35,520
the model, then visualize the results so we can actually compare different data data images in the

9
00:00:35,520 --> 00:00:37,980
amnesty dataset and get the results.

10
00:00:38,340 --> 00:00:42,240
So we'll be using contrastive loss in this network.

11
00:00:42,270 --> 00:00:43,020
So let's begin.

12
00:00:43,020 --> 00:00:48,330
So firstly, let's load some data and then we'll set our parameters law on here.

13
00:00:48,330 --> 00:00:53,580
So let's first import the libraries here, and then let's look at Amnesty.

14
00:00:53,580 --> 00:00:56,430
The set, which was done several times before.

15
00:00:57,150 --> 00:01:04,200
And now let's to find some hyperemesis said epochs in this network to train for ten who set the bad

16
00:01:04,200 --> 00:01:08,370
sights at 16 and a margin for contrast, of course, being one.

17
00:01:09,450 --> 00:01:11,550
OK, we'll discuss margin later on.

18
00:01:11,550 --> 00:01:13,980
But for no, this remember module is set to one.

19
00:01:14,640 --> 00:01:18,210
Now let's define the training and validation data sets here.

20
00:01:18,450 --> 00:01:27,120
So we just split this so we keep 50 percent of all of our training data in the validation set here next.

21
00:01:27,180 --> 00:01:34,050
What we do, we create image pairs, so let's make a function here that goes through all of the dataset,

22
00:01:34,470 --> 00:01:35,130
the entire him.

23
00:01:35,130 --> 00:01:37,190
This data set and creates Piers.

24
00:01:37,200 --> 00:01:37,420
Yeah.

25
00:01:37,860 --> 00:01:44,500
So by fitting into extreme and light training, we have Pismo for training data and labels when it appears

26
00:01:44,500 --> 00:01:45,930
is negative or positive.

27
00:01:46,410 --> 00:01:48,420
Similarly, we have it for validation.

28
00:01:48,420 --> 00:01:54,240
That's how we will evaluate our network after on this dataset, as well as a test one as well so we

29
00:01:54,240 --> 00:01:56,010
can use all of those during training.

30
00:01:59,480 --> 00:02:03,350
So you can see we get to Pierce ship here, if you wanted to take a look at it.

31
00:02:03,680 --> 00:02:10,760
We have 62000 pairs and each pier contains two images and each image has a shape of 28 by 28.

32
00:02:11,120 --> 00:02:12,970
So you can see that's what the two means here.

33
00:02:12,980 --> 00:02:19,190
So we know instead of having one, like we previously did with the embassy, the that we have to because

34
00:02:19,190 --> 00:02:20,060
it's two images.

35
00:02:20,840 --> 00:02:23,570
So let's split the training and training pairs here.

36
00:02:25,640 --> 00:02:32,720
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

37
00:02:32,720 --> 00:02:34,260
X test one next test.

38
00:02:35,480 --> 00:02:38,810
So let's visualize some of these pills and they label.

39
00:02:38,810 --> 00:02:45,530
So use a visualization function here just to quickly plot some of the images here, along with their

40
00:02:45,530 --> 00:02:46,010
labels.

41
00:02:46,520 --> 00:02:47,930
So you can see level one.

42
00:02:48,410 --> 00:02:53,090
Level one means that there are similar level zero means or this dissimilar.

43
00:02:53,270 --> 00:02:57,170
And likewise, you can see positive and negative, positive and negative.

44
00:02:58,550 --> 00:03:04,090
And we can similarly investigate our coalition peers as well should be the same type of thing.

45
00:03:04,190 --> 00:03:08,960
We have positive peer and negative peer positive and negative fear.

46
00:03:09,620 --> 00:03:12,740
And likewise, we can do it for test peers as well.

47
00:03:13,580 --> 00:03:19,340
So now we're ready to define the model, so we'll create a Euclidean distance function here that simply

48
00:03:19,340 --> 00:03:23,210
takes some vectors and then gets the Euclidean distance between them.

49
00:03:23,510 --> 00:03:26,360
And then now will define the network design here.

50
00:03:26,780 --> 00:03:30,230
So you can see this is a very simple, simple sienna.

51
00:03:30,260 --> 00:03:33,590
We just have four convolution filters at five by five kilos.

52
00:03:34,070 --> 00:03:38,510
We use a 10 inch function instead of to reload, and then we just pass it in.

53
00:03:38,510 --> 00:03:41,000
Here, we use average pooling instead of max pooling.

54
00:03:41,630 --> 00:03:43,760
And then we just flatten everything here.

55
00:03:44,210 --> 00:03:52,070
Then we have that's normalization layer, and then we output to just a 10 vector, one by 10 vector.

56
00:03:53,030 --> 00:03:55,190
And that's our embedding network right there.

57
00:03:55,790 --> 00:03:58,010
So we have inputs, inputs, one inputs.

58
00:03:58,010 --> 00:04:05,120
Two, we create the networks here told one until two we call it, and then we just create everything

59
00:04:05,120 --> 00:04:08,620
here by bringing it all together and we couple final.

60
00:04:08,630 --> 00:04:10,190
This is what I mean by bringing it all together.

61
00:04:10,190 --> 00:04:17,180
We have a final dance one out, which tells you whether to pay a similar or dissimilar.

62
00:04:17,420 --> 00:04:18,460
So let's render.

63
00:04:19,730 --> 00:04:22,990
Now we can define our contrastive loss and begin to trim.

64
00:04:23,540 --> 00:04:25,110
So let's define our loss here.

65
00:04:25,130 --> 00:04:27,260
We use a margin equal one.

66
00:04:27,350 --> 00:04:30,140
And like I said, I would explain margin for you later.

67
00:04:30,770 --> 00:04:37,010
Margin is basically the integer that defines a baseline for distance, for which peers should be classified

68
00:04:37,010 --> 00:04:38,000
as dissimilar.

69
00:04:38,450 --> 00:04:39,410
So what does that mean?

70
00:04:39,560 --> 00:04:42,260
Basically, it means that margin is simply a margin.

71
00:04:42,260 --> 00:04:47,540
It has to be the distance for which a peer has to be dissimilar so that we say it's different.

72
00:04:48,050 --> 00:04:50,060
So if it exceeds one, it's different.

73
00:04:50,060 --> 00:04:53,360
If it's not less than one the margin, then it's similar.

74
00:04:54,170 --> 00:04:58,760
And this function here returns a contrast to the loss that we calculate here.

75
00:04:59,030 --> 00:05:00,920
So let's run that.

76
00:05:01,730 --> 00:05:03,770
Now we're ready to compile a model.

77
00:05:04,580 --> 00:05:10,940
We use it R-Miss. Prop Optimizer and the metrics we'll explore during training and investigate during

78
00:05:10,940 --> 00:05:15,430
training would be accuracy, accuracy of what it's similar to the similar.

79
00:05:16,160 --> 00:05:21,140
And now we're ready to test fit a Siamese model to Dippin data that we used before.

80
00:05:21,620 --> 00:05:25,670
So you can see no, we use said extra in one extra and two.

81
00:05:26,090 --> 00:05:28,040
That's why we split data into two.

82
00:05:28,700 --> 00:05:34,130
And similarly, we have the X-File and Vivo and as well as two levels here, we have labels for treen

83
00:05:34,130 --> 00:05:35,580
as well right there.

84
00:05:36,020 --> 00:05:38,390
So now let's go ahead and train.

85
00:05:38,390 --> 00:05:44,090
This network, which doesn't take that long, takes roughly a minute to put your book so we can just

86
00:05:44,090 --> 00:05:45,290
wait for this to complete.

87
00:05:46,790 --> 00:05:49,790
OK, so well as trends, because it's going to take quite a while.

88
00:05:50,270 --> 00:05:54,620
I can just move on to showing you the previous results I got from running this notebook.

89
00:05:55,160 --> 00:05:59,390
So we can create a function here called Visualize Results and what this does.

90
00:05:59,720 --> 00:06:05,270
This plots a given metric from the history history being the history of the object that was returned

91
00:06:05,270 --> 00:06:11,540
from Model Fit metric being the metric to plot, which basically just specifies what metric we here

92
00:06:11,540 --> 00:06:17,180
we want to plot, meaning that we want to put accuracy or loss and we just input the title and then

93
00:06:17,180 --> 00:06:21,110
we just input the history that we collected during the training procedure.

94
00:06:21,620 --> 00:06:26,540
So you can see here for the model accuracy that is what it's seeing.

95
00:06:27,110 --> 00:06:28,610
It's similar or dissimilar.

96
00:06:28,640 --> 00:06:34,010
We reached some very high values, very close to ninety five, ninety six percent.

97
00:06:34,010 --> 00:06:34,730
That's pretty good.

98
00:06:35,510 --> 00:06:40,700
Similarly, you can see different loss has gone down for both train and Vo is even more for the VO and

99
00:06:40,700 --> 00:06:42,770
better accuracy for the VO, which is quite good.

100
00:06:44,030 --> 00:06:47,030
And now you can s run this here to evaluate your model.

101
00:06:47,510 --> 00:06:53,720
This basically inputs on the X test data here these two two sets, as well as some labels for them so

102
00:06:53,720 --> 00:06:57,890
we can compare whether they're this is similar to this or this is dissimilar to that.

103
00:06:58,470 --> 00:07:04,110
And then we just get the accuracy here, and we get 98 percent accuracy, that's actually pretty good

104
00:07:04,110 --> 00:07:09,630
on the testing is set even better than on the validated asset, which looked like it was topping out

105
00:07:09,630 --> 00:07:12,600
at maybe ninety six and seven percent.

106
00:07:13,950 --> 00:07:16,770
No, you can visualize predictions here with the Visualize function.

107
00:07:17,160 --> 00:07:24,750
So we just get some pins here, piers to test labels and we just saw Tree as well.

108
00:07:25,380 --> 00:07:29,150
And we just get our predictions here from the same year at work.

109
00:07:29,160 --> 00:07:31,230
So we're passing X test here.

110
00:07:31,770 --> 00:07:37,830
This is appears for the test data, the test labels and tests that test to true.

111
00:07:37,860 --> 00:07:42,450
And let's just get the results here so you can take a look at these results.

112
00:07:42,450 --> 00:07:48,360
You can see true meaning a similar kind of predictive score was point ninety nine.

113
00:07:49,110 --> 00:07:51,330
Remember, I did see an initial one.

114
00:07:51,720 --> 00:07:55,560
A highest similarity score means that they were different.

115
00:07:55,980 --> 00:07:59,030
Well, in this case, actually, this is a different representation here.

116
00:07:59,040 --> 00:08:05,160
So in this case, when it's two point ninety nine, that means that actually it is similar for this

117
00:08:05,160 --> 00:08:05,580
model.

118
00:08:06,450 --> 00:08:07,560
They don't PyTorch model that.

119
00:08:07,560 --> 00:08:08,850
Actually, it's reversed.

120
00:08:09,120 --> 00:08:15,120
So just pay attention to that and you can see if one two seven five the score is quite low.

121
00:08:15,150 --> 00:08:18,730
And now, similarly, when it's two tours, it's quite high.

122
00:08:18,750 --> 00:08:22,560
So this Sammies network is working very, very good, actually.

123
00:08:23,160 --> 00:08:30,000
So while it takes about roughly 15 minutes to train the network, I might be training on chip and CPU.

124
00:08:30,010 --> 00:08:33,930
Let's check so that can be why it's so slow.

125
00:08:34,230 --> 00:08:35,040
Hope it's not.

126
00:08:35,490 --> 00:08:36,690
Yes, it is on CPU.

127
00:08:37,110 --> 00:08:42,540
So hopefully you set this the GPU on like I did, and you have a much faster training experience.

128
00:08:43,110 --> 00:08:49,770
So that's it for this lesson will now continue with our pie towards Sammy's network, where we we will

129
00:08:49,770 --> 00:08:51,600
explore this similarity instead.

130
00:08:51,600 --> 00:08:53,670
Sort of the amnestied asset, which is even cooler.

131
00:08:54,090 --> 00:08:55,830
So I'll see you in the next lesson.

132
00:08:55,980 --> 00:08:56,430
Thank you.
