Skip to content

G-API: ONNX. Disable unnecessary outputs#18903

Merged
alalek merged 1 commit intoopencv:masterfrom
mpashchenkov:mp/onnx-disable-output
Mar 31, 2021
Merged

G-API: ONNX. Disable unnecessary outputs#18903
alalek merged 1 commit intoopencv:masterfrom
mpashchenkov:mp/onnx-disable-output

Conversation

@mpashchenkov
Copy link
Copy Markdown
Contributor

@mpashchenkov mpashchenkov commented Nov 23, 2020

Disable unnecessary outputs

How it works:

Unnecessary network outputs can be omitted. For example: FRCNN has output with INT_64 data type, which unsupported for OCV. Name of this output is "6381". We can "disable" this output. It works for dynamic output case only when we should remap onnx outputs to g-api out.
You can set layer names which you will use in remapping. You should call overload of cfgPostProc function for this.

.cfgOutputLayers({"gapi_out1", "gapi_out2"})
.cfgPostProc({cv::GMatDesc{CV_32F, {1,20}}, cv::GMatDesc{CV_32F, {5}}},
             remapRcnnPorts,
             {"6383", "6379"}
);

There are two layers with names "6383" and "6379" will be used for remapping. Data will be remapped from "6383", "6379" to "gapi_out1", "gapi_out2" with remapRcnnPorts function logic (Unfortunately we can't set out_meta for network with dynamic outputs, but we can set own outputs and remap data to them from onnx result).
Disabling is independent of cfgOutputLayers function in this case.

For CNN with static outputs use another logic. We know out_meta and don't need remapping in tis case.

Tests:

Test contains infer for FRCNN. FRCNN output with name "6381" has unsupported type.

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or other license that is incompatible with OpenCV
  • The PR is proposed to proper branch
  • There is reference to original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

Custom build:

force_builders=Custom
buildworker:Custom=linux-1
build_image:Custom=ubuntu-onnx:20.04
test_modules:Custom=gapi
test_filter:Custom=*ONNX*

Copy link
Copy Markdown
Contributor

@OrestChura OrestChura left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well done!

@OrestChura
Copy link
Copy Markdown
Contributor

OrestChura commented Dec 1, 2020

Would you also change the PR's description, please?

@asmorkalov
Copy link
Copy Markdown
Contributor

@OrestChura @mpashchenkov Friendly reminder.

Copy link
Copy Markdown
Contributor

@OrestChura OrestChura left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!


TEST_F(ONNXWithRemap, InferWithDisabledOut)
{
useModel("/object_detection_segmentation/faster-rcnn/model/FasterRCNN-10");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove leading /

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.


std::vector<bool> normalize;

std::vector<std::string> d_out_names;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It make sense to add comment with decoding of field name

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comment.

Comment on lines +562 to +563
ti.is_disabled = (std::find(params.d_out_names.begin(),
params.d_out_names.end(), ti.name) != params.d_out_names.end()) && is_postproc;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix indentation

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@dmatveev
Copy link
Copy Markdown
Contributor

.cfgDisableOutput( { with_layer's_name_that_should_be_disabled, ..., ... } )

Can't we achieve the same by calling cfgOutputLayers and NOT mentioning that particular output layer there?

@mpashchenkov
Copy link
Copy Markdown
Contributor Author

mpashchenkov commented Dec 15, 2020

@dmatveev, unfortunately no. We use remap functions; ssd-mobilenet, for example, returns four outputs, but we call cfgOutputLayers("output_name") with one name (sample and test). Now, if user needs only 3 out of 4 outputs in remap, he can disable the unnecessary. And it works only when we use remap functions (with dynamic outputs).

@mpashchenkov mpashchenkov force-pushed the mp/onnx-disable-output branch from bf4dba2 to 54c88f7 Compare February 11, 2021 09:22
@TolyaTalamanov
Copy link
Copy Markdown
Contributor

Let's discuss it locally

@mpashchenkov mpashchenkov force-pushed the mp/onnx-disable-output branch from e680faa to 4ae74e5 Compare March 5, 2021 10:00
@mpashchenkov mpashchenkov force-pushed the mp/onnx-disable-output branch from 6407c02 to f88320d Compare March 5, 2021 14:33
if (original_data != received_data) {
cv::util::throw_error
(std::logic_error
("OpenCV kernel output parameter was reallocated. \n"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to put information that it happened in ONNX backend ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed message. Added mention about ONNX.

gapi_out.begin<T>());
if (gapi_out.total() > onnx_out.total()) {
T* gptr = gapi_out.ptr<T>();
gptr[size] = static_cast<T>(end_mark);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason of using static_cast ? end_mark and gptr has the same type

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No sense. Fixed.

@mpashchenkov
Copy link
Copy Markdown
Contributor Author

@alalek, can you merge this PR?

@mpashchenkov mpashchenkov added this to the 4.5.2 milestone Mar 29, 2021
Comment on lines +127 to +150
Params<Net>& cfgPostProc(std::vector<cv::GMatDesc> &&out_metas,
PostProc &&pp) {
desc.out_metas = std::move(out_metas);
desc.custom_post_proc = std::move(pp);
return *this;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe doxygen comments are really missing here since the semantics of these methods is not very trivial

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

@mpashchenkov mpashchenkov force-pushed the mp/onnx-disable-output branch from 5214175 to a608ec7 Compare March 30, 2021 15:54
@mpashchenkov mpashchenkov force-pushed the mp/onnx-disable-output branch from a608ec7 to 61a4100 Compare March 31, 2021 09:21
Copy link
Copy Markdown
Contributor

@dmatveev dmatveev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 follow-up is required on the documentation update, but it shouldn't block the merge.

Comment on lines +120 to +134
/** @brief Configures graph output and sets the post processing function from user.

The function is used for the case of infer of networks with dynamic outputs.
Since these networks haven't known output parameters needs provide them for
construction of output of graph.
The function provides meta information of outputs and post processing function.
Post processing function is used for copy information from ONNX infer's result
to output of graph which is allocated by out meta information.

@param out_metas out meta information.
@param pp post processing function, which has two parameters. First is onnx
result, second is graph output. Both parameters is std::map that contain pair of
layer's name and cv::Mat.
@return reference to object of class Params.
*/
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please merge this now open a follow-up task to fix this text.

@alalek alalek merged commit e11408f into opencv:master Mar 31, 2021
@alalek alalek mentioned this pull request Apr 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants