Skip to content

[G-API] Support postprocessing for not argmaxed outputs#20476

Merged
alalek merged 7 commits intoopencv:masterfrom
TolyaTalamanov:at/support-unet-camvid-0001-segm-sample
Aug 6, 2021
Merged

[G-API] Support postprocessing for not argmaxed outputs#20476
alalek merged 7 commits intoopencv:masterfrom
TolyaTalamanov:at/support-unet-camvid-0001-segm-sample

Conversation

@TolyaTalamanov
Copy link
Copy Markdown
Contributor

@TolyaTalamanov TolyaTalamanov commented Jul 29, 2021

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

Overview

Some semantic segmentation networks such as unet-camvid-0001 from OMZ produce multi-plane output (1 x num_classesx H x W). In that case need to perform argmax operation for every pixel through channel plane in order to convert output to 1 x 1 x H x W representation where every pixel is class id.

Build configuration

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-2020.3.0:16.04
build_image:Custom Win=openvino-2021.3.0
build_image:Custom Mac=openvino-2021.3.0

test_modules:Custom=gapi,python2,python3,java
test_modules:Custom Win=gapi,python2,python3,java
test_modules:Custom Mac=gapi,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=*

@TolyaTalamanov
Copy link
Copy Markdown
Contributor Author

@dmatveev Could you have a look ?

@dmatveev dmatveev self-assigned this Aug 4, 2021
@dmatveev dmatveev added this to the 4.5.4 milestone Aug 4, 2021
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.

LGTM if the existing case is not broken with this change.

Comment on lines +51 to +69
void classesToColors(const cv::Mat &out_blob,
cv::Mat &mask_img) {
const int H = out_blob.size[0];
const int W = out_blob.size[1];

mask_img.create(H, W, CV_8UC3);
GAPI_Assert(out_blob.type() == CV_8UC1);
const uint8_t* const classes = out_blob.ptr<uint8_t>();

for (int rowId = 0; rowId < H; ++rowId) {
for (int colId = 0; colId < W; ++colId) {
uint8_t class_id = classes[rowId * W + colId];
mask_img.at<cv::Vec3b>(rowId, colId) =
class_id < colors.size()
? colors[class_id]
: cv::Vec3b{0, 0, 0}; // NB: sample supports 20 classes
}
}
}
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 this be expressed with our graph operators? Just wondering

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.

Do you mean call this function inside the user kernel ? Or express this algo by using already existing operations ?

Comment on lines +121 to 123
cv::resize(mask_img, out, in.size());
const float blending = 0.3f;
out = in * blending + out * (1 - blending);
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 this be moved on the graph level, too? Not critical to do it right now but worth considering for the future.

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.

On the graph level cv::Size parameter is unknown, isn't it ?
It's obviously can be custom resize operation

Comment on lines +109 to +110
// NB: If output has more than single plane, it contains probabilities
// otherwise class id.
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.

Is this robust enough? Maybe explicit enum flag is better? I just don't know.

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.

What do you mean by enum flag ? In that case you need to match model name with postprocessing enum flag, right ?
I don't think that it's a great solution, just tried not to overdesign it.

https://github.com/openvinotoolkit/open_model_zoo/blob/master/demos/common/python/models/segmentation.py#L79

@TolyaTalamanov
Copy link
Copy Markdown
Contributor Author

@alalek Can it be merged ?

@alalek alalek merged commit 24de676 into opencv:master Aug 6, 2021
@alalek alalek mentioned this pull request Oct 15, 2021
a-sajjad72 pushed a commit to a-sajjad72/opencv that referenced this pull request Mar 30, 2023
…amvid-0001-segm-sample

[G-API] Support postprocessing for not argmaxed outputs

* Support postprocessing for not argmaxed outputs

* Fix typo

* Add assert

* Remove static cast

* CamelCast to snake_case

* Fix windows warning

* Add static_cast to uint8_t

* Add const to variables
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