Skip to content

Commit 6b9ccc4

Browse files
committed
Remove ResultBase and clean up label
1 parent f0c424c commit 6b9ccc4

12 files changed

+33
-67
lines changed

src/cpp/models/include/models/results.h

Lines changed: 13 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,16 @@
1717

1818
struct MetaData;
1919

20-
struct ResultBase {
21-
ResultBase(int64_t frameId = -1, const std::shared_ptr<MetaData>& metaData = nullptr)
20+
struct InferenceResult {
21+
InferenceResult(int64_t frameId = -1, const std::shared_ptr<MetaData>& metaData = nullptr)
2222
: frameId(frameId),
2323
metaData(metaData) {}
24-
virtual ~ResultBase() {}
2524

25+
std::shared_ptr<InternalModelData> internalModelData;
26+
std::map<std::string, ov::Tensor> outputsData;
2627
int64_t frameId;
27-
2828
std::shared_ptr<MetaData> metaData;
29-
bool IsEmpty() {
30-
return frameId < 0;
31-
}
32-
33-
template <class T>
34-
T& asRef() {
35-
return dynamic_cast<T&>(*this);
36-
}
37-
38-
template <class T>
39-
const T& asRef() const {
40-
return dynamic_cast<const T&>(*this);
41-
}
42-
};
4329

44-
struct InferenceResult : public ResultBase {
45-
std::shared_ptr<InternalModelData> internalModelData;
46-
std::map<std::string, ov::Tensor> outputsData;
4730

4831
/// Returns the first output tensor
4932
/// This function is a useful addition to direct access to outputs list as many models have only one output
@@ -89,35 +72,22 @@ struct DetectedKeypoints {
8972
class Label {
9073
public:
9174
Label() {}
92-
Label(int id, std::string name): id(id), name(name) {}
75+
Label(int id, std::string name, float score): id(id), name(name), score(score) {}
9376

9477
int id;
9578
std::string name;
96-
97-
friend std::ostream& operator<< (std::ostream& os, const Label& label) {
98-
return os << label.id << " (" << label.name << ")";
99-
}
100-
};
101-
102-
class LabelScore {
103-
public:
104-
LabelScore() {}
105-
LabelScore(int id, std::string name, float score): label(Label(id, name)), score(score) {}
106-
LabelScore(Label label, float score): label(label), score(score) {}
107-
108-
Label label;
10979
float score;
11080

111-
friend std::ostream& operator<< (std::ostream& os, const LabelScore& label) {
112-
return os << label.label << ": " << std::fixed << std::setprecision(3) << label.score;
81+
friend std::ostream& operator<< (std::ostream& os, const Label& label) {
82+
return os << label.id << " (" << label.name << ")" << ": " << std::fixed << std::setprecision(3) << label.score;
11383
}
11484
};
11585

11686
class Mask {
11787
public:
118-
Mask(LabelScore label, cv::Rect roi, cv::Mat mask): label(label), roi(roi), mask(mask) {}
88+
Mask(Label label, cv::Rect roi, cv::Mat mask): label(label), roi(roi), mask(mask) {}
11989

120-
LabelScore label;
90+
Label label;
12191
cv::Rect roi;
12292
cv::Mat mask;
12393

@@ -152,16 +122,16 @@ static inline std::vector<Contour> getContours(const std::vector<Mask>& segmente
152122
if (contours.size() != 1) {
153123
throw std::runtime_error("findContours() must have returned only one contour");
154124
}
155-
combined_contours.push_back({obj.label.label.name, obj.label.score, contours[0]});
125+
combined_contours.push_back({obj.label.name, obj.label.score, contours[0]});
156126
}
157127
return combined_contours;
158128
}
159129

160130
class Box {
161131
public:
162-
Box(cv::Rect shape, std::vector<LabelScore> labels): shape(shape), labels(labels) {}
132+
Box(cv::Rect shape, std::vector<Label> labels): shape(shape), labels(labels) {}
163133
cv::Rect shape;
164-
std::vector<LabelScore> labels;
134+
std::vector<Label> labels;
165135

166136
friend std::ostream& operator<< (std::ostream& os, const Box& box) {
167137

@@ -189,7 +159,7 @@ class Box {
189159

190160
class RotatedRect {
191161
public:
192-
LabelScore label;
162+
Label label;
193163
cv::RotatedRect shape;
194164

195165
friend std::ostream& operator<< (std::ostream& os, const RotatedRect& box) {

src/cpp/models/src/anomaly_model.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ std::unique_ptr<Scene> AnomalyModel::postprocess(InferenceResult& infResult) {
8686
auto scene = std::make_unique<Scene>(infResult.frameId, infResult.metaData);
8787

8888
scene->saliency_maps.push_back(anomaly_map);
89-
auto label = LabelScore(label_id, pred_label, pred_score);
89+
auto label = Label(label_id, pred_label, pred_score);
9090
auto roi = cv::Rect(0, 0, inputImgSize.inputImgWidth, inputImgSize.inputImgHeight);
9191
scene->masks.push_back(Mask(label, roi, pred_mask));
9292
scene->boxes.push_back(Box(roi, {label}));
@@ -100,7 +100,7 @@ std::unique_ptr<Scene> AnomalyModel::postprocess(InferenceResult& infResult) {
100100
scene->boxes.push_back(
101101
Box(
102102
rect,
103-
{LabelScore(label_id, pred_label, box_score)}
103+
{Label(label_id, pred_label, box_score)}
104104
)
105105
);
106106

src/cpp/models/src/classification_model.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ std::unique_ptr<Scene> ClassificationModel::get_multilabel_predictions(Inference
328328

329329
auto scene = std::make_unique<Scene>(infResult.frameId, infResult.metaData);
330330
auto raw_scores = ov::Tensor();
331-
std::vector<LabelScore> result;
331+
std::vector<Label> result;
332332
float* raw_scoresPtr = nullptr;
333333
if (add_raw_scores) {
334334
raw_scores = ov::Tensor(logitsTensor.get_element_type(), logitsTensor.get_shape());
@@ -401,9 +401,9 @@ std::unique_ptr<Scene> ClassificationModel::get_hierarchical_predictions(Inferen
401401
}
402402

403403
auto resolved_labels = resolver->resolve_labels(predicted_labels, predicted_scores);
404-
std::vector<LabelScore> result;
404+
std::vector<Label> result;
405405
for (const auto& label : resolved_labels) {
406-
result.push_back(LabelScore(hierarchical_info.label_to_idx[label.first], label.first, label.second));
406+
result.push_back(Label(hierarchical_info.label_to_idx[label.first], label.first, label.second));
407407
}
408408
const auto& internalData = infResult.internalModelData->asRef<InternalImageModelData>();
409409
cv::Rect shape(0, 0, internalData.inputImgWidth, internalData.inputImgHeight);
@@ -449,7 +449,7 @@ std::unique_ptr<Scene> ClassificationModel::get_multiclass_predictions(Inference
449449
scene->additional_tensors["raw_scores"] = raw_scores;
450450
}
451451

452-
std::vector<LabelScore> result;
452+
std::vector<Label> result;
453453
for (size_t i = 0; i < scoresTensor.get_size(); ++i) {
454454
int ind = indicesPtr[i];
455455
if (ind < 0 || ind >= static_cast<int>(labels.size())) {

src/cpp/models/src/detection_model_ssd.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ std::unique_ptr<Scene> ModelSSD::postprocessSingleOutput(InferenceResult& infRes
163163
0.f,
164164
floatInputImgHeight) - y
165165
),
166-
{LabelScore(labelID, getLabelName(labelID), confidence)}
166+
{Label(labelID, getLabelName(labelID), confidence)}
167167
);
168168
scene->boxes.push_back(box);
169169
}
@@ -223,7 +223,7 @@ std::unique_ptr<Scene> ModelSSD::postprocessMultipleOutputs(InferenceResult& inf
223223
if (width * height >= box_area_threshold) {
224224
scene->boxes.push_back(Box(
225225
cv::Rect(x, y, width, height),
226-
{LabelScore(labels[i], getLabelName(labels[i]), confidence)}
226+
{Label(labels[i], getLabelName(labels[i]), confidence)}
227227
));
228228
}
229229
}

src/cpp/models/src/detection_model_yolo.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ std::unique_ptr<Scene> ModelYolo::postprocess(InferenceResult& infResult) {
282282
for (const auto& obj1 : objects) {
283283
bool isGoodResult = true;
284284
for (const auto& obj2 : objects) {
285-
if (obj1.labels[0].label.id == obj2.labels[0].label.id && obj1.labels[0].score < obj2.labels[0].score &&
285+
if (obj1.labels[0].id == obj2.labels[0].id && obj1.labels[0].score < obj2.labels[0].score &&
286286
intersectionOverUnion(obj1, obj2) >= iou_threshold) { // if obj1 is the same as obj2, condition
287287
// expression will evaluate to false anyway
288288
isGoodResult = false;
@@ -410,7 +410,7 @@ void ModelYolo::parseYOLOOutput(const std::string& output_name,
410410

411411
//--- Checking confidence threshold conformance and adding region to the list
412412
if (prob >= confidence_threshold) {
413-
objects.push_back(Box(obj, {LabelScore(j, getLabelName(j), prob)}));
413+
objects.push_back(Box(obj, {Label(j, getLabelName(j), prob)}));
414414
}
415415
}
416416
}
@@ -635,7 +635,7 @@ std::unique_ptr<Scene> YOLOv5::postprocess(InferenceResult& infResult) {
635635

636636
scene->boxes.push_back(Box(
637637
cv::Rect(x, y, width, height),
638-
{LabelScore(labelID, label, confidence)}
638+
{Label(labelID, label, confidence)}
639639
));
640640
}
641641

src/cpp/models/src/detection_model_yolov3_onnx.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ std::unique_ptr<Scene> ModelYoloV3ONNX::postprocess(InferenceResult& infResult)
163163
obj.height = clamp(height, 0.f, static_cast<float>(imgHeight));
164164
obj.width = clamp(width, 0.f, static_cast<float>(imgWidth));
165165

166-
scene->boxes.push_back(Box(obj, {LabelScore(classInd, getLabelName(classInd), score)}));
166+
scene->boxes.push_back(Box(obj, {Label(classInd, getLabelName(classInd), score)}));
167167
}
168168
}
169169
return scene;

src/cpp/models/src/detection_model_yolox.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ std::unique_ptr<Scene> ModelYoloX::postprocess(InferenceResult& infResult) {
195195
obj.width =
196196
clamp(validBoxes[index].right - validBoxes[index].left, 0.f, static_cast<float>(scale.inputImgWidth));
197197
scene->boxes.push_back(
198-
Box(obj, {LabelScore(classes[index], getLabelName(classes[index]), scores[index])})
198+
Box(obj, {Label(classes[index], getLabelName(classes[index]), scores[index])})
199199
);
200200
}
201201
return scene;

src/cpp/models/src/instance_segmentation.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
#include <stddef.h>
99
#include <stdint.h>
1010

11-
#include <fstream>
12-
#include <limits>
1311
#include <opencv2/core.hpp>
1412
#include <opencv2/imgproc.hpp>
1513
#include <openvino/openvino.hpp>
@@ -322,7 +320,7 @@ std::unique_ptr<Scene> MaskRCNNModel::postprocess(InferenceResult& infResult) {
322320
continue;
323321
}
324322

325-
LabelScore label(labelID, getLabelName(labelID), confidence);
323+
Label label(labelID, getLabelName(labelID), confidence);
326324

327325
cv::Rect roi;
328326

src/cpp/models/src/segmentation_model.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ std::unique_ptr<Scene> SegmentationModel::postprocess(InferenceResult& infResult
261261

262262
auto scene = std::make_unique<Scene>(infResult.frameId, infResult.metaData);
263263
auto roi = cv::Rect(0, 0, inputImgSize.inputImgWidth, inputImgSize.inputImgHeight);
264-
scene->masks.push_back(Mask(LabelScore(0, "hard_prediction", 0), roi, hard_prediction));
264+
scene->masks.push_back(Mask(Label(0, "hard_prediction", 0), roi, hard_prediction));
265265
if (return_soft_prediction) {
266266
cv::resize(soft_prediction,
267267
soft_prediction,
@@ -270,7 +270,7 @@ std::unique_ptr<Scene> SegmentationModel::postprocess(InferenceResult& infResult
270270
0.0,
271271
cv::INTER_NEAREST);
272272

273-
scene->masks.push_back(Mask(LabelScore(1, "soft_prediction", 0), roi, soft_prediction));
273+
scene->masks.push_back(Mask(Label(1, "soft_prediction", 0), roi, soft_prediction));
274274
auto iter = infResult.outputsData.find(feature_vector_name);
275275
if (infResult.outputsData.end() != iter) {
276276
scene->saliency_maps.push_back(get_activation_map(soft_prediction));

src/cpp/tilers/src/detection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ std::unique_ptr<Scene> DetectionTiler::merge_results(const std::vector<std::uniq
6666

6767
for (const auto& result : tiles_results) {
6868
for (auto& det : result->boxes) {
69-
all_detections.emplace_back(det.shape.x, det.shape.y, det.shape.x + det.shape.width, det.shape.y + det.shape.height, det.labels[0].label.id);
69+
all_detections.emplace_back(det.shape.x, det.shape.y, det.shape.x + det.shape.width, det.shape.y + det.shape.height, det.labels[0].id);
7070
all_scores.push_back(det.labels[0].score);
7171
all_detections_refs.push_back(det);
7272
}

0 commit comments

Comments
 (0)