1.32、 基于区域卷积神经网络(R-CNN)的停车标志检测(matlab)

1、基于区域卷积神经网络(R-CNN)的停车标志检测原理及流程

基于区域卷积神经网络(R-CNN)的停车标志检测原理及流程如下:

  1. 原理: R-CNN 是一种用于目标检测的深度学习模型,其核心思想是首先在输入图像中提取出候选区域(Region Proposal),然后对提取的候选区域进行卷积神经网络(CNN)特征提取和目标分类。

  2. 流程

    • 候选区域提取:首先使用选择性搜索(Selective Search)等算法从输入图像中提取出多个候选区域,这些候选区域可能包含潜在的目标。
    • 特征提取:对每个候选区域进行裁剪和缩放,然后使用预训练的卷积神经网络(如VGG、ResNet等)提取特征。
    • 目标分类:将提取的特征输入到一个支持向量机(SVM)分类器中,用于判断每个候选区域中是否包含停车标志。
    • 回归框:对被分类为停车标志的候选区域进行回归操作,将其位置优化。
    • 非极大值抑制:对重叠的候选区域进行非极大值抑制,保留得分最高的停车标志框。
  3. 训练: 在训练阶段,需要构建一个包含标注停车标志框的训练数据集。利用这些训练样本,通过监督学习的方式训练R-CNN模型,使其能够准确地检测停车标志。

  4. 评估和调优: 在训练完成后,需要对R-CNN模型进行评估,可以通过精度、召回率等指标评估模型的性能,并根据需要对模型进行调优以提高检测准确率。

R-CNN 基于候选区域的思想,能够准确地定位和识别输入图像中的目标物体。停车标志检测作为目标检测的一个应用场景,使用R-CNN可以有效地检测出图像中的停车标志,有助于自动驾驶、智能交通等领域的应用。

2、基于区域卷积神经网络(R-CNN)的停车标志检测说明

训练用于检测停车标志的 R-CNN 目标检测器

R-CNN 是一个目标检测框架,它使用卷积神经网络 (CNN) 对图像中的图像区域进行分类

R-CNN 检测器不使用滑动窗对每个区域进行分类,而是只处理那些可能包含对象的区域。这大幅降低了运行 CNN 时的计算成本。

使用 CIFAR-10 数据集对一个 CNN 进行预训练,该数据集有 50,000 个训练图像。然后,只使用 41 个训练图像针对停车标志检测对这个预训练的 CNN 进行微调。如果没有预训练 CNN,训练停车标志检测器会需要更多图像。

3、下载 CIFAR-10 图像数据

1)将 CIFAR-10 数据下载到一个临时目录

实现代码

cifar10Data = tempdir;
url = 'https://www.cs.toronto.edu/~kriz/cifar-10-matlab.tar.gz';
helperCIFAR10Data.download(url,cifar10Data);

2)加载 CIFAR-10 训练和测试数据

实现代码

[trainingImages,trainingLabels,testImages,testLabels] = helperCIFAR10Data.load(cifar10Data);

3)每个图像参数

实现代码

size(trainingImages)

4)CIFAR-10 有 10 个图像类别

实现代码

numImageCategories = 10;
categories(trainingLabels)

4、创建卷积神经网络 (CNN)

1)创建网络

CNN 由一系列层组成,每层定义一项特定计算

imageInputLayer - 图像输入层

convolution2dLayer - 卷积神经网络的二维卷积层

reluLayer - 修正线性单元 (ReLU) 层

maxPooling2dLayer - 最大池化层

fullyConnectedLayer - 全连接层

softmaxLayer- Softmax 层

classificationLayer - 神经网络的分类输出层

实现代码

[height,width,numChannels, ~] = size(trainingImages);

imageSize = [height width numChannels];
inputLayer = imageInputLayer(imageSize)

2)定义网络的中间层

中间层包含多个由卷积层、ReLU(修正线性单元)层和池化层组成的重复模块。

 这三个层构成卷积神经网络的核心构建模块。卷积层定义滤波器权重集,这些权重集在网络训练期间会更新。

ReLU 层在网络中引入非线性,让网络能够逼近非线性函数,这些函数将图像像素映射到图像语义内容。池化层在数据流经网络时对其进行下采样。

 在具有许多层的网络中,应谨慎使用池化层,以避免过早对网络中的数据进行下采样。

实现代码

filterSize = [5 5];
numFilters = 32;

middleLayers = [
convolution2dLayer(filterSize,numFilters,'Padding',2)
reluLayer()
maxPooling2dLayer(3,'Stride',2)
convolution2dLayer(filterSize,numFilters,'Padding',2)
reluLayer()
maxPooling2dLayer(3, 'Stride',2)

convolution2dLayer(filterSize,2 * numFilters,'Padding',2)
reluLayer()
maxPooling2dLayer(3,'Stride',2)
]

3)CNN 的最终层

CNN 的最终层通常包括全连接层和 softmax 损失层。

实现代码

finalLayers = [

fullyConnectedLayer(64)

reluLayer
fullyConnectedLayer(numImageCategories)

softmaxLayer
classificationLayer
]

4)对输入层、中间层和最终层进行合并

实现代码

layers = [
    inputLayer
    middleLayers
    finalLayers
    ]

5)初始化权重

使用标准差为 0.0001 的正态分布随机数初始化第一个卷积层的权重。这有助于改善训练的收敛性。

实现代码

layers(2).Weights = 0.0001 * randn([filterSize numChannels numFilters]);

5、训练CNN

1)使用 trainingOptions 函数设置网络训练算法

网络训练算法使用具有动量的随机梯度下降 (SGDM),初始学习率为 0.001。

在训练期间,初始学习率每 8 轮降低一次(1 轮定义为对整个训练数据集进行一次完整遍历)。训练算法运行 40 轮。

实现代码

opts = trainingOptions('sgdm', ...
    'Momentum', 0.9, ...
    'InitialLearnRate', 0.001, ...
    'LearnRateSchedule', 'piecewise', ...
    'LearnRateDropFactor', 0.1, ...
    'LearnRateDropPeriod', 8, ...
    'L2Regularization', 0.004, ...
    'MaxEpochs', 40, ...
    'MiniBatchSize', 128, ...
    'Verbose', true);

2) 使用 trainNetwork 函数训练网络

实现代码

doTraining = false;

if doTraining    
    % Train a network.
    cifar10Net = trainNetwork(trainingImages, trainingLabels, layers, opts);
else
    % Load pre-trained detector for the example.
    load('rcnnStopSigns.mat','cifar10Net')       
end

6、验证 CIFAR-10 网络训练

1)可视化第一个卷积层

快速可视化第一个卷积层的滤波器权重有助于识别训练中的任何直接问题

实现代码

w = cifar10Net.Layers(2).Weights;
% rescale the weights to the range [0, 1] for better visualization
w = rescale(w);
figure
montage(w)

2)使用 CIFAR-10 测试数据来测量网络的分类准确度

实现代码

YTest = classify(cifar10Net, testImages);

% Calculate the accuracy.
accuracy = sum(YTest == testLabels)/numel(testLabels)

 视图效果

7、加载训练数据

1)加载停车标志的真实值数据

实现代码

% Load the ground truth data
data = load('stopSignsAndCars.mat', 'stopSignsAndCars');
stopSignsAndCars = data.stopSignsAndCars;

% Update the path to the image files to match the local file system
visiondata = fullfile(toolboxdir('vision'),'visiondata');
stopSignsAndCars.imageFilename = fullfile(visiondata, stopSignsAndCars.imageFilename);

% Display a summary of the ground truth data
summary(stopSignsAndCars)

2)数据标签

实现代码

stopSigns = stopSignsAndCars(:, {'imageFilename','stopSign'});

% Display one training image and the ground truth bounding boxes
I = imread(stopSigns.imageFilename{1});
I = insertObjectAnnotation(I,'Rectangle',stopSigns.stopSign{1},'stop sign','LineWidth',8);

figure
imshow(I)

视图效果

 

8、训练 R-CNN 停车标志检测器

实现代码

doTraining = false;

if doTraining
    
    % Set training options
    options = trainingOptions('sgdm', ...
        'MiniBatchSize', 128, ...
        'InitialLearnRate', 1e-3, ...
        'LearnRateSchedule', 'piecewise', ...
        'LearnRateDropFactor', 0.1, ...
        'LearnRateDropPeriod', 100, ...
        'MaxEpochs', 100, ...
        'Verbose', true);
    
    % Train an R-CNN object detector. This will take several minutes.    
    rcnn = trainRCNNObjectDetector(stopSigns, cifar10Net, options, ...
    'NegativeOverlapRange', [0 0.3], 'PositiveOverlapRange',[0.5 1])
else
    % Load pre-trained network for the example.
    load('rcnnStopSigns.mat','rcnn')       
end

9、测试 R-CNN 停车标志检测器

1)对测试图像试用该检测器

实现代码

% Read test image
testImage = imread('stopSignTest.jpg');

% Detect stop signs
[bboxes,score,label] = detect(rcnn,testImage,'MiniBatchSize',128)

2)R-CNN 对象 detect (Computer Vision Toolbox) 方法返回每个检测的对象边界框、检测分数和类标签

实现代码

[score, idx] = max(score);

bbox = bboxes(idx, :);
annotation = sprintf('%s: (Confidence = %f)', label(idx), score);

outputImage = insertObjectAnnotation(testImage, 'rectangle', bbox, annotation);

figure
imshow(outputImage)

视图效果 

10、总结

基于区域卷积神经网络(R-CNN)的停车标志检测在 MATLAB 中实现的总结如下:

  1. 数据准备

    • 准备包含停车标志和其对应标注框的训练数据集和测试数据集。
    • 可以使用 MATLAB 的图像标注工具进行标注,并将标注信息保存为MATLAB数据格式。
  2. 模型构建

    • 使用MATLAB内置的卷积神经网络工具箱构建R-CNN模型,包括候选区域提取、特征提取、目标分类和位置回归等模块。
    • 在模型中集成先前训练好的卷积神经网络作为特征提取器。
  3. 训练模型

    • 利用训练数据集对R-CNN模型进行训练,包括特征提取、目标分类和位置回归这几个阶段。
    • 可以使用MATLAB深度学习工具箱提供的训练接口和函数来进行模型的训练。
  4. 评估模型

    • 使用测试数据集对训练好的R-CNN模型进行评估,评估指标包括准确率、召回率、F1分数等。
    • 可以通过混淆矩阵等方式分析模型的性能。
  5. 应用模型

    • 在实际应用中,使用训练好的R-CNN模型对输入图像进行停车标志检测。
    • 可以将检测到的停车标志框标注在图像上,或者输出检测结果的位置和类别信息。

总体而言,利用 MATLAB 中的深度学习工具箱和图像处理工具箱,可以比较方便地实现基于R-CNN的停车标志检测。通过合理的数据准备、模型构建、训练和评估流程,可以开发出准确、高效的停车标志检测系统。

11、源代码

代码

%% 基于区域卷积神经网络(R-CNN)的停车标志检测
%训练用于检测停车标志的 R-CNN 目标检测器
%R-CNN 是一个目标检测框架,它使用卷积神经网络 (CNN) 对图像中的图像区域进行分类
%R-CNN 检测器不使用滑动窗对每个区域进行分类,而是只处理那些可能包含对象的区域。这大幅降低了运行 CNN 时的计算成本。
%使用 CIFAR-10 数据集对一个 CNN 进行预训练,该数据集有 50,000 个训练图像。然后,只使用 41 个训练图像针对停车标志检测对这个预训练的 CNN 进行微调。如果没有预训练 CNN,训练停车标志检测器会需要更多图像。
%% 下载 CIFAR-10 图像数据
%将 CIFAR-10 数据下载到一个临时目录
cifar10Data = tempdir;
url = 'https://www.cs.toronto.edu/~kriz/cifar-10-matlab.tar.gz';
helperCIFAR10Data.download(url,cifar10Data);
%加载 CIFAR-10 训练和测试数据
[trainingImages,trainingLabels,testImages,testLabels] = helperCIFAR10Data.load(cifar10Data);
%每个图像是一个 32×32 RGB 图像,共有 50,000 个训练样本
size(trainingImages)
%CIFAR-10 有 10 个图像类别
numImageCategories = 10;
categories(trainingLabels)
%% 创建卷积神经网络 (CNN)
%CNN 由一系列层组成,每层定义一项特定计算
%imageInputLayer - 图像输入层
%convolution2dLayer - 卷积神经网络的二维卷积层
%reluLayer - 修正线性单元 (ReLU) 层
%maxPooling2dLayer - 最大池化层
%fullyConnectedLayer - 全连接层
%softmaxLayer- Softmax 层
%classificationLayer - 神经网络的分类输出层
[height,width,numChannels, ~] = size(trainingImages);

imageSize = [height width numChannels];
inputLayer = imageInputLayer(imageSize)
%定义网络的中间层。
%中间层包含多个由卷积层、ReLU(修正线性单元)层和池化层组成的重复模块。
% 这三个层构成卷积神经网络的核心构建模块。卷积层定义滤波器权重集,这些权重集在网络训练期间会更新。
% ReLU 层在网络中引入非线性,让网络能够逼近非线性函数,这些函数将图像像素映射到图像语义内容。池化层在数据流经网络时对其进行下采样。
% 在具有许多层的网络中,应谨慎使用池化层,以避免过早对网络中的数据进行下采样。
filterSize = [5 5];
numFilters = 32;

middleLayers = [
convolution2dLayer(filterSize,numFilters,'Padding',2)
reluLayer()
maxPooling2dLayer(3,'Stride',2)
convolution2dLayer(filterSize,numFilters,'Padding',2)
reluLayer()
maxPooling2dLayer(3, 'Stride',2)

convolution2dLayer(filterSize,2 * numFilters,'Padding',2)
reluLayer()
maxPooling2dLayer(3,'Stride',2)
]

%CNN 的最终层通常包括全连接层和 softmax 损失层。
finalLayers = [

fullyConnectedLayer(64)

reluLayer
fullyConnectedLayer(numImageCategories)

softmaxLayer
classificationLayer
]
%对输入层、中间层和最终层进行合并
layers = [
    inputLayer
    middleLayers
    finalLayers
    ]
%初始化第一个卷积层的权重
%使用标准差为 0.0001 的正态分布随机数初始化第一个卷积层的权重。这有助于改善训练的收敛性。
layers(2).Weights = 0.0001 * randn([filterSize numChannels numFilters]);
%% 训练CNN
%使用 trainingOptions 函数设置网络训练算法
%网络训练算法使用具有动量的随机梯度下降 (SGDM),初始学习率为 0.001。
% 在训练期间,初始学习率每 8 轮降低一次(1 轮定义为对整个训练数据集进行一次完整遍历)。训练算法运行 40 轮。
opts = trainingOptions('sgdm', ...
    'Momentum', 0.9, ...
    'InitialLearnRate', 0.001, ...
    'LearnRateSchedule', 'piecewise', ...
    'LearnRateDropFactor', 0.1, ...
    'LearnRateDropPeriod', 8, ...
    'L2Regularization', 0.004, ...
    'MaxEpochs', 40, ...
    'MiniBatchSize', 128, ...
    'Verbose', true);
%使用 trainNetwork 函数训练网络
doTraining = false;

if doTraining    
    % Train a network.
    cifar10Net = trainNetwork(trainingImages, trainingLabels, layers, opts);
else
    % Load pre-trained detector for the example.
    load('rcnnStopSigns.mat','cifar10Net')       
end
%% 验证 CIFAR-10 网络训练
%可视化第一个卷积层
%快速可视化第一个卷积层的滤波器权重有助于识别训练中的任何直接问题
w = cifar10Net.Layers(2).Weights;

% rescale the weights to the range [0, 1] for better visualization
w = rescale(w);

figure
montage(w)

%使用 CIFAR-10 测试数据来测量网络的分类准确度
YTest = classify(cifar10Net, testImages);

% Calculate the accuracy.
accuracy = sum(YTest == testLabels)/numel(testLabels)
%% 加载训练数据
%加载停车标志的真实值数据
% Load the ground truth data
data = load('stopSignsAndCars.mat', 'stopSignsAndCars');
stopSignsAndCars = data.stopSignsAndCars;

% Update the path to the image files to match the local file system
visiondata = fullfile(toolboxdir('vision'),'visiondata');
stopSignsAndCars.imageFilename = fullfile(visiondata, stopSignsAndCars.imageFilename);

% Display a summary of the ground truth data
summary(stopSignsAndCars)
% 数据标签
stopSigns = stopSignsAndCars(:, {'imageFilename','stopSign'});

% Display one training image and the ground truth bounding boxes
I = imread(stopSigns.imageFilename{1});
I = insertObjectAnnotation(I,'Rectangle',stopSigns.stopSign{1},'stop sign','LineWidth',8);

figure
imshow(I)
%% 训练 R-CNN 停车标志检测器
%使用 trainRCNNObjectDetector (Computer Vision Toolbox) 训练 R-CNN 目标检测器
doTraining = false;

if doTraining
    
    % Set training options
    options = trainingOptions('sgdm', ...
        'MiniBatchSize', 128, ...
        'InitialLearnRate', 1e-3, ...
        'LearnRateSchedule', 'piecewise', ...
        'LearnRateDropFactor', 0.1, ...
        'LearnRateDropPeriod', 100, ...
        'MaxEpochs', 100, ...
        'Verbose', true);
    
    % Train an R-CNN object detector. This will take several minutes.    
    rcnn = trainRCNNObjectDetector(stopSigns, cifar10Net, options, ...
    'NegativeOverlapRange', [0 0.3], 'PositiveOverlapRange',[0.5 1])
else
    % Load pre-trained network for the example.
    load('rcnnStopSigns.mat','rcnn')       
end
%% 测试 R-CNN 停车标志检测器
%对测试图像试用该检测器
% Read test image
testImage = imread('stopSignTest.jpg');

% Detect stop signs
[bboxes,score,label] = detect(rcnn,testImage,'MiniBatchSize',128)
%R-CNN 对象 detect (Computer Vision Toolbox) 方法返回每个检测的对象边界框、检测分数和类标签
[score, idx] = max(score);

bbox = bboxes(idx, :);
annotation = sprintf('%s: (Confidence = %f)', label(idx), score);

outputImage = insertObjectAnnotation(testImage, 'rectangle', bbox, annotation);

figure
imshow(outputImage)

工程文件

https://download.csdn.net/download/XU157303764/89538510

  • 27
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逼子歌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值