一、ResNet50工具箱安裝
(1)下載工具箱
https://ww2.mathworks.cn/matlabcentral/fileexchange/64626-deep-learning-toolbox-model-for-resnet-50-network
(2)在matlab打開(kāi)下載的resnet50.mlpkginstall文件
(3)使用下面代碼進(jìn)行測(cè)試,出現(xiàn)結(jié)果說(shuō)明安裝成功
clear
clc
% Access the trained model
net = resnet50();
% See details of the architecture
net.Layers
% Read the image to classify
I = imread('peppers.png');
% Adjust size of the image
sz = net.Layers(1).InputSize;
I = I(1:sz(1),1:sz(2),1:sz(3));
% Classify the image using Resnet-50
label = classify(net, I);
% Show the image and the classification results
figure
imshow(I)
text(10,20,char(label),'Color','white')
二、訓(xùn)練貓狗數(shù)據(jù)集
(1)數(shù)據(jù)集下載鏈接:
??https://pan.quark.cn/s/e043408353a5
(2)將數(shù)據(jù)集按照如下目錄進(jìn)行放置
(3)生成預(yù)訓(xùn)練模型
在命令行窗口輸入?deepNetworkDesigner(resnet50)
然后點(diǎn)擊導(dǎo)出→使用初始參數(shù)生成代碼
保存生成的網(wǎng)絡(luò)初始化參數(shù),生成的mlx文件可以叉掉:
修改文件路徑,類別數(shù)目以及相關(guān)參數(shù):
clear
clc
filename = "datasets";
%% 加載用于網(wǎng)絡(luò)初始化的參數(shù)。對(duì)于遷移學(xué)習(xí),網(wǎng)絡(luò)初始化參數(shù)是初始預(yù)訓(xùn)練網(wǎng)絡(luò)的參數(shù)。
trainingSetup = load("resnet-50.mat");
%% 設(shè)置圖像文件夾路徑和標(biāo)簽
nc = 2; %類別
imdsTrain = imageDatastore(filename,"IncludeSubfolders",true,"LabelSource","foldernames");
[imdsTrain, imdsValidation] = splitEachLabel(imdsTrain,0.8); % 80的訓(xùn)練集
%% 調(diào)整圖像大小以匹配網(wǎng)絡(luò)輸入層。
augimdsTrain = augmentedImageDatastore([224 224 3],imdsTrain);
augimdsValidation = augmentedImageDatastore([224 224 3],imdsValidation);
%% 設(shè)置訓(xùn)練選項(xiàng)
opts = trainingOptions("sgdm",...
"ExecutionEnvironment","gpu",...
"InitialLearnRate",0.01,...
"MaxEpochs",20,...
"MiniBatchSize",64,...
"Shuffle","every-epoch",...
"Plots","training-progress",...
"ValidationData",augimdsValidation);
?三、訓(xùn)練及測(cè)試結(jié)果
(1)訓(xùn)練結(jié)果
(2)導(dǎo)入一張圖片進(jìn)行測(cè)試
clear
clc
load result\net.mat
load result\traininfo.mat
%% 隨便選一張進(jìn)行測(cè)試
[file,path] = uigetfile('*.jpg');
if isequal(file,0)
disp('User selected Cancel');
else
filename = fullfile(path,file);
end
I = imread(filename);
I = imresize(I, [224 224]);
[YPred,probs] = classify(net,I);
imshow(I)
label = YPred;
title(string(label) + ", " + num2str(100*max(probs),3) + "%");
?
四、完整代碼獲?。ㄦ溄游哪?/strong>
MATLAB卷積神經(jīng)網(wǎng)絡(luò)——基于ResNet-50進(jìn)行圖像分類
如需繪制混淆矩陣圖、輸出單類別的準(zhǔn)確度等等...,可私聊小編,為你量身進(jìn)行定制。關(guān)注公眾號(hào),每日更新更多精彩內(nèi)容!?。?/p>
最后:
如果你想要進(jìn)一步了解更多的相關(guān)知識(shí),可以關(guān)注下面公眾號(hào)聯(lián)系~會(huì)不定期發(fā)布相關(guān)設(shè)計(jì)內(nèi)容包括但不限于如下內(nèi)容:信號(hào)處理、通信仿真、算法設(shè)計(jì)、matlab appdesigner,gui設(shè)計(jì)、simulink仿真......希望能幫到你!文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-834426.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-834426.html
到了這里,關(guān)于MATLAB卷積神經(jīng)網(wǎng)絡(luò)——基于ResNet-50進(jìn)行圖像分類的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!