MNIST数据集上卷积神经网络微调(MATLAB例)

This example shows how to retrain a pretrained SqueezeNet neural network to perform classification on a new collection of images.

matlab 复制代码
filename = 'mnist';

digitDatasetPath = fullfile(matlabroot,'toolbox','nnet','nndemos', ...
    'nndatasets','DigitDataset');
imds = imageDatastore(digitDatasetPath, ...
    'IncludeSubfolders',true,'LabelSource','foldernames');
labelCount = countEachLabel(imds);

numImages = numel(imds.Labels);
classNames = categories(imds.Labels)
numClasses = numel(classNames)
[imdsTrain,imdsValidation,imdsTest] = splitEachLabel(imds,0.7,0.15,"randomized");

%% 
net = imagePretrainedNetwork("squeezenet",NumClasses=numClasses)

% inputSize = net.Layers(1).InputSize(1:2);
inputSize = networkInputSize(net)
[layerName,learnableNames] = networkHead(net)
net = freezeNetwork(net,LayerNamesToIgnore=layerName);

augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain, ColorPreprocessing='gray2rgb');
augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation,ColorPreprocessing='gray2rgb');
augimdsTest = augmentedImageDatastore(inputSize(1:2),imdsTest,ColorPreprocessing='gray2rgb');
%% 
options = trainingOptions("adam", ...
    ValidationData=augimdsValidation, ...
    ValidationFrequency=5, ...
    Plots="training-progress", ...
    Metrics="accuracy", ...
    Verbose=false);
net = trainnet(augimdsTrain,net,"crossentropy",options);
%% 
YTest = minibatchpredict(net,augimdsTest);
YTest = scores2label(YTest,classNames);
Visualize the classification accuracy in a confusion chart.

TTest = imdsTest.Labels;
figure
confusionchart(TTest,YTest)

对于非常小的数据集(每个类不到 20 个图像),使用特征提取。

相关推荐
不可求~8 小时前
用 AI 读论文别只看摘要:建立可追溯的证据链
人工智能·深度学习·机器学习
今天AI了吗8 小时前
Python 基础语法(一):常量、变量、输入输出与运算符
开发语言·数据库·人工智能·python·sql·深度学习·机器学习
空堂与归9 小时前
用户分群找不到规律?用K-Means聚类算法自动发现数据模式
算法·机器学习·kmeans·聚类
wen_zhufeng12 小时前
IndexTTS 2.5 技术报告
人工智能·算法·机器学习
程高兴13 小时前
三相两电平光伏MPPT定步长扰动观察法Simulink
matlab
FL162386312917 小时前
人员聚集人群拥挤密度检测数据集VOC+YOLO格式163张2类别
人工智能·yolo·机器学习
智购科技自动售货机厂家17 小时前
2026自动售货机AI视觉识别优化:从YOLOv11n模型量化到RKNN部署的端侧推理工程实践~YH
javascript·人工智能·yolo·机器学习·计算机视觉·目标跟踪·perl
Tasiorh18 小时前
《随机森林》知识点总结
算法·随机森林·机器学习
pjj1985419 小时前
机器学习-NumPy2
人工智能·python·机器学习
AR-26710-20 小时前
机器学习复习Day1——线性回归编程作业
人工智能·机器学习