Skip to content

Batch inference not working when InferenecEngine as backend #14404

Closed

Description

@shelkesagar29
System information (version)
  • OpenCV => 4.0.1
  • Operating System / Platform => Ubuntu 16.04
  • Compiler => gcc
Detailed description

Hi,

I am using OpenCV DNN module for NN inference. Inference on single image, i.e. batch size of 1 works fine but when I try to use more than one images in inference batch, it fails giving error

what(): OpenCV(4.0.1-openvino) /home/jenkins/workspace/OpenCV/OpenVINO/build/opencv/modules/dnn/src/op_inf_engine.cpp:553: error: (-215:Assertion failed) Failed to initialize Inference Engine backend: Input blob size is not equal network input size (1350000!=270000). in function 'initPlugin'

I am using blobFromImages instead of blobFromImage. Blob has 'batch_size' images in it but just inference doesn't work.

I know OpenVINO API has SetBatch() method.

Should I instead switch to OpenVINO API for inference?

Thanks for your time.

Activity

  1. dkurt commented on Apr 24, 2019

    @dkurt
    Member

    Please provide more details. Do you use a model in native format or IR with .XML and .bin files?

  2. dkurt commented on Apr 24, 2019

    @dkurt
    Member

    @shelkesagar29, Thank you for the report! Please test the changes from PR #14407.

  3. shelkesagar29 commented on Apr 24, 2019

    @shelkesagar29
    Author

    @dkurt I was using IR format with .xml and .bin files.
    Somehow, with normal OpenCV backend, batch inference works.

  4. dkurt commented on Apr 24, 2019

    @dkurt
    Member

    @shelkesagar29, please test the changes from PR or add a reference to IR files (if it's possible). For me only native object detection models with IE backend have similar error.

  5. shelkesagar29 commented on Apr 24, 2019

    @shelkesagar29
    Author

    @dkurt I will test and let you know. Thanks for the quick response.

  6. shelkesagar29 commented on Apr 24, 2019

    @shelkesagar29
    Author

    @dkurt After few experiments, I am still not able to run batch inference through OpenCV. I made the changes suggested by you and built opencv from source with inference engine.

    This time to make things easy, I used SSD Mobilenet 300 IR files (shared here https://drive.google.com/open?id=13NZtkv9HqdfCftA2w49LVFSttTUx5L8Q) and following code snippet

    `

     #include "opencv2/core/core.hpp"
     #include "opencv2/highgui/highgui.hpp"
     #include "opencv2/imgproc/imgproc.hpp"
     #include "opencv2/dnn.hpp"
    
     using namespace cv;
     using namespace std;
     using namespace dnn;
    Size inpSize(300,300);
    Net net = readNet("./openvino_32/frozen_inference_graph.xml", 
                                  "./openvino_32/frozen_inference_graph.bin");
    net.setPreferableBackend(DNN_BACKEND_INFERENCE_ENGINE);
    net.setPreferableTarget(DNN_TARGET_CPU);
    
    vector<Mat> blob;
    Mat dummy_image = imread("./openvino_32/000067.jpg");
    vector<Mat>image_batch (3, dummy_image);
    blobFromImages(image_batch, blob, 1, inpSize, Scalar(), true);
     // Run a model.
     net.setInput(blob);                                
    std::vector<cv::Mat> detection;
    net.forward(detection);
    

    `
    This code again fails with same error
    what(): OpenCV(4.0.1-dev) /home/harmanx/opencv/modules/dnn/src/op_inf_engine.cpp:686: error: (-215:Assertion failed) Failed to initialize Inference Engine backend: Input blob size is not equal network input size (810000!=270000). in function 'initPlugin'

    This error is from op_inf_engine.cpp file.

  7. dkurt commented on Apr 29, 2019

    @dkurt
    Member

    @shelkesagar29, It looks like the problem in the generated IR:

    terminate called after throwing an instance of 'InferenceEngine::details::InferenceEngineException'
      what():  Failed to infer shapes for Reshape layer (BoxPredictor_5/ClassPredictor/BiasAdd/Reshape) with error: Invalid reshape mask (dim attribute): number of elements in input: [3,1,1,546] and output: [1,6,91] mismatch
    

    So there are two options:

    1. Try to use an origin model. OpenCV can build Inference Engine graph in runtime for it.
    2. Modify IR:

    For an every ""BoxPredictor_*/ClassPredictor/BiasAdd/Reshape"" replace target shape considering batch dimension. In example,

    from

    <layer id="94" name="BoxPredictor_0/ClassPredictor/BiasAdd/Reshape" precision="FP32" type="Reshape">
      <data dim="1,1083,91"/>

    to

    <layer id="94" name="BoxPredictor_0/ClassPredictor/BiasAdd/Reshape" precision="FP32" type="Reshape">
      <data dim="-1,1083,91"/>

    There are 6 layers to be changed,

    BoxPredictor_0/ClassPredictor/BiasAdd/Reshape
    BoxPredictor_1/ClassPredictor/BiasAdd/Reshape
    BoxPredictor_2/ClassPredictor/BiasAdd/Reshape
    BoxPredictor_3/ClassPredictor/BiasAdd/Reshape
    BoxPredictor_4/ClassPredictor/BiasAdd/Reshape
    BoxPredictor_5/ClassPredictor/BiasAdd/Reshape

  8. shelkesagar29 commented on Apr 29, 2019

    @shelkesagar29
    Author

    @dkurt That was the problem with IR generated with model optimizer.
    Now there are two options,

    1. Pass original .pb and .pbtxt file and let OpenCV build IR
    2. Instead of modifying IR afterwards, we can also do one of the following
      a. Pass batch size parameter (--batch N ) while generating IR using model optimizer.
      b. OR pass input image size considering batch size i.e for SSD mobilenet, instated of passing [1, 300, 300, 3], we can pass [N, 300, 300, 3] to accommodate batch size N

    I have tested second approach and it works fine. OpenVINO team should mention this in the documentation.

    Thank yo for your time and efforts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions