Neural style simulation with tensorflow

I've been away from my blog a bit too long already, but well, the past is the past, and we can't change it. So let's focus on the present and future anyway! Today I wan't to give a try to the so called “neural style” concept, where we can input an image, and say a painting with a “given style”, and get that style applied on the input image.

  • The first ste we take is to download the reference weights from imagenet-vgg-verydeep-19.mat
  • Also we need to ensure we have the python packages: numpy, scipy, Pillow, matplotlib
  • So I built a minimal test front end, which can be executed with my custom command:
    nv_style_transfer --content images/tubingen.jpg --style images/starry-night.jpg --output result.jpg
  • Unfortunately, the first result I got was the exception:
        import utils
      File "/mnt/array1/dev/projects/NervSeed/python/apps/deep_learning/style_transfer/", line 3, in <module>
        import matplotlib.pyplot as plt
      File "/mnt/array1/dev/projects/NervSeed/tools/linux/python-3.6/lib/python3.6/site-packages/matplotlib/", line 113, in <module>
        _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
      File "/mnt/array1/dev/projects/NervSeed/tools/linux/python-3.6/lib/python3.6/site-packages/matplotlib/backends/", line 60, in pylab_setup
        [backend_name], 0)
      File "/mnt/array1/dev/projects/NervSeed/tools/linux/python-3.6/lib/python3.6/site-packages/matplotlib/backends/", line 6, in <module>
        from six.moves import tkinter as Tk
      File "/mnt/array1/dev/projects/NervSeed/tools/linux/python-3.6/lib/python3.6/site-packages/", line 92, in __get__
        result = self._resolve()
      File "/mnt/array1/dev/projects/NervSeed/tools/linux/python-3.6/lib/python3.6/site-packages/", line 115, in _resolve
        return _import_module(self.mod)
      File "/mnt/array1/dev/projects/NervSeed/tools/linux/python-3.6/lib/python3.6/site-packages/", line 82, in _import_module
      File "/mnt/array1/dev/projects/NervSeed/tools/linux/python-3.6/lib/python3.6/tkinter/", line 36, in <module>
        import _tkinter # If this fails your Python may not be configured for Tk
    ModuleNotFoundError: No module named '_tkinter'
  • Now trying to get this Tk dependency… But, unfortunately, it seems this is something we should build python with.
  • Now trying to see if we can remove this dependency usage… OK actually the matplotlib dependency is only used to display the images for debugging, so I simply commented this module import in the file and also the plot_images function in that same file. Then the app seems to run as expected and I can see the training process going on. And finally I got my first style transfer result image :-)


⇒ I know I know, that one is pretty standard result ;-) So then I moved to more personal content (ie. family picture) with a custom painting style.

  • I first tried with the command:
    nv_style_transfer --max_size 1920 --content /mnt/array1/dev/data/deep_learning/style_transfer/content/famille_cap_antibes.jpg --style /mnt/array1/dev/data/deep_learning/style_transfer/styles/colorful_sunrise.jpg --output /mnt/array1/dev/data/deep_learning/style_transfer/family_colorful_sunrise.jpg
  • ⇒ unfortunately, this didn't work on my dual 1070 config: I run out of GPU memory:-( So it seems generating 1080p images is still a bit out of reach for me… Maybe if I try on my GTX 1080 instead ?
  • So now trying with 720p output images:
    nv_style_transfer --max_size 1280 --content /mnt/array1/dev/data/deep_learning/style_transfer/content/famille_cap_antibes.jpg --style /mnt/array1/dev/data/deep_learning/style_transfer/styles/colorful_sunrise.jpg --output /mnt/array1/dev/data/deep_learning/style_transfer/family_colorful_sunrise.jpg
  • And the first “custom result” I got didn't look so great:


  • Note that the style for that experiment came from the painting below:


  • Not quite a wonderfull success, isn't it ?… Yet, the result above was achieved with only 1000 training iterations, and the loss was still going “largely down” when the training was stopped. So I decided to give it another try, this time with 5000 iterations instead to see if this could help producing a more convincing style transfer image :-)
  • The second result I got with those 5000 iterations was:


⇒ Still not quite an amazing image, but at least some of the small artifacts from the previous rendering were removed…

  • At that point I realized that the training was taking soooo long because… my GPUs were actually busy mining crypto currencies at the same time !!! OOooops… ;-) So, maybe I can in fact produce 1080p images afterall ? Anyway, I decided to stick with 720p resolution and instead try with another painting style, which was the following:


  • And the result I got (this time with +6000 iterations) was:


⇒ Again, the result is “nice” but still not really terrific… In fact I'm starting “to feel” that what we really need as a “style” image is not really a “colorfull” painting but instead something with very specific “shapes” instead. So let's try to find that now.

  • I found this abstract painting, which I decided to use:


  • And the result, with “only” 3000 iterations seems much more convincing to me (even if we still have a few artifacts… But I'm sure this can be fixed with some more training):


By the way, I also tried once more to generate a 1080p image [with mining stopped this time!] but unfortunately I still get the same “out of GPU memory” error.
  • As mentioned in the reference section above, there is actually a fast style transfer implementation that can apparently produce similar results much faster. I should definitely give this a try one day… But not today ;-).
  • blog/2019/0210_neural_style.txt
  • Last modified: 2020/07/10 12:11
  • by