showing picture in every cnn layer in matlab

To visualize the feature maps or output of each convolutional layer in a Convolutional Neural Network (CNN) in Matlab, we can follow the below steps:

  1. Load the image and the CNN model
main.m
    % Load the image
    img = imread('image.jpg');
    % Load the pre-trained CNN model 
    net = alexnet;
109 chars
5 lines
  1. Extract the activations of all the convolutional layers
main.m
    % Extract the activations of all the convolutional layers
    activations = activations(net,img,'conv');
109 chars
3 lines
  1. Visualize the feature maps of each convolutional layer
main.m
    for i = 1:size(activations,1)
        figure;
        % Normalize the feature maps to the range [0, 1]
        imagesc(mat2gray(activations{i}));
        % Set the color map to grayscale
        colormap gray;
        % Add a color bar
        colorbar;
        % Set the title of the figure
        title(['Layer ', num2str(i), ' feature maps']);
    end
360 chars
12 lines

In the above code, we are looping over all the convolutional layers, normalizing the feature maps to the range [0, 1], setting the colormap to grayscale, adding a color bar, and setting the title of the figure.

By following the above steps, we can visualize the feature maps of each convolutional layer in a CNN. We can modify the above code according to our requirements, like for visualizing only a few chosen layers or saving the visualization images to disk.

gistlibby LogSnag