Hi and welcome back in this lesson, we'll be using Harris to implement a simple again something called
a D.C. gun, which is short for a deep convolution gun and will be using it on the amnesty to set.
So we're going to generate synthetic samples of that data set.
So it's a very good instructional lesson on guns.
So let's get started.
So open notebook, too, the one here and we can begin to listen.
So firstly, let's take a look at what we're doing.
So let's do a quick recap on guns.
Remember, what we have here is a generative network, and it discriminates in that we have two main
components.
The generator generates fake images, and it is committed to try and tell the difference between real
images and fake images.
And then it uses that to update its wits.
And then the generator sends more images again, more a more mixed images dataset here to that discriminator
with the frozen widths, and then he uses that knowledge.
The results of that two updated tweets so that it can then in turn, keep improving.
So this is how we train again.
So let's get started.
So first, let's download and install some of the packages.
We need it.
So we'll do that now shouldn't take too long so we can move on while we do that.
Next, import of libraries that we will be using, and then we can go on and loading and preparing our
data sets, including the amnesty to set.
Here we get the tree in images, tree and labels.
Then we reshape and normalize the data here, so we normalize it between minus one and one and converting
it to a float to two.
We set up a batch and buffer sized buffer.
Size is the number of images in the training dataset.
60000 to that size is 256.
And then we just create this dataset to see dataset load effectively.
That gives us a batch of data from that that that input that training data we have.
So let's run this.
And it should finish shortly.
There we go.
So now we can define what generates a model.
Now remember, the generator model takes random noise as an input and generates a synthetic output from
it.
So to do that, basically, we just have the input here, input chip being some random noise vector
and then which is the densely here.
So we have a fully connected dense layer.
However, this then goes to convert to the transpose, which is an upsampling layer, so you can see
that it up samples.
It gradually took the network so that the outputs final size is a 28 by 28 image.
That's the generator network to remember.
It's generating synthetic images so we can take a look at how generated network looks.
You can just use this function that we defined to make code generator model and returns the model here.
So we have generator here.
We have over random noise vector.
This is a one dimensional 100 item feature vector, and we just feed that noise into the generator here
and we get the image out of it, and we did not run this function above.
Unfortunately, that's why we got the error.
So now we can run this and get that nice image we saw before.
So how close is this image to the amnestied?
Is that not very close isn't.
So we need to improve that generator.
Remember when we created the model, it's initialized with random weights, so effectively it's a dumb
model that hasn't learned anything yet.
Next, we can create a function to define or create a discriminative model.
So remember, the discriminator model takes an input image.
That's why it takes an input image of this size and then uses a regular CNN network to classify whether
that network, whether that image is real or fake.
That's what it does, and that's what returns of.
Basically, that's one which is a single binary node, so zero would be real, one would be fake or
vice versa.
So let's run that.
So we've created that function.
Now let's test our discriminator on this image.
So remember this variable?
This image was called generated image here.
So let's feed that in now to our discriminative model, which we create here and get its decision.
And you can see what it tells us.
It gives us a value of point zero zero one, which means that it's a real image because we've trained
it to basically output positive values for real images and negative values for fake images.
So you can see it just I mean, this is a good untrained discriminator, so it's not really of any relevance,
but at least you understand the outputs and inputs of these building blocks for our overall good.
So now we can define the lost function and optimizer.
So firstly, that both functions will be using its binary cross entropy.
So let's create that next.
We have to create a function for the discriminative loss.
So remember, Discriminator is trying to determine whether an image is a fake.
So that loss function also uses cross entropy here, and the inputs are the real output and the fake
output here.
And then it gets distorted instead of total loss right here.
Next, we have a generator lost function, which quantifies how well it was able to trick the discriminator.
So intuitively, if your generators performing well, the discriminator were classified real and fake
images as real, meaning that the output won.
So that's our aim for this.
So let's create that.
What generates a loss and then we declare optimizes this for both of them will be using Adam with very
small lending rates here.
This is one zero zero zero one for both.
So let's create that and then we create our checkpoints here, so we'll be saving on model after every
run.
And let's do that.
So we created our checkpoint using TensorFlow and then now we could create a shooting loop.
So let's just set the parameters for our training experiment here.
This is our noise dimensional vector, which is 100, which we saw above number of examples to generate
an on a batch and the number of epochs here.
So and we just set a random seed so we can recreate this after and use this to visualize that.
Yet the animated GIF afterwards as well.
Next, we're going to use gradient tip to basically execute our trading step.
So you would have seen this similar in PI touch to PI touch.
So reading where we have where we get a batch of data and then we just pass it to the generator here.
So let's take a look at what's happening here.
So we get a noise vector here just generated from one noise and to that which is 100 and the size.
So we get enough noise for the batch at.
You need then it gradient tube, which basically allows us to calculate degree and so on a step, so
we have a generator here, so we give it a noise, we get the generated images outfit, then we give
those images here, we'll give you the generated discriminator, some real images and then we give off
a generator or discriminate to some generated images here.
That's the real output and fake output here.
And then we just get the loss from the generator here.
So we get to fake output.
So we get our generator loss and then we get our discriminator loss by comparing the real output to
the fake.
Up until now, remember, the generator needs to output the discriminator.
That's why it takes to fake output here to train itself after.
That's where it uses to this loss here.
Then we use the gradient tip here to update the gradient for them, for the boat.
And then we apply to gradients, to the new model.
So that's our gradient, sort of that's our trailing step function there.
Next, we can actually create a training loop now.
So for the number of epochs in the box we have set, we get images out of it here and then we just actually
we're producing a GIF as we treat it.
So we use this generated to save images here, and we received a model every 15 epochs and print some
stats where we train and then we'll clear the output so we can actually see the images as the train,
as they progress to the training experiment.
So it's good to babysit this training process here, so it's you can monitor and see whether your garden
is doing well next.
We can create that function that we use here generated to see if actually this should be above this.
It's my bad.
Let me just move this up again.
So when you understand or the work?
And no, we are ready to start training the model, so let's do this.
Let's start training, so are we going to train for 50 epochs here and after each epoch, we should
see the images of?
So let's stay tuned for that.
There we go.
So we can see this is the results of our first epoch after it fell roughly nine seconds and it was a
beating again every six seconds faster, though, so you can see, oh, it's getting better.
You can definitely see that we have a five and a one.
Let's see what happens in the next epoch.
I definitely see a big improvement there.
So let's keep what watching.
OK, so we are done.
And you can see after 50 books, the results are actually pretty good.
I mean, some of these look don't look great like this one looks like an e or where it this one looks
like a zero with a dot in it.
But you can see generally it's actually learned to scan has learned to generate some pretty decent images.
It's look OK here.
This looks like a nine, but it's a bit misshapen.
But I mean, this is pretty cool.
You're using it to generate synthetic images and look almost quite real.
This should get you quite excited, in my opinion.
So now we can just restore the world's two largest and latest checkpoint.
Sorry.
And can you use this now to create a GIF?
So let's create a GIF to show our progress.
And then what we do?
We use the image you function well library to create that gif that we're about to create from our training
data.
Because you can see here we have the image, see that every epoch.
So let's close this and then we can generate this here, which I mean, you just so me the below.
But let's do it again, because that was using a previously trained can that I use before we go.
You can see it as we get closer than 50 books there.
So this is pretty cool.
I mean, what you can do, maybe experiment, maybe deepen the networks, add some more parameters.
Next, you can probably trade for longer epochs.
So this is a quite good lesson, and I hope you enjoyed this tutorial.
You can take a look at the source for this tutorial here as well and hopefully enjoy the lesson.
And what we'll do next will do a similar experiment in PyTorch.
So stay tuned for that.