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.
6392 Discussions

Resize network according to the input shape

Guo__Song
Beginner
928 Views

OpenVINO R3

In the "object_detection_sample_ssd" sample, i want to detect object in different size images. I have set the following for input_info 

        input_info->getPreProcess().setResizeAlgorithm(RESIZE_BILINEAR);
        input_info->setLayout(Layout::NHWC);
        input_info->setPrecision(Precision::U8); 

and prepare input as 

        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); 

but i get the same results if i resize the input image myself. Is this correct? The input image is resizable but the input blob is still fixed with SetBlob()?

What i am trying to do is: resizing the network input according the input image shape, not only accept resizable input shape, so i can detect small objects in large images, what do i need to do? Can this be done by only downloading the models once? You know to  accelerate the  whole process of object detection for many different size images. Thank you in advance for your help~

 

0 Kudos
4 Replies
nikos1
Valued Contributor I
928 Views

> but i get the same results if i resize the input image myself. Is this correct? 

Correct!

> What i am trying to do is: resizing the network input according the input image shape, not only accept resizable input shape, so i can detect small objects in large images, what do i need to do? Can this be done by only downloading the models once? 

You may need different IR for and implement your own approach to solve this. I cannot find a way to do that out of the box in OpenVino but I may be wrong.

Cheers,

Nikos

0 Kudos
Nikolay_L_Intel1
Employee
928 Views

Hi!

OpenVINO has a Shape Inference feature that allows resizing network according to the input shape. It doesn't require downloading model and converting it to IR using MO mutliple times. CNNNetwork object can be resized by setting new input shapes (dimensions) using Inference Engine API. 
Here's code example:

    // ------------- 0. Read IR and image ----------------------------------------------
    CNNNetReader network_reader;
    network_reader.ReadNetwork("path/to/IR/xml");
    CNNNetwork network = network_reader.getNetwork();
    cv::Mat image = cv::imread("path/to/image");

    // ------------- 1. Collect the map of input names and shapes from IR---------------
    auto input_shapes = network.getInputShapes();

    // ------------- 2. Set new input shapes -------------------------------------------
    std::string input_name;
    SizeVector input_shape;
    std::tie(input_name, input_shape) = *input_shapes.begin(); // let's consider first input only
    input_shape[0] = batch_size; // set batch size to the first input dimension
    input_shape[2] = image.rows; // changes input height to the image one
    input_shape[3] = image.cols; // changes input width to the image one
    input_shapes[input_name] = input_shape;

    // ------------- 3. Call reshape ---------------------------------------------------
    network.reshape(input_shapes);
    ...

    // ------------- 4. Loading model to the plugin ------------------------------------
    ExecutableNetwork executable_network = plugin.LoadNetwork(network, {});

 

Please refer to Hello Shape Infer Sample for demonstration the feature on example of SSD-like models.
Also it was used in Smart Classroom Demo.

Currently, the process of object detection for many different size images could be done by loading multiple networks with different sizes:

    network.reshape(input_shapes_1);
    ExecutableNetwork executable_network_1 = plugin.LoadNetwork(network, {});
    network.reshape(input_shapes_2);
    ExecutableNetwork executable_network_2 = plugin.LoadNetwork(network, {});
    ...

Best regards,
Nikolay

0 Kudos
Guo__Song
Beginner
928 Views

Nikolay L. (Intel) wrote:

Hi!

OpenVINO has a Shape Inference feature that allows resizing network according to the input shape.
Here's code example:

    // ------------- 0. Read IR and image ----------------------------------------------
    CNNNetReader network_reader;
    network_reader.ReadNetwork("path/to/IR/xml");
    CNNNetwork network = network_reader.getNetwork();
    cv::Mat image = cv::imread("path/to/image");

    // ------------- 1. Collect the map of input names and shapes from IR---------------
    auto input_shapes = network.getInputShapes();

    // ------------- 2. Set new input shapes -------------------------------------------
    std::string input_name;
    SizeVector input_shape;
    std::tie(input_name, input_shape) = *input_shapes.begin(); // let's consider first input only
    input_shape[0] = batch_size; // set batch size to the first input dimension
    input_shape[2] = image.rows; // changes input height to the image one
    input_shape[3] = image.cols; // changes input width to the image one
    input_shapes[input_name] = input_shape;

    // ------------- 3. Call reshape ---------------------------------------------------
    network.reshape(input_shapes);
    ...

    // ------------- 4. Loading model to the plugin ------------------------------------
    ExecutableNetwork executable_network = plugin.LoadNetwork(network, {});

 

Please refer to Hello Shape Infer Sample for demonstration the feature on example of SSD-like models.
Also it was used in Smart Classroom Demo.

Best regards,
Nikolay

Thank you very much!

Yes, i have realized resizing network  according to the input shape. But i have to load the resized IR model every time for a different input image shape.  This is quite time-consuming if the images have different shapes, which is common in practical applications. I just wonder whether this can done without loading the IR model every time. Thank you!  

0 Kudos
Nikolay_L_Intel1
Employee
928 Views

Currently, it's possible when all possible shapes are known and number of them limited by reasonable amount.
In that case possible solution is to load multiple networks for all these sizes once before starting inference. 

0 Kudos
Reply