From 52d3588358bba6def31ea07b562842826b3ea631 Mon Sep 17 00:00:00 2001 From: Ilya Churaev Date: Thu, 26 Oct 2023 23:24:24 +0400 Subject: [PATCH] Move create model sample to the last opset (#20333) * Move create model sample to the last opset * Downgrade to opset8 * Migrate sample to the last opset * Fixed code style --- samples/cpp/model_creation_sample/main.cpp | 104 +++++++++++---------- 1 file changed, 53 insertions(+), 51 deletions(-) diff --git a/samples/cpp/model_creation_sample/main.cpp b/samples/cpp/model_creation_sample/main.cpp index 078bf6b7d0eb1a..8f2df4817bfe47 100644 --- a/samples/cpp/model_creation_sample/main.cpp +++ b/samples/cpp/model_creation_sample/main.cpp @@ -12,8 +12,7 @@ // clang-format off #include "openvino/openvino.hpp" -#include "openvino/opsets/opset1.hpp" -#include "openvino/opsets/opset8.hpp" +#include "openvino/opsets/opset13.hpp" #include "samples/args_helper.hpp" #include "samples/common.hpp" @@ -84,127 +83,130 @@ std::shared_ptr create_model(const std::string& path_to_weights) { std::vector padBegin{0, 0}; std::vector padEnd{0, 0}; - auto paramNode = std::make_shared(ov::element::Type_t::f32, ov::Shape({64, 1, 28, 28})); + auto paramNode = std::make_shared(ov::element::Type_t::f32, ov::Shape({64, 1, 28, 28})); // -------convolution 1---- auto convFirstShape = Shape{20, 1, 5, 5}; - auto convolutionFirstConstantNode = std::make_shared(element::Type_t::f32, convFirstShape, data); + auto convolutionFirstConstantNode = std::make_shared(element::Type_t::f32, convFirstShape, data); - auto convolutionNodeFirst = std::make_shared(paramNode->output(0), - convolutionFirstConstantNode->output(0), - Strides({1, 1}), - CoordinateDiff(padBegin), - CoordinateDiff(padEnd), - Strides({1, 1})); + auto convolutionNodeFirst = std::make_shared(paramNode->output(0), + convolutionFirstConstantNode->output(0), + Strides({1, 1}), + CoordinateDiff(padBegin), + CoordinateDiff(padEnd), + Strides({1, 1})); // -------Add-------------- auto addFirstShape = Shape{1, 20, 1, 1}; auto offset = shape_size(convFirstShape) * sizeof(float); - auto addFirstConstantNode = std::make_shared(element::Type_t::f32, addFirstShape, data + offset); + auto addFirstConstantNode = std::make_shared(element::Type_t::f32, addFirstShape, data + offset); - auto addNodeFirst = std::make_shared(convolutionNodeFirst->output(0), addFirstConstantNode->output(0)); + auto addNodeFirst = + std::make_shared(convolutionNodeFirst->output(0), addFirstConstantNode->output(0)); // -------MAXPOOL---------- Shape padBeginShape{0, 0}; Shape padEndShape{0, 0}; - auto maxPoolingNodeFirst = std::make_shared(addNodeFirst->output(0), - Strides{2, 2}, - padBeginShape, - padEndShape, - Shape{2, 2}, - op::RoundingType::CEIL); + auto maxPoolingNodeFirst = std::make_shared(addNodeFirst->output(0), + Strides{2, 2}, + Strides{1, 1}, + padBeginShape, + padEndShape, + Shape{2, 2}, + op::RoundingType::CEIL); // -------convolution 2---- auto convSecondShape = Shape{50, 20, 5, 5}; offset += shape_size(addFirstShape) * sizeof(float); auto convolutionSecondConstantNode = - std::make_shared(element::Type_t::f32, convSecondShape, data + offset); + std::make_shared(element::Type_t::f32, convSecondShape, data + offset); - auto convolutionNodeSecond = std::make_shared(maxPoolingNodeFirst->output(0), - convolutionSecondConstantNode->output(0), - Strides({1, 1}), - CoordinateDiff(padBegin), - CoordinateDiff(padEnd), - Strides({1, 1})); + auto convolutionNodeSecond = std::make_shared(maxPoolingNodeFirst->output(0), + convolutionSecondConstantNode->output(0), + Strides({1, 1}), + CoordinateDiff(padBegin), + CoordinateDiff(padEnd), + Strides({1, 1})); // -------Add 2------------ auto addSecondShape = Shape{1, 50, 1, 1}; offset += shape_size(convSecondShape) * sizeof(float); auto addSecondConstantNode = - std::make_shared(element::Type_t::f32, addSecondShape, data + offset); + std::make_shared(element::Type_t::f32, addSecondShape, data + offset); auto addNodeSecond = - std::make_shared(convolutionNodeSecond->output(0), addSecondConstantNode->output(0)); + std::make_shared(convolutionNodeSecond->output(0), addSecondConstantNode->output(0)); // -------MAXPOOL 2-------- - auto maxPoolingNodeSecond = std::make_shared(addNodeSecond->output(0), - Strides{2, 2}, - padBeginShape, - padEndShape, - Shape{2, 2}, - op::RoundingType::CEIL); + auto maxPoolingNodeSecond = std::make_shared(addNodeSecond->output(0), + Strides{2, 2}, + Strides{1, 1}, + padBeginShape, + padEndShape, + Shape{2, 2}, + op::RoundingType::CEIL); // -------Reshape---------- auto reshapeFirstShape = Shape{2}; auto reshapeOffset = shape_size(addSecondShape) * sizeof(float) + offset; auto reshapeFirstConstantNode = - std::make_shared(element::Type_t::i64, reshapeFirstShape, data + reshapeOffset); + std::make_shared(element::Type_t::i64, reshapeFirstShape, data + reshapeOffset); auto reshapeFirstNode = - std::make_shared(maxPoolingNodeSecond->output(0), reshapeFirstConstantNode->output(0), true); + std::make_shared(maxPoolingNodeSecond->output(0), reshapeFirstConstantNode->output(0), true); // -------MatMul 1--------- auto matMulFirstShape = Shape{500, 800}; offset = shape_size(reshapeFirstShape) * sizeof(int64_t) + reshapeOffset; auto matMulFirstConstantNode = - std::make_shared(element::Type_t::f32, matMulFirstShape, data + offset); + std::make_shared(element::Type_t::f32, matMulFirstShape, data + offset); auto matMulFirstNode = - std::make_shared(reshapeFirstNode->output(0), matMulFirstConstantNode->output(0), false, true); + std::make_shared(reshapeFirstNode->output(0), matMulFirstConstantNode->output(0), false, true); // -------Add 3------------ auto addThirdShape = Shape{1, 500}; offset += shape_size(matMulFirstShape) * sizeof(float); - auto addThirdConstantNode = std::make_shared(element::Type_t::f32, addThirdShape, data + offset); + auto addThirdConstantNode = std::make_shared(element::Type_t::f32, addThirdShape, data + offset); - auto addThirdNode = std::make_shared(matMulFirstNode->output(0), addThirdConstantNode->output(0)); + auto addThirdNode = std::make_shared(matMulFirstNode->output(0), addThirdConstantNode->output(0)); // -------Relu------------- - auto reluNode = std::make_shared(addThirdNode->output(0)); + auto reluNode = std::make_shared(addThirdNode->output(0)); // -------Reshape 2-------- auto reshapeSecondShape = Shape{2}; auto reshapeSecondConstantNode = - std::make_shared(element::Type_t::i64, reshapeSecondShape, data + reshapeOffset); + std::make_shared(element::Type_t::i64, reshapeSecondShape, data + reshapeOffset); auto reshapeSecondNode = - std::make_shared(reluNode->output(0), reshapeSecondConstantNode->output(0), true); + std::make_shared(reluNode->output(0), reshapeSecondConstantNode->output(0), true); // -------MatMul 2--------- auto matMulSecondShape = Shape{10, 500}; offset += shape_size(addThirdShape) * sizeof(float); auto matMulSecondConstantNode = - std::make_shared(element::Type_t::f32, matMulSecondShape, data + offset); + std::make_shared(element::Type_t::f32, matMulSecondShape, data + offset); - auto matMulSecondNode = std::make_shared(reshapeSecondNode->output(0), - matMulSecondConstantNode->output(0), - false, - true); + auto matMulSecondNode = std::make_shared(reshapeSecondNode->output(0), + matMulSecondConstantNode->output(0), + false, + true); // -------Add 4------------ auto add4Shape = Shape{1, 10}; offset += shape_size(matMulSecondShape) * sizeof(float); - auto add4ConstantNode = std::make_shared(element::Type_t::f32, add4Shape, data + offset); + auto add4ConstantNode = std::make_shared(element::Type_t::f32, add4Shape, data + offset); - auto add4Node = std::make_shared(matMulSecondNode->output(0), add4ConstantNode->output(0)); + auto add4Node = std::make_shared(matMulSecondNode->output(0), add4ConstantNode->output(0)); // -------softMax---------- - auto softMaxNode = std::make_shared(add4Node->output(0), 1); + auto softMaxNode = std::make_shared(add4Node->output(0), 1); softMaxNode->get_output_tensor(0).set_names({"output_tensor"}); // ------- OpenVINO function-- - auto result_full = std::make_shared(softMaxNode->output(0)); + auto result_full = std::make_shared(softMaxNode->output(0)); std::shared_ptr fnPtr = std::make_shared(result_full, ov::ParameterVector{paramNode}, "lenet");