Main Content

scores2label

Convert prediction scores to labels

Since R2024a

    Description

    example

    labels = scores2label(scores,classNames) converts the specified prediction scores to labels by returning the label with the highest score in the channel dimension.

    labels = scores2label(scores,classNames,dim) additionally specifies the channel dimension of the prediction scores.

    [labels,topScores] = scores2label(___) also returns the scores that correspond to the output labels using any of the previous syntaxes.

    Examples

    collapse all

    Load a pretrained SqueezeNet neural network.

    [net,classNames] = imagePretrainedNetwork;

    Read an image from a PNG file and classify it. To classify the image, first convert it to the data type single.

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

    X = single(im);
    scores = predict(net,X);
    [label,score] = scores2label(scores,classNames);

    Display the predicted label and corresponding score with the image.

    figure
    imshow(im)
    title(string(label) + " (Score: " + score + ")")

    Input Arguments

    collapse all

    Prediction scores, specified as a numeric array or dlarray object.

    Class names, specified as a string array, categorical array, or cell array of character vectors.

    Data Types: string | cell | categorical

    Channel dimension, specified as one of these values:

    • "auto" — Automatically determine the channel dimension of scores based on the number of elements in classNames. If scores contains exactly one dimension of size numel(classNames) that has elements that sum to one, then the function uses that dimension. Otherwise, the function throws an error.

    • Positive integer — Use the specified dimension as the channel dimension. Use this option when scores contains multiple dimensions of size numel(classNames).

    The scores2label function finds the top scores in the specified dimension.

    Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | char | string

    Output Arguments

    collapse all

    Labels with the highest score in the channel dimension of scores, returned as a categorical array.

    Scores that correspond to the output labels, returned as a numeric array or dlarray object.

    This argument has the same data type and format as scores.

    Extended Capabilities

    Version History

    Introduced in R2024a