目錄
數(shù)據(jù)集:
實(shí)驗(yàn)代碼:alexnet版
如果你的matlab不是正版,先看這里:
數(shù)據(jù)集結(jié)構(gòu):
訓(xùn)練代碼:
訓(xùn)練結(jié)果:
?圖形界面:
界面展示:
其他:?
?輸出結(jié)果:
????????實(shí)驗(yàn)思路是使用預(yù)訓(xùn)練神經(jīng)網(wǎng)絡(luò)對(duì)圖片進(jìn)行特征提取,然后再使用SVM對(duì)得到的特征進(jìn)行處理。
? ? ? ? 寫(xiě)完后試過(guò)基于形態(tài)學(xué)分類(lèi),可能是數(shù)據(jù)集的原因,用了面積、周長(zhǎng)、最小外接矩形的長(zhǎng)和寬、離心率、灰度均值、HSV均值,方差等作為特征,結(jié)果并不理想。
? ? ? ? 用的matlab2021a,老師那要的(沒(méi)法發(fā)安裝包,只能線下找我)低版本不確定能用。
數(shù)據(jù)集:
自己搞得,不太行,還是建議你用其他的網(wǎng)上數(shù)據(jù)集
實(shí)驗(yàn)代碼:alexnet版
如果你的matlab不是正版,先看這里:
如果你的matlab不是正版,無(wú)法下載Deep Learning Toolbox Model for AlexNet Network來(lái)獲得已經(jīng)訓(xùn)練好的神經(jīng)網(wǎng)絡(luò)可以去官網(wǎng)下載,或者在我這花1積分下載Deep Learning Toolbox Model for AlexNet Network - File Exchange - MATLAB CentralDownload and share free MATLAB code, including functions, models, apps, support packages and toolboxeshttps://ww2.mathworks.cn/matlabcentral/fileexchange/59133-deep-learning-toolbox-model-for-alexnet-network?s_tid=ta_fx_results
如果是在b站上下載的2022版有可能會(huì)崩潰,提前做好心理準(zhǔn)備(偶然現(xiàn)象)?
下載完后(如果是壓縮包的話先解壓),將安裝包拖進(jìn)matlab的工作目錄
?然后雙擊工作目錄里的這個(gè)安裝包來(lái)運(yùn)行它,會(huì)彈出這個(gè)界面
按提示操作
?輸入完畢后驗(yàn)證電子郵件
?設(shè)置密碼
?就開(kāi)始下載了
?下載完就能用已經(jīng)訓(xùn)練好的alexnet了
數(shù)據(jù)集結(jié)構(gòu):
數(shù)據(jù)集下載? ? ? ? 我這個(gè)數(shù)據(jù)集很一般,建議從網(wǎng)上找數(shù)據(jù)集
?
?apple數(shù)據(jù)集,里面的每個(gè)文件夾是一個(gè)類(lèi)別(名字隨便寫(xiě),不過(guò)最好不是中文),圖片是哪一種就放在那個(gè)文件夾里
訓(xùn)練代碼:
file是數(shù)據(jù)集所在文件夾
訓(xùn)練完會(huì)有提示,讓你選擇保存不保存數(shù)據(jù)集
報(bào)錯(cuò)說(shuō)alexnet什么的看上面-------如果你的matlab不是正版
clc;
clear;
file = 'D:/apple';
% 讀取file中所有圖片,以文件夾名作為標(biāo)簽
appleData = imageDatastore(file, 'IncludeSubfolders', true, 'LabelSource', 'foldernames')
% 調(diào)整大小適應(yīng)后來(lái)的AlexNet模型輸入
appleData.ReadFcn = @(x) imresize(imread(x), [227 227]);
% 劃分測(cè)試集訓(xùn)練集,會(huì)打亂
[trainImds, testImds] = splitEachLabel(appleData, 0.8, 'randomized');
net = alexnet;% 加載AlexNet模型
% 特征提取
% 使用activations函數(shù)對(duì)訓(xùn)練集和測(cè)試集的圖像進(jìn)行特征提取
% 輸出第七層的特征
featuresTrain = activations(net, trainImds, 'fc7', 'OutputAs', 'rows');
featuresTest = activations(net, testImds, 'fc7', 'OutputAs', 'rows');
fprintf('開(kāi)始訓(xùn)練');
% 訓(xùn)練SVM,fitcecoc任數(shù)訓(xùn)練SVM分類(lèi)器
svmModel = fitcecoc(featuresTrain,trainImds.Labels);
% 在測(cè)試集計(jì)算準(zhǔn)確率
fprintf('預(yù)測(cè)值:');
predictedLabels = predict(svmModel, featuresTest);
fprintf('真實(shí)值:');
testImds.Labels
fprintf('正確率:');
accuracy = mean(predictedLabels == testImds.Labels)
x = input('是否保存svmModel已訓(xùn)練模型(y/n)',"s") ;
if(x=='y')
save svm_Model svmModel
fprintf('模型已保存到svm_Model.mat')
end
訓(xùn)練結(jié)果:
appleData =
ImageDatastore - 屬性:
Files: {
'D:\apple\1\1.jpeg';
'D:\apple\1\1.jpg';
'D:\apple\1\10.jpg'
... and 197 more
}
Folders: {
'D:\apple'
}
Labels: [1; 1; 1 ... and 197 more categorical]
AlternateFileSystemRoots: {}
ReadSize: 1
SupportedOutputFormats: ["png" "jpg" "jpeg" "tif" "tiff"]
DefaultOutputFormat: "png"
ReadFcn: @readDatastoreImage
開(kāi)始訓(xùn)練預(yù)測(cè)值:真實(shí)值:
ans =
41×1 categorical 數(shù)組
1
1
1
1
1
1
1
1
1
1
1
1
1
2
2
2
2
2
2
2
2
2
2
2
2
2
2
3
3
3
3
3
3
3
3
3
3
3
3
3
3
正確率:
accuracy =
0.9512
是否保存svmModel已訓(xùn)練模型(y/n)y
模型已保存到svm_Model.mat>>
?圖形界面:
用matlab自帶的設(shè)計(jì)app做的
?我主要就寫(xiě)了一個(gè)按鈕的回調(diào),一定要記著保存SVM模型后再用,沒(méi)做讀取錯(cuò)誤報(bào)錯(cuò)。
classdef apple < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
EditField_2 matlab.ui.control.EditField
EditField_2Label matlab.ui.control.Label
EditField matlab.ui.control.EditField
Label matlab.ui.control.Label
Image matlab.ui.control.Image
Button matlab.ui.control.Button
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: Button
function ButtonPushed(app, event)
global svmModel net;% 模型
% 如果沒(méi)有模型,加載模型
if(exist('svmModel','var')&&exist('net','var'))
net = alexnet;% 加載AlexNet模型
load svm_Model svmModel% 加載已訓(xùn)練模型
end
% 打開(kāi)文件對(duì)話框 禁止多選
[file,path]=uigetfile('*.*',' Multiselect ' ,'off');
if(file==0) % 沒(méi)讀文件
return
end
% 更改路徑文本
file = [path,file];
app.EditField.Value = file;
app.Image.ImageSource = imread(file);% 更改圖像顯示
% 創(chuàng)建需要讀取特征的數(shù)據(jù)
appleData = imageDatastore(file);
% 調(diào)整大小適應(yīng)后來(lái)的AlexNet模型輸入
appleData.ReadFcn = @(x) imresize(imread(x), [227 227]);
appleData = activations(net, appleData, 'fc7', 'OutputAs', 'rows');
Labels = predict(svmModel, appleData);
app.EditField_2.Value = string(Labels);
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 415 393];
app.UIFigure.Name = 'MATLAB App';
% Create Button
app.Button = uibutton(app.UIFigure, 'push');
app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true);
app.Button.FontSize = 20;
app.Button.Position = [36 21 100 34];
app.Button.Text = '選擇文件';
% Create Image
app.Image = uiimage(app.UIFigure);
app.Image.Position = [86 134 237 237];
% Create Label
app.Label = uilabel(app.UIFigure);
app.Label.HorizontalAlignment = 'right';
app.Label.FontSize = 20;
app.Label.Position = [23 68 125 26];
app.Label.Text = '圖片文件路徑';
% Create EditField
app.EditField = uieditfield(app.UIFigure, 'text');
app.EditField.Editable = 'off';
app.EditField.Position = [157 61 230 36];
% Create EditField_2Label
app.EditField_2Label = uilabel(app.UIFigure);
app.EditField_2Label.HorizontalAlignment = 'right';
app.EditField_2Label.FontSize = 20;
app.EditField_2Label.Position = [209 22 65 26];
app.EditField_2Label.Text = '級(jí)別:';
% Create EditField_2
app.EditField_2 = uieditfield(app.UIFigure, 'text');
app.EditField_2.Editable = 'off';
app.EditField_2.Position = [273 23 50 24];
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = apple
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
界面展示:
其他:?
如果覺(jué)著一個(gè)網(wǎng)絡(luò)不好的話,這里還有一種操作:
或者你們還可以使用 Adaboost
Adaboost是一種迭代算法,其核心思想是針對(duì)同一個(gè)訓(xùn)練集訓(xùn)練不同的分類(lèi)器(弱分類(lèi)器),然后把這些弱分類(lèi)器集合起來(lái),構(gòu)成一個(gè)更強(qiáng)的最終分類(lèi)器(強(qiáng)分類(lèi)器)。
這幾個(gè)網(wǎng)絡(luò)都可以再之前的官網(wǎng)下載文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-483233.html
clc;
clear;
file = 'D:/apple';
% 讀取file中所有圖片,以文件夾名作為標(biāo)簽
appleData = imageDatastore(file, 'IncludeSubfolders', true, 'LabelSource', 'foldernames')
% 調(diào)整大小適應(yīng)后來(lái)的AlexNet模型輸入
appleData.ReadFcn = @(x) imresize(imread(x), [227 227]);
% 劃分測(cè)試集訓(xùn)練集,會(huì)打亂
[trainImds, testImds] = splitEachLabel(appleData, 0.8, 'randomized');
fprintf("\n開(kāi)始加載預(yù)訓(xùn)練模型");
net1 = alexnet;% 加載預(yù)訓(xùn)練模型
net2 = vgg16;
net3 = resnet18;
% 特征提取
% 使用activations函數(shù)對(duì)訓(xùn)練集和測(cè)試集的圖像進(jìn)行特征提取
% alexnet,他有8層神經(jīng)網(wǎng)絡(luò),
% 其中前5層是卷積層,后3層是全連接層。其中fc7層是全連接層的第2個(gè),它包含4096個(gè)神經(jīng)元
% 該層可以提取圖像的高級(jí)語(yǔ)義特征,這些特征已經(jīng)經(jīng)過(guò)多次卷積和池化操作,
% 能夠捕捉到圖像的基本形狀和紋理信息,同時(shí)又不會(huì)過(guò)于抽象
% 輸出第七層的特征,按行輸出,
fprintf("\n開(kāi)始使用預(yù)訓(xùn)練模型提取特征");
featuresTrain1 = activations(net1, trainImds, 'fc7', 'OutputAs', 'channels');
featuresTrain2 = activations(net2, trainImds, 'fc7', 'OutputAs', 'channels');
featuresTrain3 = activations(net3, trainImds, 'pool5', 'OutputAs', 'channels');
fprintf("\n已經(jīng)提取訓(xùn)練集特征提取特征");
featuresTest1 = activations(net1, testImds, 'fc7', 'OutputAs', 'channels');
featuresTest2 = activations(net2, testImds, 'fc7', 'OutputAs', 'channels');
featuresTest3 = activations(net3, testImds, 'pool5', 'OutputAs', 'channels');
fprintf("\n已經(jīng)提取測(cè)試集特征提取特征");
featuresTrain1 = reshape(featuresTrain1, [], size(featuresTrain1, 4))';
featuresTrain2 = reshape(featuresTrain2, [], size(featuresTrain2, 4))';
featuresTrain3 = reshape(featuresTrain3, [], size(featuresTrain3, 4))';
featuresTest1 = reshape(featuresTest1, [], size(featuresTest1, 4))';
featuresTest2 = reshape(featuresTest2, [], size(featuresTest2, 4))';
featuresTest3 = reshape(featuresTest3, [], size(featuresTest3, 4))';
fprintf('\n開(kāi)始訓(xùn)練');
% 訓(xùn)練SVM,fitcecoc任數(shù)訓(xùn)練SVM分類(lèi)器
svmModel_1 = fitcecoc(featuresTrain1,trainImds.Labels);
svmModel_2 = fitcecoc(featuresTrain2,trainImds.Labels);
svmModel_3 = fitcecoc(featuresTrain3,trainImds.Labels);
% 在測(cè)試集計(jì)算準(zhǔn)確率
predictedLabels1 = predict(svmModel_1, featuresTest1);
predictedLabels2 = predict(svmModel_2, featuresTest2);
predictedLabels3 = predict(svmModel_3, featuresTest3);
fprintf('alexnet 正確率:');
mean(predictedLabels1 == testImds.Labels)
fprintf('vgg16 正確率:');
mean(predictedLabels2 == testImds.Labels)
fprintf('resnet18 正確率:');
mean(predictedLabels3 == testImds.Labels)
% 返回一組數(shù)據(jù)中出現(xiàn)最頻繁的元素
predictedLabels = mode([predictedLabels1, predictedLabels2, predictedLabels3], 2);
% 計(jì)算投票分類(lèi)正確率
fprintf('投票正確率:');
accuracy = mean(predictedLabels == testImds.Labels)
x = input('是否保存已訓(xùn)練模型(y/n)',"s") ;
if(x=='y')
save svm_Model_3 svmModel_1 svmModel_2 svmModel_3
fprintf('模型已保存到 svm_Model_3.mat')
end
?輸出結(jié)果:
這個(gè)1就很晃眼,大概率不是真的。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-483233.html
appleData =
ImageDatastore - 屬性:
Files: {
'D:\apple\1\1.jpeg';
'D:\apple\1\1.jpg';
'D:\apple\1\10.jpg'
... and 197 more
}
Folders: {
'D:\apple'
}
Labels: [1; 1; 1 ... and 197 more categorical]
AlternateFileSystemRoots: {}
ReadSize: 1
SupportedOutputFormats: ["png" "jpg" "jpeg" "tif" "tiff"]
DefaultOutputFormat: "png"
ReadFcn: @readDatastoreImage
開(kāi)始加載預(yù)訓(xùn)練模型
開(kāi)始使用預(yù)訓(xùn)練模型提取特征
已經(jīng)提取訓(xùn)練集特征提取特征
已經(jīng)提取測(cè)試集特征提取特征
開(kāi)始訓(xùn)練alexnet 正確率:
ans =
0.9512
vgg16 正確率:
ans =
0.9024
resnet18 正確率:
ans =
0.9512
投票正確率:
accuracy =
1
是否保存已訓(xùn)練模型(y/n)y
模型已保存到 svm_Model_3.mat>>
到了這里,關(guān)于matlab 使用預(yù)訓(xùn)練神經(jīng)網(wǎng)絡(luò)和SVM進(jìn)行蘋(píng)果分級(jí)(帶圖形界面)支持其他物品圖片分級(jí)或者分類(lèi)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!