【图像分类】基于卷积神经网络实现花朵图像分类附matlab代码

简介: 【图像分类】基于卷积神经网络实现花朵图像分类附matlab代码

✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。

🍎个人主页:Matlab科研工作室

🍊个人信条:格物致知。

更多Matlab仿真内容点击👇

智能优化算法  神经网络预测雷达通信 无线传感器

信号处理图像处理路径规划元胞自动机无人机 电力系统

⛄ 内容介绍

在图片搜索应用中,与基于时间,地点或关键词查找等成熟的搜索方式相比,基于图片内容的搜索具有更加直观快捷的特点.因此,利用图像分类技术实现基于图像内容的搜索成为一个日益受到关注的研究领域.一般地,机器学习中的分类算法可以分为两类:基于人为构造特征的分类方法,如K近邻算法,支持向量机等;以及以卷积神经网络为基础的,不需要人为设计特征的深度学习方法.

⛄ 部分代码

%% Flower Classifier using a CNN and data augmentation


%% PART 1: Baseline Classifier

%% Create image data store

imds = imageDatastore(fullfile('Flowers'),...

'IncludeSubfolders',true,'FileExtensions','.jpg','LabelSource','foldernames');


% Count number of images per label and save the number of classes

labelCount = countEachLabel(imds);

numClasses = height(labelCount);


%% Create training and validation sets

[imdsTrainingSet, imdsValidationSet] = splitEachLabel(imds, 0.7, 'randomize');


%% Build a simple CNN

imageSize = [227 227 3];


% Specify the convolutional neural network architecture.

layers = [

   imageInputLayer(imageSize)

   

   convolution2dLayer(3,8,'Padding','same')

   batchNormalizationLayer

   reluLayer  

   

   maxPooling2dLayer(2,'Stride',2)

   

   convolution2dLayer(3,16,'Padding','same')

   batchNormalizationLayer

   reluLayer  

   

   maxPooling2dLayer(2,'Stride',2)

   

   convolution2dLayer(3,32,'Padding','same')

   batchNormalizationLayer

   reluLayer  

   

   fullyConnectedLayer(4)

   softmaxLayer

   classificationLayer];


%% Specify training options

options = trainingOptions('sgdm', ...

   'MiniBatchSize',10, ...

   'MaxEpochs',6, ...

   'InitialLearnRate',1e-4, ...

   'Shuffle','every-epoch', ...

   'ValidationData',imdsValidationSet, ...

   'ValidationFrequency',3, ...

   'Verbose',false, ...

   'Plots','training-progress');


%% Train the network

net1 = trainNetwork(imdsTrainingSet,layers,options);


%% Report accuracy of baseline classifier on validation set

YPred = classify(net1,imdsValidationSet);

YValidation = imdsValidationSet.Labels;


imdsAccuracy = sum(YPred == YValidation)/numel(YValidation);


%% Plot confusion matrix

figure, plotconfusion(YValidation,YPred)


%% PART 2: Baseline Classifier with Data Augmentation

%% Create augmented image data store

% Specify data augmentation options and values/ranges

imageAugmenter = imageDataAugmenter( ...

   'RandRotation',[-20,20], ...

   'RandXTranslation',[-5 5], ...

   'RandYTranslation',[-5 5]);


% Apply transformations (using randomly picked values) and build augmented

% data store

augImds = augmentedImageDatastore(imageSize,imdsTrainingSet, ...

   'DataAugmentation',imageAugmenter);


% (OPTIONAL) Preview augmentation results

batchedData = preview(augImds);

figure, imshow(imtile(batchedData.input))

   

%% Train the network.

net2 = trainNetwork(augImds,layers,options);


%% Report accuracy of baseline classifier with image data augmentation

YPred = classify(net2,imdsValidationSet);

YValidation = imdsValidationSet.Labels;


augImdsAccuracy = sum(YPred == YValidation)/numel(YValidation);


%% Plot confusion matrix

figure, plotconfusion(YValidation,YPred)


%% PART 3: Transfer Learning without Data Augmentation


%% Load pretrained AlexNet

net = alexnet;


%% Replace final layers

layersTransfer = net.Layers(1:end-3);


layers = [

   layersTransfer

   fullyConnectedLayer(numClasses,'WeightLearnRateFactor',20,'BiasLearnRateFactor',20)

   softmaxLayer

   classificationLayer];


%% Train network

options = trainingOptions('sgdm', ...

   'MiniBatchSize',10, ...

   'MaxEpochs',6, ...

   'InitialLearnRate',1e-4, ...

   'Shuffle','every-epoch', ...

   'ValidationData',imdsValidationSet, ...

   'ValidationFrequency',3, ...

   'Verbose',false, ...

   'Plots','training-progress');


netTransfer1 = trainNetwork(imdsTrainingSet,layers,options);


%% Compute accuracy and plot confusion matrix

YPred = classify(netTransfer1,imdsValidationSet);

YValidation = imdsValidationSet.Labels;


netTransfer1BaselineAccuracy = sum(YPred == YValidation)/numel(YValidation);

figure, plotconfusion(YValidation,YPred)


%% PART 4: Transfer Learning with Data Augmentation


%% Train network

netTransfer2 = trainNetwork(augImds,layers,options);


%% Compute accuracy and plot confusion matrix

YPred = classify(netTransfer2,imdsValidationSet);

YValidation = imdsValidationSet.Labels;


netTransfer2BaselineAccuracy = sum(YPred == YValidation)/numel(YValidation);

figure, plotconfusion(YValidation,YPred)

⛄ 运行结果

⛄ 参考文献

[1]李晓普. 基于卷积神经网络的图像分类[D]. 大连理工大学, 2015.

⛄ 完整代码

❤️部分理论引用网络文献,若有侵权联系博主删除
❤️ 关注我领取海量matlab电子书和数学建模资料
相关文章
|
3月前
|
机器学习/深度学习 PyTorch TensorFlow
卷积神经网络深度解析:从基础原理到实战应用的完整指南
蒋星熠Jaxonic,深度学习探索者。深耕TensorFlow与PyTorch,分享框架对比、性能优化与实战经验,助力技术进阶。
|
4月前
|
传感器 机器学习/深度学习 算法
【UASNs、AUV】无人机自主水下传感网络中遗传算法的路径规划问题研究(Matlab代码实现)
【UASNs、AUV】无人机自主水下传感网络中遗传算法的路径规划问题研究(Matlab代码实现)
156 0
|
4月前
|
机器学习/深度学习 算法 调度
14种智能算法优化BP神经网络(14种方法)实现数据预测分类研究(Matlab代码实现)
14种智能算法优化BP神经网络(14种方法)实现数据预测分类研究(Matlab代码实现)
447 0
|
4月前
|
机器学习/深度学习 人工智能 算法
卷积神经网络深度解析:从基础原理到实战应用的完整指南
蒋星熠Jaxonic带你深入卷积神经网络(CNN)核心技术,从生物启发到数学原理,详解ResNet、注意力机制与模型优化,探索视觉智能的演进之路。
506 11
|
3月前
|
机器学习/深度学习 人工智能 算法
【基于TTNRBO优化DBN回归预测】基于瞬态三角牛顿-拉夫逊优化算法(TTNRBO)优化深度信念网络(DBN)数据回归预测研究(Matlab代码实现)
【基于TTNRBO优化DBN回归预测】基于瞬态三角牛顿-拉夫逊优化算法(TTNRBO)优化深度信念网络(DBN)数据回归预测研究(Matlab代码实现)
198 0
|
4月前
|
机器学习/深度学习 并行计算 算法
【CPOBP-NSWOA】基于豪冠猪优化BP神经网络模型的多目标鲸鱼寻优算法研究(Matlab代码实现)
【CPOBP-NSWOA】基于豪冠猪优化BP神经网络模型的多目标鲸鱼寻优算法研究(Matlab代码实现)
120 8
|
4月前
|
算法 数据挖掘 区块链
基于遗传算法的多式联运车辆路径网络优优化研究(Matlab代码实现)
基于遗传算法的多式联运车辆路径网络优优化研究(Matlab代码实现)
155 2
|
3月前
|
传感器 机器学习/深度学习 数据采集
【航空发动机寿命预测】基于SE-ResNet网络的发动机寿命预测,C-MAPSS航空发动机寿命预测研究(Matlab代码实现)
【航空发动机寿命预测】基于SE-ResNet网络的发动机寿命预测,C-MAPSS航空发动机寿命预测研究(Matlab代码实现)
319 0
|
4月前
|
机器学习/深度学习 传感器 算法
【表面粗糙度】基于粒子群PSO算法优化-BP神经网络的表面粗糙度研究(Matlab代码实现)
【表面粗糙度】基于粒子群PSO算法优化-BP神经网络的表面粗糙度研究(Matlab代码实现)
261 7
|
4月前
|
机器学习/深度学习 算法 数据可视化
PINN物理信息神经网络用于求解二阶常微分方程(ODE)的边值问题研究(Matlab代码实现)
PINN物理信息神经网络用于求解二阶常微分方程(ODE)的边值问题研究(Matlab代码实现)
382 6

热门文章

最新文章