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 and the corresponding class names. This requires the Deep Learning Toolbox™ Model for GoogLeNet Network support package. If this support package is not installed, then the software provides a download link. For a list of all available networks, see Pretrained Deep Neural Networks.
[net,classNames] = imagePretrainedNetwork("googlenet");
Generate Image
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 analyzeNetwork
.
analyzeNetwork(net)
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];
You can view the names of the selected classes by selecting the entries in channels
.
classNames(channels)
ans = 6×1 string
"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 Computing Requirements (Parallel Computing Toolbox). To achieve more vibrant images, clip the images.
I = deepDreamImage(net,layerName,channels,OutputScaling="clipped");
|==============================================| | Iteration | Activation | Pyramid Level | | | Strength | | |==============================================| | 1 | 0.59 | 1 | | 2 | 1.24 | 1 | | 3 | 4.56 | 1 | | 4 | 7.37 | 1 | | 5 | 11.30 | 1 | | 6 | 13.99 | 1 | | 7 | 16.75 | 1 | | 8 | 19.87 | 1 | | 9 | 23.71 | 1 | | 10 | 28.93 | 1 | | 1 | 6.66 | 2 | | 2 | 9.83 | 2 | | 3 | 15.96 | 2 | | 4 | 14.49 | 2 | | 5 | 19.06 | 2 | | 6 | 22.67 | 2 | | 7 | 24.10 | 2 | | 8 | 24.78 | 2 | | 9 | 27.90 | 2 | | 10 | 32.13 | 2 | | 1 | 34.09 | 3 | | 2 | 40.02 | 3 | | 3 | 50.95 | 3 | | 4 | 50.22 | 3 | | 5 | 50.22 | 3 | | 6 | 54.28 | 3 | | 7 | 62.82 | 3 | | 8 | 70.92 | 3 | | 9 | 66.08 | 3 | | 10 | 75.71 | 3 | |==============================================| Training finished: Max epochs completed.
Display all the images together using imtile
.
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, ... OutputScaling="clipped"); 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)
See Also
imagePretrainedNetwork
| dlnetwork
| trainingOptions
| trainnet
| deepDreamImage
| occlusionSensitivity
| imageLIME
| gradCAM