Application
The propose of this app is that base on two image , we'll be able to create a new image with some AI artwork.
The first image will be the base image, meanwhile the second image is going to be the img that will give the style that we want to apply .
Finally, we will have our third image that will be generated, which we initialiazed with random color values. This image will change as we minimize the content and style of loss functions.
We define all the libraries that we are going to use.
Now we need to define the paths of the images, and then we are going to initialize gIm0 as flota64 for optimization purpose later on.
And also we keep cImArr and sImArr as float32 to avoid GPU memory erros.
Content Loss
This is in charge to make sure that the generated image x retains some of the global characterisitcs of the content image, p.
Lets image, we want to make sure that the new image look like our base image, This means that we want to respect some characteristics so it could be recognizable. To achieve this, the contect loss function is defined as the mean squared error between the feature representation of p and x, respectively, at a given layer l.
Style Loss
The style loss help us to preserve stylistic characteristics of the style image, base. Rather than using the difference between feature representations, in this case we use a Gram matrix form selected layers.
The Gram matrix is a square matrix that contains the dot products between each vectorized filter in layer l. The Gram matrix can therefore be thought of as non-normalized correlation matrix for filters in layer l.
Ascending layers in most convolutional networks such as VGG have increasingly larger receptive fields. As this receptive field grows, more large-scale characteristics of the input image are preserved. Because of this, multiple layers should be selected for “style” to incorporate both local and global stylistic qualities. To create a smooth blending between these different layers, we can assign a weight w to each layer.
Lastly, we just need to assign weighting coefficients to each of the content and style loss respectively.
Implementation
For changing our generated image to minimize the loss function, we have to define two more functions to use scipy and the keras backend. First, a function to calculate the total loss, and second, a function to calculate the gradient. Both of these get fed as input to a scipy optimization function as the objective and gradient functions respectively. Here, we use the limited-memory BFGS algorithm.
For each of the content and style images, we extract the feature representations to construct P and A (for each selected style layer), and weight the style layers uniformly. In practice, using > 500 iterations of L-BFGS-B typically creates convincing visualizations.
Lets clarify that there are many parameters that could be change , depends on the requirements or also in our knowledge that we have, we could modify an create more layers, or change float, etc.
Last updated