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

相关推荐
xx_xxxxx_1 小时前
论文阅读-SAR
人工智能·深度学习·机器学习
量子-Alex5 小时前
【大模型后训练SFT】Finetuning with Sampling: SFT Learns Better Than You Think
人工智能·深度学习·机器学习
天国梦6 小时前
哪个英语教学软件功能比较全面?我按五个维度拆了一遍
人工智能·机器学习
Evand J8 小时前
【MATLAB例程】三维RRT+APF路径规划与AOA-TDOA融合定位算法。附代码的下载链接
算法·matlab·路径规划·rrt·tdoa·apf·aoa
成为深度学习高手9 小时前
TimeBridge:用Integrated Attention与Cointegrated Attention分别桥接短期平稳与长期协整的时序预测模型
人工智能·机器学习·数据挖掘
fl1768319 小时前
工业镀金钨铜合金散热器表面缺陷识别污渍划痕分割数据集labelme格式1000张2类别低分辨率
人工智能·机器学习
成为深度学习高手10 小时前
SEMixer:以随机注意力增强patch语义、渐进混合多尺度的轻量长期时序预测模型
人工智能·深度学习·机器学习·数据挖掘·时间序列
阡陌数智11 小时前
大模型领域自适应微调:小样本场景下过拟合抑制与数据构建方法论
人工智能·深度学习·机器学习
Omics Pro11 小时前
之江实验室NAR|虚拟细胞3阶段训练范式
数据库·人工智能·算法·机器学习·自然语言处理
程序猿阿森12 小时前
Transformer 相对 CNN/RNN 的核心优势:全局依赖、并行计算与可扩展性
rnn·cnn·transformer