Intel® Distribution of OpenVINO™ Toolkit
Community assistance about the Intel® Distribution of OpenVINO™ toolkit, OpenCV, and all aspects of computer vision-related on Intel® platforms.
6403 Discussions

Efficient way to write batch of OpenCV Mat to input data blob pointer for inference using OpenVINO API

Shelke__Sagar
Beginner
1,251 Views

I have a batch of Mat stored as vector of 4.

Currently I am using following way to write data to NN input buffer

1. Get pointer to input blob

unsigned char *input_buffer;
unsigned char *input_buffer_current_image;
for (auto &item : nn_read_data.input_info) {
        input_name = item.first;
        auto input_data = item.second;
        auto input = nn_read_data.infer_request.GetBlob(input_name);
        input_buffer = input->buffer().as<PrecisionTrait<Precision::U8>::value_type *>();
}

2. Write images

for (int mb=0; mb < batchSize; mb++){ 
	frame = image_batch.at(mb);
	for (size_t pixelnum = 0; pixelnum < channel_size; ++pixelnum){
		for (size_t ch = 0; ch < num_channels; ++ch){
			input_buffer[full_image_size*mb + (ch * channel_size) + pixelnum] = frame.at<cv::Vec3b>    (pixelnum)[ch];
		}
    }

}

In this way it's taking around 34 ms to write a batch of 4. This is way more compared to OpenCV's

blobFromImages() and net.setInput()

combined.

Is there any faster/efficient way to perform the same?

0 Kudos
2 Replies
Shubha_R_Intel
Employee
1,249 Views

Dear Shelke, Sagar,

Please look at the hello_autoresize_classification sample. There you'll see 

  cv::Mat image = cv::imread(input_image_path);
        Blob::Ptr imgBlob = wrapMat2Blob(image);  // just wrap Mat data by Blob::Ptr without allocating of new memory
        infer_request.SetBlob(input_name, imgBlob);  //

 

Note the wrapMat2Blob call.

Hope it helps,

Thanks,

Shubha 

0 Kudos
Shelke__Sagar
Beginner
1,249 Views

Hi Shubha,

I have a vector of 4 Mats. function "wrapMat2Blob" writes data for a single image Mat.

After looking into hello_shape_infer_ssd example,

I tried using matU8ToBlob but for batch write, it does the same thing as I mentioned in original question.

Blob::Ptr input = infer_request.GetBlob(input_name);
for (size_t b = 0; b < batch_size; b++) {
matU8ToBlob<uint8_t>(image, input, b);
}

Is there any implementation of "wrapMat2Blob" for batch of Mat?

Thanks,

Sagar

 

0 Kudos
Reply