1
00:00:11,690 --> 00:00:17,420
In this lecture we are going to continue looking at our Siamese network code specifically to take what

2
00:00:17,420 --> 00:00:21,510
we just created and write some data generator functions.

3
00:00:21,530 --> 00:00:24,580
These will be the functions that we iterate over inside our training loop.

4
00:00:24,590 --> 00:00:32,360
Later on the basic idea is we're going to pre allocate some arrays and then fill them up with data from

5
00:00:32,360 --> 00:00:35,750
the pairs of indices that we just created.

6
00:00:35,780 --> 00:00:39,440
So we're going to start out by looking at the train and generator function

7
00:00:43,730 --> 00:00:44,810
as you can see.

8
00:00:44,900 --> 00:00:50,920
I've also specified the batch size here which will be 64 for the script inside the function.

9
00:00:50,930 --> 00:00:58,940
I'm going to set and batches to be the number of batches it's essentially the number of positive samples

10
00:00:59,150 --> 00:01:03,440
divided by the backside but rounded up since we round up.

11
00:01:03,440 --> 00:01:08,000
That means the last batch will always have less samples than the rest.

12
00:01:08,000 --> 00:01:11,680
Next we're going to enter an infinite loop inside the loop.

13
00:01:11,690 --> 00:01:15,190
We're going to shuffle the positives so that's this one.

14
00:01:15,380 --> 00:01:21,150
This is so that each epoch we'll encounter each of the positive samples in a different order.

15
00:01:21,170 --> 00:01:25,840
There's no need to shuffle the negative samples since we are already going to be choosing those at random

16
00:01:27,520 --> 00:01:31,920
next we're going to pre allocate some arrays.

17
00:01:32,120 --> 00:01:36,150
First we set and samples to be batch size times too.

18
00:01:36,200 --> 00:01:40,950
So in actuality the batch size will be 128 rather than 64.

19
00:01:41,870 --> 00:01:47,810
And this is because the first half of the dataset will be the positive samples and the second half will

20
00:01:47,810 --> 00:01:50,270
be the negative samples.

21
00:01:50,270 --> 00:01:54,360
Next we create X batch 1 x batch 2 and Y batch.

22
00:01:54,470 --> 00:01:58,730
Currently they are all just zeros and we're going to fill them up with an X for loop

23
00:02:05,600 --> 00:02:09,860
so inside this loop we're going to do this in batches times.

24
00:02:10,040 --> 00:02:14,020
First we get the indices from the positive pairs for this batch.

25
00:02:14,240 --> 00:02:19,810
So that's from AI times batch size up to AI plus 1 at times back size.

26
00:02:19,830 --> 00:02:22,860
Next we set the index J to 0.

27
00:02:23,010 --> 00:02:30,360
So J will be used to index our patches as we add samples to the pre allocated arrays.

28
00:02:30,360 --> 00:02:36,060
Next we loop through the pairs of positive samples for this batch so we get the X1 and RDX to

29
00:02:39,280 --> 00:02:47,230
then inside the loop we grab the train images at ADX one and RDX 2 and add these 2 x batch 1 and x batch

30
00:02:47,230 --> 00:02:51,130
to then we set y batch to 1.

31
00:02:51,280 --> 00:02:55,180
Since these are all matches then we increment J.

32
00:02:55,210 --> 00:03:04,450
So that on the next round we store the image in the next spot.

33
00:03:04,460 --> 00:03:10,990
Next we enter another loop this time for the negative samples but before we enter this loop we choose

34
00:03:10,990 --> 00:03:14,850
a random set of samples using NPD at random by choice.

35
00:03:16,050 --> 00:03:20,440
The difference inside this loop is we set y batch to zero.

36
00:03:20,640 --> 00:03:25,530
Since these are all non matches.

37
00:03:25,550 --> 00:03:29,950
Finally the true data for this batch only goes up to the index J.

38
00:03:30,380 --> 00:03:37,660
So we index X batch one X batch too and Y batch only up to J.

39
00:03:37,710 --> 00:03:41,660
Now we have our batch of data but there are a few small issues.

40
00:03:41,850 --> 00:03:46,920
First our images are currently of size n by h by W which is not inappropriate.

41
00:03:46,920 --> 00:03:49,030
Input shape for CNN.

42
00:03:49,200 --> 00:03:54,690
CNN wants its input to be four dimensional and in PI towards that end by color by height by with.

43
00:03:55,200 --> 00:04:01,330
So we need to add a superfluous one dimension in between the batch index and the height index.

44
00:04:01,590 --> 00:04:06,570
Additionally although we could do this in the training loop itself we can save some space by converting

45
00:04:06,600 --> 00:04:08,900
our data into torture tensor is now.

46
00:04:09,300 --> 00:04:14,580
As a side note even though Y is technically a binary integer label it's more useful to have it as a

47
00:04:14,580 --> 00:04:23,010
flow because we'll be multiplying it in our cost function so from that we get x1 x 2 and Y and then

48
00:04:23,040 --> 00:04:26,090
we yield x1 x 2 and Y.

49
00:04:26,190 --> 00:04:31,220
Note that we must enclose X1 the next 2 with square brackets to make it a list.

50
00:04:31,320 --> 00:04:35,240
So this will not work if you return a tuple and it will also not work.

51
00:04:35,250 --> 00:04:41,730
If you try to just return x 1 x 2 and Y without any brackets this is because these two variables will

52
00:04:41,730 --> 00:04:44,910
be the input into the model and Y will be the target

53
00:04:49,470 --> 00:04:51,590
nice we have the test generator.

54
00:04:51,730 --> 00:04:57,790
Now this is essentially the exact same thing except that we don't need to shuffle the data and it's

55
00:04:57,790 --> 00:05:00,970
using the test variables rather than the train variables.
