showing pictures of every layers in cnn in matlab

To show the feature maps of every layer in your convolutional neural network (CNN) using MATLAB, you can follow these steps:

  1. Load your trained CNN model in the workspace.
  2. Get the names of all the layers in the CNN using the analyzeNetwork function.
  3. Get the output of each layer of the CNN model by running the input image through the network and using the activations function.
  4. Visualize the feature maps of each layer using the montage or imshow functions.

Here's some example code:

main.m
% Load the trained CNN model
net = alexnet; 

% Get the names of all the layers in the network
layers = net.Layers; 

% Loop through each layer and display the feature maps
for i = 1:numel(layers)
    layer = layers(i);
    
    % Check if the layer is a convolutional layer
    if isa(layer,'nnet.cnn.layer.Convolution2DLayer')
        
        % Compute the activations of the current layer
        inputSize = net.Layers(1).InputSize;
        I = randn(inputSize);
        featureMap = activations(net, I, layer.Name);
        
        % Visualize the feature map using montage
        figure;
        montage(featureMap)
        title(layer.Name)
        
        % Alternatively, visualize the feature map using imshow
        % figure;
        % for j = 1:size(featureMap, 3)
        %     subplot(8, 8, j);
        %     imshow(featureMap(:,:,j));
        % end
        % title(layer.Name)
    end
end
909 chars
33 lines

In this example, we are using the alexnet pre-trained CNN model that comes with MATLAB. We loop through each layer of the network and only display the feature maps of the convolutional layers. We use a random input image to compute the activations of a layer and then visualize the feature maps using the montage function. Alternatively, you could use the imshow function to view the feature maps individually.

gistlibby LogSnag