4开启训练
4.1 下载源码
git clone https://github.com/deepcam-cn/yolov5-face
4.2 下载widerface数据集
下载网址:
https://drive.google.com/file/d/1tU_IjyOwGQfGNUvZGwWWM4SwxKp2PUQ8/view?usp=sharing
下载后,解压缩位置放到yolov5-face-master项目里data文件夹下的widerface文件夹下。
4.3 运行train2yolo.py和val2yolo.py
把数据集转成yolo训练用的格式。完成后文件夹显示如下:
4.4 运行train.py
过程显示如下:
5OpenCV-C++部署
5.1 参数配置
该部分主要是输入输出尺寸、Anchor以及Strides设置等
const float anchors[3][6] = { {4,5, 8,10, 13,16}, {23,29, 43,55, 73,105}, {146,217, 231,300, 335,433} }; const float stride[3] = { 8.0, 16.0, 32.0 }; const int inpWidth = 640; const int inpHeight = 640; float confThreshold; float nmsThreshold; float objThreshold;
5.2 模型加载以及Sigmoid的定义
该部分主要设ONNX模型的加载。
YOLO::YOLO(Net_config config) { cout << "Net use " << config.netname << endl; this->confThreshold = config.confThreshold; this->nmsThreshold = config.nmsThreshold; this->objThreshold = config.objThreshold; strcpy_s(this->netname, config.netname.c_str()); string modelFile = this->netname; modelFile += "-face.onnx"; this->net = readNet(modelFile); } void YOLO::sigmoid(Mat* out, int length) { float* pdata = (float*)(out->data); int i = 0; for (i = 0; i < length; i++) { pdata[i] = 1.0 / (1 + expf(-pdata[i])); } }
5.3 后处理部分
这里对于坐标的处理和YOLOV5保持一致,但是由于多出来Landmark,所以也多出了这一部分的处理:
if (box_score > this->objThreshold) { // 该部分与yolov5的保持一致 float face_score = sigmoid_x(pdata[15]); float cx = (sigmoid_x(pdata[0]) * 2.f - 0.5f + j) * this->stride[n]; ///cx float cy = (sigmoid_x(pdata[1]) * 2.f - 0.5f + i) * this->stride[n]; ///cy float w = powf(sigmoid_x(pdata[2]) * 2.f, 2.f) * anchor_w; ///w float h = powf(sigmoid_x(pdata[3]) * 2.f, 2.f) * anchor_h; ///h int left = (cx - 0.5*w)*ratiow; int top = (cy - 0.5*h)*ratioh; confidences.push_back(face_score); boxes.push_back(Rect(left, top, (int)(w*ratiow), (int)(h*ratioh))); // landmark的处理 vector<int> landmark(10); for (k = 5; k < 15; k+=2) { const int ind = k - 5; landmark[ind] = (int)(pdata[k] * anchor_w + j * this->stride[n])*ratiow; landmark[ind + 1] = (int)(pdata[k + 1] * anchor_h + i * this->stride[n])*ratioh; } landmarks.push_back(landmark); //} }
6参考
[1].https://github.com/hpc203/yolov5-face-landmarks-opencv-v2
[2].https://github.com/deepcam-cn/yolov5-face
[3].YOLO5Face: Why Reinventing a Face Detector
[4].https://zhuanlan.zhihu.com/p/375966269