Deep Dream Images Using GoogLeNet
This example shows how to generate images using
deepDreamImage with the pretrained convolutional neural network GoogLeNet.
Deep Dream is a feature visualization technique in deep learning that synthesizes images that strongly activate network layers. By visualizing these images, you can highlight the image features learned by a network. These images are useful for understanding and diagnosing network behavior.
You can generate interesting images by visualizing the features of the layers towards the end of the network.
The example uses Deep Learning Toolbox™ and Deep Learning Toolbox Model for GoogLeNet Network to generate the images.
Load Pretrained Network
Load a pretrained GoogLeNet Network. If the Deep Learning Toolbox Model for GoogLeNet Network support package is not installed, then the software provides a download link.
net = googlenet;
To produce images that resemble a given class the most closely, select the fully connected layer. First, locate the layer index of this layer by viewing the network architecture using
Then select the fully connected layer, in this example, 142.
layer = 142; layerName = net.Layers(layer).Name
layerName = 'loss3-classifier'
You can generate multiple images at once by selecting multiple classes. Select the classes you want to visualize by setting
channels to be the indices of those class names.
channels = [114 293 341 484 563 950];
The classes are stored in the
Classes property of the output layer (the last layer). You can view the names of the selected classes by selecting the entries in
ans = 6×1 categorical snail tiger zebra castle fountain strawberry
Generate the images using
deepDreamImage. This command uses a compatible GPU, if available. Otherwise it uses the CPU. Using a GPU requires Parallel Computing Toolbox™ and a supported GPU device. For information on supported devices, see GPU Support by Release (Parallel Computing Toolbox).
I = deepDreamImage(net,layerName,channels);
|==============================================| | Iteration | Activation | Pyramid Level | | | Strength | | |==============================================| | 1 | 0.01 | 1 | | 2 | 1.47 | 1 | | 3 | 3.63 | 1 | | 4 | 7.24 | 1 | | 5 | 10.63 | 1 | | 6 | 15.78 | 1 | | 7 | 19.05 | 1 | | 8 | 24.21 | 1 | | 9 | 27.25 | 1 | | 10 | 29.49 | 1 | | 1 | 7.93 | 2 | | 2 | 10.10 | 2 | | 3 | 14.41 | 2 | | 4 | 20.48 | 2 | | 5 | 17.10 | 2 | | 6 | 23.32 | 2 | | 7 | 27.97 | 2 | | 8 | 25.79 | 2 | | 9 | 30.26 | 2 | | 10 | 35.68 | 2 | | 1 | 33.57 | 3 | | 2 | 42.50 | 3 | | 3 | 49.39 | 3 | | 4 | 58.22 | 3 | | 5 | 58.82 | 3 | | 6 | 52.32 | 3 | | 7 | 67.45 | 3 | | 8 | 68.73 | 3 | | 9 | 75.19 | 3 | | 10 | 68.91 | 3 | |==============================================| Training finished: Max epochs completed.
Display all the images together using
figure I = imtile(I); imshow(I)
Generate More Detailed Images
Increasing the number of pyramid levels and iterations per pyramid level can produce more detailed images at the expense of additional computation.
You can increase the number of iterations using the
'NumIterations' option. Set the number of iterations to 100.
iterations = 100;
Generate a detailed image that strongly activates the 'tiger' class (channel 293). Set
'Verbose' to false to suppress detailed information on the optimization process.
channels = 293; I = deepDreamImage(net,layerName,channels, ... 'Verbose',false, ... 'NumIterations',iterations); figure imshow(I)
To produce larger and more detailed output images, you can increase both the number of pyramid levels and iterations per pyramid level.
Set the number of pyramid levels to 4.
levels = 4;
Generate a detailed image that strongly activates the 'castle' class (channel 484).
channels = 484; I = deepDreamImage(net,layerName,channels, ... 'Verbose',false, ... 'NumIterations',iterations, ... 'PyramidLevels',levels); figure imshow(I)