%% Compute dlnetwork Outputs for Inference
% Some deep learning layers behave differently during training and inference (prediction). For example, during training, dropout layers randomly set pixels to zero. During inference, dropout layers do not change the input.
% To compute the outputs of a dlnetwork object for training, use forward function. To compute the outputs of a dlnetwork object for inference, use predict function.
% This example shows how to compute the network outputs for a given input using the predict function.
% Load a pretrained GoogLeNet model using the googlenet function. This function requires the Deep Learning Toolbox™ Model for GoogLeNet Network support package. If this support package is not installed, then the function provides a download link.

net = googlenet;
classes = net.Layers(end).Classes;

% Extract the layer graph, remove the output layers, and convert it to a dlnetwork object.

lgraph = layerGraph(net);
lgraph = removeLayers(lgraph,["prob" "output"]);
dlnet = dlnetwork(lgraph)

% Read a test image using imread.

I = imread("peppers.png");
figure
imshow(I)

% Resize the image to the input size of the network and covert it to dlarray with underlying type single. Specify the dimension labels 'SSCB' (spatial, spatial, channels, batch).

inputSize = dlnet.Layers(1).InputSize(1:2);
I = imresize(I,inputSize);
dlX = dlarray(single(I),'SSCB');

% If you have Parallel Computing Toolbox™ and an available GPU, then you can use the GPU by converting the dlarray object to a gpuArray object.

executionEnvironment = "auto";
if (executionEnvironment == "auto" && canUseGPU) || executionEnvironment == "gpu"
    dlX = gpuArray(dlX);
end

% Compute the network outputs using the predict function. To convert the output to classification scores, use the softmax function.

dlY = predict(dlnet,dlX);
scores = softmax(dlY);

% Find the class with the highest score.

[~,idx] = max(scores);
label = classes(idx)

% Visualize the top five output scores using a bar chart. The output of predict, and in turn softmax is a dlarray object. To plot the data, first extract the numeric values using the extractdata function.

scores = extractdata(scores);
[scoresTop,idxTop] = maxk(scores,5);
labelsTop = classes(idxTop);
labelsTop = removecats(labelsTop);
figure
bar(labelsTop,scoresTop)
title("Classification Scores (Top 5)")
xlabel("Class")
ylabel("Score")

% Copyright 2019 The MathWorks, Inc.