From 607b04e1297258a0471a711d62918e83b2cb5b39 Mon Sep 17 00:00:00 2001 From: m12watanabe1a <40206149+m12watanabe1a@users.noreply.github.com> Date: Sun, 16 Jun 2024 23:31:39 +0900 Subject: [PATCH 1/2] Fix to suppress warnings of YOLOX --- .../include/yolox_cpp/yolox_openvino.hpp | 10 +-- .../yolox_cpp/src/yolox_openvino.cpp | 83 +++++-------------- 2 files changed, 27 insertions(+), 66 deletions(-) diff --git a/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox_openvino.hpp b/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox_openvino.hpp index 74c7b93..d3ba1de 100644 --- a/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox_openvino.hpp +++ b/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox_openvino.hpp @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include "core.hpp" #include "coco_names.hpp" @@ -22,11 +22,9 @@ namespace yolox_cpp{ private: std::string device_name_; - std::string input_name_; - std::string output_name_; - InferenceEngine::CNNNetwork network_; - InferenceEngine::ExecutableNetwork executable_network_; - InferenceEngine::InferRequest infer_request_; + std::vector blob_; + ov::Shape input_shape_; + ov::InferRequest infer_request_; }; } diff --git a/yolox_ros_cpp/yolox_cpp/src/yolox_openvino.cpp b/yolox_ros_cpp/yolox_cpp/src/yolox_openvino.cpp index a6ef7b4..3a4aad5 100644 --- a/yolox_ros_cpp/yolox_cpp/src/yolox_openvino.cpp +++ b/yolox_ros_cpp/yolox_cpp/src/yolox_openvino.cpp @@ -1,8 +1,6 @@ #include "yolox_cpp/yolox_openvino.hpp" namespace yolox_cpp{ - using namespace InferenceEngine; - YoloXOpenVINO::YoloXOpenVINO(file_name_t path_to_model, std::string device_name, float nms_th, float conf_th, std::string model_version, int num_classes, bool p6) @@ -11,55 +9,36 @@ namespace yolox_cpp{ { // Step 1. Initialize inference engine core std::cout << "Initialize Inference engine core" << std::endl; - Core ie; + ov::Core ie; // Step 2. Read a model in OpenVINO Intermediate Representation (.xml and // .bin files) or ONNX (.onnx file) format std::cout << "Read a model in OpenVINO Intermediate Representation: " << path_to_model << std::endl; - network_ = ie.ReadNetwork(path_to_model); - if (network_.getOutputsInfo().size() != 1) - throw std::logic_error("Sample supports topologies with 1 output only"); - if (network_.getInputsInfo().size() != 1) - throw std::logic_error("Sample supports topologies with 1 input only"); + const auto network = ie.read_model(path_to_model); - // Step 3. Configure input & output - std::cout << "Configuring input and output blobs" << std::endl; - // Prepare input blobs - InputInfo::Ptr input_info = network_.getInputsInfo().begin()->second; - input_name_ = network_.getInputsInfo().begin()->first; + // Step 3. Loading a model to the device + std::cout << "Loading a model to the device: " << device_name_ << std::endl; + auto compiled_model = ie.compile_model(network, device_name); + + // Step 4. Create an infer request + std::cout << "Create an infer request" << std::endl; + this->infer_request_ = compiled_model.create_infer_request(); + // Step 5. Configure input & output + std::cout << "Configuring input and output blobs" << std::endl; + this->input_shape_ = compiled_model.input(0).get_shape(); /* Mark input as resizable by setting of a resize algorithm. * In this case we will be able to set an input blob of any shape to an * infer request. Resize and layout conversions are executed automatically * during inference */ - //input_info->getPreProcess().setResizeAlgorithm(RESIZE_BILINEAR); - //input_info->setLayout(Layout::NHWC); - input_info->setPrecision(Precision::FP32); - auto input_dims = input_info->getInputData()->getDims(); - this->input_h_ = input_dims[2]; - this->input_w_ = input_dims[3]; + this->blob_.resize( + this->input_shape_.at(0) * this->input_shape_.at(1) * + this->input_shape_.at(2) * this->input_shape_.at(3)); + this->input_h_ = this->input_shape_.at(2); + this->input_w_ = this->input_shape_.at(3); std::cout << "INPUT_HEIGHT: " << this->input_h_ << std::endl; std::cout << "INPUT_WIDTH: " << this->input_w_ << std::endl; - // Prepare output blobs - if (network_.getOutputsInfo().empty()) { - std::cerr << "Network outputs info is empty" << std::endl; - throw std :: runtime_error( "Network outputs info is empty" ); - } - DataPtr output_info = network_.getOutputsInfo().begin()->second; - output_name_ = network_.getOutputsInfo().begin()->first; - - // output_info->setPrecision(Precision::FP16); - output_info->setPrecision(Precision::FP32); - - // Step 4. Loading a model to the device - std::cout << "Loading a model to the device: " << device_name_ << std::endl; - executable_network_ = ie.LoadNetwork(network_, device_name_); - - // Step 5. Create an infer request - std::cout << "Create an infer request" << std::endl; - infer_request_ = executable_network_.CreateInferRequest(); - // Prepare GridAndStrides if(this->p6_) { @@ -75,34 +54,18 @@ namespace yolox_cpp{ { // preprocess cv::Mat pr_img = static_resize(frame); - InferenceEngine::Blob::Ptr imgBlob = infer_request_.GetBlob(input_name_); - InferenceEngine::MemoryBlob::Ptr mblob = InferenceEngine::as(imgBlob); - if (!mblob) - { - THROW_IE_EXCEPTION << "We expect blob to be inherited from MemoryBlob in matU8ToBlob, " - << "but by fact we were not able to cast inputBlob to MemoryBlob"; - } // locked memory holder should be alive all time while access to its buffer happens - auto mblobHolder = mblob->wmap(); - float *blob_data = mblobHolder.as(); - blobFromImage(pr_img, blob_data); + blobFromImage(pr_img, this->blob_.data()); // do inference /* Running the request synchronously */ - infer_request_.Infer(); + this->infer_request_.set_input_tensor( + ov::Tensor{ov::element::f32, this->input_shape_, reinterpret_cast(this->blob_.data())}); + infer_request_.infer(); - // Process output - const InferenceEngine::Blob::Ptr output_blob = infer_request_.GetBlob(output_name_); - InferenceEngine::MemoryBlob::CPtr moutput = as(output_blob); - if (!moutput) { - throw std::logic_error("We expect output to be inherited from MemoryBlob, " - "but by fact we were not able to cast output to MemoryBlob"); - } - // locked memory holder should be alive all time while access to its buffer - // happens - auto moutputHolder = moutput->rmap(); - const float* net_pred = moutputHolder.as::value_type*>(); + const auto &output_tensor = this->infer_request_.get_output_tensor(); + const float* net_pred = reinterpret_cast(output_tensor.data()); float scale = std::min(input_w_ / (frame.cols*1.0), input_h_ / (frame.rows*1.0)); std::vector objects; From e71d9d2adf958d17c70a7128cc238b9dd81703fd Mon Sep 17 00:00:00 2001 From: m12watanabe1a <40206149+m12watanabe1a@users.noreply.github.com> Date: Sun, 16 Jun 2024 23:34:45 +0900 Subject: [PATCH 2/2] Fix cmake dependencies --- yolox_ros_cpp/yolox_cpp/CMakeLists.txt | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/yolox_ros_cpp/yolox_cpp/CMakeLists.txt b/yolox_ros_cpp/yolox_cpp/CMakeLists.txt index 9070482..640001e 100644 --- a/yolox_ros_cpp/yolox_cpp/CMakeLists.txt +++ b/yolox_ros_cpp/yolox_cpp/CMakeLists.txt @@ -37,13 +37,11 @@ set(ENABLE_TFLITE OFF) if(YOLOX_USE_OPENVINO) find_package(OpenVINO REQUIRED) - find_package(InferenceEngine REQUIRED) - find_package(ngraph REQUIRED) set(ENABLE_OPENVINO ON) set(TARGET_SRC src/yolox_openvino.cpp) - set(TARGET_LIBS InferenceEngine ngraph) - set(TARGET_DPENDENCIES OpenVINO InferenceEngine ngraph) + set(TARGET_LIBS openvino::runtime) + set(TARGET_DPENDENCIES OpenVINO) endif() if(YOLOX_USE_TENSORRT)