Skip to content

Commit 9384335

Browse files
committed
Merge branch 'feature/cpp_refactoring' into rhecker/model_api_refactor_classification
2 parents 9a20f0e + 25e44e7 commit 9384335

File tree

7 files changed

+136
-72
lines changed

7 files changed

+136
-72
lines changed

examples/cpp/main.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55

66
#include <stddef.h>
7-
#include <tasks/classification.h>
87
#include <tasks/detection.h>
98
#include <tasks/results.h>
109

@@ -18,25 +17,34 @@
1817
#include <stdexcept>
1918
#include <string>
2019

21-
int main(int argc, char* argv[]) {
20+
int main(int argc, char* argv[]) try {
2221
if (argc != 3) {
2322
throw std::runtime_error(std::string{"Usage: "} + argv[0] + " <path_to_model> <path_to_image>");
2423
}
2524

2625
cv::Mat image = cv::imread(argv[2]);
27-
//cv::cvtColor(image, image, cv::COLOR_BGR2RGB);
26+
cv::cvtColor(image, image, cv::COLOR_BGR2RGB);
2827

2928
if (!image.data) {
3029
throw std::runtime_error{"Failed to read the image"};
3130
}
3231

33-
//// Instantiate Object Detection model
34-
auto model = Classification::load(argv[1]); // works with SSD models. Download it using Python Model API
32+
// Instantiate Object Detection model
33+
auto model = DetectionModel::load(argv[1], {}); // works with SSD models. Download it using Python Model API
3534

36-
//// Run the inference
35+
// Run the inference
3736
auto result = model.infer(image);
3837

39-
//// Process detections
40-
std::cout << result << std::endl;
41-
std::cout << "expected: " << std::endl << "1 (bicycle): 0.825, 11 (dog): 0.873, 14 (person): 0.824, [0], [0], [0]" << std::endl;
38+
// Process detections
39+
for (auto& obj : result.objects) {
40+
std::cout << " " << std::left << std::setw(9) << obj.label << " | " << std::setw(10) << obj.confidence << " | "
41+
<< std::setw(4) << int(obj.x) << " | " << std::setw(4) << int(obj.y) << " | " << std::setw(4)
42+
<< int(obj.x + obj.width) << " | " << std::setw(4) << int(obj.y + obj.height) << "\n";
43+
}
44+
} catch (const std::exception& error) {
45+
std::cerr << error.what() << '\n';
46+
return 1;
47+
} catch (...) {
48+
std::cerr << "Non-exception object thrown\n";
49+
return 1;
4250
}

src/cpp/include/tasks/detection/ssd.h

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "adapters/inference_adapter.h"
1111
#include "tasks/results.h"
12+
#include "utils/config.h"
1213
#include "utils/preprocessing.h"
1314

1415
enum SSDOutputMode { single, multi };
@@ -29,20 +30,8 @@ class SSD {
2930

3031
SSD(std::shared_ptr<InferenceAdapter> adapter, cv::Size input_shape) : adapter(adapter), input_shape(input_shape) {
3132
auto config = adapter->getModelConfig();
32-
{
33-
auto iter = config.find("labels");
34-
if (iter != config.end()) {
35-
labels = iter->second.as<std::vector<std::string>>();
36-
} else {
37-
std::cout << "could not find labels from model config" << std::endl;
38-
}
39-
}
40-
{
41-
auto iter = config.find("confidence_threshold");
42-
if (iter != config.end()) {
43-
confidence_threshold = iter->second.as<float>();
44-
}
45-
}
33+
labels = utils::get_from_any_maps("labels", config, {}, labels);
34+
confidence_threshold = utils::get_from_any_maps("confidence_threshold", config, {}, confidence_threshold);
4635
}
4736
std::map<std::string, ov::Tensor> preprocess(cv::Mat);
4837
DetectionResult postprocess(InferenceResult& infResult);

src/cpp/include/tasks/instance_segmentation.h

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "adapters/inference_adapter.h"
1212
#include "tasks/results.h"
13+
#include "utils/config.h"
1314
#include "utils/vision_pipeline.h"
1415

1516
class InstanceSegmentation {
@@ -30,20 +31,10 @@ class InstanceSegmentation {
3031
});
3132

3233
auto config = adapter->getModelConfig();
33-
auto iter = config.find("labels");
34-
if (iter != config.end()) {
35-
labels = iter->second.as<std::vector<std::string>>();
36-
} else {
37-
std::cout << "could not find labels from model config" << std::endl;
38-
}
39-
40-
{
41-
auto iter = config.find("confidence_threshold");
42-
if (iter != config.end()) {
43-
confidence_threshold = iter->second.as<float>();
44-
}
45-
}
34+
labels = utils::get_from_any_maps("labels", config, {}, labels);
35+
confidence_threshold = utils::get_from_any_maps("confidence_threshold", config, {}, confidence_threshold);
4636
}
37+
4738
static cv::Size serialize(std::shared_ptr<ov::Model>& ov_model);
4839
static InstanceSegmentation load(const std::string& model_path);
4940

src/cpp/include/tasks/semantic_segmentation.h

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "adapters/inference_adapter.h"
1111
#include "tasks/results.h"
12+
#include "utils/config.h"
1213
#include "utils/preprocessing.h"
1314
#include "utils/vision_pipeline.h"
1415

@@ -27,26 +28,9 @@ class SemanticSegmentation {
2728
});
2829

2930
auto config = adapter->getModelConfig();
30-
auto iter = config.find("labels");
31-
if (iter != config.end()) {
32-
labels = iter->second.as<std::vector<std::string>>();
33-
} else {
34-
std::cout << "could not find labels from model config" << std::endl;
35-
}
36-
37-
{
38-
auto iter = config.find("soft_threshold");
39-
if (iter != config.end()) {
40-
soft_threshold = iter->second.as<float>();
41-
}
42-
}
43-
44-
{
45-
auto iter = config.find("blur_strength");
46-
if (iter != config.end()) {
47-
blur_strength = iter->second.as<int>();
48-
}
49-
}
31+
labels = utils::get_from_any_maps("labels", config, {}, labels);
32+
soft_threshold = utils::get_from_any_maps("soft_threshold", config, {}, soft_threshold);
33+
blur_strength = utils::get_from_any_maps("blur_strength", config, {}, blur_strength);
5034
}
5135

5236
static cv::Size serialize(std::shared_ptr<ov::Model>& ov_model);

src/cpp/src/tasks/detection/ssd.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,6 @@ void SSD::prepareMultipleOutputs(std::shared_ptr<ov::Model> ov_model) {
148148
for (auto& name : output_names) {
149149
std::cout << "output name: " << name << std::endl;
150150
}
151-
152-
// ov::preprocess::PrePostProcessor ppp(ov_model);
153-
154-
// for (const auto& output_name : output_names) {
155-
// if (output_name != "labels") { //TODO: Discover why this isnt needed in original?
156-
// ppp.output(output_name).tensor().set_element_type(ov::element::f32);
157-
// }
158-
// }
159-
// ov_model = ppp.build();
160151
}
161152

162153
std::vector<std::string> SSD::filterOutXai(const std::vector<std::string>& names) {

tests/cpp/public_scope.json

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
[
2+
{
3+
"name": "otx_models/Lite-hrnet-18.xml",
4+
"type": "SegmentationModel",
5+
"test_data": [
6+
{
7+
"image": "coco128/images/train2017/000000000074.jpg",
8+
"reference": [
9+
"0: 0.537, 1: 0.463, [426,640,2], [0], [0]; object: 0.675, 508, object: 0.527, 65, object: 0.507, 18, object: 0.624, 144, object: 0.538, 67, object: 0.507, 15, object: 0.518, 41, object: 0.507, 8, object: 0.505, 14, object: 0.885, 2138, "
10+
]
11+
}
12+
]
13+
},
14+
{
15+
"name": "otx_models/Lite-hrnet-18_mod2.xml",
16+
"type": "SegmentationModel",
17+
"test_data": [
18+
{
19+
"image": "coco128/images/train2017/000000000074.jpg",
20+
"reference": [
21+
"0: 0.992, 1: 0.008, [426,640,2], [0], [0]; object: 0.555, 112, object: 0.506, 17, object: 0.555, 154, object: 0.511, 19, object: 0.514, 52, "
22+
]
23+
}
24+
]
25+
},
26+
{
27+
"name": "otx_models/segmentation_model_with_xai_head.xml",
28+
"type": "SegmentationModel",
29+
"test_data": [
30+
{
31+
"image": "coco128/images/train2017/000000000074.jpg",
32+
"reference": [
33+
"0: 1.000, 1: 0.000, [426,640,3], [426,640,3], [1,600,1,1]; backpack: 0.505, 2, "
34+
]
35+
}
36+
]
37+
},
38+
{
39+
"name": "otx_models/maskrcnn_model_with_xai_head.xml",
40+
"type": "MaskRCNNModel",
41+
"test_data": [
42+
{
43+
"image": "coco128/images/train2017/000000000074.jpg",
44+
"reference": [
45+
"61, 277, 358, 382, 17 (horse): 0.998, 18312, RotatedRect: 212.000 327.000 290.000 100.000 0.000; 1, 14, 162, 321, 2 (car): 0.994, 25867, RotatedRect: 54.067 173.034 285.208 156.889 61.996; 327, 96, 341, 134, 1 (bicycle): 0.930, 279, RotatedRect: 333.500 114.000 36.000 13.000 90.000; 460, 106, 493, 148, 1 (bicycle): 0.898, 786, RotatedRect: 476.284 126.621 27.308 45.993 19.179; 294, 93, 315, 153, 1 (bicycle): 0.869, 789, RotatedRect: 304.000 124.000 58.000 18.000 90.000; 278, 109, 290, 152, 1 (bicycle): 0.817, 355, RotatedRect: 283.500 130.000 42.000 11.000 90.000; 4, 4, 102, 191, 2 (car): 0.701, 9658, RotatedRect: 51.806 97.259 184.445 95.281 89.246; 270, 93, 290, 152, 1 (bicycle): 0.660, 723, RotatedRect: 280.500 122.500 17.000 59.000 0.000; 322, 114, 343, 152, 18 (sheep): 0.520, 298, RotatedRect: 332.000 133.000 34.000 14.000 90.000; 4; [1,1280,1,1]; "
46+
]
47+
}
48+
]
49+
},
50+
{
51+
"name": "otx_models/is_resnet50_maskrcnn_coco_reduced.xml",
52+
"type": "MaskRCNNModel",
53+
"test_data": [
54+
{
55+
"image": "coco128/images/train2017/000000000074.jpg",
56+
"reference": [
57+
"59, 277, 360, 380, 16 (horse): 0.999, 19053, RotatedRect: 210.000 327.500 101.000 296.000 90.000; 2, 9, 162, 318, 2 (car): 0.999, 31153, RotatedRect: 82.086 163.312 307.394 156.997 89.669; 294, 94, 316, 153, 1 (bicycle): 0.985, 840, RotatedRect: 305.000 123.500 59.000 18.000 90.000; 326, 97, 341, 136, 1 (bicycle): 0.974, 397, RotatedRect: 332.500 116.000 38.000 13.000 90.000; 461, 105, 493, 150, 1 (bicycle): 0.918, 846, RotatedRect: 476.052 126.972 27.619 47.834 16.928; 350, 92, 386, 149, 1 (bicycle): 0.807, 1458, RotatedRect: 369.319 119.891 54.848 34.230 82.405; 279, 110, 291, 146, 1 (bicycle): 0.788, 312, RotatedRect: 284.000 127.500 35.000 10.000 90.000; 0; [0]; horse: 0.999, 668; car: 0.999, 782; bicycle: 0.985, 127; bicycle: 0.974, 87; bicycle: 0.918, 122; bicycle: 0.807, 140; bicycle: 0.788, 79; "
58+
]
59+
}
60+
]
61+
},
62+
{
63+
"name": "otx_models/det_mobilenetv2_atss_bccd.xml",
64+
"type": "DetectionModel",
65+
"test_data": [
66+
{
67+
"image": "BloodImage_00007.jpg",
68+
"reference": [
69+
"494, 159, 637, 308, 2 (WBC): 0.697; 28, 139, 135, 228, 1 (RBC): 0.628; 535, 375, 638, 479, 1 (RBC): 0.524; 513, 8, 633, 152, 1 (RBC): 0.430; 21, 291, 143, 399, 1 (RBC): 0.422; 196, 86, 410, 286, 1 (RBC): 0.422; [0]; [0]"
70+
]
71+
}
72+
]
73+
},
74+
{
75+
"name": "otx_models/detection_model_with_xai_head.xml",
76+
"type": "DetectionModel",
77+
"test_data": [
78+
{
79+
"image": "coco128/images/train2017/000000000074.jpg",
80+
"reference": [
81+
"61, 277, 355, 379, 1 (person): 0.364; 461, 105, 495, 149, 1 (person): 0.305; [1,2,6,8]; [1,320,1,1]"
82+
]
83+
}
84+
]
85+
},
86+
{
87+
"name": "otx_models/detection_model_with_xai_head.xml",
88+
"type": "DetectionModel",
89+
"tiler": "DetectionTiler",
90+
"input_res": "(3500,3500)",
91+
"test_data": [
92+
{
93+
"image": "coco128/images/train2017/000000000074.jpg",
94+
"reference": [
95+
"336, 2275, 1944, 3114, 1 (person): 0.361; 2523, 862, 2709, 1224, 1 (person): 0.313; [1,2,35,46]; [1,320,1,1]"
96+
]
97+
}
98+
]
99+
}
100+
]

tests/cpp/test_accuracy.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,14 @@ TEST_P(ModelParameterizedTest, AccuracyTest) {
103103
} else if (data.type == "MaskRCNNModel") {
104104
GTEST_SKIP();
105105
} else if (data.type == "ClassificationModel") {
106-
auto model = Classification::load(model_path);
107-
for (auto& test_data : data.test_data) {
108-
std::string image_path = DATA_DIR + '/' + test_data.image;
109-
cv::Mat image = cv::imread(image_path);
110-
auto result = model.infer(image);
111-
EXPECT_EQ(std::string{result}, test_data.reference[0]);
112-
}
106+
GTEST_SKIP();
107+
//auto model = Classification::load(model_path);
108+
//for (auto& test_data : data.test_data) {
109+
// std::string image_path = DATA_DIR + '/' + test_data.image;
110+
// cv::Mat image = cv::imread(image_path);
111+
// auto result = model.infer(image);
112+
// EXPECT_EQ(std::string{result}, test_data.reference[0]);
113+
//}
113114
} else {
114115
FAIL() << "No implementation for model type " << data.type;
115116
}

0 commit comments

Comments
 (0)