Skip to content

[G-API]: Adding reshape for CNN input.#18240

Merged
alalek merged 4 commits intoopencv:masterfrom
mpashchenkov:mp/ocv-gapi-input-cnn-reshape
Mar 10, 2021
Merged

[G-API]: Adding reshape for CNN input.#18240
alalek merged 4 commits intoopencv:masterfrom
mpashchenkov:mp/ocv-gapi-input-cnn-reshape

Conversation

@mpashchenkov
Copy link
Copy Markdown
Contributor

@mpashchenkov mpashchenkov commented Sep 1, 2020

Adding reshape for CNN input

Reshape changes shape for CNN input dims.

To use input reshape, you should call function cfgInputReshape for Params:

  1. .cfgInputReshape( { {layer1_name, shape1}, {layer3_name, shape3} } ) - for some layers;
  2. .cfgInputReshape(layer1_name, shape1) - for single layer;
  3. .cfgInputReshape( { layer1_name, layer3_name } ) - for reshaping by input image.

layer..._name - name of input layer, which should to be reshaped.
shape... - vector of dimensions, which reshape will use (1,3,300,300 for example).

Behavior:

  • input_reshape_table from IEUnit contains pairs layer's name --- dimensions for reshape;
  • Unfortunately, now current CNN input layers dimensions are not checked, but if provided incorrect dimensions in 1st and 2nd cases of cfgInputReshape then IE throw error message;
  • In 3rd case of cfgInputReshape are constructing element of input_reshape_table by current CNN dimensions and height and width input image;
  • If user call 1st, 2nd and 3rd case of .cfgInputReshape at the same time then reshape by dimensions is preferred then by input image;
  • Call of InferenceEngine::CNNNetwork::reshape function is located in OutMeta() of Infer, InferROI, InferList and InferList2.

Tests:
New fixture InferWithReshape:

  • TestInfer - simple infer by shapes;
  • TestInferInImage - infer by input image size;
  • TestInferBehavior - launch with 1st and 3rd case of .cfgInputReshape at the same time;
  • TestInferForSingleLayer - launch with 2nd case of .cfgInputReshape;
  • TestInferList - infer by ROIs with CNN input reshape;
  • TestInferList2;
  • TestInferListBGR - infer GFrame (BGR).
    New fixture InferWithReshapeNV12:
  • TestInferListYUV - infer GFrame (YUV)..

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

Magic commands

force_builders=Custom,Custom Win,Custom Mac
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_image:Custom=centos:7
buildworker:Custom=linux-1
build_gapi_standalone:Custom=ade-0.1.1f

xbuild_image:Custom=ubuntu-openvino-2021.1.0:20.04
xbuild_image:Custom Win=openvino-2021.1.0
xbuild_image:Custom Mac=openvino-2021.1.0

test_modules:Custom=gapi
test_modules:Custom Win=gapi
test_modules:Custom Mac=gapi

# disabled due high memory usage: test_opencl:Custom=ON
test_opencl:Custom=OFF
test_bigdata:Custom=1
test_filter:Custom=*

@mpashchenkov
Copy link
Copy Markdown
Contributor Author

@OrestChura, @aDanPin, please review.

@mpashchenkov mpashchenkov force-pushed the mp/ocv-gapi-input-cnn-reshape branch from 20d59f4 to d1887e4 Compare November 12, 2020 12:51
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.

Overall looks consistent

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.

Great! Thanks!
But please check if PR causes IE tests failure

@mpashchenkov
Copy link
Copy Markdown
Contributor Author

@dmatveev

@asmorkalov
Copy link
Copy Markdown
Contributor

@dmatveev @alalek The patch is ready, let's merge it.

@dmatveev
Copy link
Copy Markdown
Contributor

dmatveev commented Dec 1, 2020

cfgInputReshape( { {layer1_name, shape1}, {layer3_name, shape3} } )

So what is shape here? In case of IE, isn't reshape just a boolean flag and the input blob is reshaped to the input image dimensions? Why specify a shape here at all?

@dmatveev
Copy link
Copy Markdown
Contributor

dmatveev commented Dec 1, 2020

..also, if this function is not called for all N inputs, then I'd limit it to configuring a single input only.

Params<Net>& cfgInputReshape(const std::map<std::string, std::vector<std::size_t>> &r) {
desc.reshape_inputs = r;
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.

Really I'd add an overload with const std::string, const std::vector<size_t>.

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.

@mpashchenkov what about 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.

Consider to add rvalue implementation, because it will be used in most of cases

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 overloads.

// cv::Mat-wrapped blob there to support IE's optimal "GetBlob idiom"
// Still, constant data is to set only once.
this_request.SetBlob(p.first, wrapIE(p.second.first, p.second.second));
}
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.

Where did it go?

Copy link
Copy Markdown
Contributor Author

@mpashchenkov mpashchenkov Jan 20, 2021

Choose a reason for hiding this comment

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

It was a duplicate code. Already fixed.

// Reshape for configured map with layers' names and their new shapes
if (!params.reshape_inputs.empty()) {
GAPI_Assert(params.reshape_inputs.size() <= params.num_in);
net.reshape(params.reshape_inputs);
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 how it works for your purpose? Why we're not looking at the input data dimensions? Isn't this the case?

@dmatveev
Copy link
Copy Markdown
Contributor

dmatveev commented Dec 1, 2020

Another question - how does this reshape thing co-exists with the automatic preprocessing we set?

@asmorkalov
Copy link
Copy Markdown
Contributor

@mpashchenkov Please take a look on new comments and merge conflict.

@mpashchenkov mpashchenkov force-pushed the mp/ocv-gapi-input-cnn-reshape branch from 991afe9 to 1189a29 Compare January 12, 2021 09:26
Params<Net>& cfgInputReshape(const std::map<std::string, std::vector<std::size_t>> &r) {
desc.reshape_inputs = r;
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.

@mpashchenkov what about this?

size_t to_hw_shift = 0;
switch (layout) {
case InferenceEngine::NHWC:
{
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 don't need {/} as you don't declare local variables here

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.

const auto input_name = ii->name();
const auto idx = getIdxByName(uu->params.reshape_in_names, input_name);
uu->params.reshape_in_shapes[uu->params.reshape_in_names.at(idx)] = input_dims;
}
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.

Can you please explain what happens here in this function?

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 comments about steps.

auto &&ii = uu.inputs.at(uu.params.input_names.at(0));
auto &&mm = in_metas.at(1u);
configureInputInfo(ii, mm);
configureInputInfo(ii, const_cast<IEUnit*>(&uu), mm);
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.

Generally, const_cast is not good. It breaks the above contracts where we declare our inputs as const.

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 FIXME.

Comment on lines +526 to +527
if (!uu->params.reshape_in_names.empty()) {
configureReshape(ii, uu, {meta.size.height, meta.size.width});
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.

Not sure if this logic is all correct.

Hypothetically, one kind of reshape shouldn't exclude the other one - and so for an N-input network the user could specify particular shapes for layers X and Y and keep it dynamic (based on input) for Z.

Also, is {W,H} the right shape? Does it work well with SetResizeAlgorithm() / SetBlob thing we also use?

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.

New behavior:
You can call reshape by shapes parameter for some layers and call reshape by image size for another layers.
If both calls contain same layers names, then reshaping by shapes is preferred.

@dmatveev
Copy link
Copy Markdown
Contributor

@TolyaTalamanov please have a look on this.

Params<Net>& cfgInputReshape(const std::map<std::string, std::vector<std::size_t>> &r) {
desc.reshape_inputs = r;
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.

Consider to add rvalue implementation, because it will be used in most of cases

@mpashchenkov mpashchenkov changed the title [G-API]: Add reshape for CNN input. [G-API]: Adding reshape for CNN input. Jan 20, 2021
@mpashchenkov mpashchenkov requested a review from dmatveev January 22, 2021 15:20
@mpashchenkov
Copy link
Copy Markdown
Contributor Author

Updated magic commands (openvino-2021.1.0 version). Old version doesn't support some blob functionality. @alalek, please restart build for this PR, it should work correctly.

@alalek
Copy link
Copy Markdown
Member

alalek commented Jan 25, 2021

Old version doesn't support some blob functionality

You should add #if guards with version check to handle that:

  • do not fail compilation. Use NonImplemented exceptions in #else branch.
  • do not fail tests. Skip them.

P.S. OV 2020.3 LTS should be supported too (with partially disabled functionality)

/cc @opencv/intel-gapi

@mpashchenkov mpashchenkov force-pushed the mp/ocv-gapi-input-cnn-reshape branch from a705a8a to 7bb1fe3 Compare February 26, 2021 13:13
@mpashchenkov mpashchenkov force-pushed the mp/ocv-gapi-input-cnn-reshape branch from 7bb1fe3 to 1e65dae Compare March 1, 2021 08:54
@TolyaTalamanov
Copy link
Copy Markdown
Contributor

Good job, thanks! 👍

@mpashchenkov
Copy link
Copy Markdown
Contributor Author

@alalek, CN Jenkins can't reach https://github.com/opencv/opencv.git, but build bot's build is correct. Do need to restart CN Jenkins or can merge it by build bot's result?

@alalek
Copy link
Copy Markdown
Member

alalek commented Mar 3, 2021

Ignore, these failures are not related to any patch.

@mpashchenkov
Copy link
Copy Markdown
Contributor Author

@alalek, can you merge this PR?

@alalek alalek merged commit 12fa8d8 into opencv:master Mar 10, 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
…nn-reshape

[G-API]: Adding reshape for CNN input.

* Added CNN input IE reshape

* rbs

* Added unordered_set instead vector

* Alignment
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