Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7675ec1

Browse files
committed
Fix for OpenVINO 2024.0
1 parent 1eb061f commit 7675ec1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+728
-545
lines changed

modules/dnn/src/ie_ngraph.cpp

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
#include <opencv2/dnn/shape_utils.hpp>
1515

1616
#ifdef HAVE_DNN_NGRAPH
17+
#if INF_ENGINE_VER_MAJOR_GE(INF_ENGINE_RELEASE_2024_0)
18+
#include <openvino/core/extension.hpp>
19+
#else
1720
#include <ie_extension.h>
21+
#endif
1822
#endif // HAVE_DNN_NGRAPH
1923

2024
#include <opencv2/core/utils/configuration.private.hpp>
@@ -23,6 +27,8 @@
2327
#include "opencv2/core/utils/filesystem.hpp"
2428
#include "opencv2/core/utils/filesystem.private.hpp"
2529

30+
#include <openvino/op/fake_quantize.hpp>
31+
2632
namespace cv { namespace dnn {
2733

2834
#ifdef HAVE_DNN_NGRAPH
@@ -84,7 +90,7 @@ class NgraphCustomOp: public ov::op::Op {
8490
public:
8591
OPENVINO_OP(kOpenCVLayersType);
8692

87-
NgraphCustomOp(const ngraph::OutputVector& inputs, Ptr<Layer>& cvLayer, const std::vector<Mat>& outputs, const std::vector<Mat>& internals):
93+
NgraphCustomOp(const ov::OutputVector& inputs, Ptr<Layer>& cvLayer, const std::vector<Mat>& outputs, const std::vector<Mat>& internals):
8894
Op(inputs), cvLayer(cvLayer), outputs(outputs), internals(internals)
8995
{
9096
constructor_validate_and_infer_types();
@@ -103,7 +109,7 @@ class NgraphCustomOp: public ov::op::Op {
103109
}
104110
}
105111

106-
std::shared_ptr<ngraph::Node> clone_with_new_inputs(const ngraph::OutputVector& new_args) const override
112+
std::shared_ptr<ov::Node> clone_with_new_inputs(const ov::OutputVector& new_args) const override
107113
{
108114
return std::make_shared<NgraphCustomOp>(new_args, cvLayer, outputs, internals);
109115
}
@@ -133,18 +139,18 @@ class NgraphCustomOp: public ov::op::Op {
133139

134140
#else
135141

136-
class NgraphCustomOp: public ngraph::op::Op {
142+
class NgraphCustomOp: public ov::op::Op {
137143
public:
138-
const ngraph::NodeTypeInfo& get_type_info() const override
144+
const ov::NodeTypeInfo& get_type_info() const override
139145
{
140-
static constexpr ngraph::NodeTypeInfo type_info{kOpenCVLayersType, static_cast<uint64_t>(0)};
146+
static constexpr ov::NodeTypeInfo type_info{kOpenCVLayersType, static_cast<uint64_t>(0)};
141147
return type_info;
142148
}
143149

144150
#if INF_ENGINE_VER_MAJOR_GE(INF_ENGINE_RELEASE_2020_3)
145-
NgraphCustomOp(const ngraph::OutputVector& inputs,
151+
NgraphCustomOp(const ov::OutputVector& inputs,
146152
#else
147-
NgraphCustomOp(const ngraph::NodeVector& inputs,
153+
NgraphCustomOp(const ov::NodeVector& inputs,
148154
#endif
149155
const std::map<std::string, InferenceEngine::Parameter>& params = {}):
150156
Op(inputs), params(params)
@@ -164,28 +170,28 @@ class NgraphCustomOp: public ngraph::op::Op {
164170
set_output_size(shapes.size());
165171
for (size_t i = 0; i < shapes.size(); ++i)
166172
{
167-
ngraph::Shape output_shape(shapes[i]);
173+
ov::Shape output_shape(shapes[i]);
168174
set_output_type(i, get_input_element_type(0), output_shape);
169175
}
170176
}
171177

172178
#if INF_ENGINE_VER_MAJOR_GE(INF_ENGINE_RELEASE_2020_4)
173-
std::shared_ptr<ngraph::Node> clone_with_new_inputs(const ngraph::OutputVector& new_args) const override
179+
std::shared_ptr<ov::Node> clone_with_new_inputs(const ov::OutputVector& new_args) const override
174180
{
175181
return std::make_shared<NgraphCustomOp>(new_args, params);
176182
}
177183
#else
178-
std::shared_ptr<ngraph::Node> copy_with_new_args(const ngraph::NodeVector& new_args) const override
184+
std::shared_ptr<ov::Node> copy_with_new_args(const ov::NodeVector& new_args) const override
179185
{
180186
#if INF_ENGINE_VER_MAJOR_GE(INF_ENGINE_RELEASE_2020_3)
181-
return std::make_shared<NgraphCustomOp>(ngraph::as_output_vector(new_args), params);
187+
return std::make_shared<NgraphCustomOp>(ov::as_output_vector(new_args), params);
182188
#else
183189
return std::make_shared<NgraphCustomOp>(new_args, params);
184190
#endif
185191
}
186192
#endif
187193

188-
bool visit_attributes(ngraph::AttributeVisitor& visitor) override
194+
bool visit_attributes(ov::AttributeVisitor& visitor) override
189195
{
190196
for (auto& attr : params)
191197
{
@@ -203,7 +209,7 @@ class InfEngineNgraphCustomLayer : public InferenceEngine::ILayerExecImpl
203209
{
204210
public:
205211
#if INF_ENGINE_VER_MAJOR_GE(INF_ENGINE_RELEASE_2020_2)
206-
explicit InfEngineNgraphCustomLayer(const std::shared_ptr<ngraph::Node>& _node)
212+
explicit InfEngineNgraphCustomLayer(const std::shared_ptr<ov::Node>& _node)
207213
{
208214
node = std::dynamic_pointer_cast<NgraphCustomOp>(_node);
209215
CV_Assert(node);
@@ -350,11 +356,11 @@ class InfEngineNgraphExtension : public InferenceEngine::IExtension
350356
void GetVersion(const InferenceEngine::Version*&) const noexcept override {}
351357

352358
#if INF_ENGINE_VER_MAJOR_GE(INF_ENGINE_RELEASE_2020_2)
353-
std::vector<std::string> getImplTypes(const std::shared_ptr<ngraph::Node>& node) override {
359+
std::vector<std::string> getImplTypes(const std::shared_ptr<ov::Node>& node) override {
354360
return {"CPU"};
355361
}
356362

357-
InferenceEngine::ILayerImpl::Ptr getImplementation(const std::shared_ptr<ngraph::Node>& node, const std::string& implType) override {
363+
InferenceEngine::ILayerImpl::Ptr getImplementation(const std::shared_ptr<ov::Node>& node, const std::string& implType) override {
358364
if (std::dynamic_pointer_cast<NgraphCustomOp>(node) && implType == "CPU") {
359365
return std::make_shared<InfEngineNgraphCustomLayer>(node);
360366
}
@@ -383,13 +389,13 @@ class InfEngineNgraphExtension : public InferenceEngine::IExtension
383389

384390
#endif // OpenVINO >= 2022.1
385391

386-
InfEngineNgraphNode::InfEngineNgraphNode(ngraph::Output<ngraph::Node>&& _node)
392+
InfEngineNgraphNode::InfEngineNgraphNode(ov::Output<ov::Node>&& _node)
387393
: BackendNode(DNN_BACKEND_INFERENCE_ENGINE_NGRAPH), node(std::move(_node)) {
388394
CV_Assert(node.get_node());
389395
CV_Assert(node.get_node_shared_ptr());
390396
}
391397

392-
InfEngineNgraphNode::InfEngineNgraphNode(const ngraph::Output<ngraph::Node>& _node)
398+
InfEngineNgraphNode::InfEngineNgraphNode(const ov::Output<ov::Node>& _node)
393399
: BackendNode(DNN_BACKEND_INFERENCE_ENGINE_NGRAPH), node(_node) {
394400
CV_Assert(node.get_node());
395401
CV_Assert(node.get_node_shared_ptr());
@@ -401,9 +407,9 @@ InfEngineNgraphNode::InfEngineNgraphNode(const std::vector<Ptr<BackendNode> >& n
401407
: BackendNode(DNN_BACKEND_INFERENCE_ENGINE_NGRAPH), cvLayer(cvLayer_)
402408
{
403409
#if INF_ENGINE_VER_MAJOR_GE(INF_ENGINE_RELEASE_2020_3)
404-
ngraph::OutputVector inp_nodes;
410+
ov::OutputVector inp_nodes;
405411
#else
406-
ngraph::NodeVector inp_nodes;
412+
ov::NodeVector inp_nodes;
407413
#endif
408414
for (const auto& node : nodes)
409415
inp_nodes.emplace_back(node.dynamicCast<InfEngineNgraphNode>()->node);
@@ -455,20 +461,20 @@ void InfEngineNgraphNet::createNet(Target targetId) {
455461
if (!hasNetOwner)
456462
{
457463
CV_Assert(!requestedOutputs.empty());
458-
ngraph::ResultVector outs;
464+
ov::ResultVector outs;
459465

460466
for (auto output_node_it = requestedOutputs.begin(); output_node_it != requestedOutputs.end(); ++output_node_it)
461467
{
462468
CV_LOG_DEBUG(NULL, "DNN/NGRAPH: Add 'Result' output: " << output_node_it->first);
463469
CV_Assert(output_node_it->second);
464-
auto out = std::make_shared<ngraph::op::Result>(output_node_it->second->node);
470+
auto out = std::make_shared<ov::op::v0::Result>(output_node_it->second->node);
465471
#if INF_ENGINE_VER_MAJOR_GE(INF_ENGINE_RELEASE_2022_1)
466472
out->set_friendly_name(output_node_it->first + (output_node_it->second->node.get_node()->get_output_size() == 1 ? "" : ".0"));
467473
#endif
468474
outs.push_back(out);
469475
}
470476
CV_Assert_N(!inputs_vec.empty(), !outs.empty());
471-
ngraph_function = std::make_shared<ngraph::Function>(outs, inputs_vec);
477+
ngraph_function = std::make_shared<ov::Model>(outs, inputs_vec);
472478
init(targetId);
473479
}
474480
}
@@ -490,23 +496,23 @@ void InfEngineNgraphNet::init(Target targetId)
490496
auto nodes = ngraph_function->get_ordered_ops();
491497
for (auto& node : nodes)
492498
{
493-
auto parameter = std::dynamic_pointer_cast<ngraph::op::Parameter>(node);
494-
if (parameter && parameter->get_element_type() == ngraph::element::f32)
499+
auto parameter = std::dynamic_pointer_cast<ov::op::v0::Parameter>(node);
500+
if (parameter && parameter->get_element_type() == ov::element::f32)
495501
{
496-
parameter->set_element_type(ngraph::element::f16);
502+
parameter->set_element_type(ov::element::f16);
497503
}
498-
auto constant = std::dynamic_pointer_cast<ngraph::op::Constant>(node);
499-
if (constant && constant->get_element_type() == ngraph::element::f32)
504+
auto constant = std::dynamic_pointer_cast<ov::op::v0::Constant>(node);
505+
if (constant && constant->get_element_type() == ov::element::f32)
500506
{
501507
const float* floatsData = constant->get_data_ptr<float>();
502-
size_t total = ngraph::shape_size(constant->get_shape());
508+
size_t total = ov::shape_size(constant->get_shape());
503509
Mat floats(1, total, CV_32F, (void*)floatsData);
504510
Mat halfs;
505511
floats.convertTo(halfs, CV_16F);
506512

507-
auto new_const = std::make_shared<ngraph::op::Constant>(ngraph::element::f16, constant->get_shape(), halfs.data);
513+
auto new_const = std::make_shared<ov::op::v0::Constant>(ov::element::f16, constant->get_shape(), halfs.data);
508514
new_const->set_friendly_name(constant->get_friendly_name());
509-
ngraph::replace_node(constant, new_const);
515+
ov::replace_node(constant, new_const);
510516
}
511517
}
512518
ngraph_function->validate_nodes_and_infer_types();
@@ -624,18 +630,18 @@ void InfEngineNgraphNet::init(Target targetId)
624630
initPlugin(cnn);
625631
}
626632

627-
ngraph::ParameterVector InfEngineNgraphNet::setInputs(const std::vector<cv::Mat>& inputs,
633+
ov::ParameterVector InfEngineNgraphNet::setInputs(const std::vector<cv::Mat>& inputs,
628634
const std::vector<std::string>& names) {
629635
CV_Assert_N(inputs.size() == names.size());
630-
ngraph::ParameterVector current_inp;
636+
ov::ParameterVector current_inp;
631637
for (size_t i = 0; i < inputs.size(); i++)
632638
{
633639
std::vector<size_t> shape = getShape<size_t>(inputs[i]);
634-
auto inp = std::make_shared<ngraph::op::Parameter>(ngraph::element::f32, ngraph::Shape(shape));
640+
auto inp = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape(shape));
635641
inp->set_friendly_name(names[i]);
636642

637643
auto it = std::find_if(inputs_vec.begin(), inputs_vec.end(),
638-
[&inp](const std::shared_ptr<ngraph::op::Parameter>& a) {
644+
[&inp](const std::shared_ptr<ov::op::v0::Parameter>& a) {
639645
return a->get_friendly_name() == inp->get_friendly_name();
640646
});
641647
if (it == inputs_vec.end()) {
@@ -1236,28 +1242,28 @@ void InfEngineNgraphNet::forward(const std::vector<Ptr<BackendWrapper> >& outBlo
12361242
#endif // OpenVINO >= 2022.1
12371243
}
12381244

1239-
ngraph::Output<ngraph::Node> ngraphQuantize(ngraph::Output<ngraph::Node> input, float output_sc, float output_zp) {
1245+
ov::Output<ov::Node> ngraphQuantize(ov::Output<ov::Node> input, float output_sc, float output_zp) {
12401246
float outLow = -128, outHigh = 127;
12411247
float inpLow = output_sc * (outLow - output_zp);
12421248
float inpHigh = output_sc * (outHigh - output_zp);
1243-
return std::make_shared<ngraph::op::FakeQuantize>(input,
1244-
std::make_shared<ngraph::op::Constant>(ngraph::element::f32, ngraph::Shape{1}, &inpLow),
1245-
std::make_shared<ngraph::op::Constant>(ngraph::element::f32, ngraph::Shape{1}, &inpHigh),
1246-
std::make_shared<ngraph::op::Constant>(ngraph::element::f32, ngraph::Shape{1}, &outLow),
1247-
std::make_shared<ngraph::op::Constant>(ngraph::element::f32, ngraph::Shape{1}, &outHigh),
1249+
return std::make_shared<ov::op::v0::FakeQuantize>(input,
1250+
std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{1}, &inpLow),
1251+
std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{1}, &inpHigh),
1252+
std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{1}, &outLow),
1253+
std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{1}, &outHigh),
12481254
256 // levels
12491255
);
12501256
}
12511257

1252-
ngraph::Output<ngraph::Node> ngraphDequantize(ngraph::Output<ngraph::Node> input, float input_sc, float input_zp) {
1258+
ov::Output<ov::Node> ngraphDequantize(ov::Output<ov::Node> input, float input_sc, float input_zp) {
12531259
float inpLow = -128, inpHigh = 127;
12541260
float outLow = input_sc * (inpLow - input_zp);
12551261
float outHigh = input_sc * (inpHigh - input_zp);
1256-
return std::make_shared<ngraph::op::FakeQuantize>(input,
1257-
std::make_shared<ngraph::op::Constant>(ngraph::element::f32, ngraph::Shape{1}, &inpLow),
1258-
std::make_shared<ngraph::op::Constant>(ngraph::element::f32, ngraph::Shape{1}, &inpHigh),
1259-
std::make_shared<ngraph::op::Constant>(ngraph::element::f32, ngraph::Shape{1}, &outLow),
1260-
std::make_shared<ngraph::op::Constant>(ngraph::element::f32, ngraph::Shape{1}, &outHigh),
1262+
return std::make_shared<ov::op::v0::FakeQuantize>(input,
1263+
std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{1}, &inpLow),
1264+
std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{1}, &inpHigh),
1265+
std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{1}, &outLow),
1266+
std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{1}, &outHigh),
12611267
256 // levels
12621268
);
12631269
}

modules/dnn/src/ie_ngraph.hpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
#pragma warning(disable : 4245)
1818
#pragma warning(disable : 4268)
1919
#endif
20+
#if INF_ENGINE_VER_MAJOR_GE(INF_ENGINE_RELEASE_2024_0)
21+
#include <openvino/openvino.hpp>
22+
#else
2023
#include <ngraph/ngraph.hpp>
24+
#endif
2125
#ifdef _MSC_VER
2226
#pragma warning(pop)
2327
#endif
@@ -45,7 +49,7 @@ class InfEngineNgraphNet
4549
void forward(const std::vector<Ptr<BackendWrapper> >& outBlobsWrappers, bool isAsync);
4650

4751
void initPlugin(InferenceEngine::CNNNetwork& net);
48-
ngraph::ParameterVector setInputs(const std::vector<cv::Mat>& inputs, const std::vector<std::string>& names);
52+
ov::ParameterVector setInputs(const std::vector<cv::Mat>& inputs, const std::vector<std::string>& names);
4953

5054
void addBlobs(const std::vector<cv::Ptr<BackendWrapper> >& ptrs);
5155

@@ -56,8 +60,8 @@ class InfEngineNgraphNet
5660
//private:
5761
detail::NetImplBase& netImpl_;
5862

59-
ngraph::ParameterVector inputs_vec;
60-
std::shared_ptr<ngraph::Function> ngraph_function;
63+
ov::ParameterVector inputs_vec;
64+
std::shared_ptr<ov::Model> ngraph_function;
6165

6266
InferenceEngine::ExecutableNetwork netExec;
6367
#if INF_ENGINE_VER_MAJOR_GE(INF_ENGINE_RELEASE_2022_1)
@@ -93,13 +97,13 @@ class InfEngineNgraphNode : public BackendNode
9397
std::vector<Mat*>& inputs, std::vector<Mat>& outputs,
9498
std::vector<Mat>& internals);
9599

96-
InfEngineNgraphNode(ngraph::Output<ngraph::Node>&& _node);
97-
InfEngineNgraphNode(const ngraph::Output<ngraph::Node>& _node);
100+
InfEngineNgraphNode(ov::Output<ov::Node>&& _node);
101+
InfEngineNgraphNode(const ov::Output<ov::Node>& _node);
98102

99103
void setName(const std::string& name);
100104

101105
// Inference Engine network object that allows to obtain the outputs of this layer.
102-
ngraph::Output<ngraph::Node> node;
106+
ov::Output<ov::Node> node;
103107
Ptr<InfEngineNgraphNet> net;
104108
Ptr<dnn::Layer> cvLayer;
105109
};
@@ -148,8 +152,8 @@ class NgraphBackendLayer : public Layer
148152
InferenceEngine::CNNNetwork t_net;
149153
};
150154

151-
ngraph::Output<ngraph::Node> ngraphQuantize(ngraph::Output<ngraph::Node> input, float output_sc, float output_zp);
152-
ngraph::Output<ngraph::Node> ngraphDequantize(ngraph::Output<ngraph::Node> input, float input_sc, float input_zp);
155+
ov::Output<ov::Node> ngraphQuantize(ov::Output<ov::Node> input, float output_sc, float output_zp);
156+
ov::Output<ov::Node> ngraphDequantize(ov::Output<ov::Node> input, float input_sc, float input_zp);
153157

154158
#endif // HAVE_DNN_NGRAPH
155159

modules/dnn/src/int8layers/batch_norm_layer.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#include "../ie_ngraph.hpp"
99

1010
#include <opencv2/dnn/shape_utils.hpp>
11+
#include <openvino/op/constant.hpp>
12+
#include <openvino/op/add.hpp>
13+
#include <openvino/op/multiply.hpp>
1114

1215
namespace cv
1316
{
@@ -250,11 +253,11 @@ class BatchNormLayerInt8Impl CV_FINAL : public BatchNormLayerInt8
250253
std::vector<size_t> shape(input.get_shape().size(), 1);
251254
shape[1] = origin_weights.total();
252255

253-
ngraph::Output<ngraph::Node> res;
254-
auto ieWeights = std::make_shared<ngraph::op::Constant>(ngraph::element::f32, shape, origin_weights.data);
255-
auto ieBias = std::make_shared<ngraph::op::Constant>(ngraph::element::f32, shape, origin_bias.data);
256-
res = std::make_shared<ngraph::op::v1::Multiply>(input, ieWeights);
257-
res = std::make_shared<ngraph::op::v1::Add>(res, ieBias);
256+
ov::Output<ov::Node> res;
257+
auto ieWeights = std::make_shared<ov::op::v0::Constant>(ov::element::f32, shape, origin_weights.data);
258+
auto ieBias = std::make_shared<ov::op::v0::Constant>(ov::element::f32, shape, origin_bias.data);
259+
res = std::make_shared<ov::op::v1::Multiply>(input, ieWeights);
260+
res = std::make_shared<ov::op::v1::Add>(res, ieBias);
258261

259262
res = ngraphQuantize(res, output_sc, output_zp);
260263
return new InfEngineNgraphNode(res);

0 commit comments

Comments
 (0)