%% Visualize Which Parts of an Image Influence Classification Score
% Import the pretrained network GoogLeNet.

net = googlenet;

% Import the image and resize to match the input size for the network.

X = imread("sherlock.jpg");

inputSize = net.Layers(1).InputSize(1:2);
X = imresize(X,inputSize);

% Display the image.

imshow(X)

% Classify the image to get the class label.

label = classify(net,X)

% Use occlusionSensitivity to determine which parts of the image positively influence the classification result.

scoreMap = occlusionSensitivity(net,X,label);

% Plot the result over the original image with transparency to see which areas of the image affect the classification score.

figure
imshow(X)
hold on
imagesc(scoreMap,'AlphaData',0.5);
colormap jet

% The red parts of the map show the areas which have a positive contribution to the specified label. The dog's left eye and ear strongly influence the network's prediction of golden retriever.
% You can get similar results using the gradient class activation mapping (Grad-CAM) technique. Grad-CAM uses the gradient of the classification score with respect to the last convolutional layer in a network in order to understand which parts of the image are most important for classification. For an example, see Grad-CAM Reveals the Why Behind Deep Learning Decisions.
% Copyright 2019 The MathWorks, Inc.