当前位置: 首页 > news >正文

中国核工业华兴建设有限公司嘉兴做网站优化哪家好

中国核工业华兴建设有限公司,嘉兴做网站优化哪家好,正规网站建设加盟合作,wordpress cms 教程博客【opencv dnn模块 示例(3) 目标检测 object_detection (2) YOLO object detection】 测试了yolov3 及之前系列的模型#xff0c;有在博客【opencv dnn模块 示例(15) opencv4.2版本dnn支持cuda加速#xff08;vs2015异常解决#xff09;】 说明了如何使用dnn模块进行cuda…博客【opencv dnn模块 示例(3) 目标检测 object_detection (2) YOLO object detection】 测试了yolov3 及之前系列的模型有在博客【opencv dnn模块 示例(15) opencv4.2版本dnn支持cuda加速vs2015异常解决】 说明了如何使用dnn模块进行cuda加速推理。 本文说明yolo v4的网络改进和测试情况。 文章目录 1、前言2、Yolo v42.1、网络结构2.1.1、跨阶段部分网络Cross Stage Partial NetworksCSPNet2.1.2、空间金字塔池化网络Spatial Pyramid Pooling NetworkSPPNet2.1.3、空间注意力机制Spatial Attention ModuleSAM2.1.4、路径聚合网络Path Aggregation NetworkPANet2.1.5、Mish激活函数 2.2、改进之处2.2.1、马赛克Mosaic数据增强 CutMix数据增强2.2.2、自对抗训练Self-Adversarial TrainingSAT2.2.3、改进的DropoutDropBlock2.2.4、标签平滑Label Smoothing2.2.5、CIoU Loss损失函数2.2.6、DIoU-NMS 3、测试3.1、训练3.2、测试代码 1、前言 yolo v1~v3 的作者Joseph Redmon于2020年初宣布停止一切CV研究原因是自己的开源算法已经用在军事和隐私问题上对他的道德造成了巨大的考验。他的退出是学术界对于AI道德问题一记警钟。salute… 俄罗斯大神AlexAB作为YOLO系列的继任者在Redmon宣发放弃更新Yolo后两个月发布了YOLOv4。我们以前在windows上测试YOLO算法时使用的就是AlexAB 的 darknet开源项目 版本。 首先回顾下Yolo v3网络整个主干网络Backbone为Darkent53包含53个卷积层全连接fc层用于图像分类这里已移除输出head包含三个尺度输入为608*608时分别为19*1938*3876*76。 CBL 是Yolov3网络结构中的最小组件 Res Unit 借鉴Resnet中残差结构让网络构建更深 ResX 是Yolov3的大组件每个Res模块前CBL起到下采样过程输入60*608的图像经过5次Res模块后得到的特征图是608-304-152-76-38-19。 Concat张量拼接会扩充两个张量的维度例如26*26*256和26*26*512两个张量拼接结果是26*26*768。Concat和cfg文件中的route功能一样。将大感受野的特征图进行上采样得到于小感受野特征图相同的大小进行维度拼接达到多尺度特征融合的目的从而加强小目标检测能力。 add张量相加张量直接相加不会扩充维度例如104*104*128和104*104*128相加结果还是104*104*128。add和cfg文件中的shortcut功能一样。 2、Yolo v4 核心思想 yolov4筛选了一些从yolov3发布至今被用在各式各样检测器上能够提高检测精度的tricks并加以组合及适当创新的算法实现了速度和精度的完美平衡。虽然有许多技巧可以提高卷积神经网络CNN的准确性但是某些技巧仅适合在某些模型上运行或者仅在某些问题上运行或者仅在小型数据集上运行。 主要调优手段 加权残差连接(WRC)、跨阶段部分连接(CSP)、跨小批量标准化(CmBN)、自对抗训练(SAT)、Mish激活、马赛克数据增强、CmBN、DropBlock正则化、CIoU Loss等等。经过一系列的堆料终于实现了目前最优的实验结果43.5的AP(在Tesla V100上MS COCO数据集的实时速度约为 65FPS)。 2.1、网络结构 相较于Darknet53网络YoloV4的骨干网络使用CSPDarknet53如下图 主要区别 1将原来的Darknet53与CSPNet进行结合形成Backbone网络。 2采用SPPNet适应不同尺寸的输入图像大小且可以增大感受野 3采用SAM引入空间注意力机制 4采用PANet充分利用了特征融合 5激活函数由MIsh替换Leaky ReLU 在yolov3中每个卷积层之后包含一个批归一化层和一个Leaky ReLU。而在yolov4的主干网络CSPDarknet53中使用Mish替换原来的Leak ReLU。 2.1.1、跨阶段部分网络Cross Stage Partial NetworksCSPNet 2019年提出用来解决网络优化中的重复梯度信息问题在ImageNet dataset和MS COCO数据集上有很好的测试效果。且易于实现在ResNet、ResNeXt和DenseNet网络结构上都能通用。 CSPNet结构实现更丰富的梯度组合同时减少计算量将基本层的特征图分成两部分1主干部分继续堆叠原来的残差块2、支路部分则相当于一个残差边经过少量处理直接连接到最后。 在这里插入图片描述 2.1.2、空间金字塔池化网络Spatial Pyramid Pooling NetworkSPPNet yolov1背景yolov1训练时的分辨率224×224测试时448×448。 yolov2背景yolov2保持yolov1的操作不变但在原训练的基础上又加上了10个epoch的448×448高分辨率样本进行微调使网络特征逐渐适应 448×448 的分辨率然后再使用 448×448 的样本进行测试缓解了分辨率突然切换造成的影响。 目的使得网络模型的输入图像不再有固定尺寸的大小限制。通过最大池化将不同尺寸的输入图像变得尺寸一致。 优点增大感受野。 2.1.3、空间注意力机制Spatial Attention ModuleSAM yolov4采用改进的SAM方法Channel attention moduleCAM - SAMSpatial Attention Module - CBAMConvolutional Block AM - 改进的SAM 特征图注意力机制Channel Attention Module在Channel维度上对每一个特征图channel加一个权重然后通过sigmoid得到对应的概率值最后乘上输入图像相当于对输入图像的特征图进行加权即注意力。如32×32×256对256个通道进行加权。 空间注意力机制Spatial Attention Module在Spatial维度上对每一个空间位置Spatial加一个权重然后通过sigmoid得到对应的概率值最后乘上输入图像相当于对输入图像的所有位置特征进行加权即注意力。如32×32×256对任意空间位置进行 优化原因 1由于CBAM计算比较复杂且耗时而yolo的出发点是速度故只计算空间位置的注意力机制。 2常规的SAM最大值池化层和平均池化层分别作用于输入的feature map得到两组shape相同的feature map再将结果输入到一个卷积层。 过程过于复杂yolo采取直接卷积进行简化。 2.1.4、路径聚合网络Path Aggregation NetworkPANet 背景 PANet发表于CVPR2018其是COCO2017实例分割比赛的冠军也是目标检测比赛的第二名。 具体方式 yolov4采用改进的PANet方法 优化历程 FPNetFeature Pyramid Networks - PANetPath Aggregation Network - 改进的PAN 优化原因 1FPNet网络采取自上而下的方式将高层特征逐层与中高层、中层、中底层、低层特征进行融合。缺点是无法自下而上融合而PANet的优化了该部分不足详见示意图的b部分。 2FANet采用特征相加的融合方式而yolo采用特征拼接的融合方式。加法可以得到一个加强版的特征图但特征权重不大于1而拼接可能得到大于1的特征图。 aFPNet通过 融合高层特征 来提升目标检测的效果。 bBottom-up Path Augmentation通过 融合低层特征边缘形状等来提升目标检测的效果。 cAdaptive Feature Pooling采用 拼接特征融合。拼接相比加法特征更明显可以提高检测效果。 dBox branch类别和定位分支。 eFully-connected Fusion用于分割中像素级的预测。 2.1.5、Mish激活函数 Mish在负值的时候并不是完全截断允许比较小的负梯度流入。实验中随着层深的增加ReLU激活函数精度迅速下降而Mish激活函数在训练稳定性、平均准确率(1%-2.8%)、峰值准确率(1.2% - 3.6%)等方面都有全面的提高。 2.2、改进之处 BackBone训练策略数据增强、自对抗训练、DropBlock正则化、类标签平滑、CIoU损失函数、DIoU-NMS等。 2.2.1、马赛克Mosaic数据增强 CutMix数据增强 最大特点使得yolov4只通过单CPU就能完成训练不用再担心设备问题。 具体方式 11、采用常用的数据增强方法如亮度、饱和度、对比度随机缩放、旋转、翻转等对所有的图像进行数据增强 22、采用CutMix数据增强方法。详细见下。 33、采取马赛克Mosaic数据增强方法即随机取四张图像拼接为一张图像。 2.2.2、自对抗训练Self-Adversarial TrainingSAT 在第一阶段在原始图像的基础上添加噪音并设置权重阈值让神经网络对自身进行对抗性攻击训练。 在第二阶段用正常的方法训练神经网络去检测目标。 备注详细可参考对抗攻击的快速梯度符号法FGSM。 2.2.3、改进的DropoutDropBlock 之前的dropout是随机删除一些点现在是整块删除。 b图Dropout是随机删除一些神经元如a图的红点但对于整张图来说效果并不明显。比如眼睛被删除我们仍然可以通过眼睛的周边特征眼角、眼圈等去近似识别。 c图DropBlock是随机删除一大块神经元。 如将狗头的左耳全部删除。 2.2.4、标签平滑Label Smoothing 问题标签绝对化要么0要么1。该现象将导致神经网络在训练过程中自我良好从而过拟合。 具体方式将绝对化标签进行平滑 如[00] ~ [0.050.95] 即分类结果具有一定的模糊化使得网络的抗过拟合能力增强。 使用前分类结果相对不错但各类别之间存在一定的误差使用后分类结果比较好簇内距离变小簇间距离变大。 2.2.5、CIoU Loss损失函数 效果采用CIoU Loss损失函数使得预测框回归的速度和精度更高一些。 loss优化历程经典IOU损失 - GIOU损失Generalized IoU - DIOU损失Distance IoU - CIOU损失。 Iou仅考虑有目标框有交集的重叠面积情况GIou考虑边界框不重合的问题、可以在没有交集情况下继续训练DIou在前面的基础上考虑了边界框的中心点的欧氏距离CIou进一步考虑长宽比的尺度信息。 2.2.6、DIoU-NMS 在检测结果中若存在多个检测框的IOU大于置信度阈值 1NMS非极大值抑制只取IoU最大值对应的框。 2DIoU-NMS只取公式计算得到的最大值对应的框。取最高置信度的IoU并计算最高置信度候选框M与其余所有框Bi的中心点距离。优点在有遮挡的情况下识别效果更好。 3SOFT-NMS对于不满足要求且与最大置信度对应的检测框高度重叠的检测框不直接删除而采取降低置信度的方式。优点召回率更高 3、测试 yolov3 和 yolov4 使用相同代码即可测试。可以参看 【opencv dnn模块 示例(3) 目标检测 object_detection (2) YOLO object detection】。 项目介绍和下载地址 https://github.com/AlexeyAB/darknet#pre-trained-models 3.1、训练 yolo v4同样使用darknet框架训练直接使用官方程序脚本同yolov3即可。 3.2、测试代码 #include fstream #include sstream#include opencv2/dnn.hpp #include opencv2/imgproc.hpp #include opencv2/highgui.hppusing namespace cv; using namespace dnn;float confThreshold, nmsThreshold; std::vectorstd::string classes;void postprocess(Mat frame, const std::vectorMat out, Net net);void drawPred(int classId, float conf, int left, int top, int right, int bottom, Mat frame);void callback(int pos, void* userdata);int main(int argc, char** argv) {// 根据选择的检测模型文件进行配置 confThreshold 0.5;nmsThreshold 0.4;float scale 0.00392;Scalar mean {0,0,0};bool swapRB true;int inpWidth 416; // 416, 608 ...int inpHeight 416;String modelPath ../../data/testdata/dnn/yolov4.weights;String configPath ../../data/testdata/dnn/yolov4.cfg;String framework ;//int backendId cv::dnn::DNN_BACKEND_OPENCV;//int targetId cv::dnn::DNN_TARGET_CPU; int backendId cv::dnn::DNN_BACKEND_CUDA;int targetId cv::dnn::DNN_TARGET_CUDA;String classesFile ../../data/dnn/object_detection_classes_yolov4.txt;// Open file with classes names.if (!classesFile.empty()) {const std::string file classesFile;std::ifstream ifs(file.c_str());if (!ifs.is_open())CV_Error(Error::StsError, File file not found);std::string line;while (std::getline(ifs, line)) {classes.push_back(line);}}// Load a model.Net net readNet(modelPath, configPath, framework);net.setPreferableBackend(backendId);net.setPreferableTarget(targetId);std::vectorString outNames net.getUnconnectedOutLayersNames();// Create a windowstatic const std::string kWinName Deep learning object detection in OpenCV;// Open a video file or an image file or a camera stream.VideoCapture cap;cap.open(0);// Process frames.Mat frame, blob;while (waitKey(1) 0) {cap frame;if (frame.empty()) {waitKey();break;}// Create a 4D blob from a frame.Size inpSize(inpWidth 0 ? inpWidth : frame.cols,inpHeight 0 ? inpHeight : frame.rows);blobFromImage(frame, blob, scale, inpSize, mean, swapRB, false);// Run a model.net.setInput(blob);if (net.getLayer(0)-outputNameToIndex(im_info) ! -1) // Faster-RCNN or R-FCN{resize(frame, frame, inpSize);Mat imInfo (Mat_float(1, 3) inpSize.height, inpSize.width, 1.6f);net.setInput(imInfo, im_info);}std::vectorMat outs;net.forward(outs, outNames);postprocess(frame, outs, net);// Put efficiency information.std::vectordouble layersTimes;double freq getTickFrequency() / 1000;double t net.getPerfProfile(layersTimes) / freq;std::string label format(Inference time: %.2f ms, t);putText(frame, label, Point(0, 15), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 255, 0));imshow(kWinName, frame);}return 0; }void postprocess(Mat frame, const std::vectorMat outs, Net net) {static std::vectorint outLayers net.getUnconnectedOutLayers();static std::string outLayerType net.getLayer(outLayers[0])-type;std::vectorint classIds;std::vectorfloat confidences;std::vectorRect boxes;if (net.getLayer(0)-outputNameToIndex(im_info) ! -1) // Faster-RCNN or R-FCN{// Network produces output blob with a shape 1x1xNx7 where N is a number of// detections and an every detection is a vector of values// [batchId, classId, confidence, left, top, right, bottom]CV_Assert(outs.size() 1);float* data (float*)outs[0].data;for (size_t i 0; i outs[0].total(); i 7) {float confidence data[i 2];if (confidence confThreshold) {int left (int)data[i 3];int top (int)data[i 4];int right (int)data[i 5];int bottom (int)data[i 6];int width right - left 1;int height bottom - top 1;classIds.push_back((int)(data[i 1]) - 1); // Skip 0th background class id.boxes.push_back(Rect(left, top, width, height));confidences.push_back(confidence);}}}else if (outLayerType DetectionOutput) {// Network produces output blob with a shape 1x1xNx7 where N is a number of// detections and an every detection is a vector of values// [batchId, classId, confidence, left, top, right, bottom]CV_Assert(outs.size() 1);float* data (float*)outs[0].data;for (size_t i 0; i outs[0].total(); i 7) {float confidence data[i 2];if (confidence confThreshold) {int left (int)(data[i 3] * frame.cols);int top (int)(data[i 4] * frame.rows);int right (int)(data[i 5] * frame.cols);int bottom (int)(data[i 6] * frame.rows);int width right - left 1;int height bottom - top 1;classIds.push_back((int)(data[i 1]) - 1); // Skip 0th background class id.boxes.push_back(Rect(left, top, width, height));confidences.push_back(confidence);}}}else if (outLayerType Region) {for (size_t i 0; i outs.size(); i) {// Network produces output blob with a shape NxC where N is a number of// detected objects and C is a number of classes 4 where the first 4// numbers are [center_x, center_y, width, height]float* data (float*)outs[i].data;for (int j 0; j outs[i].rows; j, data outs[i].cols) {Mat scores outs[i].row(j).colRange(5, outs[i].cols);Point classIdPoint;double confidence;minMaxLoc(scores, 0, confidence, 0, classIdPoint);if (confidence confThreshold) {int centerX (int)(data[0] * frame.cols);int centerY (int)(data[1] * frame.rows);int width (int)(data[2] * frame.cols);int height (int)(data[3] * frame.rows);int left centerX - width / 2;int top centerY - height / 2;classIds.push_back(classIdPoint.x);confidences.push_back((float)confidence);boxes.push_back(Rect(left, top, width, height));}}}}elseCV_Error(Error::StsNotImplemented, Unknown output layer type: outLayerType);std::vectorint indices;NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, indices);for (size_t i 0; i indices.size(); i) {int idx indices[i];Rect box boxes[idx];drawPred(classIds[idx], confidences[idx], box.x, box.y,box.x box.width, box.y box.height, frame);} }void drawPred(int classId, float conf, int left, int top, int right, int bottom, Mat frame) {rectangle(frame, Point(left, top), Point(right, bottom), Scalar(0, 255, 0));std::string label format(%.2f, conf);if (!classes.empty()) {CV_Assert(classId (int)classes.size());label classes[classId] : label;}int baseLine;Size labelSize getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, baseLine);top max(top, labelSize.height);rectangle(frame, Point(left, top - labelSize.height),Point(left labelSize.width, top baseLine), Scalar::all(255), FILLED);putText(frame, label, Point(left, top), FONT_HERSHEY_SIMPLEX, 0.5, Scalar()); }
http://www.dnsts.com.cn/news/12246.html

相关文章:

  • 文案推广发布网站大全专业的网站设计师
  • 如皋网站建设做网站的伪原创怎么弄
  • 几年前我为客户建设网站长沙seo搜索
  • 西瓜网络深圳网站建设 东莞网站建设建设规划工程许可证在当地什么网站
  • 网站设计师专业网站改版要多少钱
  • 给网站添加百度地图网站首页文件名通常是
  • 东光网站制作网站规划内容方案
  • 网站开发 前景做网站服务器收费吗
  • wordpress网站主修改艾融软件是外包公司么
  • 网站源码下载 app高质量的网站内容建设
  • 医院网站系统源码用哪个软件制作网页
  • 常州建设局职称网站活动宣传推广的形式有哪些
  • 北京天仪建设工程质量检测所网站网站建设方案报价费用明细价格
  • 正规代加工项目重庆网站免费优化
  • 关于进一步加强网站建设自己公司怎样做免费的网站
  • 网站抓取诊断创网站
  • 前端如何兼职做网站html 如何嵌入网站页面
  • 网站建设评分标准专门做外链的网站
  • 做网站的价企业管理咨询经营范围有哪些
  • 做网站三大主流框架北京建设注册中心网站首页
  • 做拍卖网站有哪些专业做app软件开发公司
  • 南宁网站开发公司php 网站开发收费
  • 如何将网站让百度收录物流网站方案
  • 做分销商城网站的郑州网站优化关键词
  • 网站建设设计问卷游戏程序员工资大概多少
  • 建设大型网站制作品牌seo推广技巧
  • 用啥网站做首页做的好的网站着陆页
  • 网站开发虚拟主机是什么商业设计理念
  • 带做网站价位自己在家搭建服务器
  • 网站设计 优帮云wordpress分站点