Neural artistic style transfer experiments with Keras

Artistic style transfer using neural networks is a technique proposed by Gatys, Ecker and Bethge in the paper: arXiv:1508.06576 [cs.CV] which exploits a trained convolutional network in order to reconstruct the elements of a picture adopting the artistic style of a particular painting.

I've written a Python snippet (available in this GIST and at the end of this post) based on Keras and VGG16/19 convolutional networks, that can be used to perform some experiments. In fact, considering the huge number of variables and parameters, this kind of problems is very sensitive to the initial conditions and a different starting state can lead to different minima which content doesn't meet our requirements. In the script, it's possible to choose among six initial canvas types:

  • Random: RGB random pixels from a uniform distribution
  • Random from style: random pixels sampled from the painting
  • Random from picture: random pixels sampled from the picture
  • Style/Picture: Painting or picture full original content
  • Custom: picture/canvas loaded from disk

 

These different canvases can be used in (at least) three different "templates" (naming is mine), which values are also defined in the script:

  • Style and picture over random canvas: this approach uses a "random-from-style" canvas in order to rebuild all picture elements using a specific artistic style (shapes, traits and colors)
  •  Style over picture: starting from the picture, this method can be used to rebuild the original content keeping most of the shapes but adopting artistic traits and colors from a particular painting
  • Picture over style:  starting from the painting, this approach tries to merge both contents in an oniric fashion where the artistic style is predominant and the original picture drives a complex rearrangement of many painting shapes in order to let it accommodate its own main elements.

 

All the experiments have been performed using both a standard stochastic gradient descent (with momentum or RMSProp) approach and two different minimization algorithms: Limited memory BFGS (L-BFGS) and Conjugate gradient (CG). L-BFGS is the fastest but often drives to a premature precision loss (in particular with GPU Float 32 numbers). My favorite choice is CG but it's possible to try other algorithms already implemented by SciPy.

The convolutional network is VGG16 and the picture is always the same Tübingen panorama used in the paper and shown also in the Wikipedia page. "Alpha-style" and "Alpha-content" are the weights of painting and picture contents in the global loss function. Please, read the paper for further and more detailed information. VGG16 layers used for style loss evaluation are: block(1,2,3,4,5)_conv1. Even if there are no dimension constraints, all the images are 512 x 512 px, in order to speed up the process.

First experiment: V. Van Gogh, The starry night

Style and picture over random canvas (Alpha-style = 1.0, Alpha-content = 0.25, Picture layer = 'block4_conv1'):

Starry night

9 CG iterations with a total number of 4500 steps

Style over picture (Alpha-style = 0.0025, Alpha-content = 1.0, Picture layer = 'block4_conv1'):

Starry night 2

28 CG iterations with a total number of 13000 steps

Picture over style (Alpha-style = 0.001, Alpha-content = 1.0, Picture layer = 'block5_conv1'):

19 CG iterations with a total number of 3120 steps

Second experiment: G. Braque, Port en Normandie

Style and picture over random canvas (Alpha-style = 1.0, Alpha-content = 0.25, Picture layer = 'block4_conv1'):

Port 1

20 CG iterations with a total number of 6400 steps

Style over picture (Alpha-style = 0.0025, Alpha-content = 1.0, Picture layer = 'block4_conv1'):

Port 2

2 CG iterations with a total number of 320 steps

Picture over style (Alpha-style = 0.001, Alpha-content = 1.0, Picture layer = 'block5_conv1'):

Port 3

42 CG iterations with a total number of 2300 steps

Third experiment: S. Dalì, Ship with butterfly sails:

Style and picture over random canvas (Alpha-style = 1.0, Alpha-content = 0.25, Picture layer = 'block4_conv1'):

Ship 1

15 CG iterations with a total number of 2315 steps

Style over picture (Alpha-style = 0.0025, Alpha-content = 1.0, Picture layer = 'block4_conv1'):

Dalì 2

3 CG iterations with a total number of 1000 steps

Picture over style (Alpha-style = 0.001, Alpha-content = 1.0, Picture layer = 'block5_conv1'):

Dalì 3

15 CG iterations with a total number of 3600 steps

Source code (Python 2.7, Keras, SciPy):

Share it!
Aenean mattis venenatis
AI Software engineer, Data scientist and consultant

Leave a Reply