Skip to content

Add asynchronous mode for dnn's Net with Intel's Inference Engine backend #13667

Closed

Description

@dkurt
Detailed description

Intel's Inference Engine supports asynchronous forward passes using multiple inference requests. Let users enable async. networks inference.

Initial proposal

  • Requires adding a new method Net::enableAsync(bool) to specify if we want to execute network asynchronously.
  • Net::setInput is used to set input data in the internal inputs queue and Net::forward to retrieve outputs in the same order.
  • Net::forward returns an empty Mat in case of output is not ready => User need to check that output is not empty.

Considered scenarios of Net in async mode:

Standard loop
net.enableAsync(1);

net.setInput(inp1);
out1 = net.forward();

net.setInput(inp2);
out = net.forward();  // out is output for inp1 if out1 is empty. Otherwise it's output for inp2
...

This way user should store inputs and pop them by ready outputs (i.e. render detections).

Prefetch
net.enableAsync(1);

net.setInput(inp1);
net.setInput(inp2);
...

out1 = net.forward();
out = net.forward();  // out is output for inp1 if out1 is empty. Otherwise it's output for inp2
Syncing
net.enableAsync(1);

net.setInput(inp1);

out = net.forward();  // assume out is empty here (not ready)
out = net.forward();  // assume out is not ready again

net.enableAsync(0);
out = net.forward();  // wait here until output is ready

Syncing let users control maximum number of asynchronous requests. In example, if number of fetched inputs equals to specified maximum number of requests, we disable async mode and wait for the output of the first input in queue. Note that it doen't disable asynchronous computations at that time but specifies that next Net::forward invocation should return output.

Steps to reproduce

Activity

  1. added
    effort: few weeks
    on Jan 21, 2019
  2. added this to the 4.x milestone on Jan 21, 2019
  3. self-assigned this
    on Jan 21, 2019
  4. AyushP123 commented on Jan 22, 2019

    @AyushP123
    Contributor

    Hi @dkurt, have you started working on this or should I have a go at it.

  5. dkurt commented on Jan 22, 2019

    @dkurt
    MemberAuthor

    @AyushP123, In my opinion it's quite complicated task. If you have no experience working with OpenCV's deep learning module it's better to start with simpler issues. In example:

    #13479
    #13462
    #13626

  6. AyushP123 commented on Jan 22, 2019

    @AyushP123
    Contributor

    Thanks a lot for your response @dkurt, will start those issues. Have been using OpenCV's dnn for a year, but didnt play a lot with the codebase.

  7. dkurt commented on Jan 22, 2019

    @dkurt
    MemberAuthor

    @AyushP123, wow, that's cool. Thank you!

    We can collaborate doing this feature at some point. In example, I can prepare all the internal changes and then ask you to modify our samples to enable asynchronous mode. What do you think about it?

  8. AyushP123 commented on Jan 22, 2019

    @AyushP123
    Contributor

    Sounds awesome!!

  9. AyushP123 commented on Jan 22, 2019

    @AyushP123
    Contributor

    Wanted to understand a bit ( sorry if I sound dumb ):
    Q1. Will Net::forward() launch a thread which will keep reading inputs from the input queue?? and run the network in parallel for each input??
    Q2. if the network is a fully convolution network then there is a possibility that inp2 might finish before inp1, how do we ensure that in standard loop out is the output for inp1??.

    Is there any resource that I should read for understanding this a bit more??.

  10. dkurt commented on Jan 22, 2019

    @dkurt
    MemberAuthor

    @AyushP123, In my current vision,

    Q1. Will Net::forward() launch a thread which will keep reading inputs from the input queue?? and run the network in parallel for each input??

    Yes. That means that forward initiates asynchronous inference. We can continue capturing new frames from camera and call setInput for example. This way user should collect frames somehow and render detections on them by readiness.

    Q2. if the network is a fully convolution network then there is a possibility that inp2 might finish before inp1, how do we ensure that in standard loop out is the output for inp1??.

    You're right. inp2 can be processed faster. However Net::forward should return an empty Mat if inp1 is still processing or out1. After that we can return out2 (output for inp2) which will be ready at that moment.

  11. dkurt commented on Jan 28, 2019

    @dkurt
    MemberAuthor

    Proposal number 2

    Instead of introducing new behavior of Net::forward, add new methods with Async suffix for existing methods:

    Mat forward(const String& outputName = String());
    void forward(OutputArrayOfArrays outputBlobs, const String& outputName = String());
    void forward(OutputArrayOfArrays outputBlobs, const std::vector<String>& outBlobNames);
    CV_WRAP_AS(forwardAndRetrieve) void forward(CV_OUT std::vector<std::vector<Mat> >& outputBlobs, const std::vector<String>& outBlobNames);
    Mat forwardAsync(const String& outputName = String());
    void forwardAsync(OutputArrayOfArrays outputBlobs, const String& outputName = String());
    void forwardAsync(OutputArrayOfArrays outputBlobs, const std::vector<String>& outBlobNames);
    CV_WRAP_AS(forwardAndRetrieveAsync) void forwardAsync(CV_OUT std::vector<std::vector<Mat> >& outputBlobs, const std::vector<String>& outBlobNames);

    No enableAsync(1) is required - forwardAsync initiates asynchronous request and returns an empty Mat if it's not ready.

    To finish existing requests synchronously, do not turn enableAsync(0) but just call a regular forward.

  12. dkurt commented on Apr 22, 2019

    @dkurt
    MemberAuthor

    @AyushP123, Hi!

    Sorry for the long time waiting. We were making this feature as much stable and easy to use as it possible and I think the last version is good enough.

    So as we discussed earlier I would like to ask you to propose a PR with modified sample which can demonstrate a new feature. I think that it's better to modify both C++ and Python versions of object detection sample. We have the following expectations from it:

    • Add a new argument which can control number of maximum asynchronous requests at the same time. That means if we reached a limit of std::future queue, we'll wait for the end of the oldest one. This argument will have especial values: 1 for synchronous mode and -1 for unlimited number of requests.

    • The sample should demonstrate the profit of asynchronous mode. That means you need to introduce appropriate way to measure number of processed frames per second which shows the real difference between synchronous mode and asynchronous with different limits.

    You may check the tests for C++ and Python that added in the PR #13694 for reference.

    If you have no time for it please let us know so we can open a separate issue for this task. Thanks you!

  13. AyushP123 commented on May 15, 2019

    @AyushP123
    Contributor

    Hi @dkurt, sorry for the late response, I got the notification for the closure of this PR but not for the comment weirdly. I would like to contribute towards writing a sample to demonstrate the feature. I don't think anyone has started working on it. I am a bit busy with work these days, but I will definitely open a PR by this weekend if you are okay with it.

  14. AyushP123 commented on May 15, 2019

    @AyushP123
    Contributor

    Ahh, I am late, saw the PRs #14538, #14516 merged. I will try to contribute more actively for other issues.

  15. dkurt commented on May 16, 2019

    @dkurt
    MemberAuthor

    @AyushP123, sure, thank you!

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

Metadata

Metadata

Assignees

Labels

category: dnneffort: few weeksfeature

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions