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 个图像),使用特征提取。

相关推荐
Q26433650233 小时前
【有源码】基于 Hadoop 生态的化妆品销售数据存储分析与可视化 面向化妆品行业的用户画像构建与销售机会识别研究
大数据·hadoop·python·机器学习·spark·毕业设计·课程设计
菜鸟~noob2339 小时前
【电子战】 第09篇:阵列信号处理——波束形成、MVDR、MUSIC【含matlab代码】
开发语言·matlab·信号处理
菜鸟~noob23313 小时前
【电子战】第08篇:多普勒测向的工程实现与优化【含matlab代码】
开发语言·matlab
菜鸟~noob23313 小时前
【电子战】第06篇:干涉仪与相位差测向【含matlab代码】
开发语言·matlab
workflower14 小时前
人形机器人安全伦理标准
人工智能·安全·机器学习·机器人·无人机
千里码aicood17 小时前
基于机器学习的雷达低慢小目标识别技术的研究
人工智能·机器学习
leoZ23117 小时前
2026-09-10-静态扫描连错三次-用运行环境当裁判
前端·javascript·vue.js·人工智能·opencv·机器学习·数据挖掘
齐齐大魔王17 小时前
机器学习(七)
人工智能·机器学习
来让爷抱一个17 小时前
2026 纹理一致性实战:贴图不许花脸,百智云精灵图把材质契约写进素材包
人工智能·机器学习·材质·贴图
幻影123!18 小时前
AlphaZero 五子棋实战(五):评估工具 —— 学会在错误的地方掉头
人工智能·机器学习·强化学习·alpha zero