Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Deep Dream Images Using AlexNet

This example shows how to generate images using deepDreamImage with the pretrained convolutional neural network AlexNet.

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 Neural Network Toolbox™, Neural Network Toolbox Model for AlexNet Network to generate the images. Optionally, you can use Image Processing Toolbox™ to display multiple images together.

Load Pretrained Network

Load pretrained AlexNet Network (Neural Network Toolbox for AlexNet Model). If Neural Network Toolbox Model for AlexNet Network is not installed, then the software provides a download link.

net = alexnet;

Generate Image

To produce images that resemble a given class the most closely, select the final fully connected layer. First, locate the layer index of this layer by viewing the network architecture in the Layers property of the network net.

net.Layers
ans = 

  25x1 Layer array with layers:

     1   'data'     Image Input                   227x227x3 images with 'zerocenter' normalization
     2   'conv1'    Convolution                   96 11x11x3 convolutions with stride [4  4] and padding [0  0  0  0]
     3   'relu1'    ReLU                          ReLU
     4   'norm1'    Cross Channel Normalization   cross channel normalization with 5 channels per element
     5   'pool1'    Max Pooling                   3x3 max pooling with stride [2  2] and padding [0  0  0  0]
     6   'conv2'    Convolution                   256 5x5x48 convolutions with stride [1  1] and padding [2  2  2  2]
     7   'relu2'    ReLU                          ReLU
     8   'norm2'    Cross Channel Normalization   cross channel normalization with 5 channels per element
     9   'pool2'    Max Pooling                   3x3 max pooling with stride [2  2] and padding [0  0  0  0]
    10   'conv3'    Convolution                   384 3x3x256 convolutions with stride [1  1] and padding [1  1  1  1]
    11   'relu3'    ReLU                          ReLU
    12   'conv4'    Convolution                   384 3x3x192 convolutions with stride [1  1] and padding [1  1  1  1]
    13   'relu4'    ReLU                          ReLU
    14   'conv5'    Convolution                   256 3x3x192 convolutions with stride [1  1] and padding [1  1  1  1]
    15   'relu5'    ReLU                          ReLU
    16   'pool5'    Max Pooling                   3x3 max pooling with stride [2  2] and padding [0  0  0  0]
    17   'fc6'      Fully Connected               4096 fully connected layer
    18   'relu6'    ReLU                          ReLU
    19   'drop6'    Dropout                       50% dropout
    20   'fc7'      Fully Connected               4096 fully connected layer
    21   'relu7'    ReLU                          ReLU
    22   'drop7'    Dropout                       50% dropout
    23   'fc8'      Fully Connected               1000 fully connected layer
    24   'prob'     Softmax                       softmax
    25   'output'   Classification Output         crossentropyex with 'tench', 'goldfish', and 998 other classes

Then, select the final fully connected layer. The final fully connected layer is layer 23.

layer = 23;

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 = [9 188 231 563 855 975];

The class names are stored in the ClassNames property of the output layer (the last layer). You can view the names of the selected classes by selecting the entries in channels.

net.Layers(end).ClassNames(channels)
ans =

  6×1 cell array

    {'hen'              }
    {'Yorkshire terrier'}
    {'Shetland sheepdog'}
    {'fountain'         }
    {'theater curtain'  }
    {'geyser'           }

Generate the images using deepDreamImage. This command uses a compatible GPU, if available. Otherwise it uses the CPU. A CUDA® enabled NVIDIA® GPU with compute capability 3.0 or higher is required for training on a GPU.

I = deepDreamImage(net,layer,channels);
|============================================|
|    Pyramid   |  Iteration   |  Activation  |
|     Level    |              |   Strength   |
|============================================|
|            1 |            1 |         1.28 |
|            1 |            2 |         3.53 |
|            1 |            3 |         6.66 |
|            1 |            4 |        12.28 |
|            1 |            5 |        20.37 |
|            1 |            6 |        27.00 |
|            1 |            7 |        34.37 |
|            1 |            8 |        41.65 |
|            1 |            9 |        53.58 |
|            1 |           10 |        57.42 |
|            2 |            1 |        12.20 |
|            2 |            2 |        16.67 |
|            2 |            3 |        19.98 |
|            2 |            4 |        17.74 |
|            2 |            5 |        21.88 |
|            2 |            6 |        26.09 |
|            2 |            7 |        31.12 |
|            2 |            8 |        28.62 |
|            2 |            9 |        36.90 |
|            2 |           10 |        34.66 |
|            3 |            1 |        41.71 |
|            3 |            2 |        55.74 |
|            3 |            3 |        52.63 |
|            3 |            4 |        57.24 |
|            3 |            5 |        58.00 |
|            3 |            6 |        70.20 |
|            3 |            7 |        71.79 |
|            3 |            8 |        65.81 |
|            3 |            9 |        72.26 |
|            3 |           10 |        79.38 |
|============================================|

If you have Image Processing Toolbox installed, you can display all the images together using montage. Otherwise, you can display the images individually using imshow.

figure
montage(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 'hen' class (channel 9). Set 'Verbose' to false to suppress detailed information on the optimization process.

channels = 9;
I = deepDreamImage(net,layer,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 'pot' class (channel 739).

channels = 739;

I = deepDreamImage(net,layer,channels, ...
    'Verbose',false, ...
    'NumIterations',iterations, ...
    'PyramidLevels',levels);

figure
imshow(I)

See Also

|

Related Topics

Was this topic helpful?