📚 The CoCalc Library - books, templates and other resources
License: OTHER
Deep Dreams (with Caffe)
Credits: Forked from DeepDream by Google
This notebook demonstrates how to use the Caffe neural network framework to produce "dream" visuals shown in the Google Research blog post.
It'll be interesting to see what imagery people are able to generate using the described technique. If you post images to Google+, Facebook, or Twitter, be sure to tag them with #deepdream so other researchers can check them out too.
##Dependencies This notebook is designed to have as few dependencies as possible:
Standard Python scientific stack: NumPy, SciPy, PIL, IPython. Those libraries can also be installed as a part of one of the scientific packages for Python, such as Anaconda or Canopy.
Caffe deep learning framework (installation instructions).
Google protobuf library that is used for Caffe model manipulation.
Loading DNN model
In this notebook we are going to use a GoogLeNet model trained on ImageNet dataset. Feel free to experiment with other models from Caffe Model Zoo. One particularly interesting model was trained in MIT Places dataset. It produced many visuals from the original blog post.
Producing dreams
Making the "dream" images is very simple. Essentially it is just a gradient ascent process that tries to maximize the L2 norm of activations of a particular DNN layer. Here are a few simple tricks that we found useful for getting good images:
offset image by a random jitter
normalize the magnitude of gradient ascent steps
apply ascent across multiple scales (octaves)
First we implement a basic gradient ascent step function, applying the first two tricks:
Next we implement an ascent through different scales. We call these scales "octaves".
Now we are ready to let the neural network reveal its dreams! Let's take a cloud image as a starting point:
Running the next code cell starts the detail generation process. You may see how new patterns start to form, iteration by iteration, octave by octave.
The complexity of the details generated depends on which layer's activations we try to maximize. Higher layers produce complex features, while lower ones enhance edges and textures, giving the image an impressionist feeling:
We encourage readers to experiment with layer selection to see how it affects the results. Execute the next code cell to see the list of different layers. You can modify the make_step
function to make it follow some different objective, say to select a subset of activations to maximize, or to maximize multiple layers at once. There is a huge design space to explore!
What if we feed the deepdream
function its own output, after applying a little zoom to it? It turns out that this leads to an endless stream of impressions of the things that the network saw during training. Some patterns fire more often than others, suggestive of basins of attraction.
We will start the process from the same sky image as above, but after some iteration the original image becomes irrelevant; even random noise can be used as the starting point.
Be careful running the code above, it can bring you into very strange realms!
Controlling dreams
The image detail generation method described above tends to produce some patterns more often the others. One easy way to improve the generated image diversity is to tweak the optimization objective. Here we show just one of many ways to do that. Let's use one more input image. We'd call it a "guide".
Note that the neural network we use was trained on images downscaled to 224x224 size. So high resolution images might have to be downscaled, so that the network could pick up their features. The image we use here is already small enough.
Now we pick some target layer and extract guide image features.
Instead of maximizing the L2-norm of current image activations, we try to maximize the dot-products between activations of current image, and their best matching correspondences from the guide image.
This way we can affect the style of generated images without using a different training set.