Skip to content

Commit a948ea6

Browse files
committed
review updates
1 parent 2a36cf8 commit a948ea6

15 files changed

+70
-141
lines changed

demos/common/cpp/models/include/models/detection_model_faceboxes.h

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include <utility>
2323
#include <vector>
2424

25+
#include <utils/nms.hpp>
26+
2527
#include "models/detection_model.h"
2628

2729
namespace ov {
@@ -32,25 +34,6 @@ struct ResultBase;
3234

3335
class ModelFaceBoxes : public DetectionModel {
3436
public:
35-
struct Anchor {
36-
float left;
37-
float top;
38-
float right;
39-
float bottom;
40-
41-
float getWidth() const {
42-
return (right - left) + 1.0f;
43-
}
44-
float getHeight() const {
45-
return (bottom - top) + 1.0f;
46-
}
47-
float getXCenter() const {
48-
return left + (getWidth() - 1.0f) / 2.0f;
49-
}
50-
float getYCenter() const {
51-
return top + (getHeight() - 1.0f) / 2.0f;
52-
}
53-
};
5437
static const int INIT_VECTOR_SIZE = 200;
5538

5639
ModelFaceBoxes(const std::string& modelFileName,

demos/common/cpp/models/include/models/detection_model_retinaface.h

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <string>
2121
#include <vector>
2222

23+
#include <utils/nms.hpp>
24+
2325
#include "models/detection_model.h"
2426

2527
namespace ov {
@@ -30,26 +32,6 @@ struct ResultBase;
3032

3133
class ModelRetinaFace : public DetectionModel {
3234
public:
33-
struct Anchor {
34-
float left;
35-
float top;
36-
float right;
37-
float bottom;
38-
39-
float getWidth() const {
40-
return (right - left) + 1.0f;
41-
}
42-
float getHeight() const {
43-
return (bottom - top) + 1.0f;
44-
}
45-
float getXCenter() const {
46-
return left + (getWidth() - 1.0f) / 2.0f;
47-
}
48-
float getYCenter() const {
49-
return top + (getHeight() - 1.0f) / 2.0f;
50-
}
51-
};
52-
5335
static const int LANDMARKS_NUM = 5;
5436
static const int INIT_VECTOR_SIZE = 200;
5537
/// Loads model and performs required initialization

demos/common/cpp/models/include/models/detection_model_retinaface_pt.h

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <vector>
2323

2424
#include <opencv2/core/types.hpp>
25+
#include <utils/nms.hpp>
2526

2627
#include "models/detection_model.h"
2728

@@ -41,26 +42,6 @@ class ModelRetinaFacePT : public DetectionModel {
4142
float height;
4243
};
4344

44-
struct Rect {
45-
float left;
46-
float top;
47-
float right;
48-
float bottom;
49-
50-
float getWidth() const {
51-
return (right - left) + 1.0f;
52-
}
53-
float getHeight() const {
54-
return (bottom - top) + 1.0f;
55-
}
56-
float getXCenter() const {
57-
return left + (getWidth() - 1.0f) / 2.0f;
58-
}
59-
float getYCenter() const {
60-
return top + (getHeight() - 1.0f) / 2.0f;
61-
}
62-
};
63-
6445
/// Loads model and performs required initialization
6546
/// @param model_name name of model to load
6647
/// @param confidenceThreshold - threshold to eliminate low-confidence detections.
@@ -91,10 +72,10 @@ class ModelRetinaFacePT : public DetectionModel {
9172
int imgWidth,
9273
int imgHeight);
9374
std::vector<ModelRetinaFacePT::Box> generatePriorData();
94-
std::vector<ModelRetinaFacePT::Rect> getFilteredProposals(const ov::Tensor& boxesTensor,
95-
const std::vector<size_t>& indicies,
96-
int imgWidth,
97-
int imgHeight);
75+
std::vector<Anchor> getFilteredProposals(const ov::Tensor& boxesTensor,
76+
const std::vector<size_t>& indicies,
77+
int imgWidth,
78+
int imgHeight);
9879

9980
void prepareInputsOutputs(std::shared_ptr<ov::Model>& model) override;
10081
};

demos/common/cpp/models/include/models/detection_model_yolov3_onnx.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ class ModelYoloV3ONNX: public DetectionModel {
4242

4343
protected:
4444
void prepareInputsOutputs(std::shared_ptr<ov::Model>& model) override;
45-
float getScore(const ov::Tensor& scoresTensor, size_t classInd, size_t boxInd);
4645

4746
std::string boxesOutputName;
4847
std::string scoresOutputName;

demos/common/cpp/models/include/models/detection_model_yolox.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class ModelYoloX: public DetectionModel {
4545

4646
protected:
4747
void prepareInputsOutputs(std::shared_ptr<ov::Model>& model) override;
48-
void prepareGridsAndStrides();
48+
void setStridesGrids();
4949

5050
double boxIOUThreshold;
5151
std::vector<std::pair<size_t, size_t>> grids;

demos/common/cpp/models/include/models/image_model.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ class ImageModel : public ModelBase {
4444

4545
size_t netInputHeight = 0;
4646
size_t netInputWidth = 0;
47-
INTERPOLATION_MODE interpolationMode = LINEAR;
47+
cv::InterpolationFlags interpolationMode = cv::INTER_LINEAR;
4848
RESIZE_MODE resizeMode = RESIZE_FILL;
4949
};

demos/common/cpp/models/src/detection_model_faceboxes.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ void ModelFaceBoxes::prepareInputsOutputs(std::shared_ptr<ov::Model>& model) {
100100
priorBoxes(featureMaps);
101101
}
102102

103-
void calculateAnchors(std::vector<ModelFaceBoxes::Anchor>& anchors,
103+
void calculateAnchors(std::vector<Anchor>& anchors,
104104
const std::vector<float>& vx,
105105
const std::vector<float>& vy,
106106
const int minSize,
@@ -126,7 +126,7 @@ void calculateAnchors(std::vector<ModelFaceBoxes::Anchor>& anchors,
126126
}
127127
}
128128

129-
void calculateAnchorsZeroLevel(std::vector<ModelFaceBoxes::Anchor>& anchors,
129+
void calculateAnchorsZeroLevel(std::vector<Anchor>& anchors,
130130
const int fx,
131131
const int fy,
132132
const std::vector<int>& minSizes,
@@ -193,14 +193,14 @@ std::pair<std::vector<size_t>, std::vector<float>> filterScores(const ov::Tensor
193193
return {indices, scores};
194194
}
195195

196-
std::vector<ModelFaceBoxes::Anchor> filterBoxes(const ov::Tensor& boxesTensor,
197-
const std::vector<ModelFaceBoxes::Anchor>& anchors,
198-
const std::vector<size_t>& validIndices,
199-
const std::vector<float>& variance) {
196+
std::vector<Anchor> filterBoxes(const ov::Tensor& boxesTensor,
197+
const std::vector<Anchor>& anchors,
198+
const std::vector<size_t>& validIndices,
199+
const std::vector<float>& variance) {
200200
auto shape = boxesTensor.get_shape();
201201
const float* boxesPtr = boxesTensor.data<float>();
202202

203-
std::vector<ModelFaceBoxes::Anchor> boxes;
203+
std::vector<Anchor> boxes;
204204
boxes.reserve(ModelFaceBoxes::INIT_VECTOR_SIZE);
205205
for (auto i : validIndices) {
206206
auto objStart = shape[2] * i;

demos/common/cpp/models/src/detection_model_retinaface.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ void ModelRetinaFace::prepareInputsOutputs(std::shared_ptr<ov::Model>& model) {
129129
auto s = anchorCfg[idx].stride;
130130
auto anchorNum = anchorsFpn[s].size();
131131

132-
anchors.push_back(std::vector<ModelRetinaFace::Anchor>(height * width * anchorNum));
132+
anchors.push_back(std::vector<Anchor>(height * width * anchorNum));
133133
for (size_t iw = 0; iw < width; ++iw) {
134134
size_t sw = iw * s;
135135
for (size_t ih = 0; ih < height; ++ih) {
@@ -146,8 +146,8 @@ void ModelRetinaFace::prepareInputsOutputs(std::shared_ptr<ov::Model>& model) {
146146
}
147147
}
148148

149-
std::vector<ModelRetinaFace::Anchor> ratioEnum(const ModelRetinaFace::Anchor& anchor, const std::vector<int>& ratios) {
150-
std::vector<ModelRetinaFace::Anchor> retVal;
149+
std::vector<Anchor> ratioEnum(const Anchor& anchor, const std::vector<int>& ratios) {
150+
std::vector<Anchor> retVal;
151151
const auto w = anchor.getWidth();
152152
const auto h = anchor.getHeight();
153153
const auto xCtr = anchor.getXCenter();
@@ -166,8 +166,8 @@ std::vector<ModelRetinaFace::Anchor> ratioEnum(const ModelRetinaFace::Anchor& an
166166
return retVal;
167167
}
168168

169-
std::vector<ModelRetinaFace::Anchor> scaleEnum(const ModelRetinaFace::Anchor& anchor, const std::vector<int>& scales) {
170-
std::vector<ModelRetinaFace::Anchor> retVal;
169+
std::vector<Anchor> scaleEnum(const Anchor& anchor, const std::vector<int>& scales) {
170+
std::vector<Anchor> retVal;
171171
const auto w = anchor.getWidth();
172172
const auto h = anchor.getHeight();
173173
const auto xCtr = anchor.getXCenter();
@@ -184,12 +184,12 @@ std::vector<ModelRetinaFace::Anchor> scaleEnum(const ModelRetinaFace::Anchor& an
184184
return retVal;
185185
}
186186

187-
std::vector<ModelRetinaFace::Anchor> generateAnchors(const int baseSize,
187+
std::vector<Anchor> generateAnchors(const int baseSize,
188188
const std::vector<int>& ratios,
189189
const std::vector<int>& scales) {
190-
ModelRetinaFace::Anchor baseAnchor{0.0f, 0.0f, baseSize - 1.0f, baseSize - 1.0f};
190+
Anchor baseAnchor{0.0f, 0.0f, baseSize - 1.0f, baseSize - 1.0f};
191191
auto ratioAnchors = ratioEnum(baseAnchor, ratios);
192-
std::vector<ModelRetinaFace::Anchor> retVal;
192+
std::vector<Anchor> retVal;
193193

194194
for (const auto& ra : ratioAnchors) {
195195
auto addon = scaleEnum(ra, scales);
@@ -245,11 +245,11 @@ void filterScores(std::vector<float>& scores,
245245
}
246246
}
247247

248-
void filterBoxes(std::vector<ModelRetinaFace::Anchor>& boxes,
248+
void filterBoxes(std::vector<Anchor>& boxes,
249249
const std::vector<size_t>& indices,
250250
const ov::Tensor& boxesTensor,
251251
int anchorNum,
252-
const std::vector<ModelRetinaFace::Anchor>& anchors) {
252+
const std::vector<Anchor>& anchors) {
253253
const auto& shape = boxesTensor.get_shape();
254254
const float* boxesPtr = boxesTensor.data<float>();
255255
const auto boxPredLen = shape[1] / anchorNum;
@@ -279,7 +279,7 @@ void filterLandmarks(std::vector<cv::Point2f>& landmarks,
279279
const std::vector<size_t>& indices,
280280
const ov::Tensor& landmarksTensor,
281281
int anchorNum,
282-
const std::vector<ModelRetinaFace::Anchor>& anchors,
282+
const std::vector<Anchor>& anchors,
283283
const float landmarkStd) {
284284
const auto& shape = landmarksTensor.get_shape();
285285
const float* landmarksPtr = landmarksTensor.data<float>();

demos/common/cpp/models/src/detection_model_retinaface_pt.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,11 @@ std::vector<ModelRetinaFacePT::Box> ModelRetinaFacePT::generatePriorData() {
197197
return anchors;
198198
}
199199

200-
std::vector<ModelRetinaFacePT::Rect> ModelRetinaFacePT::getFilteredProposals(const ov::Tensor& boxesTensor,
200+
std::vector<Anchor> ModelRetinaFacePT::getFilteredProposals(const ov::Tensor& boxesTensor,
201201
const std::vector<size_t>& indicies,
202202
int imgWidth,
203203
int imgHeight) {
204-
std::vector<ModelRetinaFacePT::Rect> rects;
204+
std::vector<Anchor> rects;
205205
rects.reserve(indicies.size());
206206

207207
const auto& shape = boxesTensor.get_shape();
@@ -218,10 +218,10 @@ std::vector<ModelRetinaFacePT::Rect> ModelRetinaFacePT::getFilteredProposals(con
218218
const float cY = priors[i].cY + pRawBox->cY * variance[0] * prior.height;
219219
const float width = prior.width * exp(pRawBox->width * variance[1]);
220220
const float height = prior.height * exp(pRawBox->height * variance[1]);
221-
rects.push_back(Rect{clamp(cX - width / 2, 0.f, 1.f) * imgWidth,
222-
clamp(cY - height / 2, 0.f, 1.f) * imgHeight,
223-
clamp(cX + width / 2, 0.f, 1.f) * imgWidth,
224-
clamp(cY + height / 2, 0.f, 1.f) * imgHeight});
221+
rects.push_back(Anchor{clamp(cX - width / 2, 0.f, 1.f) * imgWidth,
222+
clamp(cY - height / 2, 0.f, 1.f) * imgHeight,
223+
clamp(cX + width / 2, 0.f, 1.f) * imgWidth,
224+
clamp(cY + height / 2, 0.f, 1.f) * imgHeight});
225225
}
226226

227227
return rects;

demos/common/cpp/models/src/detection_model_yolov3_onnx.cpp

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -39,60 +39,50 @@ ModelYoloV3ONNX::ModelYoloV3ONNX(const std::string& modelFileName,
3939
const std::vector<std::string>& labels,
4040
const std::string& layout)
4141
: DetectionModel(modelFileName, confidenceThreshold, false, labels, layout) {
42-
interpolationMode = CUBIC;
42+
interpolationMode = cv::INTER_CUBIC;
4343
resizeMode = RESIZE_KEEP_ASPECT_LETTERBOX;
4444
}
4545

4646

4747
void ModelYoloV3ONNX::prepareInputsOutputs(std::shared_ptr<ov::Model>& model) {
4848
// --------------------------- Configure input & output -------------------------------------------------
49-
// --------------------------- Prepare input ------------------------------------------------------
49+
// --------------------------- Prepare inputs ------------------------------------------------------
5050
const ov::OutputVector& inputs = model->inputs();
5151
if (inputs.size() != 2) {
5252
throw std::logic_error("YoloV3ONNX model wrapper expects models that have 2 inputs");
5353
}
5454

55-
// Check first image input
56-
std::string imageInputName = inputs.begin()->get_any_name();
57-
inputsNames.push_back(imageInputName);
58-
59-
const ov::Shape& imageShape = inputs.begin()->get_shape();
60-
const ov::Layout& imageLayout = getInputLayout(inputs.front());
61-
62-
if (imageShape.size() != 4 && imageShape[ov::layout::channels_idx(imageLayout)] != 3) {
63-
throw std::logic_error("Expected 4D image input with 3 channels");
64-
}
65-
6655
ov::preprocess::PrePostProcessor ppp(model);
67-
ppp.input(imageInputName).tensor().set_element_type(ov::element::u8).set_layout({"NHWC"});
68-
69-
ppp.input(imageInputName).model().set_layout(imageLayout);
70-
71-
// Check second info input
72-
std::string infoInputName = (++inputs.begin())->get_any_name();
73-
inputsNames.push_back(infoInputName);
74-
75-
const ov::Shape infoShape = (++inputs.begin())->get_shape();
76-
const ov::Layout& infoLayout = getInputLayout(inputs.at(1));
77-
78-
if (infoShape.size() != 2 && infoShape[ov::layout::channels_idx(infoLayout)] != 2) {
79-
throw std::logic_error("Expected 2D image info input with 2 channels");
56+
inputsNames.reserve(inputs.size());
57+
for (auto& input : inputs) {
58+
const ov::Shape& currentShape = input.get_shape();
59+
std::string currentName = input.get_any_name();
60+
const ov::Layout& currentLayout = getInputLayout(input);
61+
62+
if (currentShape.size() == 4) {
63+
if (currentShape[ov::layout::channels_idx(currentLayout)] != 3) {
64+
throw std::logic_error("Expected 4D image input with 3 channels");
65+
}
66+
inputsNames[0] = currentName;
67+
netInputWidth = currentShape[ov::layout::width_idx(currentLayout)];
68+
netInputHeight = currentShape[ov::layout::height_idx(currentLayout)];
69+
ppp.input(currentName).tensor().set_element_type(ov::element::u8).set_layout({"NHWC"});
70+
} else if (currentShape.size() == 2) {
71+
if (currentShape[ov::layout::channels_idx(currentLayout)] != 2) {
72+
throw std::logic_error("Expected 2D image info input with 2 channels");
73+
}
74+
inputsNames[1] = currentName;
75+
ppp.input(currentName).tensor().set_element_type(ov::element::i32);
76+
}
77+
ppp.input(currentName).model().set_layout(currentLayout);
8078
}
8179

82-
ppp.input(infoInputName).tensor().set_element_type(ov::element::i32);
83-
84-
ppp.input(infoInputName).model().set_layout(infoLayout);
85-
86-
// --------------------------- Reading image input parameters -------------------------------------------
87-
netInputWidth = imageShape[ov::layout::width_idx(imageLayout)];
88-
netInputHeight = imageShape[ov::layout::height_idx(imageLayout)];
89-
90-
// --------------------------- Prepare output -----------------------------------------------------
91-
if (model->outputs().size() != 3) {
80+
// --------------------------- Prepare outputs -----------------------------------------------------
81+
const ov::OutputVector& outputs = model->outputs();
82+
if (outputs.size() != 3) {
9283
throw std::logic_error("YoloV3ONNX model wrapper expects models that have 3 outputs");
9384
}
9485

95-
const ov::OutputVector& outputs = model->outputs();
9686
for (auto& output : outputs) {
9787
const ov::Shape& currentShape = output.get_partial_shape().get_max_shape();
9888
std::string currentName = output.get_any_name();
@@ -129,7 +119,7 @@ std::shared_ptr<InternalModelData> ModelYoloV3ONNX::preprocess(const InputData&
129119
return ImageModel::preprocess(inputData, request);
130120
}
131121

132-
float ModelYoloV3ONNX::getScore(const ov::Tensor& scoresTensor, size_t classInd, size_t boxInd) {
122+
float getScore(const ov::Tensor& scoresTensor, size_t classInd, size_t boxInd) {
133123
const float* scoresPtr = scoresTensor.data<float>();
134124
const auto shape = scoresTensor.get_shape();
135125
int N = shape[2];

0 commit comments

Comments
 (0)