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

相关推荐
头歌实践平台1 小时前
头歌 卷积神经网络卷积核和结构设计实验
人工智能·深度学习·cnn
逻辑君1 小时前
Foresight研究报告【20260013】
人工智能·机器学习
明月照山海-1 小时前
机器学习周报四十七
人工智能·机器学习
【建模先锋】2 小时前
独家复现!从 FFT 频域特征到全连接图分类:基于图卷积神经网络 GCN 的滚动轴承故障诊断模型
深度学习·分类·cnn·信号处理·代码分享·图卷积神经网络·轴承故障诊断
Bingorl2 小时前
机器学习之朴素贝叶斯算法
人工智能·算法·机器学习
星浩AI2 小时前
(六)模型微调效果测试:基于 BERT 的中文评价情感分析[附源码]
人工智能·机器学习·llm
weixin_468466853 小时前
机器学习数据预处理新手实战指南
人工智能·python·算法·机器学习·编程·数据预处理
zhangfeng11333 小时前
glibc = GNU C Library (GNU C 标准库)CentOS 7 (glibc 2.17) pip支持
c语言·人工智能·神经网络·机器学习·centos·gnu
春日见3 小时前
自动驾驶数据驱动规控进化之路
运维·服务器·人工智能·深度学习·算法·机器学习·自动驾驶