首页 > 编程知识 正文

matlab卷积神经网络图像分类,matlab实现卷积神经网络

时间:2023-05-05 02:15:03 阅读:25566 作者:2032

文章目录

前言使用步骤识别结果前言

利用MATLAB实践MNIST手写数字识别,下载手写数据集的准备有点麻烦。 MATLAB 2021版可以直接调用MNIST的部分数据进行CNN手写数字识别实践。 直接乘坐程序。

一、使用步骤1 .培训、测试及保存网络代码如下(

%CNN手写数字识别程序% $ 2021/8/6 Gavin $ % % matlabtrainnetwork (请参阅)帮助文档(3359 blog.csdn.net/weixin _ 43935696/) 111992192%%读取MATLAB附带的数字图像数据集,数据集有10000张0-9图像。 每个数字都有1000张图像digitdatasetpath=full file (mat lata ) imds=image datastore (digitdatasetpath,' FileExtensions ', { ' } %counteachlabel(imds ) )显示标签文件夹和其中的文件数。 10 * 2表) a=自述(imds,1 ); %读取数据集的第一个图像%whos a %查看图像数据结构和数据类型: 28x28,uint8figurenumImages=10000; perm=randperm(numimages,20 ); %随机选择20个,perm为1*20double序列for i=1:20 %该循环为MATLAB附带的样品subplot (4,5,I ); imshow(imds.files{perm(I ) }; %显示随机选择的20张图像drawnow; endnumTrainingFiles=750; %每个数字图像文件夹中的1000张图像中有750张将用于培训[imdsTrain,IMDstest]=spliteachlabel(IMDS,numTrainingFiles,' randomize %%将每个标签文件夹中的文件随机分为两组,750个为imdsTrain,其余为imdsTest%%的常规CNN网络layers=[ imageInputLayer([28 28 1],' ) ' Padding ',' same ' ) batchnormalizationlayer ',' Name ',' bn1 ' ) relulayer ',' relu1' ) maxpoolayer 'pool1' ) cood ' conv2 ' ) batchnormalizationlayer ) (Name )、' Stride '、2、' Name '、' pool2' ) convolution 2d layer (3,32,' paddidior ) ' conv3 ' ) Batchnoror 'relu3' ) fullyconnectedlayer(10,' Name ',' fullc ' ) softmaxlayer ) Name ),' soft ' ) clasisier soft ) ) clamationlayer652 %%改进的LeNet-5网络% layers=[ . % imageinputlayer ] [ 28281 ]、' Name '和' image input ' same ' ) %tanhlayer('Name )、' tanh1 ) %maxpooling2dlayer([2]、' name )、' maxpool1'、' Stride )、[2] con fc1 ' ) %fullyconnectedlayer(84,' Name ',' fc2 ' ) fullyconnectedlayer (10,' Name ',' fc ' ) %softmaxlayer )。 图形; plot(layergraph ) layers ) %网络图选项=training选项(' sgdm ', %MATLAB示例优化器' LearnRateSchedule ',) . LearnRateDropPeriod ',5, MaxEpochs ',20, %学习整个数据集的最大次数' MiniBatchSize ',128, %每个batch有128个采样% save minist _ le net5 netsavezzg _ CNN _1nety pred=classify (net,imdsTest ); %测试集中包含网络预测结果YTest=imdsTest.Labels; accuracy=sum(ypred==ytest )/numel ) ytest; %网络测试集中的精度磁盘(accuracy ); %0.9936,训练需要10'54'%LeNet-5:精度0.9820,训练需要7'40' 2小时。 手写数字识别码如下(

%测试自己写在图画书上的数字%load Minist_LeNet5 net; %已培训的LeNet5网络load zzg_cnn_1 net %已培训的CNN网络imds _ zzg=image datastore (f : (2021 MATLAB ) shouxieees ' IncludeSubfolders ',false, ' LabelSource ',' foldernames ' ); len=numel(imds_zzg.files ); %读取imds_zzg中的文件数% len=length (读取所有) imds_zzg ); imds_zzg的文件数figure('name '、' CNN手写数字识别、版权所有:周志刚、' NumberTitle '、' off ' ); fori=1: lentest _ image=readimage (imds _ zzg,I ); %导入手写数字图像subplot(8、5、I ); imshow(imds_zzg.files{I}; pause(1) %drawnow; shape=size(test_image; dimension=numel(shape; if dimension2test _ image=RGB2gray (test _ image; %灰度化end test _ image=im resize (test _ image,[ 28,28 ]; %保修输入为28 * 28 test _ image=im complement (test _ image ); 反转%并输入网络时,确保图像背景为黑色,数字部分为白色的result=classify(net,test_image )。 结果(disp ); 在命令窗口中显示%识别结果title ([ ' CNN识别结果: ' char ) result )在手写数字图像标题中显示识别结果end二识别结果

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。