当前位置:   article > 正文

matlab2019a使用 GoogLeNet 对图像进行分类(Deep Learning Toolbox系列篇2)_深度学习matlab的goolenet

深度学习matlab的goolenet

 

说明:此示例是加载已经训练好的googLeNet网络。

1. GoogLeNet网络介绍

此示例说明如何使用预训练的深度卷积神经网络 GoogLeNet 对图像进行分类。

GoogLeNet 已经对超过一百万个图像进行了训练,可以将图像分为 1000 个对象类别(例如键盘、咖啡杯、铅笔和多种动物)。该网络已基于大量图像学习了丰富的特征表示。网络以图像作为输入,然后输出图像中对象的标签以及每个对象类别的概率。

 

完整代码示例:

  1. clear
  2. close all
  3. clc
  4. % Access the trained model
  5. net = googlenet;
  6. % See details of the architecture
  7. net.Layers
  8. % Read the image to classify
  9. I = imread('peppers.png');
  10. % Adjust size of the image
  11. sz = net.Layers(1).InputSize
  12. I = I(1:sz(1),1:sz(2),1:sz(3));
  13. % Classify the image using GoogLeNet
  14. label = classify(net, I)
  15. % Show the image and the classification results
  16. figure
  17. imshow(I)
  18. text(10,20,char(label),'Color','white')

 

一般情况下,在第5行会出现报错,那是因为我们需要下载和安装GoogLeNet Network模块,在报错的红字中有安装的路径(Add-On-Explorer),只需要点击进去登陆便可以下载此模块:

代码运行结果:

完美识别了此张图片为辣椒.

 

官方help示例;

  1. clear
  2. close all
  3. clc
  4. %% 加载预训练网络
  5. net = googlenet;
  6. inputSize = net.Layers(1).InputSize
  7. classNames = net.Layers(end).ClassNames;
  8. numClasses = numel(classNames);
  9. disp(classNames(randperm(numClasses,10)))
  10. %% 读取图像并调整图像大小
  11. I = imread('peppers.png');
  12. figure
  13. imshow(I)
  14. size(I)
  15. I = imresize(I,inputSize(1:2));
  16. figure
  17. imshow(I)
  18. %% 对图像进行分类
  19. [label,scores] = classify(net,I);
  20. label
  21. figure
  22. imshow(I)
  23. title(string(label) + ", " + num2str(100*scores(classNames == label),3) + "%");
  24. %% 显示排名靠前的预测值
  25. [~,idx] = sort(scores,'descend');
  26. idx = idx(5:-1:1);
  27. classNamesTop = net.Layers(end).ClassNames(idx);
  28. scoresTop = scores(idx);
  29. figure
  30. barh(scoresTop)
  31. xlim([0 1])
  32. title('Top 5 Predictions')
  33. xlabel('Probability')
  34. yticklabels(classNamesTop)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/357056
推荐阅读
相关标签
  

闽ICP备14008679号