Skip to content

Commit f4c7525

Browse files
committed
Refactor predicitons filtering for SSD
1 parent a42a877 commit f4c7525

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

model_api/cpp/models/src/detection_model_ssd.cpp

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
namespace {
3535
constexpr char saliency_map_name[]{"saliency_map"};
3636
constexpr char feature_vector_name[]{"feature_vector"};
37+
constexpr float box_area_threshold = 1.0f;
3738

3839
struct NumAndStep {
3940
size_t detectionsNum, objectSize;
@@ -83,6 +84,11 @@ std::vector<std::string> filterOutXai(const std::vector<std::string>& names) {
8384
std::copy_if (names.begin(), names.end(), std::back_inserter(filtered), [](const std::string& name){return name != saliency_map_name && name != feature_vector_name;});
8485
return filtered;
8586
}
87+
88+
89+
float clamp_and_round(float val, float min, float max) {
90+
return std::round(std::max(min, std::min(max, val)));
91+
}
8692
}
8793

8894
std::string ModelSSD::ModelType = "ssd";
@@ -214,23 +220,14 @@ std::unique_ptr<ResultBase> ModelSSD::postprocessMultipleOutputs(InferenceResult
214220
desc.confidence = confidence;
215221
desc.labelID = labels[i];
216222
desc.label = getLabelName(desc.labelID);
217-
desc.x = clamp(
218-
round((boxes[i * numAndStep.objectSize] * widthScale - padLeft) * invertedScaleX),
219-
0.f,
220-
floatInputImgWidth);
221-
desc.y = clamp(
222-
round((boxes[i * numAndStep.objectSize + 1] * heightScale - padTop) * invertedScaleY),
223-
0.f,
224-
floatInputImgHeight);
225-
desc.width = clamp(
226-
round((boxes[i * numAndStep.objectSize + 2] * widthScale - padLeft) * invertedScaleX),
227-
0.f,
228-
floatInputImgWidth) - desc.x;
229-
desc.height = clamp(
230-
round((boxes[i * numAndStep.objectSize + 3] * heightScale - padTop) * invertedScaleY),
231-
0.f,
232-
floatInputImgHeight) - desc.y;
233-
result->objects.push_back(desc);
223+
desc.x = clamp_and_round((boxes[i * numAndStep.objectSize] * widthScale - padLeft) * invertedScaleX, 0.f, floatInputImgWidth);
224+
desc.y = clamp_and_round((boxes[i * numAndStep.objectSize + 1] * heightScale - padTop) * invertedScaleY, 0.f, floatInputImgHeight);
225+
desc.width = clamp_and_round((boxes[i * numAndStep.objectSize + 2] * widthScale - padLeft) * invertedScaleX, 0.f, floatInputImgWidth) - desc.x;
226+
desc.height = clamp_and_round((boxes[i * numAndStep.objectSize + 3] * heightScale - padTop) * invertedScaleY, 0.f, floatInputImgHeight) - desc.y;
227+
228+
if (desc.width * desc.height >= box_area_threshold) {
229+
result->objects.push_back(desc);
230+
}
234231
}
235232
}
236233

0 commit comments

Comments
 (0)