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.

deepDreamImage

Visualize network features using deep dream

Syntax

I = deepDreamImage(net,layer,channels)
I = deepDreamImage(___,Name,Value)

Description

example

I = deepDreamImage(net,layer,channels) returns an array of images that strongly activate the channels channels within the network net of the layer with numeric index or name given by layer. These images highlight the features learned by a network.

I = deepDreamImage(___,Name,Value) returns an image with additional options specified by one or more Name,Value pair arguments using any of the previous syntaxes.

Examples

collapse all

Create a simple convolutional neural network and visualize the learned features using deepDreamImage.

Load sample data.

[XTrain,TTrain] = digitTrain4DArrayData;

Construct a simple network to classify the digit image data.

layers = [ ...
    imageInputLayer([28 28 1])
    convolution2dLayer(5,20)
    reluLayer
    maxPooling2dLayer(2,'Stride',2)
    fullyConnectedLayer(10)
    softmaxLayer
    classificationLayer];

Create training options.

options = trainingOptions('sgdm');

Train the network.

net = trainNetwork(XTrain,TTrain,layers,options);
Training on single CPU.
Initializing image normalization.
|=========================================================================================|
|     Epoch    |   Iteration  | Time Elapsed |  Mini-batch  |  Mini-batch  | Base Learning|
|              |              |  (seconds)   |     Loss     |   Accuracy   |     Rate     |
|=========================================================================================|
|            1 |            1 |         0.88 |       2.3026 |        7.81% |       0.0100 |
|            2 |           50 |        17.83 |       2.2735 |       33.59% |       0.0100 |
|            3 |          100 |        34.88 |       1.6613 |       48.44% |       0.0100 |
|            4 |          150 |        51.86 |       1.1803 |       64.06% |       0.0100 |
|            6 |          200 |        69.82 |       1.0499 |       64.06% |       0.0100 |
|            7 |          250 |        87.64 |       0.8391 |       76.56% |       0.0100 |
|            8 |          300 |       105.68 |       0.6981 |       77.34% |       0.0100 |
|            9 |          350 |       124.04 |       0.7084 |       77.34% |       0.0100 |
|           11 |          400 |       141.05 |       0.4902 |       87.50% |       0.0100 |
|           12 |          450 |       157.53 |       0.3839 |       91.41% |       0.0100 |
|           13 |          500 |       174.68 |       0.2986 |       92.19% |       0.0100 |
|           15 |          550 |       191.65 |       0.2583 |       93.75% |       0.0100 |
|           16 |          600 |       208.03 |       0.2009 |       97.66% |       0.0100 |
|           17 |          650 |       224.60 |       0.2642 |       92.97% |       0.0100 |
|           18 |          700 |       241.28 |       0.1448 |       97.66% |       0.0100 |
|           20 |          750 |       257.68 |       0.1314 |       96.88% |       0.0100 |
|           21 |          800 |       274.27 |       0.1232 |       97.66% |       0.0100 |
|           22 |          850 |       290.90 |       0.1009 |       98.44% |       0.0100 |
|           24 |          900 |       307.38 |       0.1051 |      100.00% |       0.0100 |
|           25 |          950 |       323.95 |       0.1483 |       97.66% |       0.0100 |
|           26 |         1000 |       340.95 |       0.0743 |       99.22% |       0.0100 |
|           27 |         1050 |       358.96 |       0.0603 |      100.00% |       0.0100 |
|           29 |         1100 |       375.05 |       0.0769 |       99.22% |       0.0100 |
|           30 |         1150 |       391.33 |       0.0524 |      100.00% |       0.0100 |
|           30 |         1170 |       397.73 |       0.0566 |      100.00% |       0.0100 |
|=========================================================================================|

To produce images that resemble a given digit the most closely, specify the final fully connected layer. View the network layers.

net.Layers
ans = 

  7x1 Layer array with layers:

     1   'imageinput'    Image Input             28x28x1 images with 'zerocenter' normalization
     2   'conv'          Convolution             20 5x5x1 convolutions with stride [1  1] and padding [0  0]
     3   'relu'          ReLU                    ReLU
     4   'maxpool'       Max Pooling             2x2 max pooling with stride [2  2] and padding [0  0]
     5   'fc'            Fully Connected         10 fully connected layer
     6   'softmax'       Softmax                 softmax
     7   'classoutput'   Classification Output   crossentropyex with '0', '1', and 8 other classes

Specify the last fully connected layer.

layer = 'fc';

Specify first channel to visualize digit 0.

channels = 1;

Generate and display image.

I = deepDreamImage(net,layer,channels,'Verbose',false);
imshow(I)

Input Arguments

collapse all

Trained network, specified as a SeriesNetwork object, returned by the trainNetwork function.

Layer to visualize, specified as a positive integer scalar or character vector. To visualize classification layer features, select the last fully connected layer before the classification layer.

    Tip:   Selecting ReLU or dropout layers for visualization may not produce useful images because of the effect that these layers have on the network gradients.

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | char

Queried channels, specified as scalar or vector of channel indices. If channels is a vector, the layer activations for each channel are optimized independently. The possible choices for channels depend on the selected layer. For convolutional layers, the NumFilters property specifies the number of output channels. For fully connected layers, the OutputSize property specifies the number of output channels.

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: deepDreamImage(net,layer,channels,'NumItetations',100,'ExecutionEnvironment','gpu') generates images using 100 iterations per pyramid level and uses the GPU.

collapse all

Image to initialize Deep Dream. Use this syntax to see how an image is modified to maximize network layer activations. The minimum height and width of the initial image depend on all the layers up to and including the selected layer:

  • For layers towards the end of the network, the initial image must be at least the same height and width as the image input layer.

  • For layers towards the beginning of the network, the height and width of the initial image can be smaller than the image input layer. However, it must be large enough to produce a scalar output at the selected layer.

  • The number of channels of the initial image must match the number of channels in the image input layer of the network.

If you do not specify an initial image, the software uses a random image with pixels drawn from a standard normal distribution. See also 'PyramidLevels'.

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Number of multi-resolution image pyramid levels to use to generate the output image, specified as a positive integer. Increase the number of pyramid levels to produce larger output images at the expense of additional computation. To produce an image of the same size as the initial image, set the number of levels to 1.

Example: 'PyramidLevels',3

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Scale between each pyramid level, specified as a scalar with value > 1. Reduce the pyramid scale to incorporate fine grain details into the output image. Adjusting the pyramid scale can help generate more informative images for layers at the beginning of the network.

Example: 'PyramidScale',1.4

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Number of iterations per pyramid level, specified as a positive integer. Increase the number of iterations to produce more detailed images at the expense of additional computation.

Example: 'NumIterations',10

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Type of scaling to apply to output image, specified as the comma-separated pair consisting of 'OutputScaling' and one of the following:

ValueDescription
'linear'Scale output pixel values in the interval [0,1]. The output image corresponding to each layer channel, I(:,:,:,channel), is scaled independently.
'none'Disable output scaling.

Example: 'OutputScaling','linear'

Data Types: char

Indicator to display progress information in the command window, specified as the comma-separated pair consisting of 'Verbose' and either 1 (true) or 0 (false). The displayed information includes the pyramid level, iteration, and the activation strength.

Example: 'Verbose',0

Data Types: logical

Execution environment for the function, specified as the comma-separated pair consisting of 'ExecutionEnvironment' and one of the following values.

ValueDescription
'auto'Use a GPU if available, otherwise use the CPU.
'gpu'Use the GPU. To use a GPU, you must have Parallel Computing Toolbox™, and a CUDA®-enabled NVIDIA® GPU with compute capability 3.0 or higher. If a suitable GPU is not available, the software returns an error.
'cpu'Use the CPU.

Example: 'ExecutionEnvironment','auto'

Data Types: char

Output Arguments

collapse all

Output image, specified by a sequence of grayscale or truecolor (RGB) images stored in a 4–D array. Images are concatenated along the fourth dimension of I such that the image that maximizes the output of channels(k) is I(:,:,:,k). You can display the output image using imshow.

Algorithms

This function implements a version of deep dream that uses a multi-resolution image pyramid and Laplacian Pyramid Gradient Normalization to generate high-resolution images. For more information on Laplacian Pyramid Gradient Normalization, see this blog post: DeepDreaming with TensorFlow.

References

[1] DeepDreaming with TensorFlow. https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/tutorials/deepdream/deepdream.ipynb

Introduced in R2017a

Was this topic helpful?