Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions yolox_ros_cpp/yolox_cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 4 additions & 6 deletions yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox_openvino.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <vector>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <inference_engine.hpp>
#include <openvino/openvino.hpp>

#include "core.hpp"
#include "coco_names.hpp"
Expand All @@ -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<float> blob_;
ov::Shape input_shape_;
ov::InferRequest infer_request_;
};
}

Expand Down
83 changes: 23 additions & 60 deletions yolox_ros_cpp/yolox_cpp/src/yolox_openvino.cpp
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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_)
{
Expand All @@ -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<InferenceEngine::MemoryBlob>(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<float *>();
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<float *>(this->blob_.data())});
infer_request_.infer();

// Process output
const InferenceEngine::Blob::Ptr output_blob = infer_request_.GetBlob(output_name_);
InferenceEngine::MemoryBlob::CPtr moutput = as<InferenceEngine::MemoryBlob>(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<const PrecisionTrait<Precision::FP32>::value_type*>();
const auto &output_tensor = this->infer_request_.get_output_tensor();
const float* net_pred = reinterpret_cast<float *>(output_tensor.data());

float scale = std::min(input_w_ / (frame.cols*1.0), input_h_ / (frame.rows*1.0));
std::vector<Object> objects;
Expand Down