MATLAB学习help之——Classify Image Using GoogLeNet from Neural Network Toolbox

这一个例子是利用已经训练好的深度学习网络, GoogLeNet, 其具体的解释详见另一篇转载的博客。

GoogLeNet has been trained on over a million images and can classify images into 1000 object categories (such as keyboard, coffee mug, pencil, and many animals). The network has learned rich feature representations for a wide range of images. The network takes an image as input and outputs a label for the object in the image together with the probabilities for each of the object categories.

Step 1.
加载深度学习网络, 并查看输入层的大小。

net = googlenet;
inputSize = net.Layers(1).InputSize

得到

inputSize =

224 224 3

网络层的最后一个层为输出层,随机查看1000种分类中的10个

classNames = net.Layers(end).ClassNames;
numClasses = numel(classNames);
disp(classNames(randperm(numClasses,10)))

得到
'speedboat'
'window screen'
'isopod'
'wooden spoon'
'lipstick'
'drake'
'hyena'
'dumbbell'
'strawberry'
'custard apple'

Step2.
载入图片并重置尺寸

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

size(I)

I = imresize(I,inputSize(1:2));
figure
imshow(I)

得到
ans =

384 512 3
原图如下


MATLAB学习help之——Classify Image Using GoogLeNet from Neural Network Toolbox_第1张图片
图片.png

重置后


MATLAB学习help之——Classify Image Using GoogLeNet from Neural Network Toolbox_第2张图片
图片.png

Step 3.
分类图片

[label,scores] = classify(net,I);
label

得到


MATLAB学习help之——Classify Image Using GoogLeNet from Neural Network Toolbox_第3张图片
图片.png

显示图片并计算概率

figure
imshow(I)
title(string(label) + ", " + num2str(100*scores(classNames == label),3) + "%");
MATLAB学习help之——Classify Image Using GoogLeNet from Neural Network Toolbox_第4张图片
图片.png

显示概率前5的分类结果

[~,idx] = sort(scores,'descend');
idx = idx(5:-1:1);
classNamesTop = net.Layers(end).ClassNames(idx);
scoresTop = scores(idx);

figure
barh(scoresTop)
xlim([0 1])
title('Top 5 Predictions')
xlabel('Probability')
yticklabels(classNamesTop)
MATLAB学习help之——Classify Image Using GoogLeNet from Neural Network Toolbox_第5张图片
图片.png

总结:

主要学习了 如何 调用已经训练好的 深度学习网络 googlenet, 对于另外的深度学习网络,方法相同


图片.png

你可能感兴趣的:(MATLAB学习help之——Classify Image Using GoogLeNet from Neural Network Toolbox)