You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Batch inference not working when InferenecEngine as backend #14404
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?
@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.
@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.
#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'
@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:
Try to use an origin model. OpenCV can build Inference Engine graph in runtime for it.
Modify IR:
For an every ""BoxPredictor_*/ClassPredictor/BiasAdd/Reshape"" replace target shape considering batch dimension. In example,
@dkurt That was the problem with IR generated with model optimizer.
Now there are two options,
Pass original .pb and .pbtxt file and let OpenCV build IR
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.
Activity
dkurt commentedon Apr 24, 2019
Please provide more details. Do you use a model in native format or IR with .XML and .bin files?
dkurt commentedon Apr 24, 2019
@shelkesagar29, Thank you for the report! Please test the changes from PR #14407.
shelkesagar29 commentedon Apr 24, 2019
@dkurt I was using IR format with .xml and .bin files.
Somehow, with normal OpenCV backend, batch inference works.
dkurt commentedon Apr 24, 2019
@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.
shelkesagar29 commentedon Apr 24, 2019
@dkurt I will test and let you know. Thanks for the quick response.
shelkesagar29 commentedon Apr 24, 2019
@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
`
`
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.
dkurt commentedon Apr 29, 2019
@shelkesagar29, It looks like the problem in the generated IR:
So there are two options:
For an every ""BoxPredictor_*/ClassPredictor/BiasAdd/Reshape"" replace target shape considering batch dimension. In example,
from
to
There are 6 layers to be changed,
BoxPredictor_0/ClassPredictor/BiasAdd/ReshapeBoxPredictor_1/ClassPredictor/BiasAdd/ReshapeBoxPredictor_2/ClassPredictor/BiasAdd/ReshapeBoxPredictor_3/ClassPredictor/BiasAdd/ReshapeBoxPredictor_4/ClassPredictor/BiasAdd/ReshapeBoxPredictor_5/ClassPredictor/BiasAdd/Reshapeshelkesagar29 commentedon Apr 29, 2019
@dkurt That was the problem with IR generated with model optimizer.
Now there are two options,
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.