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


           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在技术处理的思维导图:

    1.MATLAB源码

    1. clc;
    2. clear;
    3. close all;
    4. warning off;
    5. addpath(genpath(pwd));
    6. %****************************************************************************
    7. %更多关于matlab和fpga的搜索“fpga和matlab”的CSDN博客:
    8. %matlab/FPGA项目开发合作
    9. %https://blog.csdn.net/ccsss22?type=blog
    10. %****************************************************************************
    11. %% Download Pretrained Network
    12. % Set the modelName from the above ones to download that pretrained model.
    13. modelName = 'YOLOv4-coco';
    14. model = helper.downloadPretrainedYOLOv4(modelName);
    15. net = model.net;
    16. %% Load Data
    17. % Unzip the vehicle images and load the vehicle ground truth data.
    18. unzip vehicleDatasetImages.zip
    19. data = load('vehicleDatasetGroundTruth.mat');
    20. vehicleDataset = data.vehicleDataset;
    21. % Add the full path to the local vehicle data folder.
    22. vehicleDataset.imageFilename = fullfile(pwd, vehicleDataset.imageFilename);
    23. rng('default')
    24. shuffledIndices = randperm(height(vehicleDataset));
    25. idx = floor(0.6 * length(shuffledIndices));
    26. trainingDataTbl = vehicleDataset(shuffledIndices(1:idx), :);
    27. testDataTbl = vehicleDataset(shuffledIndices(idx+1:end), :);
    28. % Create an image datastore for loading the images.
    29. imdsTrain = imageDatastore(trainingDataTbl.imageFilename);
    30. imdsTest = imageDatastore(testDataTbl.imageFilename);
    31. % Create a datastore for the ground truth bounding boxes.
    32. bldsTrain = boxLabelDatastore(trainingDataTbl(:, 2:end));
    33. bldsTest = boxLabelDatastore(testDataTbl(:, 2:end));
    34. % Combine the image and box label datastores.
    35. trainingData = combine(imdsTrain, bldsTrain);
    36. testData = combine(imdsTest, bldsTest);
    37. helper.validateInputData(trainingData);
    38. helper.validateInputData(testData);
    39. %% Data Augmentation
    40. augmentedTrainingData = transform(trainingData, @helper.augmentData);
    41. augmentedData = cell(4,1);
    42. for k = 1:4
    43. data = read(augmentedTrainingData);
    44. augmentedData{k} = insertShape(data{1,1}, 'Rectangle', data{1,2});
    45. reset(augmentedTrainingData);
    46. end
    47. figure
    48. montage(augmentedData, 'BorderSize', 10)
    49. %% Preprocess Training Data
    50. % Specify the network input size.
    51. networkInputSize = net.Layers(1).InputSize;
    52. preprocessedTrainingData = transform(augmentedTrainingData, @(data)helper.preprocessData(data, networkInputSize));
    53. % Read the preprocessed training data.
    54. data = read(preprocessedTrainingData);
    55. % Display the image with the bounding boxes.
    56. I = data{1,1};
    57. bbox = data{1,2};
    58. annotatedImage = insertShape(I, 'Rectangle', bbox);
    59. annotatedImage = imresize(annotatedImage,2);
    60. figure
    61. imshow(annotatedImage)
    62. % Reset the datastore.
    63. reset(preprocessedTrainingData);
    64. %% Modify Pretrained YOLO v4 Network
    65. rng(0)
    66. trainingDataForEstimation = transform(trainingData, @(data)helper.preprocessData(data, networkInputSize));
    67. numAnchors = 9;
    68. [anchorBoxes, meanIoU] = estimateAnchorBoxes(trainingDataForEstimation, numAnchors);
    69. % Specify the classNames to be used in the training.
    70. classNames = {'vehicle'};
    71. [lgraph, networkOutputs, anchorBoxes, anchorBoxMasks] = configureYOLOv4(net, classNames, anchorBoxes, modelName);
    72. %% Specify Training Options
    73. numEpochs = 90;
    74. miniBatchSize = 4;
    75. learningRate = 0.001;
    76. warmupPeriod = 1000;
    77. l2Regularization = 0.001;
    78. penaltyThreshold = 0.5;
    79. velocity = [];
    80. %% Train Model
    81. if canUseParallelPool
    82. dispatchInBackground = true;
    83. else
    84. dispatchInBackground = false;
    85. end
    86. mbqTrain = minibatchqueue(preprocessedTrainingData, 2,...
    87. "MiniBatchSize", miniBatchSize,...
    88. "MiniBatchFcn", @(images, boxes, labels) helper.createBatchData(images, boxes, labels, classNames), ...
    89. "MiniBatchFormat", ["SSCB", ""],...
    90. "DispatchInBackground", dispatchInBackground,...
    91. "OutputCast", ["", "double"]);
    92. % Convert layer graph to dlnetwork.
    93. net = dlnetwork(lgraph);
    94. % Create subplots for the learning rate and mini-batch loss.
    95. fig = figure;
    96. [lossPlotter, learningRatePlotter] = helper.configureTrainingProgressPlotter(fig);
    97. iteration = 0;
    98. % Custom training loop.
    99. for epoch = 1:numEpochs
    100. reset(mbqTrain);
    101. shuffle(mbqTrain);
    102. while(hasdata(mbqTrain))
    103. iteration = iteration + 1;
    104. [XTrain, YTrain] = next(mbqTrain);
    105. % Evaluate the model gradients and loss using dlfeval and the
    106. % modelGradients function.
    107. [gradients, state, lossInfo] = dlfeval(@modelGradients, net, XTrain, YTrain, anchorBoxes, anchorBoxMasks, penaltyThreshold, networkOutputs);
    108. % Apply L2 regularization.
    109. gradients = dlupdate(@(g,w) g + l2Regularization*w, gradients, net.Learnables);
    110. % Determine the current learning rate value.
    111. currentLR = helper.piecewiseLearningRateWithWarmup(iteration, epoch, learningRate, warmupPeriod, numEpochs);
    112. % Update the network learnable parameters using the SGDM optimizer.
    113. [net, velocity] = sgdmupdate(net, gradients, velocity, currentLR);
    114. % Update the state parameters of dlnetwork.
    115. net.State = state;
    116. % Display progress.
    117. if mod(iteration,10)==1
    118. helper.displayLossInfo(epoch, iteration, currentLR, lossInfo);
    119. end
    120. % Update training plot with new points.
    121. helper.updatePlots(lossPlotter, learningRatePlotter, iteration, currentLR, lossInfo.totalLoss);
    122. end
    123. end
    124. % Save the trained model with the anchors.
    125. anchors.anchorBoxes = anchorBoxes;
    126. anchors.anchorBoxMasks = anchorBoxMasks;
    127. save('yolov4_trained', 'net', 'anchors');
    128. %% Evaluate Model
    129. confidenceThreshold = 0.5;
    130. overlapThreshold = 0.5;
    131. % Create a table to hold the bounding boxes, scores, and labels returned by
    132. % the detector.
    133. numImages = size(testDataTbl, 1);
    134. results = table('Size', [0 3], ...
    135. 'VariableTypes', {'cell','cell','cell'}, ...
    136. 'VariableNames', {'Boxes','Scores','Labels'});
    137. % Run detector on images in the test set and collect results.
    138. reset(testData)
    139. while hasdata(testData)
    140. % Read the datastore and get the image.
    141. data = read(testData);
    142. image = data{1};
    143. % Run the detector.
    144. executionEnvironment = 'auto';
    145. [bboxes, scores, labels] = detectYOLOv4(net, image, anchors, classNames, executionEnvironment);
    146. % Collect the results.
    147. tbl = table({bboxes}, {scores}, {labels}, 'VariableNames', {'Boxes','Scores','Labels'});
    148. results = [results; tbl];
    149. end
    150. % Evaluate the object detector using Average Precision metric.
    151. [ap, recall, precision] = evaluateDetectionPrecision(results, testData);
    152. % The precision-recall (PR) curve shows how precise a detector is at varying
    153. % levels of recall. Ideally, the precision is 1 at all recall levels.
    154. % Plot precision-recall curve.
    155. figure
    156. plot(recall, precision)
    157. xlabel('Recall')
    158. ylabel('Precision')
    159. grid on
    160. title(sprintf('Average Precision = %.2f', ap))
    161. %% Detect Objects Using Trained YOLO v4
    162. reset(testData)
    163. data = read(testData);
    164. % Get the image.
    165. I = data{1};
    166. % Run the detector.
    167. executionEnvironment = 'auto';
    168. [bboxes, scores, labels] = detectYOLOv4(net, I, anchors, classNames, executionEnvironment);
    169. % Display the detections on image.
    170. if ~isempty(scores)
    171. I = insertObjectAnnotation(I, 'rectangle', bboxes, scores);
    172. end
    173. figure
    174. imshow(I)

    2.yolov4仿真效果

     

     

     

    资源

     

  • 相关阅读:
    Springboot人体健康检测微信小程序毕业设计-附源码012142
    青年领袖分论坛精彩回顾 | 第二届始祖数字化可持续发展峰会
    小程序picker-view 初始值设置的坑
    用anacnda创建虚拟环境用不用指定python版本
    深度分析AMQP以及在rabbitMQ中的应用
    数据库和缓存如何保持一致性
    【打卡】【Linux的设备驱动管理之内核对象】21天学习挑战赛—RK3399平台开发入门到精通-Day15
    微信小程序学习(二):常用样式和组件
    QT中QIcon图标不显示的问题
    DHTMLX JS Gantt Library 7.1.13
  • 原文地址:https://blog.csdn.net/ccsss22/article/details/125464984