Skip to content

Commit 5b961f9

Browse files
committed
update wrappers and preprocessing
1 parent 3805e3f commit 5b961f9

File tree

7 files changed

+52
-72
lines changed

7 files changed

+52
-72
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
#include "models/detection_model.h"
2525
#include "utils/image_utils.h"
2626

27-
28-
2927
class ModelYoloV3ONNX: public DetectionModel {
3028
public:
3129
/// Constructor.
@@ -39,7 +37,7 @@ class ModelYoloV3ONNX: public DetectionModel {
3937
float confidenceThreshold,
4038
const std::vector<std::string>& labels = std::vector<std::string>(),
4139
const std::string& layout = "");
42-
40+
4341
std::unique_ptr<ResultBase> postprocess(InferenceResult& infResult) override;
4442
std::shared_ptr<InternalModelData> preprocess(const InputData& inputData, ov::InferRequest& request) override;
4543

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
#include "utils/image_utils.h"
3030
#include "utils/nms.hpp"
3131

32-
33-
3432
class ModelYoloX: public DetectionModel {
3533
public:
3634
/// Constructor.
@@ -47,17 +45,16 @@ class ModelYoloX: public DetectionModel {
4745
float boxIOUThreshold = 0.5,
4846
const std::vector<std::string>& labels = std::vector<std::string>(),
4947
const std::string& layout = "");
50-
48+
5149
std::unique_ptr<ResultBase> postprocess(InferenceResult& infResult) override;
5250
std::shared_ptr<InternalModelData> preprocess(const InputData& inputData, ov::InferRequest& request) override;
5351

5452
protected:
5553
void prepareInputsOutputs(std::shared_ptr<ov::Model>& model) override;
5654
void prepareGridsAndStrides();
57-
Anchor xywh2xyxy(float x, float y, float width, float height);
5855

5956
double boxIOUThreshold;
60-
std::vector<std::pair<int, int>> grids;
61-
std::vector<int> expandedStrides;
62-
static const int numberOfClasses = 80;
57+
std::vector<std::pair<size_t, size_t>> grids;
58+
std::vector<size_t> expandedStrides;
59+
static const size_t numberOfClasses = 80;
6360
};

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include "models/results.h"
3535
#include "utils/image_utils.h"
3636

37-
3837
ModelYoloV3ONNX::ModelYoloV3ONNX(const std::string& modelFileName,
3938
float confidenceThreshold,
4039
const std::vector<std::string>& labels,
@@ -77,7 +76,7 @@ void ModelYoloV3ONNX::prepareInputsOutputs(std::shared_ptr<ov::Model>& model) {
7776
const ov::Layout& infoLayout = getInputLayout(inputs.at(1));
7877

7978
if (infoShape.size() != 2 && infoShape[ov::layout::channels_idx(infoLayout)] != 2) {
80-
throw std::logic_error("Expected 2D info input with 2 channels");
79+
throw std::logic_error("Expected 2D image info input with 2 channels");
8180
}
8281

8382
ppp.input(infoInputName).tensor().set_element_type(ov::element::i32);
@@ -106,8 +105,9 @@ void ModelYoloV3ONNX::prepareInputsOutputs(std::shared_ptr<ov::Model>& model) {
106105
} else if (currentShape[1] == numberOfClasses) {
107106
scoresOutputName = currentName;
108107
ppp.output(currentName).tensor().set_element_type(ov::element::f32);
109-
} else
108+
} else {
110109
throw std::logic_error("Expected shapes [:,:,4], [:,numClasses,:] and [:,3] for outputs");
110+
}
111111
outputsNames.push_back(currentName);
112112
}
113113
model = ppp.build();
@@ -116,6 +116,7 @@ void ModelYoloV3ONNX::prepareInputsOutputs(std::shared_ptr<ov::Model>& model) {
116116
std::shared_ptr<InternalModelData> ModelYoloV3ONNX::preprocess(const InputData& inputData,
117117
ov::InferRequest& request) {
118118
const auto& origImg = inputData.asRef<ImageInputData>().inputImage;
119+
119120
int* img_size = new int[2];
120121
img_size[0] = origImg.rows;
121122
img_size[1] = origImg.cols;
@@ -127,7 +128,7 @@ std::shared_ptr<InternalModelData> ModelYoloV3ONNX::preprocess(const InputData&
127128
}
128129

129130
float ModelYoloV3ONNX::getScore(const ov::Tensor& scoresTensor, size_t classInd, size_t boxInd) {
130-
float* scoresPtr = scoresTensor.data<float>();
131+
const float* scoresPtr = scoresTensor.data<float>();
131132
const auto shape = scoresTensor.get_shape();
132133
int N = shape[2];
133134

@@ -160,9 +161,10 @@ std::unique_ptr<ResultBase> ModelYoloV3ONNX::postprocess(InferenceResult& infRes
160161
int classInd = indicesData[i * indicesStride + 1];
161162
int boxInd = indicesData[i * indicesStride + 2];
162163

163-
if (batchInd == -1)
164+
if (batchInd == -1) {
164165
break;
165-
166+
}
167+
166168
float score = getScore(scores, classInd, boxInd);
167169

168170
if (score > confidenceThreshold) {
@@ -183,11 +185,10 @@ std::unique_ptr<ResultBase> ModelYoloV3ONNX::postprocess(InferenceResult& infRes
183185
obj.labelID = classInd;
184186
obj.label = getLabelName(classInd);
185187

186-
187188
result->objects.push_back(obj);
188189

189190
}
190191
}
191-
192+
192193
return std::unique_ptr<ResultBase>(result);
193194
}

demos/common/cpp/models/src/detection_model_yolox.cpp

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@
3535
#include "utils/image_utils.h"
3636
#include "utils/nms.hpp"
3737

38-
3938
ModelYoloX::ModelYoloX(const std::string& modelFileName,
4039
float confidenceThreshold,
4140
float boxIOUThreshold,
4241
const std::vector<std::string>& labels,
4342
const std::string& layout)
4443
: DetectionModel(modelFileName, confidenceThreshold, false, labels, layout),
45-
boxIOUThreshold(boxIOUThreshold) {}
46-
44+
boxIOUThreshold(boxIOUThreshold) {
45+
resizeMode = RESIZE_KEEP_ASPECT;
46+
}
4747

4848
void ModelYoloX::prepareInputsOutputs(std::shared_ptr<ov::Model>& model) {
4949
// --------------------------- Configure input & output -------------------------------------------------
@@ -89,11 +89,10 @@ void ModelYoloX::prepareInputsOutputs(std::shared_ptr<ov::Model>& model) {
8989
model = ppp.build();
9090
}
9191

92-
9392
void ModelYoloX::prepareGridsAndStrides() {
94-
std::vector<int> strides = {8, 16, 32};
95-
std::vector<int> hsizes(3);
96-
std::vector<int> wsizes(3);
93+
std::vector<size_t> strides = {8, 16, 32};
94+
std::vector<size_t> hsizes(3);
95+
std::vector<size_t> wsizes(3);
9796

9897
for (size_t i = 0; i < strides.size(); ++i) {
9998
hsizes[i] = netInputHeight / strides[i];
@@ -113,29 +112,14 @@ void ModelYoloX::prepareGridsAndStrides() {
113112
std::shared_ptr<InternalModelData> ModelYoloX::preprocess(const InputData& inputData,
114113
ov::InferRequest& request) {
115114
const auto& origImg = inputData.asRef<ImageInputData>().inputImage;
116-
double scaleX = static_cast<double>(netInputWidth) / origImg.cols;
117-
double scaleY = static_cast<double>(netInputHeight) / origImg.rows;
118-
double scale = std::min(scaleX, scaleY);
119-
cv::Mat resizedImage;
120-
cv::resize(origImg, resizedImage, cv::Size(0, 0), scale, scale);
121-
122-
cv::Mat paddedImg;
123-
cv::copyMakeBorder(resizedImage, paddedImg, 0, netInputHeight - resizedImage.rows,
124-
0, netInputWidth - resizedImage.cols, cv::BORDER_CONSTANT, cv::Scalar(114, 114, 114));
125-
paddedImg = inputTransform(paddedImg);
126-
127-
request.set_input_tensor(wrapMat2Tensor(paddedImg));
128-
return std::make_shared<InternalScaleData>(origImg.cols, origImg.rows, scale, scale);
129-
}
115+
double scale = std::min(static_cast<double>(netInputWidth) / origImg.cols,
116+
static_cast<double>(netInputHeight) / origImg.rows);
130117

118+
cv::Mat resizedImage = resizeImageExt(origImg, netInputWidth, netInputHeight, resizeMode,
119+
interpolationMode, nullptr, cv::Scalar(114, 114, 114));
131120

132-
Anchor ModelYoloX::xywh2xyxy(float x, float y, float width, float height){
133-
Anchor result;
134-
result.left = x - width / 2;
135-
result.top = y - height / 2;
136-
result.right = x + width / 2;
137-
result.bottom = y + height / 2;
138-
return result;
121+
request.set_input_tensor(wrapMat2Tensor(resizedImage));
122+
return std::make_shared<InternalScaleData>(origImg.cols, origImg.rows, scale, scale);
139123
}
140124

141125
std::unique_ptr<ResultBase> ModelYoloX::postprocess(InferenceResult& infResult) {
@@ -146,7 +130,7 @@ std::unique_ptr<ResultBase> ModelYoloX::postprocess(InferenceResult& infResult)
146130
const ov::Tensor& output = infResult.outputsData[outputsNames[0]];
147131
const auto& outputShape = output.get_shape();
148132
float* outputPtr = output.data<float>();
149-
133+
150134
// Generate detection results
151135
DetectionResult* result = new DetectionResult(infResult.frameId, infResult.metaData);
152136

@@ -181,14 +165,15 @@ std::unique_ptr<ResultBase> ModelYoloX::postprocess(InferenceResult& infResult)
181165
score *= maxClassScore;
182166
if (score < confidenceThreshold)
183167
continue;
184-
168+
185169
// Add successful boxes
186170
scores.push_back(score);
187171
classes.push_back(mainClass);
188-
Anchor trueBox = xywh2xyxy(outputPtr[startPos + 0], outputPtr[startPos + 1], outputPtr[startPos + 2], outputPtr[startPos + 3]);
172+
Anchor trueBox = {outputPtr[startPos + 0] - outputPtr[startPos + 2] / 2, outputPtr[startPos + 1] - outputPtr[startPos + 3] / 2,
173+
outputPtr[startPos + 0] + outputPtr[startPos + 2] / 2, outputPtr[startPos + 1] + outputPtr[startPos + 3] / 2};
189174
validBoxes.push_back(Anchor({trueBox.left / scale.scaleX, trueBox.top / scale.scaleY,
190175
trueBox.right / scale.scaleX, trueBox.bottom / scale.scaleY}));
191-
}
176+
}
192177

193178
// NMS for valid boxes
194179
std::vector<int> keep = nms(validBoxes, scores, boxIOUThreshold, true);
@@ -204,6 +189,6 @@ std::unique_ptr<ResultBase> ModelYoloX::postprocess(InferenceResult& infResult)
204189
obj.label = getLabelName(classes[index]);
205190
result->objects.push_back(obj);
206191
}
207-
192+
208193
return std::unique_ptr<ResultBase>(result);
209194
}

demos/common/cpp/utils/include/utils/image_utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ enum INTERPOLATION_MODE {
3232
};
3333

3434
cv::Mat resizeImageExt(const cv::Mat& mat, int width, int height, RESIZE_MODE resizeMode = RESIZE_FILL,
35-
INTERPOLATION_MODE interpolationMode = LINEAR, cv::Rect* roi = nullptr);
35+
INTERPOLATION_MODE interpolationMode = LINEAR, cv::Rect* roi = nullptr, cv::Scalar BorderConstant = cv::Scalar(0, 0, 0));

demos/common/cpp/utils/include/utils/nms.hpp

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,25 @@
1919
#include "opencv2/core.hpp"
2020
#include <vector>
2121

22-
2322
struct Anchor {
24-
float left;
25-
float top;
26-
float right;
27-
float bottom;
23+
float left;
24+
float top;
25+
float right;
26+
float bottom;
2827

29-
float getWidth() const {
30-
return (right - left) + 1.0f;
31-
}
32-
float getHeight() const {
33-
return (bottom - top) + 1.0f;
34-
}
35-
float getXCenter() const {
36-
return left + (getWidth() - 1.0f) / 2.0f;
37-
}
38-
float getYCenter() const {
39-
return top + (getHeight() - 1.0f) / 2.0f;
40-
}
41-
};
28+
float getWidth() const {
29+
return (right - left) + 1.0f;
30+
}
31+
float getHeight() const {
32+
return (bottom - top) + 1.0f;
33+
}
34+
float getXCenter() const {
35+
return left + (getWidth() - 1.0f) / 2.0f;
36+
}
37+
float getYCenter() const {
38+
return top + (getHeight() - 1.0f) / 2.0f;
39+
}
40+
};
4241

4342
template <typename Anchor>
4443
std::vector<int> nms(const std::vector<Anchor>& boxes, const std::vector<float>& scores,

demos/common/cpp/utils/src/image_utils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include "utils/image_utils.h"
1818

1919
cv::Mat resizeImageExt(const cv::Mat& mat, int width, int height, RESIZE_MODE resizeMode,
20-
INTERPOLATION_MODE interpolationMode, cv::Rect* roi) {
20+
INTERPOLATION_MODE interpolationMode, cv::Rect* roi, cv::Scalar BorderConstant) {
2121
if (width == mat.cols && height == mat.rows) {
2222
return mat;
2323
}
@@ -44,7 +44,7 @@ cv::Mat resizeImageExt(const cv::Mat& mat, int width, int height, RESIZE_MODE re
4444
int dy = resizeMode == RESIZE_KEEP_ASPECT ? 0 : (height - resizedImage.rows) / 2;
4545

4646
cv::copyMakeBorder(resizedImage, dst, dy, height - resizedImage.rows - dy,
47-
dx, width - resizedImage.cols - dx, cv::BORDER_CONSTANT, cv::Scalar(0, 0, 0));
47+
dx, width - resizedImage.cols - dx, cv::BORDER_CONSTANT, BorderConstant);
4848
if (roi) {
4949
*roi = cv::Rect(dx, dy, resizedImage.cols, resizedImage.rows);
5050
}

0 commit comments

Comments
 (0)