1
00:00:00,330 --> 00:00:07,130
Hi and welcome back in this lesson, we'll be using Harris to implement a simple again something called

2
00:00:07,140 --> 00:00:13,290
a D.C. gun, which is short for a deep convolution gun and will be using it on the amnesty to set.

3
00:00:13,300 --> 00:00:17,310
So we're going to generate synthetic samples of that data set.

4
00:00:17,790 --> 00:00:20,430
So it's a very good instructional lesson on guns.

5
00:00:20,430 --> 00:00:21,420
So let's get started.

6
00:00:21,450 --> 00:00:25,890
So open notebook, too, the one here and we can begin to listen.

7
00:00:26,520 --> 00:00:29,110
So firstly, let's take a look at what we're doing.

8
00:00:29,130 --> 00:00:31,920
So let's do a quick recap on guns.

9
00:00:32,400 --> 00:00:37,980
Remember, what we have here is a generative network, and it discriminates in that we have two main

10
00:00:37,980 --> 00:00:38,700
components.

11
00:00:39,090 --> 00:00:44,400
The generator generates fake images, and it is committed to try and tell the difference between real

12
00:00:44,400 --> 00:00:45,900
images and fake images.

13
00:00:46,350 --> 00:00:48,780
And then it uses that to update its wits.

14
00:00:49,260 --> 00:00:56,880
And then the generator sends more images again, more a more mixed images dataset here to that discriminator

15
00:00:56,880 --> 00:00:59,940
with the frozen widths, and then he uses that knowledge.

16
00:01:00,160 --> 00:01:06,300
The results of that two updated tweets so that it can then in turn, keep improving.

17
00:01:06,600 --> 00:01:08,520
So this is how we train again.

18
00:01:09,060 --> 00:01:10,310
So let's get started.

19
00:01:10,320 --> 00:01:14,160
So first, let's download and install some of the packages.

20
00:01:14,160 --> 00:01:14,670
We need it.

21
00:01:15,300 --> 00:01:19,710
So we'll do that now shouldn't take too long so we can move on while we do that.

22
00:01:20,310 --> 00:01:30,060
Next, import of libraries that we will be using, and then we can go on and loading and preparing our

23
00:01:30,060 --> 00:01:32,420
data sets, including the amnesty to set.

24
00:01:32,430 --> 00:01:35,430
Here we get the tree in images, tree and labels.

25
00:01:35,850 --> 00:01:41,760
Then we reshape and normalize the data here, so we normalize it between minus one and one and converting

26
00:01:41,760 --> 00:01:43,170
it to a float to two.

27
00:01:44,010 --> 00:01:46,680
We set up a batch and buffer sized buffer.

28
00:01:46,680 --> 00:01:49,950
Size is the number of images in the training dataset.

29
00:01:49,950 --> 00:01:53,250
60000 to that size is 256.

30
00:01:53,790 --> 00:01:58,860
And then we just create this dataset to see dataset load effectively.

31
00:01:59,280 --> 00:02:05,220
That gives us a batch of data from that that that input that training data we have.

32
00:02:05,790 --> 00:02:07,650
So let's run this.

33
00:02:10,010 --> 00:02:11,540
And it should finish shortly.

34
00:02:11,630 --> 00:02:12,140
There we go.

35
00:02:12,770 --> 00:02:15,710
So now we can define what generates a model.

36
00:02:16,220 --> 00:02:23,660
Now remember, the generator model takes random noise as an input and generates a synthetic output from

37
00:02:23,660 --> 00:02:23,870
it.

38
00:02:24,320 --> 00:02:30,620
So to do that, basically, we just have the input here, input chip being some random noise vector

39
00:02:31,340 --> 00:02:33,110
and then which is the densely here.

40
00:02:33,110 --> 00:02:34,790
So we have a fully connected dense layer.

41
00:02:35,210 --> 00:02:42,200
However, this then goes to convert to the transpose, which is an upsampling layer, so you can see

42
00:02:42,200 --> 00:02:43,280
that it up samples.

43
00:02:43,280 --> 00:02:49,520
It gradually took the network so that the outputs final size is a 28 by 28 image.

44
00:02:49,610 --> 00:02:51,140
That's the generator network to remember.

45
00:02:51,140 --> 00:02:57,230
It's generating synthetic images so we can take a look at how generated network looks.

46
00:02:57,770 --> 00:03:03,470
You can just use this function that we defined to make code generator model and returns the model here.

47
00:03:03,950 --> 00:03:05,450
So we have generator here.

48
00:03:05,960 --> 00:03:07,770
We have over random noise vector.

49
00:03:07,790 --> 00:03:15,680
This is a one dimensional 100 item feature vector, and we just feed that noise into the generator here

50
00:03:16,190 --> 00:03:20,520
and we get the image out of it, and we did not run this function above.

51
00:03:20,870 --> 00:03:23,470
Unfortunately, that's why we got the error.

52
00:03:23,530 --> 00:03:26,870
So now we can run this and get that nice image we saw before.

53
00:03:27,710 --> 00:03:31,310
So how close is this image to the amnestied?

54
00:03:31,310 --> 00:03:33,240
Is that not very close isn't.

55
00:03:33,800 --> 00:03:35,870
So we need to improve that generator.

56
00:03:36,260 --> 00:03:41,720
Remember when we created the model, it's initialized with random weights, so effectively it's a dumb

57
00:03:41,720 --> 00:03:43,760
model that hasn't learned anything yet.

58
00:03:44,450 --> 00:03:49,850
Next, we can create a function to define or create a discriminative model.

59
00:03:50,450 --> 00:03:54,020
So remember, the discriminator model takes an input image.

60
00:03:54,020 --> 00:04:01,070
That's why it takes an input image of this size and then uses a regular CNN network to classify whether

61
00:04:01,070 --> 00:04:04,490
that network, whether that image is real or fake.

62
00:04:04,700 --> 00:04:06,590
That's what it does, and that's what returns of.

63
00:04:06,590 --> 00:04:12,650
Basically, that's one which is a single binary node, so zero would be real, one would be fake or

64
00:04:12,650 --> 00:04:13,430
vice versa.

65
00:04:14,120 --> 00:04:15,470
So let's run that.

66
00:04:15,860 --> 00:04:17,420
So we've created that function.

67
00:04:17,960 --> 00:04:20,960
Now let's test our discriminator on this image.

68
00:04:21,350 --> 00:04:22,760
So remember this variable?

69
00:04:22,760 --> 00:04:24,980
This image was called generated image here.

70
00:04:26,120 --> 00:04:32,720
So let's feed that in now to our discriminative model, which we create here and get its decision.

71
00:04:32,900 --> 00:04:35,010
And you can see what it tells us.

72
00:04:35,540 --> 00:04:42,260
It gives us a value of point zero zero one, which means that it's a real image because we've trained

73
00:04:42,260 --> 00:04:47,430
it to basically output positive values for real images and negative values for fake images.

74
00:04:47,430 --> 00:04:52,970
So you can see it just I mean, this is a good untrained discriminator, so it's not really of any relevance,

75
00:04:52,970 --> 00:04:59,120
but at least you understand the outputs and inputs of these building blocks for our overall good.

76
00:04:59,810 --> 00:05:02,750
So now we can define the lost function and optimizer.

77
00:05:03,080 --> 00:05:06,830
So firstly, that both functions will be using its binary cross entropy.

78
00:05:07,430 --> 00:05:10,280
So let's create that next.

79
00:05:10,280 --> 00:05:12,980
We have to create a function for the discriminative loss.

80
00:05:13,430 --> 00:05:17,810
So remember, Discriminator is trying to determine whether an image is a fake.

81
00:05:18,260 --> 00:05:24,500
So that loss function also uses cross entropy here, and the inputs are the real output and the fake

82
00:05:24,500 --> 00:05:25,310
output here.

83
00:05:25,760 --> 00:05:28,910
And then it gets distorted instead of total loss right here.

84
00:05:30,350 --> 00:05:37,070
Next, we have a generator lost function, which quantifies how well it was able to trick the discriminator.

85
00:05:37,550 --> 00:05:44,210
So intuitively, if your generators performing well, the discriminator were classified real and fake

86
00:05:44,210 --> 00:05:47,060
images as real, meaning that the output won.

87
00:05:47,660 --> 00:05:49,390
So that's our aim for this.

88
00:05:49,430 --> 00:05:50,480
So let's create that.

89
00:05:50,480 --> 00:05:57,170
What generates a loss and then we declare optimizes this for both of them will be using Adam with very

90
00:05:57,170 --> 00:05:58,370
small lending rates here.

91
00:05:58,370 --> 00:06:01,220
This is one zero zero zero one for both.

92
00:06:01,490 --> 00:06:08,060
So let's create that and then we create our checkpoints here, so we'll be saving on model after every

93
00:06:08,060 --> 00:06:08,510
run.

94
00:06:09,110 --> 00:06:10,430
And let's do that.

95
00:06:10,820 --> 00:06:16,930
So we created our checkpoint using TensorFlow and then now we could create a shooting loop.

96
00:06:16,940 --> 00:06:21,020
So let's just set the parameters for our training experiment here.

97
00:06:22,010 --> 00:06:27,710
This is our noise dimensional vector, which is 100, which we saw above number of examples to generate

98
00:06:27,710 --> 00:06:30,980
an on a batch and the number of epochs here.

99
00:06:31,160 --> 00:06:37,060
So and we just set a random seed so we can recreate this after and use this to visualize that.

100
00:06:37,070 --> 00:06:40,100
Yet the animated GIF afterwards as well.

101
00:06:40,970 --> 00:06:45,920
Next, we're going to use gradient tip to basically execute our trading step.

102
00:06:46,310 --> 00:06:49,670
So you would have seen this similar in PI touch to PI touch.

103
00:06:49,670 --> 00:06:57,170
So reading where we have where we get a batch of data and then we just pass it to the generator here.

104
00:06:57,710 --> 00:06:59,630
So let's take a look at what's happening here.

105
00:06:59,720 --> 00:07:07,010
So we get a noise vector here just generated from one noise and to that which is 100 and the size.

106
00:07:07,460 --> 00:07:09,470
So we get enough noise for the batch at.

107
00:07:09,510 --> 00:07:17,760
You need then it gradient tube, which basically allows us to calculate degree and so on a step, so

108
00:07:17,760 --> 00:07:24,090
we have a generator here, so we give it a noise, we get the generated images outfit, then we give

109
00:07:24,090 --> 00:07:29,460
those images here, we'll give you the generated discriminator, some real images and then we give off

110
00:07:29,460 --> 00:07:32,400
a generator or discriminate to some generated images here.

111
00:07:32,850 --> 00:07:35,220
That's the real output and fake output here.

112
00:07:35,220 --> 00:07:38,160
And then we just get the loss from the generator here.

113
00:07:38,700 --> 00:07:40,170
So we get to fake output.

114
00:07:40,620 --> 00:07:46,530
So we get our generator loss and then we get our discriminator loss by comparing the real output to

115
00:07:46,530 --> 00:07:47,070
the fake.

116
00:07:47,070 --> 00:07:52,470
Up until now, remember, the generator needs to output the discriminator.

117
00:07:52,890 --> 00:07:55,980
That's why it takes to fake output here to train itself after.

118
00:07:55,980 --> 00:07:57,900
That's where it uses to this loss here.

119
00:07:58,710 --> 00:08:03,360
Then we use the gradient tip here to update the gradient for them, for the boat.

120
00:08:03,870 --> 00:08:07,440
And then we apply to gradients, to the new model.

121
00:08:07,950 --> 00:08:11,660
So that's our gradient, sort of that's our trailing step function there.

122
00:08:12,570 --> 00:08:15,120
Next, we can actually create a training loop now.

123
00:08:15,120 --> 00:08:22,890
So for the number of epochs in the box we have set, we get images out of it here and then we just actually

124
00:08:22,890 --> 00:08:24,810
we're producing a GIF as we treat it.

125
00:08:25,260 --> 00:08:32,340
So we use this generated to save images here, and we received a model every 15 epochs and print some

126
00:08:32,340 --> 00:08:38,940
stats where we train and then we'll clear the output so we can actually see the images as the train,

127
00:08:38,940 --> 00:08:41,700
as they progress to the training experiment.

128
00:08:41,790 --> 00:08:48,810
So it's good to babysit this training process here, so it's you can monitor and see whether your garden

129
00:08:48,810 --> 00:08:51,060
is doing well next.

130
00:08:51,090 --> 00:08:57,120
We can create that function that we use here generated to see if actually this should be above this.

131
00:08:57,780 --> 00:08:58,530
It's my bad.

132
00:08:59,460 --> 00:09:01,620
Let me just move this up again.

133
00:09:03,390 --> 00:09:06,030
So when you understand or the work?

134
00:09:08,250 --> 00:09:14,400
And no, we are ready to start training the model, so let's do this.

135
00:09:16,500 --> 00:09:22,140
Let's start training, so are we going to train for 50 epochs here and after each epoch, we should

136
00:09:22,140 --> 00:09:23,070
see the images of?

137
00:09:23,610 --> 00:09:25,470
So let's stay tuned for that.

138
00:09:26,580 --> 00:09:27,060
There we go.

139
00:09:27,060 --> 00:09:34,240
So we can see this is the results of our first epoch after it fell roughly nine seconds and it was a

140
00:09:34,260 --> 00:09:40,320
beating again every six seconds faster, though, so you can see, oh, it's getting better.

141
00:09:40,590 --> 00:09:44,880
You can definitely see that we have a five and a one.

142
00:09:44,910 --> 00:09:46,380
Let's see what happens in the next epoch.

143
00:09:46,980 --> 00:09:49,080
I definitely see a big improvement there.

144
00:09:49,290 --> 00:09:50,850
So let's keep what watching.

145
00:10:12,490 --> 00:10:13,950
OK, so we are done.

146
00:10:14,050 --> 00:10:18,960
And you can see after 50 books, the results are actually pretty good.

147
00:10:18,970 --> 00:10:26,020
I mean, some of these look don't look great like this one looks like an e or where it this one looks

148
00:10:26,020 --> 00:10:27,510
like a zero with a dot in it.

149
00:10:27,520 --> 00:10:33,310
But you can see generally it's actually learned to scan has learned to generate some pretty decent images.

150
00:10:33,520 --> 00:10:34,700
It's look OK here.

151
00:10:35,230 --> 00:10:37,930
This looks like a nine, but it's a bit misshapen.

152
00:10:38,530 --> 00:10:40,100
But I mean, this is pretty cool.

153
00:10:40,150 --> 00:10:46,750
You're using it to generate synthetic images and look almost quite real.

154
00:10:47,410 --> 00:10:49,420
This should get you quite excited, in my opinion.

155
00:10:50,260 --> 00:10:54,220
So now we can just restore the world's two largest and latest checkpoint.

156
00:10:54,220 --> 00:10:54,550
Sorry.

157
00:10:55,300 --> 00:10:59,320
And can you use this now to create a GIF?

158
00:10:59,500 --> 00:11:03,400
So let's create a GIF to show our progress.

159
00:11:05,410 --> 00:11:06,940
And then what we do?

160
00:11:07,000 --> 00:11:15,820
We use the image you function well library to create that gif that we're about to create from our training

161
00:11:15,820 --> 00:11:16,180
data.

162
00:11:16,180 --> 00:11:20,440
Because you can see here we have the image, see that every epoch.

163
00:11:20,980 --> 00:11:28,420
So let's close this and then we can generate this here, which I mean, you just so me the below.

164
00:11:28,420 --> 00:11:35,960
But let's do it again, because that was using a previously trained can that I use before we go.

165
00:11:35,980 --> 00:11:39,760
You can see it as we get closer than 50 books there.

166
00:11:40,780 --> 00:11:41,950
So this is pretty cool.

167
00:11:41,960 --> 00:11:48,280
I mean, what you can do, maybe experiment, maybe deepen the networks, add some more parameters.

168
00:11:49,330 --> 00:11:51,610
Next, you can probably trade for longer epochs.

169
00:11:52,150 --> 00:11:56,470
So this is a quite good lesson, and I hope you enjoyed this tutorial.

170
00:11:57,070 --> 00:12:03,370
You can take a look at the source for this tutorial here as well and hopefully enjoy the lesson.

171
00:12:03,370 --> 00:12:08,260
And what we'll do next will do a similar experiment in PyTorch.

172
00:12:08,380 --> 00:12:09,520
So stay tuned for that.
