[Bug fix]add yolov7face landmarks (#1297)

* add yolov7face benchmark

* fix review problem

* fix review problems
This commit is contained in:
CoolCola
2023-02-14 18:36:28 +08:00
committed by GitHub
parent c25d1cc1bc
commit a5d23c57d0
4 changed files with 48 additions and 6 deletions

View File

@@ -24,7 +24,7 @@ namespace facedet {
Yolov7FacePostprocessor::Yolov7FacePostprocessor() {
conf_threshold_ = 0.5;
nms_threshold_ = 0.45;
max_wh_ = 7680.0;
landmarks_per_face_ = 5;
}
bool Yolov7FacePostprocessor::Run(const std::vector<FDTensor>& infer_result,
@@ -36,6 +36,8 @@ bool Yolov7FacePostprocessor::Run(const std::vector<FDTensor>& infer_result,
for (size_t bs = 0; bs < batch; ++bs) {
(*results)[bs].Clear();
// must be setup landmarks_per_face before reserve
(*results)[bs].landmarks_per_face = landmarks_per_face_;
(*results)[bs].Reserve(infer_result[0].shape[1]);
if (infer_result[0].dtype != FDDataType::FP32) {
FDERROR << "Only support post process with float32 data." << std::endl;
@@ -61,6 +63,15 @@ bool Yolov7FacePostprocessor::Run(const std::vector<FDTensor>& infer_result,
(*results)[bs].boxes.emplace_back(std::array<float, 4>{
(x - w / 2.f), (y - h / 2.f), (x + w / 2.f), (y + h / 2.f)});
(*results)[bs].scores.push_back(confidence);
// decode landmarks (default 5 landmarks)
if (landmarks_per_face_ > 0) {
float* landmarks_ptr = const_cast<float*>(reg_cls_ptr + 6);
for (size_t j = 0; j < landmarks_per_face_ * 3; j += 3) {
(*results)[bs].landmarks.emplace_back(
std::array<float, 2>{landmarks_ptr[j], landmarks_ptr[j + 1]});
}
}
}
if ((*results)[bs].boxes.size() == 0) {
@@ -79,9 +90,9 @@ bool Yolov7FacePostprocessor::Run(const std::vector<FDTensor>& infer_result,
float ipt_h = iter_ipt->second[0];
float ipt_w = iter_ipt->second[1];
float scale = std::min(out_h / ipt_h, out_w / ipt_w);
float pad_h = (out_h - ipt_h * scale) / 2;
float pad_w = (out_w - ipt_w * scale) / 2;
for (size_t i = 0; i < (*results)[bs].boxes.size(); ++i) {
float pad_h = (out_h - ipt_h * scale) / 2;
float pad_w = (out_w - ipt_w * scale) / 2;
// clip box
(*results)[bs].boxes[i][0] = std::max(((*results)[bs].boxes[i][0] - pad_w) / scale, 0.0f);
(*results)[bs].boxes[i][1] = std::max(((*results)[bs].boxes[i][1] - pad_h) / scale, 0.0f);
@@ -92,6 +103,16 @@ bool Yolov7FacePostprocessor::Run(const std::vector<FDTensor>& infer_result,
(*results)[bs].boxes[i][2] = std::min((*results)[bs].boxes[i][2], ipt_w - 1.0f);
(*results)[bs].boxes[i][3] = std::min((*results)[bs].boxes[i][3], ipt_h - 1.0f);
}
// scale and clip landmarks
for (size_t i = 0; i < (*results)[bs].landmarks.size(); ++i) {
(*results)[bs].landmarks[i][0] =
std::max(((*results)[bs].landmarks[i][0] - pad_w) / scale, 0.0f);
(*results)[bs].landmarks[i][1] =
std::max(((*results)[bs].landmarks[i][1] - pad_h) / scale, 0.0f);
(*results)[bs].landmarks[i][0] = std::min((*results)[bs].landmarks[i][0], ipt_w - 1.0f);
(*results)[bs].landmarks[i][1] = std::min((*results)[bs].landmarks[i][1], ipt_h - 1.0f);
}
}
return true;
}

View File

@@ -56,11 +56,19 @@ class FASTDEPLOY_DECL Yolov7FacePostprocessor{
/// Get nms_threshold, default 0.45
float GetNMSThreshold() const { return nms_threshold_; }
/// Set landmarks_per_face, default 5
void SetLandmarksPerFace(const int& landmarks_per_face) {
landmarks_per_face_ = landmarks_per_face;
}
/// Get landmarks_per_face, default 5
int GetLandmarksPerFace() const { return landmarks_per_face_; }
protected:
float conf_threshold_;
float nms_threshold_;
bool multi_label_;
float max_wh_;
int landmarks_per_face_;
};
} // namespace facedet

View File

@@ -60,7 +60,8 @@ void BindYOLOv7Face(pybind11::module& m) {
return results;
})
.def_property("conf_threshold", &vision::facedet::Yolov7FacePostprocessor::GetConfThreshold, &vision::facedet::Yolov7FacePostprocessor::SetConfThreshold)
.def_property("nms_threshold", &vision::facedet::Yolov7FacePostprocessor::GetNMSThreshold, &vision::facedet::Yolov7FacePostprocessor::SetNMSThreshold);
.def_property("nms_threshold", &vision::facedet::Yolov7FacePostprocessor::GetNMSThreshold, &vision::facedet::Yolov7FacePostprocessor::SetNMSThreshold)
.def_property("landmarks_per_face", &vision::facedet::Yolov7FacePostprocessor::GetLandmarksPerFace, &vision::facedet::Yolov7FacePostprocessor::SetLandmarksPerFace);
pybind11::class_<vision::facedet::YOLOv7Face, FastDeployModel>(m, "YOLOv7Face")
.def(pybind11::init<std::string, std::string, RuntimeOption,

View File

@@ -107,6 +107,13 @@ class Yolov7FacePostprocessor:
"""
return self._postprocessor.nms_threshold
@property
def landmarks_per_face(self):
"""
landmarks per face for postprocessing, default is 5
"""
return self._postprocessor.landmarks_per_face
@conf_threshold.setter
def conf_threshold(self, conf_threshold):
assert isinstance(conf_threshold, float),\
@@ -119,6 +126,11 @@ class Yolov7FacePostprocessor:
"The value to set `nms_threshold` must be type of float."
self._postprocessor.nms_threshold = nms_threshold
@landmarks_per_face.setter
def landmarks_per_face(self, landmarks_per_face):
assert isinstance(landmarks_per_face, int),\
"The value to set `landmarks_per_face` must be type of int."
self._postprocessor.landmarks_per_face = landmarks_per_face
class YOLOv7Face(FastDeployModel):
def __init__(self,