How to implement Grad-CAM in 3D?
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi,
I've got a trained 3D CNN model and I would like to visualize its classification result. However, Grad-CAM seems to be implemented for 2D models for now. To be more exact, the predict function in gradcam expects the input to be 2D image:
dlImg = dlarray(single(img),'SSC'); % 224x224x224 dlarray
dlnet = dlnetwork(lgraph); % 1x1 dlnetwork
%Specify the names of the softmax and feature map layers to use with the Grad-CAM helper function. For the feature map layer, specify either the last ReLU layer with non-singleton spatial dimensions, or the last layer that gathers the outputs of ReLU layers (such as a depth concatenation or an addition layer). If your network does not contain any ReLU layers, specify the name of the final convolutional layer that has non-singleton spatial dimensions in the output. Use the function analyzeNetwork to examine your network and select the correct layers. For GoogLeNet, the name of the softmax layer is 'prob' and the depth concatenation layer is 'inception_5b-output'.
softmaxName = 'prob';
featureLayerName = 'res5b_relu';
%To use automatic differentiation, convert the sherlock image to a dlarray.
%Compute the Grad-CAM gradient for the image by calling dlfeval on the gradcam function.
[featureMap, dScoresdMap] = dlfeval(@gradcam, dlnet, dlImg, softmaxName, featureLayerName, classfn);
function [featureMap,dScoresdMap] = gradcam(dlnet, dlImg, softmaxName, featureLayerName, classIdx)
[scores,featureMap] = predict(dlnet, dlImg, 'Outputs', {softmaxName, featureLayerName});
classScore = scores(classIdx);
dScoresdMap = dlgradient(classScore,featureMap);
end
This function is used and is taken from the example Grad-CAM Reveals the Why Behind Deep Learning Decisions. The exact error I get:
Error using dlfeval (line 43)
Layer 'VideoInputLayer': Invalid input data. Invalid number of spatial dimensions. Layer expects 3 but received 2.
Error in adniviz (line 58)
[featureMap, dScoresdMap] = dlfeval(@gradcam, dlnet, dlImg, softmaxName, featureLayerName, classfn);
I get the same error with just:
[scores,featureMap] = predict(dlnet, dlImg, 'Outputs', {softmaxName, featureLayerName});
Error using dlnetwork/predict (line 406)
Layer 'VideoInputLayer': Invalid input data. Invalid number of spatial dimensions. Layer expects 3 but received 2.
3 commentaires
Ava Dolores
le 20 Fév 2024
Modifié(e) : Ava Dolores
le 20 Fév 2024
Hello,
I have been training my CNN on alzheimer's vs control 3D Nii files of the brain. I want to use gradCAM to visualize why I am getting specific classification results.
Using your code as reference, I am getting a 5-D dlarray for the featureMap and dScoresdMap. I beleive this is because when using dlarray we are putting in SSSCB, so the channel and batch are the fourth and fifth dimensions. The 5-D dlarray makes it very difficult to visualize the gradCAM data using volshow becuase it is not a 3D array.
Why am I getting a 5-D array, how can I fix this, and how should I visualize the data?
I have attached my workspace, my modified version of your code, and the error I get when trying volshow.



Thank you for your time and help!
Réponses (1)
Gayathri
le 8 Mai 2025
Modifié(e) : Gayathri
le 8 Mai 2025
I understand that @Juuso Korhonen is using MATLAB R2020b, and hence trying to write a code for "gradCAM" from scratch.
@Ava Dolores @Raquel I hope you are using a MATLAB version later than MATLAB R2021a. If yes, a MATLAB inbuilt "gradCAM" function is available which explains the network predictions.
Please refer to the examples in the above link, which explains in detail on how to use the "gradCAM" function for classification and regression tasks.
And if suppose you are using an older version of MATLAB, where this function is not available, please refer to the below code which will serve the purpose. But its recommended to use the "gradCAM" function which will give accurate results.
img = imread('peppers.png');
imgResized = imresize(img, [224 224]);
imgSingle = im2single(imgResized);
dlImg = dlarray(imgSingle, 'SSC');
%Load pretrained ResNet-50
net = resnet50;
lgraph = layerGraph(net);
lgraph = removeLayers(lgraph, 'ClassificationLayer_fc1000');
%Convert to dlnetwork
dlnet = dlnetwork(lgraph);
scores = predict(dlnet, dlImg, 'Outputs', 'fc1000');
[~, classIdx] = max(extractdata(scores));
classNames = net.Layers(end).Classes;
predictedLabel = string(classNames(classIdx));
softmaxName = 'fc1000';
featureLayerName = 'activation_49_relu';
cam = gradcam(dlnet, dlImg, softmaxName, featureLayerName, classIdx);
cam = imresize(cam, [224 224]);
figure
imshow(imgResized)
hold on
imagesc(cam, 'AlphaData', 0.5); % Overlay heatmap
colormap jet
colorbar
title("Grad-CAM for: " + predictedLabel)
hold off
% ========== Grad-CAM Functions ==========
function cam = gradcam(dlnet, dlImg, softmaxName, featureLayerName, classIdx)
cam = dlfeval(@gradcamModel, dlnet, dlImg, softmaxName, featureLayerName, classIdx);
cam = gather(extractdata(cam));
end
function cam = gradcamModel(dlnet, dlImg, softmaxName, featureLayerName, classIdx)
[scores, featureMap] = predict(dlnet, dlImg, 'Outputs', {softmaxName, featureLayerName});
classScore = scores(classIdx);
dScoresdMap = dlgradient(classScore, featureMap);
weights = mean(mean(dScoresdMap, 1), 2);
cam = sum(featureMap .* weights, 3); % Sum over channels
cam = max(cam, 0);
end
The above code is just an illustration by loading a pretrained "Resnet-50" network and then use it to do the predictions on a single image.
Hope this helps!
0 commentaires
Voir également
Catégories
En savoir plus sur Verification dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!