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

相关推荐
Lambert-Wu6 小时前
常用激活函数总结
人工智能·深度学习·机器学习
ForDreamMusk6 小时前
批量归一化
人工智能·算法·机器学习
秉烛夜读9 小时前
机器学习:基本术语
机器学习
2zcode9 小时前
项目文档:基于MATLAB图像处理的乳腺癌辅助检测系统设计与实现
图像处理·人工智能·matlab
逆向编程11 小时前
如何使用MATLAB的bode函数进行伯德图分析?
开发语言·matlab
wahahaman12 小时前
基于GBDT的次日降水量预测实验
人工智能·python·机器学习·数据分析
学究天人13 小时前
数学公理体系大全:第一章 命题逻辑:真值之舞
人工智能·算法·机器学习·数学建模·动态规划·图论·抽象代数
齐昭王城13 小时前
PI0、PI0.5、PI0-FAST 原理讲解
人工智能·机器学习
workflower15 小时前
人形机器人全身运动控制能力大幅提升
人工智能·深度学习·机器学习·设计模式·机器人·自动化
秉烛夜读16 小时前
机器学习:简介
机器学习