一:YOLOv5简介:
YOLOv5是一种单阶段目标检测算法,该算法在YOLOv4的基础上添加了一些新的改进思路,使其速度与精度都得到了极大的性能提升。主要的改进思路如下所示:
输入端:在模型训练阶段,提出了一些改进思路,主要包括Mosaic数据增强、自适应锚框计算、自适应图片缩放;
基准网络:融合其它检测算法中的一些新思路,主要包括:Focus结构与CSP结构;
Neck网络:目标检测网络在BackBone与最后的Head输出层之间往往会插入一些层,Yolov5中添加了FPN+PAN结构;
Head输出层:输出层的锚框机制与YOLOv4相同,主要改进的是训练时的损失函数GIOU_Loss,以及预测框筛选的DIOU_nms。
二:思路
1.YOLOv5环境搭建:百度
2.数据集标注及训练:百度
3.模型训练:以上两步完成的情况下,一键训练
4模型导出:pt模型转onnx模型,以实现opencv的dnn模块推理,一键导出
5.C++封装动态链接库
6.VM算法模块调用封装好的动态链接库
7.模块打包测试
三:过程
C++封装动态链接库:
1.vs下安装配置opencv
2.封装推理函数DLL 以下给出关键推理部分
void YoloV5::detect(const cv::Mat& mat, std::vector<Result>& detected_boxes,
float score_threshold, float iou_threshold)
{
if (mat.empty()) return;
// this->transform(mat);
const int input_height = input_node_dims.at(2);
const int input_width = input_node_dims.at(3);
int img_height = static_cast<int>(mat.rows);
int img_width = static_cast<int>(mat.cols);
// resize & unscale
cv::Mat mat_rs;
YoloV5ScaleParams scale_params;
this->resize_unscale(mat, mat_rs, input_height, input_width, scale_params);
// 1. make input tensor
Ort::Value input_tensor = this->transform(mat_rs);
// 2. inference scores & boxes.
auto output_tensors = ort_session->Run(
Ort::RunOptions{ nullptr }, input_node_names.data(),
&input_tensor, 1, output_node_names.data(), num_outputs
);
//3. rescale & exclude & nms
generate_bboxes_nms(scale_params, detected_boxes, output_tensors, score_threshold, iou_threshold, img_height, img_width);
}
3.封装其他函数,加载模型和类别,设置阈值等
VM算法模块调用封装好的动态链接库
1.借助工具生成三个工程
2.配置简单界面
3.调用已封装好的动态链接库:
1.复制所需dll到VM的公共文件夹下
2.编写算法模块
3.调试
(这部分站内已有详细教程)
四:结果(附件有测试视频)