Home

Awesome

neural-dream

This is a PyTorch implementation of DeepDream. The code is based on neural-style-pt.

<div align="center"> <img src="https://raw.githubusercontent.com/ProGamerGov/neural-dream/master/examples/outputs/places365_big.png" width="710px"> </div>

Here we DeepDream a photograph of the Golden Gate Bridge with a variety of settings:

<div align="center"> <img src="https://raw.githubusercontent.com/ProGamerGov/neural-dream/master/examples/inputs/golden_gate.jpg" height="250px"> <img src="https://raw.githubusercontent.com/ProGamerGov/neural-dream/master/examples/outputs/goldengate_3a_5x5_reduce.png" height="250px"> <img src="https://raw.githubusercontent.com/ProGamerGov/neural-dream/master/examples/outputs/places205_4b_pool_proj.png" height="250px"> <img src="https://raw.githubusercontent.com/ProGamerGov/neural-dream/master/examples/outputs/places365_inception_4a_pool_proj.png" height="250px"> <img src="https://raw.githubusercontent.com/ProGamerGov/neural-dream/master/examples/outputs/goldengate_4d_5x5_s10.png" height="250px"> <img src="https://raw.githubusercontent.com/ProGamerGov/neural-dream/master/examples/outputs/goldengate_4d_3x3_reduce_avg10_lp4.png" height="250px"> </div>

Specific Channel Selection

You can select individual or specific combinations of channels.

Clockwise from upper left: 119, 1, 29, and all channels of the inception_4d_3x3_reduce layer

<div align="center"> <img src="https://raw.githubusercontent.com/ProGamerGov/neural-dream/master/examples/outputs/tubingen_c119.png" height="250px"> <img src="https://raw.githubusercontent.com/ProGamerGov/neural-dream/master/examples/outputs/tubingen_c1.png" height="250px"> <img src="https://raw.githubusercontent.com/ProGamerGov/neural-dream/master/examples/outputs/tubingen_4d_3x3_reduce_call.png" height="250px"> <img src="https://raw.githubusercontent.com/ProGamerGov/neural-dream/master/examples/outputs/tubingen_c29.png" height="250px"> </div>

Clockwise from upper left: 25, 108, 25 & 108, and 25 & 119 from the inception_4d_3x3_reduce layer

<div align="center"> <img src="https://raw.githubusercontent.com/ProGamerGov/neural-dream/master/examples/outputs/tubingen_c25.png" height="250px"> <img src="https://raw.githubusercontent.com/ProGamerGov/neural-dream/master/examples/outputs/tubingen_c108.png" height="250px"> <img src="https://raw.githubusercontent.com/ProGamerGov/neural-dream/master/examples/outputs/tubingen_c25_119.png" height="250px"> <img src="https://raw.githubusercontent.com/ProGamerGov/neural-dream/master/examples/outputs/tubingen_c25_108.png" height="250px"> </div>

Channel Selection Based On Activation Strength

You can select channels automatically based on their activation strength.

Clockwise from upper left: The top 10 weakest channels, the 10 most average channels, the top 10 strongest channels, and all channels of the inception_4e_3x3_reduce layer

<div align="center"> <img src="https://raw.githubusercontent.com/ProGamerGov/neural-dream/master/examples/outputs/tubingen_w10.png" height="250px"> <img src="https://raw.githubusercontent.com/ProGamerGov/neural-dream/master/examples/outputs/tubingen_avg10.png" height="250px"> <img src="https://raw.githubusercontent.com/ProGamerGov/neural-dream/master/examples/outputs/tubingen_4e_3x3_reduce_all.png" height="250px"> <img src="https://raw.githubusercontent.com/ProGamerGov/neural-dream/master/examples/outputs/tubingen_s10.png" height="250px"> </div>

Setup:

Dependencies:

Optional dependencies:

After installing the dependencies, you'll need to run the following script to download the BVLC GoogleNet model:

python models/download_models.py

This will download the original BVLC GoogleNet model.

If you have a smaller memory GPU then using the NIN Imagenet model could be an alternative to the BVLC GoogleNet model, though it's DeepDream quality is nowhere near that of the other models. You can get the details on the model from BVLC Caffe ModelZoo. The NIN model is downloaded when you run the download_models.py script with default parameters.

To download most of the compatible models, run the download_models.py script with following parameters:

python models/download_models.py -models all

You can find detailed installation instructions for Ubuntu and Windows in the installation guide.

Usage

Basic usage:

python neural_dream.py -content_image <image.jpg>

cuDNN usage with NIN Model:

python neural_dream.py -content_image examples/inputs/brad_pitt.jpg -output_image pitt_nin_cudnn.png -model_file models/nin_imagenet.pth -gpu 0 -backend cudnn -num_iterations 10 -seed 876 -dream_layers relu0,relu3,relu7,relu12 -dream_weight 10 -image_size 512 -optimizer adam -learning_rate 0.1

cuDNN NIN Model Picasso Brad Pitt

Note that paths to images should not contain the ~ character to represent your home directory; you should instead use a relative path or a full absolute path.

Options:

Optimization options:

Output options:

Layer options:

Channel options:

Octave options:

Laplacian Pyramid options:

Zoom options:

FFT options:

Tiling options:

GIF options:

Help options:

Other options:

Frequently Asked Questions

Problem: The program runs out of memory and dies

Solution: Try reducing the image size: -image_size 512 (or lower). Note that different image sizes will likely require non-default values for -octave_scale and -num_octaves for optimal results. If you are running on a GPU, you can also try running with -backend cudnn to reduce memory usage.

Problem: -backend cudnn is slower than default NN backend

Solution: Add the flag -cudnn_autotune; this will use the built-in cuDNN autotuner to select the best convolution algorithms.

Problem: Get the following error message:

Missing key(s) in state_dict: "classifier.0.bias", "classifier.0.weight", "classifier.3.bias", "classifier.3.weight". Unexpected key(s) in state_dict: "classifier.1.weight", "classifier.1.bias", "classifier.4.weight", "classifier.4.bias".

Solution: Due to a mix up with layer locations, older models require a fix to be compatible with newer versions of PyTorch. The included donwload_models.py script will automatically perform these fixes after downloading the models.

Problem: Get the following error message:

Given input size: (...). Calculated output size: (...). Output size is too small

Solution: Use a larger -image_size value and/or adjust the octave parameters so that the smallest octave size is larger.

Memory Usage

By default, neural-dream uses the nn backend for convolutions and Adam for optimization. These give good results, but can both use a lot of memory. You can reduce memory usage with the following:

With the default settings, neural-dream uses about 1.3 GB of GPU memory on my system; switching to cuDNN reduces the GPU memory footprint to about 1 GB.

Multi-GPU scaling

You can use multiple CPU and GPU devices to process images at higher resolutions; different layers of the network will be computed on different devices. You can control which GPU and CPU devices are used with the -gpu flag, and you can control how to split layers across devices using the -multidevice_strategy flag.

For example in a server with four GPUs, you can give the flag -gpu 0,1,2,3 to process on GPUs 0, 1, 2, and 3 in that order; by also giving the flag -multidevice_strategy 3,6,12 you indicate that the first two layers should be computed on GPU 0, layers 3 to 5 should be computed on GPU 1, layers 6 to 11 should be computed on GPU 2, and the remaining layers should be computed on GPU 3. You will need to tune the -multidevice_strategy for your setup in order to achieve maximal resolution.

We can achieve very high quality results at high resolution by combining multi-GPU processing with multiscale generation as described in the paper <a href="https://arxiv.org/abs/1611.07865">Controlling Perceptual Factors in Neural Style Transfer</a> by Leon A. Gatys, Alexander S. Ecker, Matthias Bethge, Aaron Hertzmann and Eli Shechtman.