Skip to content

Commit 2294c06

Browse files
authored
Merge ModelBase into ImageModel (#278)
* Merge ModelBase into ImageModel Example works, but probably some missing parts. * Fix clang linter * Rename ImageModel to BaseModel as opposed to ModelBase as it was. So first it was ImageModel: ModelBase. Then ModelBase merged into ImageModel now rename ImageModel to BaseModel
1 parent f1ada42 commit 2294c06

31 files changed

+485
-582
lines changed

src/cpp/models/include/models/anomaly_model.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
*/
55

66
#pragma once
7-
#include "models/image_model.h"
7+
#include "models/base_model.h"
88

99
namespace ov {
1010
class Model;
1111
} // namespace ov
1212
struct AnomalyResult;
1313
struct ImageInputData;
1414

15-
class AnomalyModel : public ImageModel {
15+
class AnomalyModel : public BaseModel {
1616
public:
1717
AnomalyModel(std::shared_ptr<ov::Model>& model, const ov::AnyMap& configuration);
1818
AnomalyModel(std::shared_ptr<InferenceAdapter>& adapter, const ov::AnyMap& configuration = {});

src/cpp/models/include/models/image_model.h renamed to src/cpp/models/include/models/base_model.h

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
#include <memory>
1010
#include <string>
1111

12+
#include "adapters/inference_adapter.h"
1213
#include "models/input_data.h"
13-
#include "models/model_base.h"
14+
#include "models/results.h"
15+
#include "utils/args_helper.hpp"
1416
#include "utils/image_utils.h"
17+
#include "utils/ocv_common.hpp"
1518

1619
namespace ov {
1720
class InferRequest;
@@ -20,22 +23,37 @@ struct InputData;
2023
struct InternalModelData;
2124

2225
// ImageModel implements preprocess(), ImageModel's direct or indirect children are expected to implement prostprocess()
23-
class ImageModel : public ModelBase {
26+
class BaseModel {
2427
public:
2528
/// Constructor
2629
/// @param modelFile name of model to load
2730
/// @param useAutoResize - if true, image is resized by openvino
2831
/// @param layout - model input layout
29-
ImageModel(const std::string& modelFile,
30-
const std::string& resize_type,
31-
bool useAutoResize,
32-
const std::string& layout = "");
32+
BaseModel(const std::string& modelFile,
33+
const std::string& resize_type,
34+
bool useAutoResize,
35+
const std::string& layout = "");
3336

34-
ImageModel(std::shared_ptr<ov::Model>& model, const ov::AnyMap& configuration);
35-
ImageModel(std::shared_ptr<InferenceAdapter>& adapter, const ov::AnyMap& configuration = {});
36-
using ModelBase::ModelBase;
37+
BaseModel(std::shared_ptr<ov::Model>& model, const ov::AnyMap& configuration);
38+
BaseModel(std::shared_ptr<InferenceAdapter>& adapter, const ov::AnyMap& configuration = {});
39+
40+
virtual std::shared_ptr<InternalModelData> preprocess(const InputData& inputData, InferenceInput& input);
41+
virtual std::unique_ptr<ResultBase> postprocess(InferenceResult& infResult) = 0;
42+
43+
void load(ov::Core& core, const std::string& device, size_t num_infer_requests = 1);
44+
45+
std::shared_ptr<ov::Model> prepare();
46+
47+
virtual size_t getNumAsyncExecutors() const;
48+
virtual bool isReady();
49+
virtual void awaitAll();
50+
virtual void awaitAny();
51+
virtual void setCallback(
52+
std::function<void(std::unique_ptr<ResultBase>, const ov::AnyMap& callback_args)> callback);
53+
54+
std::shared_ptr<ov::Model> getModel();
55+
std::shared_ptr<InferenceAdapter> getInferenceAdapter();
3756

38-
std::shared_ptr<InternalModelData> preprocess(const InputData& inputData, InferenceInput& input) override;
3957
static std::vector<std::string> loadLabels(const std::string& labelFilename);
4058
std::shared_ptr<ov::Model> embedProcessing(std::shared_ptr<ov::Model>& model,
4159
const std::string& inputName,
@@ -54,7 +72,7 @@ class ImageModel : public ModelBase {
5472

5573
protected:
5674
RESIZE_MODE selectResizeMode(const std::string& resize_type);
57-
void updateModelInfo() override;
75+
virtual void updateModelInfo();
5876
void init_from_config(const ov::AnyMap& top_priority, const ov::AnyMap& mid_priority);
5977

6078
std::string getLabelName(size_t labelID) {
@@ -73,4 +91,18 @@ class ImageModel : public ModelBase {
7391
bool reverse_input_channels = false;
7492
std::vector<float> scale_values;
7593
std::vector<float> mean_values;
94+
95+
protected:
96+
virtual void prepareInputsOutputs(std::shared_ptr<ov::Model>& model) = 0;
97+
98+
InputTransform inputTransform = InputTransform();
99+
100+
std::shared_ptr<ov::Model> model;
101+
std::vector<std::string> inputNames;
102+
std::vector<std::string> outputNames;
103+
std::string modelFile;
104+
std::shared_ptr<InferenceAdapter> inferenceAdapter;
105+
std::map<std::string, ov::Layout> inputsLayouts;
106+
ov::Layout getInputLayout(const ov::Output<ov::Node>& input);
107+
std::function<void(std::unique_ptr<ResultBase>, const ov::AnyMap&)> lastCallback;
76108
};

src/cpp/models/include/models/classification_model.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include <unordered_map>
1313
#include <vector>
1414

15-
#include "models/image_model.h"
15+
#include "models/base_model.h"
1616

1717
namespace ov {
1818
class Model;
@@ -88,7 +88,7 @@ class ProbabilisticLabelsResolver : public GreedyLabelsResolver {
8888
SimpleLabelsGraph label_tree;
8989
};
9090

91-
class ClassificationModel : public ImageModel {
91+
class ClassificationModel : public BaseModel {
9292
public:
9393
ClassificationModel(std::shared_ptr<ov::Model>& model, const ov::AnyMap& configuration);
9494
ClassificationModel(std::shared_ptr<InferenceAdapter>& adapter, const ov::AnyMap& configuration = {});

src/cpp/models/include/models/detection_model.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77

88
#include <string>
99

10-
#include "models/image_model.h"
10+
#include "models/base_model.h"
1111

1212
struct DetectionResult;
1313
struct ImageInputData;
1414
struct InferenceAdatper;
1515

16-
class DetectionModel : public ImageModel {
16+
class DetectionModel : public BaseModel {
1717
public:
1818
DetectionModel(std::shared_ptr<ov::Model>& model, const ov::AnyMap& configuration);
1919
DetectionModel(std::shared_ptr<InferenceAdapter>& adapter, const ov::AnyMap& configuration = {});

src/cpp/models/include/models/instance_segmentation.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <string>
99
#include <vector>
1010

11-
#include "models/image_model.h"
11+
#include "models/base_model.h"
1212

1313
namespace ov {
1414
class Model;
@@ -19,7 +19,7 @@ struct InstanceSegmentationResult;
1919
struct ImageInputData;
2020
struct SegmentedObject;
2121

22-
class MaskRCNNModel : public ImageModel {
22+
class MaskRCNNModel : public BaseModel {
2323
public:
2424
MaskRCNNModel(std::shared_ptr<ov::Model>& model, const ov::AnyMap& configuration);
2525
MaskRCNNModel(std::shared_ptr<InferenceAdapter>& adapter, const ov::AnyMap& configuration = {});

src/cpp/models/include/models/keypoint_detection.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <string>
99
#include <vector>
1010

11-
#include "models/image_model.h"
11+
#include "models/base_model.h"
1212

1313
namespace ov {
1414
class Model;
@@ -18,7 +18,7 @@ struct ResultBase;
1818
struct KeypointDetectionResult;
1919
struct ImageInputData;
2020

21-
class KeypointDetectionModel : public ImageModel {
21+
class KeypointDetectionModel : public BaseModel {
2222
public:
2323
KeypointDetectionModel(std::shared_ptr<ov::Model>& model, const ov::AnyMap& configuration);
2424
KeypointDetectionModel(std::shared_ptr<InferenceAdapter>& adapter, const ov::AnyMap& configuration = {});

src/cpp/models/include/models/model_base.h

Lines changed: 0 additions & 73 deletions
This file was deleted.

src/cpp/models/include/models/segmentation_model.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <string>
99
#include <vector>
1010

11-
#include "models/image_model.h"
11+
#include "models/base_model.h"
1212

1313
namespace ov {
1414
class Model;
@@ -20,7 +20,7 @@ struct ImageResultWithSoftPrediction;
2020
struct ImageInputData;
2121
struct Contour;
2222

23-
class SegmentationModel : public ImageModel {
23+
class SegmentationModel : public BaseModel {
2424
public:
2525
SegmentationModel(std::shared_ptr<ov::Model>& model, const ov::AnyMap& configuration);
2626
SegmentationModel(std::shared_ptr<InferenceAdapter>& adapter, const ov::AnyMap& configuration = {});

src/cpp/models/src/anomaly_model.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <openvino/core/model.hpp>
1111
#include <ostream>
1212

13-
#include "models/image_model.h"
13+
#include "models/base_model.h"
1414
#include "models/input_data.h"
1515
#include "models/internal_model_data.h"
1616
#include "models/results.h"
@@ -29,23 +29,23 @@ void AnomalyModel::init_from_config(const ov::AnyMap& top_priority, const ov::An
2929
}
3030

3131
AnomalyModel::AnomalyModel(std::shared_ptr<ov::Model>& model, const ov::AnyMap& configuration)
32-
: ImageModel(model, configuration) {
32+
: BaseModel(model, configuration) {
3333
init_from_config(configuration, model->get_rt_info<ov::AnyMap>("model_info"));
3434
}
3535

3636
AnomalyModel::AnomalyModel(std::shared_ptr<InferenceAdapter>& adapter, const ov::AnyMap& configuration)
37-
: ImageModel(adapter, configuration) {
37+
: BaseModel(adapter, configuration) {
3838
init_from_config(configuration, adapter->getModelConfig());
3939
}
4040

4141
std::unique_ptr<AnomalyResult> AnomalyModel::infer(const ImageInputData& inputData) {
42-
auto result = ImageModel::inferImage(inputData);
42+
auto result = BaseModel::inferImage(inputData);
4343

4444
return std::unique_ptr<AnomalyResult>(static_cast<AnomalyResult*>(result.release()));
4545
}
4646

4747
std::vector<std::unique_ptr<AnomalyResult>> AnomalyModel::inferBatch(const std::vector<ImageInputData>& inputImgs) {
48-
auto results = ImageModel::inferBatchImage(inputImgs);
48+
auto results = BaseModel::inferBatchImage(inputImgs);
4949
std::vector<std::unique_ptr<AnomalyResult>> anoResults;
5050
anoResults.reserve(results.size());
5151
for (auto& result : results) {
@@ -177,7 +177,7 @@ void AnomalyModel::prepareInputsOutputs(std::shared_ptr<ov::Model>& model) {
177177
const ov::Layout& inputLayout = getInputLayout(input);
178178

179179
if (!embedded_processing) {
180-
model = ImageModel::embedProcessing(
180+
model = BaseModel::embedProcessing(
181181
model,
182182
inputNames[0],
183183
inputLayout,
@@ -194,7 +194,7 @@ void AnomalyModel::prepareInputsOutputs(std::shared_ptr<ov::Model>& model) {
194194
}
195195

196196
void AnomalyModel::updateModelInfo() {
197-
ImageModel::updateModelInfo();
197+
BaseModel::updateModelInfo();
198198

199199
model->set_rt_info(AnomalyModel::ModelType, "model_info", "model_type");
200200
model->set_rt_info(task, "model_info", "task");

0 commit comments

Comments
 (0)