Skip to content

Commit ec8c27b

Browse files
authored
Merge pull request #3595 from akorobeinikov/ak/yolo_models_github
C++ yolo wrappers for yolox and yolov3-onnx
2 parents 137e306 + aa726e0 commit ec8c27b

26 files changed

+592
-117
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
};
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
// Copyright (C) 2022 Intel Corporation
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
*/
16+
17+
#pragma once
18+
19+
#include <string>
20+
#include <vector>
21+
22+
#include <openvino/openvino.hpp>
23+
24+
#include "models/detection_model.h"
25+
26+
class ModelYoloV3ONNX: public DetectionModel {
27+
public:
28+
/// Constructor.
29+
/// @param modelFileName name of model to load
30+
/// @param confidenceThreshold - threshold to eliminate low-confidence detections.
31+
/// Any detected object with confidence lower than this threshold will be ignored.
32+
/// @param labels - array of labels for every class. If this array is empty or contains less elements
33+
/// than actual classes number, default "Label #N" will be shown for missing items.
34+
/// @param layout - model input layout
35+
ModelYoloV3ONNX(const std::string& modelFileName,
36+
float confidenceThreshold,
37+
const std::vector<std::string>& labels = std::vector<std::string>(),
38+
const std::string& layout = "");
39+
40+
std::unique_ptr<ResultBase> postprocess(InferenceResult& infResult) override;
41+
std::shared_ptr<InternalModelData> preprocess(const InputData& inputData, ov::InferRequest& request) override;
42+
43+
protected:
44+
void prepareInputsOutputs(std::shared_ptr<ov::Model>& model) override;
45+
46+
std::string boxesOutputName;
47+
std::string scoresOutputName;
48+
std::string indicesOutputName;
49+
static const int numberOfClasses = 80;
50+
};
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
// Copyright (C) 2022 Intel Corporation
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
*/
16+
17+
#pragma once
18+
#include <memory>
19+
#include <string>
20+
#include <vector>
21+
22+
#include <openvino/openvino.hpp>
23+
24+
#include "models/detection_model.h"
25+
26+
class ModelYoloX: public DetectionModel {
27+
public:
28+
/// Constructor.
29+
/// @param modelFileName name of model to load
30+
/// @param confidenceThreshold - threshold to eliminate low-confidence detections.
31+
/// Any detected object with confidence lower than this threshold will be ignored.
32+
/// @param boxIOUThreshold - threshold to treat separate output regions as one object for filtering
33+
/// during postprocessing (only one of them should stay). The default value is 0.5
34+
/// @param labels - array of labels for every class. If this array is empty or contains less elements
35+
/// than actual classes number, default "Label #N" will be shown for missing items.
36+
/// @param layout - model input layout
37+
ModelYoloX(const std::string& modelFileName,
38+
float confidenceThreshold,
39+
float boxIOUThreshold = 0.5,
40+
const std::vector<std::string>& labels = std::vector<std::string>(),
41+
const std::string& layout = "");
42+
43+
std::unique_ptr<ResultBase> postprocess(InferenceResult& infResult) override;
44+
std::shared_ptr<InternalModelData> preprocess(const InputData& inputData, ov::InferRequest& request) override;
45+
46+
protected:
47+
void prepareInputsOutputs(std::shared_ptr<ov::Model>& model) override;
48+
void setStridesGrids();
49+
50+
double boxIOUThreshold;
51+
std::vector<std::pair<size_t, size_t>> grids;
52+
std::vector<size_t> expandedStrides;
53+
static const size_t numberOfClasses = 80;
54+
};

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ class HpeAssociativeEmbedding : public ImageModel {
6565
int targetSize;
6666
float confidenceThreshold;
6767
float delta;
68-
RESIZE_MODE resizeMode;
6968

7069
std::string embeddingsTensorName;
7170
std::string heatmapsTensorName;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <string>
2222

2323
#include "models/model_base.h"
24+
#include "utils/image_utils.h"
2425

2526
namespace ov {
2627
class InferRequest;
@@ -43,4 +44,6 @@ class ImageModel : public ModelBase {
4344

4445
size_t netInputHeight = 0;
4546
size_t netInputWidth = 0;
47+
cv::InterpolationFlags interpolationMode = cv::INTER_LINEAR;
48+
RESIZE_MODE resizeMode = RESIZE_FILL;
4649
};

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class StyleTransferModel : public ImageModel {
3636
/// @param layout - model input layout
3737
StyleTransferModel(const std::string& modelFileName, const std::string& layout = "");
3838

39-
std::shared_ptr<InternalModelData> preprocess(const InputData& inputData, ov::InferRequest& request) override;
4039
std::unique_ptr<ResultBase> postprocess(InferenceResult& infResult) override;
4140

4241
protected:

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>();

0 commit comments

Comments
 (0)