使用OpenVINO API 2.0从cv::Mat和cv::Rect创建一个包含单个ROI的张量。

huangapple go评论79阅读模式
英文:

Create tensor with a single ROI from cv::Mat and cv::Rect with OpenVINO API 2.0

问题

OpenVINO API 2.0改变了图像的预处理方式。

文档 描述了一种用于从另一个张量构造感兴趣区域张量的重载。

Tensor(const Tensor& other, const Coordinate& begin, const Coordinate& end)

我找到了一个简短的示例,但并不清楚如何翻译成我的用例。

/** input_tensor指向先前网络的输入,
    cropROI包含输出边界框的坐标 **/
ov::Tensor input_tensor(ov::element::f32, ov::Shape({1, 3, 20, 20}));
ov::Coordinate begin({0, 0, 0, 0});
ov::Coordinate end({1, 2, 3, 3});
//...

我的目标是在不需要在调用 set_input_tensorSetBlob的替代品)之前复制ROI内存的情况下,对图像(cv::Mat)和边界框(cv::Rect)执行推理。我尚未找到足够的示例或文档来说明如何实现这一目标。我目前不确定如何将cv::Rect转换为ov::Coordinate

以前,我可以使用API 1.0来执行此操作,示例如下:

const InferenceEngine::ROI roi(0, bounding_box.x, bounding_box.y, bounding_box.width, bounding_box.height);
const InferenceEngine::TensorDesc tensor_desc(InferenceEngine::Precision::U8, { 1, channels, image_height, image_width }, InferenceEngine::Layout::NHWC);
const InferenceEngine::TensorDesc roi_tensor_desc = InferenceEngine::make_roi_desc(tensor_desc, roi, true);
const InferenceEngine::Blob::Ptr image_blob = InferenceEngine::make_shared_blob<uint8_t>(roi_tensor_desc, image.data);
request.SetBlob(input_name, image_blob);
英文:

OpenVINO API 2.0 changed how images are preprocessed.

Documentation describes an overload used to construct a region of interest tensor from another tensor.

Tensor(const Tensor& other, const Coordinate& begin, const Coordinate& end)

I found a brief example but did not find it clear enough to translate my use case.

/** input_tensor points to input of a previous network and
    cropROI contains coordinates of output bounding box **/
ov::Tensor input_tensor(ov::element::f32, ov::Shape({1, 3, 20, 20}));
ov::Coordinate begin({0, 0, 0, 0});
ov::Coordinate end({1, 2, 3, 3});
//...

My goal is to take an image (cv::Mat) and bounding box (cv::Rect) and execute inference on the ROI without having to copy the ROI memory before calling set_input_tensor (replacement for SetBlob). I have not been able to find sufficient examples or documentation for how to achieve this. I am currently unsure how to translate a cv::Rect into ov::Coordinates.

Previously I was able to do this with API 1.0 using the following example:

const InferenceEngine::ROI roi(0, bounding_box.x, bounding_box.y, bounding_box.width, bounding_box.height);
const InferenceEngine::TensorDesc tensor_desc(InferenceEngine::Precision::U8, { 1, channels, image_height, image_width }, InferenceEngine::Layout::NHWC);
const InferenceEngine::TensorDesc roi_tensor_desc = InferenceEngine::make_roi_desc(tensor_desc, roi, true);
const InferenceEngine::Blob::Ptr image_blob = InferenceEngine::make_shared_blob<uint8_t>(roi_tensor_desc, image.data);
request.SetBlob(input_name, image_blob);

答案1

得分: 0

OpenVINO API 2.0 changed layout work for models with NHWC. If your model has input { 1, channels, image_height, image_width } with NHWC layout, after conversion to IR with cmd '--layout=input_1(NHWC)' '--input_shape=[1, image_height, image_width, channels]', you have the input shape [1, image_height, image_width, channels]. Thus, you need to make ROI relative to this size.

For example, to make ROI tensor, you need:

const auto cv::Rect bounding_box = <>;
const auto shared_tensor = ov::Tensor(<data>); // it's tensor with shared input data
// N, H, W, C
const auto begin = ov::Coordinate({0, bounding_box.y, bounding_box.x, 0}); // the coordinates first bounding box corner
const auto end = ov::Coordinate({1, bounding_box.y + bounding_box.height, bounding_box.x + bounding_box.width, 3}); // the coordinates second bounding box corner
const auto roi_tensor = ov::Tensor(shared_tensor, begin, end); 

ov::Coordinate is class to represent coordinates on shapes. In this case these coordinates are of the bounding box.

英文:

OpenVINO API 2.0 changed layout work for models with NHWC. If your model has input { 1, channels, image_height, image_width } with NHWC layout, after conversion to IR with cmd '--layout=input_1(NHWC)' '--input_shape=[1, image_height, image_width, channels]', you have the input shape [1, image_height, image_width, channels]. Thus, you need to make ROI relative to this size.

For example, to make ROI tensor, you need:

const auto cv::Rect bounding_box = <>;
const auto shared_tensor = ov::Tensor(<data>); // it's tensor with shared input data
// N, H, W, C
const auto begin = ov::Coordinate({0, bounding_box.y, bounding_box.x, 0}); // the coordinates first bounding box corner
const auto end = ov::Coordinate({1, bounding_box.y + bounding_box.height, bounding_box.x + bounding_box.width, 3}); // the coordinates second bounding box corner
const auto roi_tensor = ov::Tensor(shared_tensor, begin, end); 

ov::Coordinate is class to represent coordinates on shapes. In this case these coordinates are of the bounding box.

答案2

得分: 0

在以前的遗留API中,您需要指定h和w维度。

而在OV 2.0 API中,您需要指定ROI张量的起始和结束坐标。

您可以参考此处获取更多信息。

英文:

Previously in legacy API, you need to specify h and w dimensions.

Meanwhile, in OV 2.0 API you need to specify the starting and ending of coordinates for the ROI tensor.

You may refer to here for more info.

huangapple
  • 本文由 发表于 2023年7月17日 13:36:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701723.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定