Google’s Deep Dream in PyCharm

Happy Friday everyone,

Reading the subject of this blog post I hope you’re all ready to have some fun! A month ago, Google released the code in an IPython Notebook letting everyone experiment with neural networks, image recognition algorithms and techniques as described in their Inceptionism: Going Deeper into Neural Networks article. Neural networks are known for their ability to recognize different shapes, forms, sophisticated objects, and even people’s faces. Well, Google engineers used an upside-down approach: you show different images to a pre-trained neural network and let it draw what it sees on the images, with the ultimate goal of generating new creative imagery based on artificial intelligence!

deepdream0

The example code for image generation kindly shared by Google is available as an IPython Notebook on Google’s GitHub repository. I couldn’t miss the chance to try it in PyCharm. PyCharm natively supports IPython Notebook since v4 as described in this blog post. In the latest v4.5, this support is even better and can be applied to many areas, including quantitative research as described here.

While Google’s code example relies on as few dependencies as possible, several things still need to be installed first. I won’t describe the whole process of installing all the dependencies as they slightly differ across various platforms, but I’ll show you how PyCharm helps you along the way.

First, you need to install PyCharm from the official website. Both Professional and Community editions natively support IPython Notebook. Next, you’ll need to get the deepdream code from the Google’s GitHub repository. That’s a very simple task as you can get it automatically from the PyCharm’s welcome screen:

deepdream1

You can simply enter the repo URL (https://github.com/google/deepdream) in the dialog and PyCharm will clone the project for you:

deepdream2_1

After cloning, PyCharm will offer you to open the newly imported project, so here we go:

deepdream3

Now let’s take care of the dependencies. For a clean installation, I recommend creating a fresh Python virtual environment, and this is another case where PyCharm helps us. Go to Settings (Preferences for Mac users) | Project | Project Interpreter and add a new virtual environment as shown on the screenshot:

deepdream4

You’ll get a clean Python virtual environment with only a few standard packages inside. As you can see on the next screenshot, PyCharm highlights the missing dependencies in the code:

deepdream5

Place the caret on the missing dependency, use the so-called quick-fix action with Alt+Enter, and PyCharm will offer you to install it:

deepdream6

In a similar manner, install the other dependencies mentioned in the first cell of the IPython Notebook and highlighted by PyCharm. Alternatively, you can go back to Settings | Project Interpreter and install everything using the built-in package manager:

deepdream7

Note: As an option, you can choose to install the Anaconda or Canopy packages which include the superset of the scientific stack comprising most of the mentioned libraries as recommended by Google engineers. These packages are really huge and bloat your environment, so I prefer to install only what’s needed and have a minimal virtual environment.

The most difficult part is to install the last but most important dependency—the Caffe deep learning framework, developed by the Berkeley Vision and Learning Center. The installation process deserves a post of its own as it’s long and complicated. Nor PyCharm neither any other tool can help here, as this framework isn’t available on any package repository. Anaconda and Canopy don’t have Caffe on board either. So you have to install it manually. The installation guide can be found on the BVLC official website. It’s pretty descriptive and I hope you won’t meet any problems installing Caffe along the way.

The only thing I’d like to note here is that Caffe can be installed with or without the CUDA library and driver. CUDA installation is huge and will consume a lot of time, so if you want to get quick results, install Caffe without CUDA. But if you need maximum performance and don’t want to spend extra time waiting for image generation, installing CUDA is the way to go.

Finally, when you’re done installing all the dependencies we’re ready to go and have some fun. You can start with running cells one by one inside PyCharm clicking the green “Run” button. On the first cell with Python code, PyCharm will automatically offer you to start the IPython Notebook kernel:

deepdream8

Make sure you’re following the code and substitute the required paths to Caffe models and images:

deepdream9

The standard installation of Caffe have the bvlc_googlenet model already pre-installed. You only need to specify the path to your Caffe installation. Later, you can play with other models, trained on different image sets that can be found in the BVLC official github repository.

Going cell by cell in the example IPython Notebook, you can generate images from the standard pictures that come with the example project:

deepdream10

After you’re done with the default examples, it’s time to do something really fun and weird. Experiment with your own pictures and code modifications!

I decided to find out what the neural network would see on the PyCharm logo. I added the logo to the project and changed the code:

deepdream11

And this was the output:

deepdream12

Weird and funky, isn’t it? But wait, that’s just the beginning. Let’s go further and write some custom code. When modifying the code, PyCharm helps you with autocompletion:

deepdream13

It also helps with function parameters:

deepdream14

I involved a different layer which is responsible for image actualization enhancing edges and textures, giving the image an impressionist feeling:

deepdream15

The code authors encourage readers to experiment with layer selection to see how it affects the results. There’s definitely lots of room to explore the design!

Note: You can get the list of layers by executing the net.blobs.keys() code.

The real fun starts when we start feeding a neural network its own output. This leads to very unusual and weird results. You can see true dreams of a neural network and art generated by artificial intelligence.

Executing the following code we produce a number of frames. When transformed slightly, each of them becomes an input on the next cycle of neural network dreaming:

Here is the original image:

pycharm

And here is what we can see on frame #8:

deepdream17

Is it a dog appearing out of PyCharm’s logo snake’s head?

And on frame #14 I can see a frog’s head on a bird:

deepdream18

The further you go, the more interesting results you get. Here’s frame #26:

deepdream19

In frame #49, there’s something completely bizarre:

deepdream20

This technique of applying different layers using different image sequences leads to the ultimately infinite number of results and filters we can use to produce and modify images.

The authors of this idea encourage you to play with the code, generate your own images, and post them to Google+, Facebook or Twitter. Be sure to tag them with #deepdream so other researchers can check them out too. Also, use the #pycharm hashtag if you like this post and if PyCharm helps you create your own spectacular pictures.

I hope you enjoyed this fun experiment with Google deepdreams and PyCharm.

Talk to you soon!
-Dmitry

About Dmitry Filippov

Product Marketing Manager at JetBrains
This entry was posted in Cool Feature and tagged , , , . Bookmark the permalink.

9 Responses to Google’s Deep Dream in PyCharm

  1. Stu says:

    I had a bit of a play with it, but got really sick of dog-slugs – it’d be awesome if you could show using some different models and making some non-dogslug type images.

  2. Nice post Dmitry! I think you might also like the bat-country Python package for deep dreaming. It’s takes much of the Google code, makes it more Pythonic and extendible, and more like a real module.

  3. Christopher says:

    Groovy. By far some of the most bizarre python code I’ve ever seen

  4. Jay says:

    Wow. Awesome work and totally amazing. I’ve read quite a bit about the algorithm etc which Google released. I have a question what level of programming would be required to utilize the source codes and experiment like you have?

    Kind regards

    Jay

  5. J.T. says:

    You all need to turn frame #8 into a t-shirt with “tools matter”!

  6. MM says:

    I’ve been successful compiling Caffe from this guide
    https://gist.github.com/robertsdionne/f58a5fc6e5d1d5d2f798
    but I can’t get PyCharm to see the “module” created in the caffe/distribute/python directory.
    (this directory is in my PYTHONPATH from the virtualenv/command line, but I can’t run the notebook in the IDE)
    Am I missing something?

    • Dmitry Filippov says:

      Please run PyCharm with PYTHONPATH pointing to your Caffe installation. For example in linux it would be
      #export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH
      #/path/to/pycharm/pycharm.sh

  7. Pingback: DEEP LEARNING REVOLUTION, summer 2015, state of the art & topnotch links | Turing Machine

  8. asad says:

    This blog awesome and i learn a lot about programming from here.The best thing about this blog is that you doing from beginning to experts level.

Leave a Reply

Your email address will not be published. Required fields are marked *