The cost function of the neural style transfer algorithm had 2
a content cost component and a style cost component. 3
Let's start by defining the content cost component. 4
Remember that this is the overall cost function of the neural style transfer algorithm. 5
So, let's figure out what should the content cost function be. 6
Let's say that you use hidden layer l to compute the content cost. 7
If l is a very small number, if you use hidden layer one, 8
then it will really force your generated image 9
to pixel values very similar to your content image. 10
Whereas, if you use a very deep layer, 11
then it's just asking, "Well, 12
if there is a dog in your content image, 13
then make sure there is a dog somewhere in your generated image. " 14
So in practice, layer l chosen somewhere in between. 15
It's neither too shallow nor too deep in the neural network. 16
And because you plan this yourself, 17
in the problem exercise that you did at the end of this week, 18
I'll leave you to gain some intuitions with 19
the concrete examples in the problem exercise as well. 20
But usually, I was chosen to be somewhere 21
in the middle of the layers of the neural network, 22
neither too shallow nor too deep. 23
What you can do is then use a pre-trained ConvNet, 24
maybe a VGG network, 25
or could be some other neural network as well. 26
And now, you want to measure, 27
given a content image and given a generated image, 28
how similar are they in content. 29
So let's let this 30
a_superscript_[l](c) and this be the activations of layer l on these two images, 31
on the images C and G. So, 32
if these two activations are similar, 33
then that would seem to imply that both images have similar content. 34
So, what we'll do is define 35
J_content(C,G) as just how 36
soon or how different are these two activations. 37
So, we'll take the element-wise difference between 38
these hidden unit activations in layer l, 39
between when you pass in the content image compared 40
to when you pass in the generated image, 41
and take that squared. 42
And you could have a normalization constant in front or not, 43
so it's just one of the two or something else. 44
It doesn't really matter since this can be adjusted as well by this hyperparameter alpha. 45
So, just be clear 46
on using this notation as if both of these have been unrolled into vectors, 47
so then, this becomes the square root of the l_2 norm between this and this, 48
after you've unrolled them both into vectors. 49
There's really just the element-wise sum of 50
squared differences between these two activation. 51
But it's really just the element-wise sum of 52
squares of differences between the activations in layer l, 53
between the images in C and G. And so, 54
when later you perform gradient descent on J_of_G to try to find a value of G, 55
so that the overall cost is low, 56
this will incentivize the algorithm to find an image G, 57
so that these hidden layer activations are similar to what you got for the content image. 58
So, that's how you define the content cost function for the neural style transfer. 59