首页 > 其他分享 >【yolov4】基于yolov4深度学习网络目标检测MATLAB仿真

【yolov4】基于yolov4深度学习网络目标检测MATLAB仿真

时间:2022-10-10 17:05:13浏览次数:44  
标签:仿真 yolov4 helper %% testData image MATLAB net data


       YOLO发展至YOLOv3时,基本上这个系列都达到了一个高潮阶段,很多实际任务中,都会见到YOLOv3的身上,而对于较为简单和场景,比如没有太密集的目标和极端小的目标,多数时候仅用YOLOv2即可。除了YOLO系列,也还有其他很多优秀的工作,比如结构同样简洁的RetinaNet和SSD。后者SSD其实也会常在实际任务中见到,只不过就性能而言,要略差于YOLOv3,当然,这也是因为SSD并没有去做后续的升级,反倒很多新工作如RFB-Net、DSSD等工作都将其作为baseline。论性能,RetinaNet当然是不输于YOLOv3的,只是,相较于YOLOv3,RetinaNet的一个较为致命的问题就是:速度太慢。而这一个问题的主要原因就是RetinaNet使用较大的输出图像尺寸和较重的检测头。

yolov4的创新点
1.输入端的创新点:训练时对输入端的改进,主要包括Mosaic数据增强、cmBN、SAT自对抗训练

2.BackBone主干网络:各种方法技巧结合起来,包括:CSPDarknet53、Mish激活函数、Dropblock

3.Neck:目标检测网络在BackBone和最后的输出层之间往往会插入一些层,比如Yolov4中的SPP模块、FPN+PAN结构

4.Head:输出层的锚框机制和Yolov3相同,主要改进的是训练时的回归框位置损失函数CIOU_Loss,以及预测框筛选的nms变为DIOU_nms

       通俗的讲,就是说这个YOLO-v4算法是在原有YOLO目标检测架构的基础上,采用了近些年CNN领域中最优秀的优化策略,从数据处理、主干网络、网络训练、激活函数、损失函数等各个方面都有着不同程度的优化,虽没有理论上的创新,但是会受到许许多多的工程师的欢迎,各种优化算法的尝试。文章如同于目标检测的trick综述,效果达到了实现FPS与Precision平衡的目标检测 new baseline。

          yolov4 网络结构的采用的算法,其中保留了yolov3的head部分,修改了主干网络为CSPDarknet53,同时采用了SPP(空间金字塔池化)的思想来扩大感受野,PANet作为neck部分。

【yolov4】基于yolov4深度学习网络目标检测MATLAB仿真_sed

 

yolov4在技术处理的思维导图:

【yolov4】基于yolov4深度学习网络目标检测MATLAB仿真_人工智能_02

1.MATLAB源码

clc;
clear;
close all;
warning off;
addpath(genpath(pwd));

%****************************************************************************
%matlab/FPGA项目开发合作
%****************************************************************************
%% Download Pretrained Network
% Set the modelName from the above ones to download that pretrained model.
modelName = 'YOLOv4-coco';
model = helper.downloadPretrainedYOLOv4(modelName);
net = model.net;

%% Load Data
% Unzip the vehicle images and load the vehicle ground truth data.
unzip vehicleDatasetImages.zip
data = load('vehicleDatasetGroundTruth.mat');
vehicleDataset = data.vehicleDataset;

% Add the full path to the local vehicle data folder.
vehicleDataset.imageFilename = fullfile(pwd, vehicleDataset.imageFilename);


rng('default')
shuffledIndices = randperm(height(vehicleDataset));
idx = floor(0.6 * length(shuffledIndices));
trainingDataTbl = vehicleDataset(shuffledIndices(1:idx), :);
testDataTbl = vehicleDataset(shuffledIndices(idx+1:end), :);

% Create an image datastore for loading the images.
imdsTrain = imageDatastore(trainingDataTbl.imageFilename);
imdsTest = imageDatastore(testDataTbl.imageFilename);

% Create a datastore for the ground truth bounding boxes.
bldsTrain = boxLabelDatastore(trainingDataTbl(:, 2:end));
bldsTest = boxLabelDatastore(testDataTbl(:, 2:end));

% Combine the image and box label datastores.
trainingData = combine(imdsTrain, bldsTrain);
testData = combine(imdsTest, bldsTest);

helper.validateInputData(trainingData);
helper.validateInputData(testData);

%% Data Augmentation
augmentedTrainingData = transform(trainingData, @helper.augmentData);


augmentedData = cell(4,1);
for k = 1:4
data = read(augmentedTrainingData);
augmentedData{k} = insertShape(data{1,1}, 'Rectangle', data{1,2});
reset(augmentedTrainingData);
end
figure
montage(augmentedData, 'BorderSize', 10)

%% Preprocess Training Data
% Specify the network input size.
networkInputSize = net.Layers(1).InputSize;


preprocessedTrainingData = transform(augmentedTrainingData, @(data)helper.preprocessData(data, networkInputSize));

% Read the preprocessed training data.
data = read(preprocessedTrainingData);

% Display the image with the bounding boxes.
I = data{1,1};
bbox = data{1,2};
annotatedImage = insertShape(I, 'Rectangle', bbox);
annotatedImage = imresize(annotatedImage,2);
figure
imshow(annotatedImage)

% Reset the datastore.
reset(preprocessedTrainingData);

%% Modify Pretrained YOLO v4 Network

rng(0)
trainingDataForEstimation = transform(trainingData, @(data)helper.preprocessData(data, networkInputSize));
numAnchors = 9;
[anchorBoxes, meanIoU] = estimateAnchorBoxes(trainingDataForEstimation, numAnchors);

% Specify the classNames to be used in the training.
classNames = {'vehicle'};


[lgraph, networkOutputs, anchorBoxes, anchorBoxMasks] = configureYOLOv4(net, classNames, anchorBoxes, modelName);

%% Specify Training Options
numEpochs = 90;
miniBatchSize = 4;
learningRate = 0.001;
warmupPeriod = 1000;
l2Regularization = 0.001;
penaltyThreshold = 0.5;
velocity = [];

%% Train Model
if canUseParallelPool
dispatchInBackground = true;
else
dispatchInBackground = false;
end

mbqTrain = minibatchqueue(preprocessedTrainingData, 2,...
"MiniBatchSize", miniBatchSize,...
"MiniBatchFcn", @(images, boxes, labels) helper.createBatchData(images, boxes, labels, classNames), ...
"MiniBatchFormat", ["SSCB", ""],...
"DispatchInBackground", dispatchInBackground,...
"OutputCast", ["", "double"]);



% Convert layer graph to dlnetwork.
net = dlnetwork(lgraph);

% Create subplots for the learning rate and mini-batch loss.
fig = figure;
[lossPlotter, learningRatePlotter] = helper.configureTrainingProgressPlotter(fig);

iteration = 0;
% Custom training loop.
for epoch = 1:numEpochs

reset(mbqTrain);
shuffle(mbqTrain);

while(hasdata(mbqTrain))
iteration = iteration + 1;

[XTrain, YTrain] = next(mbqTrain);

% Evaluate the model gradients and loss using dlfeval(@modelGradients, net, XTrain, YTrain, anchorBoxes, anchorBoxMasks, penaltyThreshold, networkOutputs);

% Apply L2 regularization.
gradients = dlupdate(@(g,w) g + l2Regularization*w, gradients, net.Learnables);

% Determine the current learning rate value.
currentLR = helper.piecewiseLearningRateWithWarmup(iteration, epoch, learningRate, warmupPeriod, numEpochs);

% Update the network learnable parameters using the SGDM optimizer.
[net, velocity] = sgdmupdate(net, gradients, velocity, currentLR);

% Update the state parameters of dlnetwork.
net.State = state;

% Display progress.
if mod(iteration,10)==1
helper.displayLossInfo(epoch, iteration, currentLR, lossInfo);
end

% Update training plot with new points.
helper.updatePlots(lossPlotter, learningRatePlotter, iteration, currentLR, lossInfo.totalLoss);
end
end

% Save the trained model with the anchors.
anchors.anchorBoxes = anchorBoxes;
anchors.anchorBoxMasks = anchorBoxMasks;

save('yolov4_trained', 'net', 'anchors');

%% Evaluate Model
confidenceThreshold = 0.5;
overlapThreshold = 0.5;

% Create a table to hold the bounding boxes, scores, and labels returned by
% the detector.
numImages = size(testDataTbl, 1);
results = table('Size', [0 3], ...
'VariableTypes', {'cell','cell','cell'}, ...
'VariableNames', {'Boxes','Scores','Labels'});

% Run detector on images in the test set and collect results.
reset(testData)
while hasdata(testData)
% Read the datastore and get the image.
data = read(testData);
image = data{1};

% Run the detector.
executionEnvironment = 'auto';
[bboxes, scores, labels] = detectYOLOv4(net, image, anchors, classNames, executionEnvironment);

% Collect the results.
tbl = table({bboxes}, {scores}, {labels}, 'VariableNames', {'Boxes','Scores','Labels'});
results = [results; tbl];
end

% Evaluate the object detector using Average Precision metric.
[ap, recall, precision] = eval(results, testData);

% The precision-recall (PR) curve shows how precise a detector is at varying
% levels of recall. Ideally, the precision is 1 at all recall levels.

% Plot precision-recall curve.
figure
plot(recall, precision)
xlabel('Recall')
ylabel('Precision')
grid on
title(sprintf('Average Precision = %.2f', ap))

%% Detect Objects Using Trained YOLO v4
reset(testData)
data = read(testData);

% Get the image.
I = data{1};

% Run the detector.
executionEnvironment = 'auto';
[bboxes, scores, labels] = detectYOLOv4(net, I, anchors, classNames, executionEnvironment);

% Display the detections on image.
if ~isempty(scores)
I = insertObjectAnnotation(I, 'rectangle', bboxes, scores);
end
figure
imshow(I)

2.yolov4仿真效果

【yolov4】基于yolov4深度学习网络目标检测MATLAB仿真_目标检测_03

【yolov4】基于yolov4深度学习网络目标检测MATLAB仿真_目标检测_04

 

【yolov4】基于yolov4深度学习网络目标检测MATLAB仿真_深度学习_05

 

【yolov4】基于yolov4深度学习网络目标检测MATLAB仿真_yolov4_06

 

【yolov4】基于yolov4深度学习网络目标检测MATLAB仿真_深度学习_07

资源

 

标签:仿真,yolov4,helper,%%,testData,image,MATLAB,net,data
From: https://blog.51cto.com/u_15815923/5744757

相关文章

  • 基于matlab的图象拼接--数字图像拼接技术
    全景图(Panorama),或者说是图像拼接(Mosaic)技术是由于摄像器材的视角限制,不可能一次拍出很大图片而产生的。本文主要围绕拼接合成技术展开讨论,首先关注一下拼接的主要用途,再......
  • Matlab R2015b+CUDA7.5+vs2013深度学习网络GPU搭建
    传统的跟踪算法大多从物体的外观出发,只能在线学习,从当前的视频中在线抓取数据进行学习跟踪的算法,如:TLD、Struck、KCF,这类算法必须足够简单才行,否则耗时严重。当然现在也有人......
  • DTMF信号系统的Matlab仿真
        双音多频(DualToneMultiFrequency,DTMF)信号是音频电话中的拨号信号,由美国AT&T贝尔公司实验室研制,并用于电话网络中。这种信号制式具有很高的拨号速度,且容易......
  • 基于灰度变换的图像增强及MATLAB实现
    一  引言:图像增强技术是不考虑图像降质的原因,只将图像中感兴趣的特征有选择地突出,而衰减其不需要的特征,故改善后的图像不一定要去逼近原图像。如突出目标物轮廓,去除各类......
  • 基于Matlab实现的图像特效处理毕业设计
    数字图像是指由被称作象素的小块区域组成的二维矩阵。将一幅二维的图像通过有限个离散点来表示就成为了数字图像,其中的每个点称为图像元素,即像素。像素值往往用来表示像素的......
  • 基于matlab仿真的数字调制与解调设计
    随着通信系统复杂性的增加,传统的手工分析与电路板试验等分析设计方法已经不能适应发展的需要,通信系统计算机模拟仿真技术日益显示出其巨大的优越性。计算机仿真是根据被研究......
  • 基于matlab的-数字调制技术仿真
    数字信号的调制与解凋是实现频带传输的关键技术。数字调制有三种基本方式:幅移键控ASK、频移键控FSK、相移键控PSK。实际应用中基本上是采用这三种调制方式以及这些调制方式......
  • 基于matlab的连续系统的频域分析
    一、实验目的:1、掌握连续时间系统变换区域分析的基本方法。二、实验设备:安装有matlab6.5以上版本的PC机一台。三、实验内容、源程序及执行结果如图所示系统:(1)对不同的RC值......
  • matlab中的三维可视化实现
    图形三维立体可视化在Matlab中的实现和处理三维图形的绘制除了常用的网格图、表面图和等高线等方法外,Matlab还提供了一些立体可视化函数用于绘制更为复杂的立体和向量对象。......
  • 基于Qlearning的倒立摆控制算法matlab程序
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%clc;clearallcloseall%flops(0);%holdoff%%=====================......