Tsu Kernik
Tsu Kernik

Reputation: 175

How to get from AlexNet multiple image labels

So, I am doing my Master's Thesis on the influence of super-resolution algorithms on the accuracy rate of image labeling by AlexNet. I am using Matlab with the pre-trained version of AlexNet.

The problem is that, by using

[label, scores] = classify(net, 'image')

I only get one label, whereas I would like to get multiple labels, e.g., five, and their corresponding scores by AlexNet.

I do not know how to do it and would really be very thankful if anyone could give me, at least, a hint.

Upvotes: 1

Views: 1713

Answers (1)

rayryeng
rayryeng

Reputation: 104535

The scores matrix tells you how each class scored when classifying the data. Specifically, each column i tells you how the score of the ith class faired when trying to classifying the input with class i. Each row is one input into AlexNet.

If you want to get the top k scores for each input, you can sort - specifically look at the second output variable and sort each row individually. After, you can extract out the first k columns of the result which tells you the top 5 classes or labels that were associated with the input. If you're concerned about the actual classes, look at the first output.

% First classify the image(s) you would like
[label, scores] = classify(net, ...);

k = 5; % We want 5 classes
[scores_sorted, classes] = sort(scores, 2); % Sort each row individually
scores_sorted = scores_sorted(:, 1 : k);
classes = classes(:, 1 : k);

scores_sorted and classes will now give you k column matrices where each row tells you the top k classes assigned for each input (stored in classes) as well as the scores provided by AlexNet (scores_sorted).

To finally determine what the actual classes are, when you create AlexNet, the final layer has the categories of classification. Assuming that you created the default net:

net = alexnet;

... you can determine what the classes are for each label ID by:

c = net.Layers(end).ClassNames;

This is a cell array of classes, and there should be 1000 elements in total. Therefore, given the classes variable from the code I wrote above, you can thus do:

out = net.Layers(end).ClassNames(classes);

classes would thus be used to index into the cell array and it will thus will give you a N x k cell array where each row in this cell array tells you the labels assigned by AlexNet for the top k classes per input image.

Upvotes: 2

Related Questions