
MATLAB构建神经网络识别FashionMnist数据集(附代码)
用MATLAB神经网络构造器构建模型识别FashionMnist数据集
·
目录
数据集介绍:
FashionMNIST 是一个替代 MNIST 手写数字集 的图像数据集。 它是由 Zalando(一家德国的时尚科技公司)旗下的研究部门提供。其涵盖了来自 10 种类别不同商品的正面图片。数据集链接:https://pan.quark.cn/s/c1e1f62059c5,部分图片如下所示:
使用多层感知机构建模型:
代码:
clear
clc
matlabroot = './FashionMnist';
digitDatasetPath = fullfile(matlabroot);
imds = imageDatastore(digitDatasetPath,'IncludeSubfolders',true,'LabelSource','foldernames');
numTrainFiles = 5000;
[imdsTrain,imdsValidation] = splitEachLabel(imds,numTrainFiles,'randomize');
layers = [
imageInputLayer([28 28 1],"Name","imageinput")
fullyConnectedLayer(256,"Name","fc")
batchNormalizationLayer("Name","batchnorm")
reluLayer("Name","relu")
fullyConnectedLayer(128,"Name","fc_1")
batchNormalizationLayer("Name","batchnorm_1")
reluLayer("Name","relu_1")
fullyConnectedLayer(64,"Name","fc_2")
batchNormalizationLayer("Name","batchnorm_2")
reluLayer("Name","relu_2")
fullyConnectedLayer(10,"Name","fc_3")
softmaxLayer("Name","softmax")
classificationLayer("Name","classoutput")];
options = trainingOptions('sgdm', ...
'InitialLearnRate',0.01, ...
'MaxEpochs',4, ...
'Shuffle','every-epoch', ...
'ValidationData',imdsValidation, ...
'ValidationFrequency',30, ...
'Verbose',false, ...
'Plots','training-progress');
net = trainNetwork(imdsTrain,layers,options);
YPred = classify(net,imdsValidation);
YValidation = imdsValidation.Labels;
accuracy = sum(YPred == YValidation)/numel(YValidation);
disp(accuracy);
模型结构:
运行结果:
使用卷积神经网络构造模型:
代码:
clear
clc
matlabroot = './FashionMnist';
digitDatasetPath = fullfile(matlabroot);
imds = imageDatastore(digitDatasetPath,'IncludeSubfolders',true,'LabelSource','foldernames');
numTrainFiles = 5000;
[imdsTrain,imdsValidation] = splitEachLabel(imds,numTrainFiles,'randomize');
layers = [
imageInputLayer([28 28 1],"Name","imageinput")
convolution2dLayer([3 3],8,"Name","conv","Padding","same")
batchNormalizationLayer("Name","batchnorm")
reluLayer("Name","relu")
maxPooling2dLayer([5 5],"Name","maxpool","Padding","same")
convolution2dLayer([3 3],16,"Name","conv_1","Padding","same")
batchNormalizationLayer("Name","batchnorm_1")
reluLayer("Name","relu_1")
fullyConnectedLayer(10,"Name","fc_3")
softmaxLayer("Name","softmax")
classificationLayer("Name","classoutput")];
options = trainingOptions('sgdm', ...
'InitialLearnRate',0.01, ...
'MaxEpochs',4, ...
'Shuffle','every-epoch', ...
'ValidationData',imdsValidation, ...
'ValidationFrequency',30, ...
'Verbose',false, ...
'Plots','training-progress');
net = trainNetwork(imdsTrain,layers,options);
YPred = classify(net,imdsValidation);
YValidation = imdsValidation.Labels;
accuracy = sum(YPred == YValidation)/numel(YValidation);
disp(accuracy);
模型结构:
运行结果:
希望能够帮助到你!如果有错误之处,敬请指正!

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐
所有评论(0)