Skip to content

G-API: ONNX. Const input#18902

Merged
alalek merged 2 commits intoopencv:masterfrom
mpashchenkov:mp/onnx-const-input
Jan 12, 2021
Merged

G-API: ONNX. Const input#18902
alalek merged 2 commits intoopencv:masterfrom
mpashchenkov:mp/onnx-const-input

Conversation

@mpashchenkov
Copy link
Copy Markdown
Contributor

@mpashchenkov mpashchenkov commented Nov 23, 2020

Added support of const input for onnx backend.

How it works:

ORT API infer is Run method:

auto outputs = this_session.Run(Ort::RunOptions{nullptr},
                                in_run_names.data(),
                                &in_tensors.front(),
                                input_names.size(),
                                out_names.data(),
                                out_names.size());

We should call Run() with order of input layers' names. We can put names in different order. If we want to use const input than we call constInput(name, cv::Mat):

auto net = cv::gapi::onnx::Params<YoloNet>{model_path}
.constInput("image_shape", ins[1])

After that name will be added in order of input names and input tensor for Mat's data is created:

input_names.emplace_back(c_in_pair.first);
in_tensors.emplace_back(createTensor(this_memory_info,
                                     in_tensor_info[idx],
                                     c_in_pair.second.first));

And we call Run with new input names and appropriate tensors, where names: {in_layer_name1, const_input_name_for_layer2} and tensors: {tensor_for_layer1, tensor_for_layer2_with_data_from_C_I).

Tests:

Test contains infer for YoloV3. YoloV3's input is resized image (416x416) and tensor with real size.

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:

build_gapi_standalone:Linux x64=ade-0.1.1f
build_gapi_standalone:Win64=ade-0.1.1f
build_gapi_standalone:Mac=ade-0.1.1f
build_gapi_standalone:Linux x64 Debug=ade-0.1.1f
build_gapi_standalone:Custom=ade-0.1.1f

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


build_image:Custom Win=openvino-2020.3.0
build_image:Custom Mac=openvino-2020.3.0
test_modules:Custom Win=gapi
test_modules:Custom Mac=gapi

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.

I have some comments, but overall seems solid

|| ade::util::any_of(out_tensor_info, is_dyn);
if (is_dynamic && !params.custom_post_proc) {
if (is_dynamic
&& !params.custom_post_proc) {
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.

so, why this change? Is it necessary?

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.

The idea of the author.

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.

Mb for the debug, to produce errors on different lines (?)

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.

you could leave formatting for this if as-is.

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.

The main question is -- why altering the IE version?

# For now, check the old name ORT_INSTALL_DIR
if(ORT_INSTALL_DIR AND NOT ONNXRT_ROOT_DIR)
set(ONNXRT_ROOT_DIR ORT_INSTALL_DIR)
set(ONNXRT_ROOT_DIR ${ORT_INSTALL_DIR})
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.

Isn't this merged already?

Copy link
Copy Markdown
Contributor Author

@mpashchenkov mpashchenkov Dec 14, 2020

Choose a reason for hiding this comment

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

I caught it in this branch. It is merged.

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.

But then there should be no such change

|| ade::util::any_of(out_tensor_info, is_dyn);
if (is_dynamic && !params.custom_post_proc) {
if (is_dynamic
&& !params.custom_post_proc) {
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.

you could leave formatting for this if as-is.

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.

👍 let's merge this!

@dmatveev dmatveev self-assigned this Dec 14, 2020
@dmatveev dmatveev added this to the 4.5.1. milestone Dec 14, 2020
@dmatveev
Copy link
Copy Markdown
Contributor

@mpashchenkov note you don't have IE builds in your MR script -- I'd add those to check if IE is still valid.

You can combine it with a rebase to turn 16 commits into one.

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

@alalek
Copy link
Copy Markdown
Member

alalek commented Dec 14, 2020

Please squash commits and rebase as 1 commit.
GitHub diff should be clean from garbage and "non-existed" changes.

if (params.num_out == 1u && params.output_names.empty()) {
params.output_names = { out_tensor_info.front().name };
}

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.

unnecessary change

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.

};

namespace {
std::string pdims(const std::vector<int64_t> &dims) {
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.

namespace {

Again, use "static" instead.

Subscribe and check all ongoing notifications/discussions about G-API PRs / Issues.
It is really annoying to repeat the same things.

P.S. "Why" answer you may find in corresponding discussion.

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.

Wait, that's the backend's code, and we use anonymous namespaces as a more "modern" replacement to static there a lot.

I remember there were problems in tests but what's wrong with the module code?

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 is not related to anonymous types in templates.
It is mostly to have better names in debugger.

Anonymous namespace is still necessary for internal types (but avoid using them in tests due to templates issues)

if (!initialized)
{
// Since G-API has no own test data (yet), it is taken from the common space
const char* testDataPath = getenv("OPENCV_TEST_DATA_PATH");
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.

OPENCV_TEST_DATA_PATH

Again, it points to opencv_extra/testdata only.

It can't be used for extra locations with:

  • DNN's large models (OPENCV_DNN_TEST_DATA_PATH)
  • ONNX models from LFS ( ??? , on CI OPENCV_DNN_TEST_DATA_PATH is reused as fallback)

They are dedicated locations, so they should have own variables with fallbacks.


This function has zero effect (OPENCV_TEST_DATA_PATH is added by ts module).


I believe it is time to grab all these initTestDataPath() calls and put them into test_main.cpp. See example here.

/cc @dmatveev

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.

Good point, thanks! I believe this code is a copy-paste from some other module where we were stealing DL models from DNN or so.

If the existing tests' lookups for test data (not models) continue working without this, it makes sense to remove this block.

Also, a good point on unifying overall initTestDataPath once and for everybody. @mpashchenkov can you please open a ticket on that? With the mentioned example in. Thanks!

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.

Ticket created.

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.

@alalek
Why does the function have zero effect?
What is the correct way to call findDataFile function?
It doesn't work without initTestDataPath.
It is same in ts module, but how to use it correctly?
My way:

  • export OPENCV_TEST_DATA_PATH = /path_to/opencv_extra/testdata/
  • call initTestDataPath (useless step, as I understand it)
  • call findDataFile

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.

@alalek, can you give advice about this?

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.

There is no "gapi" test data in opencv_extra, so no need to pass its prefix:

-CV_TEST_MAIN("gapi")
+CV_TEST_MAIN("")


TEST_F(ONNXYoloV3MultInput, InferConstInput)
{
useModel("/object_detection_segmentation/yolov3/model/yolov3-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.

again, 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.

}
};

class ONNXYoloV3MultInput : public ONNXWithRemap {
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.

ONNXYoloV3MultInput

ONNXYoloV3MultiInput ?

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.

+1

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.

useModel("/object_detection_segmentation/yolov3/model/yolov3-10");
// Tensor with incorrect image size
// is used for check case when InputLayers and constInput have same names
cv::Mat bad_shape;
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.

Is it a bad arg test? Which check should fail?

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.

This test checks use of data from const input when input names are incorrectly set. If input names contain names from const input, then calculation will be performed according to data given by .ConstInput() function.

Comment on lines +347 to +412
const auto out_size = std::min(out_onnx.at(i).total(), out_gapi.at(i).total());
for (size_t d_idx = 0; d_idx < out_size; ++d_idx) {
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.

These checks are not reliable.
If out_gapi.at(i).total() is zero then no checks are performed at all.

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 assert for zero size check.

}

// FIXME: taken from the DNN module
void normAssert(const cv::InputArray& ref, const cv::InputArray& test,
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.

const cv::InputArray&

InputArray is already a const reference.

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.

Comment on lines +103 to +105
if (optr[size] <= 0) {
std::cout << optr[size] << std::endl;
}
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.

Message is unclear, so it can't help with investigation of test failures.

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.

Removed.

virtual void SetUp() {
const int yolo_in_h = 416;
const int yolo_in_w = 416;
cv::Mat yolov3_input, shape, prep_mat;;
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.

;;

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.

@alalek
Copy link
Copy Markdown
Member

alalek commented Dec 14, 2020

added this to the 4.5.1. milestone 9 hours ago

Please do not create NEW milestones!

@dmatveev
Copy link
Copy Markdown
Contributor

Please do not create NEW milestones!

Sorry, that was a typo. It was supposed to be 4.5.1

in_names_without_const.clear();
std::copy_if(params.input_names.begin(), params.input_names.end(),
std::back_inserter(in_names_without_const),
[&](std::string& name) {
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 it can be const std::string&

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.

in_tensor_info[idx],
c_in_pair.second.first));
// Puts const input names in sequence for Run
// Ort can match input tensors to CNN inputs by names
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.

Ort ?

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.

Renamed to ONNXRuntime.

const T* const inptr = in.ptr<T>();
T* const optr = out.ptr<T>();
const size_t size = std::min(out.total(), in.total());
for (size_t i = 0; i < size; ++i) {
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.

std::copy ?

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.

Yes, thanks.

}
}

void remapSsdPorts(const std::unordered_map<std::string, cv::Mat> &onnx,
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.

remapSSDPorts

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

@alalek anything else remaining here w.r.t tests?

@alalek alalek merged commit 3eaeca5 into opencv:master Jan 12, 2021
@alalek alalek mentioned this pull request Apr 9, 2021
a-sajjad72 pushed a commit to a-sajjad72/opencv that referenced this pull request Mar 30, 2023
G-API: ONNX. Const input

* Added const input for ONNX backend

* Returned initMatrixRandu, added some comments, rebase
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.

5 participants