Skip to content

Instantly share code, notes, and snippets.

@yanndebray
Created November 3, 2025 16:32
Show Gist options
  • Save yanndebray/f2edba7d638be8269567dd9c5bd695fe to your computer and use it in GitHub Desktop.
Save yanndebray/f2edba7d638be8269567dd9c5bd695fe to your computer and use it in GitHub Desktop.

Yann: classify image called 'cat-mike.png' with pretrained network

Copilot: To classify an image called cat-mike.png using a pretrained network in MATLAB, you can use the following code snippet with the Deep Learning Toolbox. This example utilizes the GoogLeNet model, which is pretrained on the ImageNet dataset.

% Load the pretrained GoogLeNet network
[net, classNames] = imagePretrainedNetwork("googlenet");

% Read and resize the image
I = imread('cat-mike.png');
inputSize = net.Layers(1).InputSize;
I = imresize(I, inputSize(1:2));

% Classify the image
scores = predict(net, single(I));
[label, score] = scores2label(scores, classNames);

% Display the image and the predicted label
figure;
imshow(I);
title(string(label));

Explanation:

  1. Load the Network: The imagePretrainedNetwork function loads the GoogLeNet model and its class names.
  2. Read and Resize: The image is read and resized to match the input size required by the network.
  3. Classification: The predict function is used to classify the image, and scores2label converts the prediction scores to a human-readable label.
  4. Display: Finally, the image is displayed with the predicted label as the title.

Make sure you have the Deep Learning Toolbox and the GoogLeNet support package installed to run this code successfully.

Yann: how can i get a list of the top labels

Copilot: To get a list of the top labels from prediction scores in MATLAB, you can use the scores2label function from the Deep Learning Toolbox. This function converts prediction scores to labels and can also return the top scores associated with those labels.

Here's how you can implement it:

% Assume 'scores' is a numeric array containing prediction scores
% and 'classNames' is a cell array or string array of class names.

% Example of using scores2label to get the top labels and scores
[labels, topScores] = scores2label(scores, classNames);

% Display the top labels and their corresponding scores
for i = 1:length(labels)
    fprintf('Label: %s, Score: %.4f\n', labels(i), topScores(i));
end

Explanation:

  • scores: This is the output from your neural network prediction, where each element corresponds to the score for a class.
  • classNames: This should be a list of class names corresponding to the scores.
  • The function scores2label returns the labels with the highest scores and can also return the scores themselves.

This approach allows you to easily retrieve and display the top labels along with their confidence scores.

Yann: what about the second element of the list of classes?

Copilot: To access the second element of the list of classes in MATLAB, you can simply index into the array of class names. Assuming you have a variable classNames that contains the list of class labels, you can retrieve the second element as follows:

% Assuming classNames is a cell array or string array of class labels
secondClass = classNames{2}; % For cell array
% or
secondClass = classNames(2); % For string array

% Display the second class
disp(secondClass);

Explanation:

  • If classNames is a cell array, use curly braces {} to access the element.
  • If it is a string array, use parentheses () to access the element.
  • This code will display the second class label from the list. Make sure that the list has at least two elements to avoid indexing errors.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment