Skip to content

Added Steps support in DNN Slice layer#19546

Merged
alalek merged 3 commits intoopencv:3.4from
LupusSanctus:am/slice_steps
Mar 26, 2021
Merged

Added Steps support in DNN Slice layer#19546
alalek merged 3 commits intoopencv:3.4from
LupusSanctus:am/slice_steps

Conversation

@LupusSanctus
Copy link
Copy Markdown

@LupusSanctus LupusSanctus commented Feb 16, 2021

Merge with extra: opencv/opencv_extra#854

Fixes #18920: added steps parameter support for Slice layer

  • 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
force_builders=Custom,Custom Win,Custom Mac
build_image:Custom=ubuntu-openvino-2021.2.0:20.04
build_image:Custom Win=openvino-2021.1.0
build_image:Custom Mac=openvino-2021.2.0

test_modules:Custom=dnn,python2,python3,java
test_modules:Custom Win=dnn,python2,python3,java
test_modules:Custom Mac=dnn,python2,python3,java

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

@alalek alalek assigned sl-sergei and unassigned sl-sergei Feb 20, 2021
@LupusSanctus LupusSanctus force-pushed the am/slice_steps branch 2 times, most recently from 96eaa99 to af88ecf Compare February 28, 2021 21:51
@LupusSanctus
Copy link
Copy Markdown
Author

@alalek, the code was corrected in accordance with comments. Is it needed to rebase this patch on 3.4 branch?

@alalek
Copy link
Copy Markdown
Member

alalek commented Mar 3, 2021

Is it needed to rebase this patch on 3.4 branch

We will backport it separately later (there is some CUDA-specific code).

@asmorkalov
Copy link
Copy Markdown
Contributor

/cc @asenyaev

@LupusSanctus LupusSanctus changed the base branch from master to 3.4 March 9, 2021 20:41
@LupusSanctus
Copy link
Copy Markdown
Author

@alalek, rebased on 3.4 branch.

Copy link
Copy Markdown
Member

@alalek alalek left a comment

Choose a reason for hiding this comment

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

Thank you for update!

outputs[i][j] = normalize_axis_range(sliceRanges[i][j], inpShape[j]).size();

if (! sliceSteps.empty() && sliceSteps[i][j] > 1)
outputs[i][j] = (outputs[i][j] + sliceSteps[i][j] - 1) / sliceSteps[i][j];
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.

Please add out of range checks for sliceSteps[i][j] access.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@alalek, checks were added.

Comment on lines +627 to +630
if (dim + 1 < dimsNum)
getSliceRecursive(inpMat, inpIdx, sliceRanges, sliceSteps, dim + 1, dimsNum, outputs, outIdx);
else
outputs.at<float>(outIdx.data()) = inpMat.at<float>(inpIdx.data());
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.

IMHO, provided implementation is really slow.

I believe we should have optimized case for 2D (the last 2 dims) processing at least.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@alalek, yes, but if it's ok with you, let's separate this optimization activity and implement it not in this scope.

Comment on lines 651 to 656
DictValue steps_dict = layerParams.get("steps");
for (int i = 0; i < steps_dict.size(); ++i)
{
if (steps.get<int>(i) != 1)
if (steps_dict.get<int>(i) != 1) {
CV_Error(Error::StsNotImplemented,
"Slice layer only supports steps = 1");
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.

Do we want to support this case?

Copy link
Copy Markdown
Author

@LupusSanctus LupusSanctus Mar 25, 2021

Choose a reason for hiding this comment

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

@alalek, this case could be useful for opset9, which doesn't support step!=1, however, it means that initially onnx model could not be generated with step value != 1, hence, this check is useless.
Just in case, I've checked the model from the issue (which relates to the PR, where this code block was added), there is no step parameter (in opset9 slice layer "no step parameter" => step=1). Removed this code block.

Comment on lines +626 to +627
if (backend == DNN_BACKEND_OPENCV)
applyTestTag(target == DNN_TARGET_OPENCL ? CV_TEST_TAG_DNN_SKIP_OPENCL : CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
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.

Implementation should fallback on CPU code instead of test skipping.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@alalek, corrected.

@alalek alalek merged commit 3e48a91 into opencv:3.4 Mar 26, 2021
@OlivierLDff
Copy link
Copy Markdown
Contributor

OlivierLDff commented Mar 26, 2021

Hi, is there any plan to merge this in master?
I merged it in my fork to try to load yolov5 dnn. This work but i have different result in i run the inference on cpu or with cuda backend.
Have you tried CUDA backend with this and yolov5 model?
The result are coherent with OPENCV backend, but with CUDA result are a bit "off" and are scaled x2.
Should i open an issue, or is this something known?
Thanks for the great work.

@alalek
Copy link
Copy Markdown
Member

alalek commented Mar 26, 2021

This patch will be merged to master, but without the CUDA part (CUDA code path will fallback on CPU - it works, but slow)

@OlivierLDff
Copy link
Copy Markdown
Contributor

Ok, is there any issue i can follow about this?
Why no CUDA? is there any plan? As you said CPU backend doesn't fit real time application.

@alalek
Copy link
Copy Markdown
Member

alalek commented Mar 26, 2021

Fallback CUDA-CPU-CUDA is slow due to data transfers and synchronization points.

Currently CUDA optimizations are maintained by community, so feel free to propose a patch after the merge to master (wait for "(4.x) Merge 3.4" regular PR).

@alalek alalek mentioned this pull request Mar 27, 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