Skip to content

Commit 61e53f5

Browse files
committed
Remove input shape as input for the tasks
orig_width and height are stored in rt_info anyway. Serialization is a separate step and it _should_ output the same model
1 parent 58c7110 commit 61e53f5

File tree

9 files changed

+39
-38
lines changed

9 files changed

+39
-38
lines changed

src/cpp/include/tasks/classification.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ class Classification {
1919
std::shared_ptr<InferenceAdapter> adapter;
2020
VisionPipeline<ClassificationResult> pipeline;
2121

22-
Classification(std::shared_ptr<InferenceAdapter> adapter, cv::Size input_shape)
23-
: adapter(adapter),
24-
input_shape(input_shape) {
22+
Classification(std::shared_ptr<InferenceAdapter> adapter)
23+
: adapter(adapter) {
2524
pipeline = VisionPipeline<ClassificationResult>(
2625
adapter,
2726
[&](cv::Mat image) {
@@ -56,7 +55,7 @@ class Classification {
5655
}
5756
}
5857

59-
static cv::Size serialize(std::shared_ptr<ov::Model>& ov_model);
58+
static void serialize(std::shared_ptr<ov::Model>& ov_model);
6059
static Classification load(const std::string& model_path);
6160

6261
ClassificationResult infer(cv::Mat image);

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,17 @@ class SSD {
2828
public:
2929
std::shared_ptr<InferenceAdapter> adapter;
3030

31-
SSD(std::shared_ptr<InferenceAdapter> adapter, cv::Size input_shape) : adapter(adapter), input_shape(input_shape) {
31+
SSD(std::shared_ptr<InferenceAdapter> adapter) : adapter(adapter), input_shape(input_shape) {
3232
auto config = adapter->getModelConfig();
3333
labels = utils::get_from_any_maps("labels", config, {}, labels);
3434
confidence_threshold = utils::get_from_any_maps("confidence_threshold", config, {}, confidence_threshold);
35+
input_shape.width = utils::get_from_any_maps("orig_width", config, {}, input_shape.width);
36+
input_shape.height = utils::get_from_any_maps("orig_height", config, {}, input_shape.height);
3537
}
3638
std::map<std::string, ov::Tensor> preprocess(cv::Mat);
3739
DetectionResult postprocess(InferenceResult& infResult);
3840

39-
static cv::Size serialize(std::shared_ptr<ov::Model> ov_model);
41+
static void serialize(std::shared_ptr<ov::Model> ov_model);
4042

4143
SSDOutputMode output_mode;
4244

src/cpp/include/tasks/instance_segmentation.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ class InstanceSegmentation {
1818
std::shared_ptr<InferenceAdapter> adapter;
1919
VisionPipeline<InstanceSegmentationResult> pipeline;
2020

21-
InstanceSegmentation(std::shared_ptr<InferenceAdapter> adapter, cv::Size input_shape)
22-
: adapter(adapter),
23-
input_shape(input_shape) {
21+
InstanceSegmentation(std::shared_ptr<InferenceAdapter> adapter)
22+
: adapter(adapter) {
2423
pipeline = VisionPipeline<InstanceSegmentationResult>(
2524
adapter,
2625
[&](cv::Mat image) {
@@ -33,9 +32,11 @@ class InstanceSegmentation {
3332
auto config = adapter->getModelConfig();
3433
labels = utils::get_from_any_maps("labels", config, {}, labels);
3534
confidence_threshold = utils::get_from_any_maps("confidence_threshold", config, {}, confidence_threshold);
35+
input_shape.width = utils::get_from_any_maps("orig_width", config, {}, input_shape.width);
36+
input_shape.height = utils::get_from_any_maps("orig_height", config, {}, input_shape.width);
3637
}
3738

38-
static cv::Size serialize(std::shared_ptr<ov::Model>& ov_model);
39+
static void serialize(std::shared_ptr<ov::Model>& ov_model);
3940
static InstanceSegmentation load(const std::string& model_path);
4041

4142
InstanceSegmentationResult infer(cv::Mat image);

src/cpp/include/tasks/semantic_segmentation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class SemanticSegmentation {
3333
blur_strength = utils::get_from_any_maps("blur_strength", config, {}, blur_strength);
3434
}
3535

36-
static cv::Size serialize(std::shared_ptr<ov::Model>& ov_model);
36+
static void serialize(std::shared_ptr<ov::Model>& ov_model);
3737
static SemanticSegmentation load(const std::string& model_path);
3838

3939
std::map<std::string, ov::Tensor> preprocess(cv::Mat);

src/cpp/src/tasks/classification.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ std::vector<size_t> get_non_xai_output_indices(const std::vector<ov::Output<ov::
8282
}
8383
} // namespace
8484

85-
cv::Size Classification::serialize(std::shared_ptr<ov::Model>& ov_model) {
85+
void Classification::serialize(std::shared_ptr<ov::Model>& ov_model) {
8686
// --------------------------- Configure input & output -------------------------------------------------
8787
// --------------------------- Prepare input ------------------------------------------------------
8888
auto config = ov_model->has_rt_info("model_info") ? ov_model->get_rt_info<ov::AnyMap>("model_info") : ov::AnyMap{};
@@ -171,7 +171,9 @@ cv::Size Classification::serialize(std::shared_ptr<ov::Model>& ov_model) {
171171
if (multiclass) {
172172
addOrFindSoftmaxAndTopkOutputs(ov_model, topk, output_raw_scores);
173173
}
174-
return cv::Size(input_shape[0], input_shape[1]);
174+
175+
ov_model->set_rt_info(input_shape[0], "model_info", "orig_width");
176+
ov_model->set_rt_info(input_shape[1], "model_info", "orig_height");
175177
}
176178

177179
Classification Classification::load(const std::string& model_path) {
@@ -185,16 +187,14 @@ Classification Classification::load(const std::string& model_path) {
185187
throw std::runtime_error("Incorrect or unsupported model_type");
186188
}
187189

188-
cv::Size origin_input_shape;
189190
if (utils::model_has_embedded_processing(model)) {
190191
std::cout << "model already was serialized" << std::endl;
191-
origin_input_shape = utils::get_input_shape_from_model_info(model);
192192
} else {
193-
origin_input_shape = Classification::serialize(model);
193+
Classification::serialize(model);
194194
}
195195
auto adapter = std::make_shared<OpenVINOInferenceAdapter>();
196196
adapter->loadModel(model, core, "AUTO");
197-
return Classification(adapter, origin_input_shape);
197+
return Classification(adapter);
198198
}
199199

200200
ClassificationResult Classification::infer(cv::Mat image) {

src/cpp/src/tasks/detection.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,15 @@ DetectionModel DetectionModel::load(const std::string& model_path, const ov::Any
2222
throw std::runtime_error("Incorrect or unsupported model_type");
2323
}
2424

25-
cv::Size origin_input_shape;
2625
if (utils::model_has_embedded_processing(model)) {
2726
std::cout << "model already was serialized" << std::endl;
28-
origin_input_shape = utils::get_input_shape_from_model_info(model);
27+
//utils::get_input_shape_from_model_info(model);
2928
} else {
30-
origin_input_shape = SSD::serialize(model);
29+
SSD::serialize(model);
3130
}
3231
auto adapter = std::make_shared<OpenVINOInferenceAdapter>();
3332
adapter->loadModel(model, core, "AUTO");
34-
return DetectionModel(std::make_unique<SSD>(adapter, origin_input_shape), configuration);
33+
return DetectionModel(std::make_unique<SSD>(adapter), configuration);
3534
}
3635

3736
InferenceInput DetectionModel::preprocess(cv::Mat image) {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ std::map<std::string, ov::Tensor> SSD::preprocess(cv::Mat image) {
6767
return input;
6868
}
6969

70-
cv::Size SSD::serialize(std::shared_ptr<ov::Model> ov_model) {
70+
void SSD::serialize(std::shared_ptr<ov::Model> ov_model) {
7171
auto output_mode = ov_model->outputs().size() > 1 ? SSDOutputMode::multi : SSDOutputMode::single;
7272

7373
auto input_tensor = ov_model->inputs()[0];
@@ -111,7 +111,9 @@ cv::Size SSD::serialize(std::shared_ptr<ov::Model> ov_model) {
111111
// prepareMultipleOutputs(ov_model); //This does nothing from what I can see.
112112
}
113113

114-
return cv::Size(input_shape[0], input_shape[1]);
114+
115+
ov_model->set_rt_info(input_shape[0], "model_info", "orig_width");
116+
ov_model->set_rt_info(input_shape[1], "model_info", "orig_height");
115117
}
116118

117119
void SSD::prepareSingleOutput(std::shared_ptr<ov::Model> ov_model) {

src/cpp/src/tasks/instance_segmentation.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ cv::Mat segm_postprocess(const SegmentedObject& box, const cv::Mat& unpadded, in
121121
return im_mask;
122122
}
123123

124-
cv::Size InstanceSegmentation::serialize(std::shared_ptr<ov::Model>& ov_model) {
124+
void InstanceSegmentation::serialize(std::shared_ptr<ov::Model>& ov_model) {
125125
if (ov_model->inputs().size() != 1) {
126126
throw std::logic_error("MaskRCNNModel model wrapper supports topologies with only 1 input");
127127
}
@@ -145,6 +145,7 @@ cv::Size InstanceSegmentation::serialize(std::shared_ptr<ov::Model>& ov_model) {
145145
mean_values = utils::get_from_any_maps("mean_values", config, ov::AnyMap{}, mean_values);
146146
uint8_t pad_value = 0;
147147
bool reverse_input_channels = false;
148+
reverse_input_channels = utils::get_from_any_maps("reverse_input_channels", config, ov::AnyMap{}, reverse_input_channels);
148149

149150
ov_model = utils::embedProcessing(
150151
ov_model,
@@ -180,7 +181,8 @@ cv::Size InstanceSegmentation::serialize(std::shared_ptr<ov::Model>& ov_model) {
180181
saliency_map_name + ", " + feature_vector_name + " and 3 or 4 other outputs");
181182
}
182183

183-
return input_shape;
184+
ov_model->set_rt_info(input_shape.width, "model_info", "orig_width");
185+
ov_model->set_rt_info(input_shape.height, "model_info", "orig_height");
184186
}
185187

186188
InstanceSegmentation InstanceSegmentation::load(const std::string& model_path) {
@@ -194,16 +196,14 @@ InstanceSegmentation InstanceSegmentation::load(const std::string& model_path) {
194196
throw std::runtime_error("Incorrect or unsupported model_type");
195197
}
196198

197-
cv::Size origin_input_shape;
198199
if (utils::model_has_embedded_processing(model)) {
199200
std::cout << "model already was serialized" << std::endl;
200-
origin_input_shape = utils::get_input_shape_from_model_info(model);
201201
} else {
202-
origin_input_shape = serialize(model);
202+
serialize(model);
203203
}
204204
auto adapter = std::make_shared<OpenVINOInferenceAdapter>();
205205
adapter->loadModel(model, core, "AUTO");
206-
return InstanceSegmentation(adapter, origin_input_shape);
206+
return InstanceSegmentation(adapter);
207207
}
208208

209209
InstanceSegmentationResult InstanceSegmentation::infer(cv::Mat image) {

src/cpp/src/tasks/semantic_segmentation.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,17 @@ SemanticSegmentation SemanticSegmentation::load(const std::string& model_path) {
3131
throw std::runtime_error("Incorrect or unsupported model_type");
3232
}
3333

34-
cv::Size origin_input_shape;
3534
if (utils::model_has_embedded_processing(model)) {
3635
std::cout << "model already was serialized" << std::endl;
37-
origin_input_shape = utils::get_input_shape_from_model_info(model);
3836
} else {
39-
origin_input_shape = SemanticSegmentation::serialize(model);
37+
SemanticSegmentation::serialize(model);
4038
}
4139
auto adapter = std::make_shared<OpenVINOInferenceAdapter>();
4240
adapter->loadModel(model, core, "AUTO");
4341
return SemanticSegmentation(adapter);
4442
}
4543

46-
cv::Size SemanticSegmentation::serialize(std::shared_ptr<ov::Model>& ov_model) {
44+
void SemanticSegmentation::serialize(std::shared_ptr<ov::Model>& ov_model) {
4745
if (ov_model->inputs().size() != 1) {
4846
throw std::logic_error("Segmentation model wrapper supports topologies with only 1 input");
4947
}
@@ -90,14 +88,13 @@ cv::Size SemanticSegmentation::serialize(std::shared_ptr<ov::Model>& ov_model) {
9088
scale_values = utils::get_from_any_maps("scale_values", config, ov::AnyMap{}, scale_values);
9189
mean_values = utils::get_from_any_maps("mean_values", config, ov::AnyMap{}, mean_values);
9290

93-
auto input_shape = ov::Shape{shape[ov::layout::width_idx(layout)], shape[ov::layout::height_idx(layout)]};
9491

9592
ov_model = utils::embedProcessing(ov_model,
9693
input.get_any_name(),
9794
layout,
9895
resize_mode,
9996
interpolation_mode,
100-
input_shape,
97+
ov::Shape{shape[ov::layout::width_idx(layout)], shape[ov::layout::height_idx(layout)]},
10198
pad_value,
10299
reverse_input_channels,
103100
mean_values,
@@ -115,7 +112,10 @@ cv::Size SemanticSegmentation::serialize(std::shared_ptr<ov::Model>& ov_model) {
115112
}
116113
ov_model = ppp.build();
117114

118-
return cv::Size(input_shape[0], input_shape[1]);
115+
cv::Size input_shape(shape[ov::layout::width_idx(layout)],
116+
shape[ov::layout::height_idx(layout)]);
117+
ov_model->set_rt_info(input_shape.width, "model_info", "orig_width");
118+
ov_model->set_rt_info(input_shape.height, "model_info", "orig_height");
119119
}
120120

121121
std::map<std::string, ov::Tensor> SemanticSegmentation::preprocess(cv::Mat image) {
@@ -162,7 +162,6 @@ SemanticSegmentationResult SemanticSegmentation::postprocess(InferenceResult& in
162162
SemanticSegmentationResult result;
163163
result.resultImage = hard_prediction;
164164
if (return_soft_prediction) {
165-
std::cout << " got a soft prediction..." << std::endl;
166165
cv::resize(soft_prediction, soft_prediction, infResult.inputImageSize, 0.0, 0.0, cv::INTER_NEAREST);
167166
result.soft_prediction = soft_prediction;
168167
auto iter = infResult.data.find(feature_vector_name);
@@ -227,7 +226,6 @@ cv::Mat SemanticSegmentation::create_hard_prediction_from_soft_prediction(cv::Ma
227226

228227
bool applyBlurAndSoftThreshold = (blur_strength > -1 && soft_threshold < std::numeric_limits<float>::infinity());
229228
if (applyBlurAndSoftThreshold) {
230-
std::cout << "applying blur and soft threshold: " << blur_strength << std::endl;
231229
cv::blur(soft_prediction_blurred, soft_prediction_blurred, cv::Size{blur_strength, blur_strength});
232230
}
233231

0 commit comments

Comments
 (0)