opencv/modules/gapi/include/opencv2/gapi
Dmitry Matveev fc5d412ba7
Merge pull request #23597 from dmatveev:dm/gapi_onnx_py_integration
G-API: Integration branch for ONNX & Python-related changes #23597

# Changes overview

## 1. Expose ONNX backend's Normalization and Mean-value parameters in Python

* Since Python G-API bindings rely on `Generic` infer to express Inference, the `Generic` specialization of `onnx::Params` was extended with new methods to control normalization (`/255`) and mean-value; these methods were exposed in the Python bindings
* Found some questionable parts in the existing API which I'd like to review/discuss (see comments)

UPD:
1. Thanks to @TolyaTalamanov normalization inconsistencies have been identified with `squeezenet1.0-9` ONNX model itself; tests using these model were updated to DISABLE normalization and NOT using mean/value.
2. Questionable parts were removed and tests still pass.

### Details (taken from @TolyaTalamanov's comment):

`squeezenet1.0.*onnx` - doesn't require scaling to [0,1] and mean/std because the weights of the first convolution already scaled. ONNX documentation is broken. So the correct approach to use this models is:

1. ONNX: apply preprocessing from the documentation: https://github.com/onnx/models/blob/main/vision/classification/imagenet_preprocess.py#L8-L44 but without normalization step:
```
# DON'T DO IT:
# mean_vec = np.array([0.485, 0.456, 0.406])
# stddev_vec = np.array([0.229, 0.224, 0.225])
# norm_img_data = np.zeros(img_data.shape).astype('float32')
# for i in range(img_data.shape[0]):
#     norm_img_data[i,:,:] = (img_data[i,:,:]/255 - mean_vec[i]) / stddev_vec[i]
#     # add batch channel
#     norm_img_data = norm_img_data.reshape(1, 3, 224, 224).astype('float32')
#     return norm_img_data

# INSTEAD
return img_data.reshape(1, 3, 224, 224)
```

2. G-API: Convert image from BGR to RGB and then pass to `apply` as-is with configuring parameters:
```
net = cv.gapi.onnx.params('squeezenet', model_filename)
net.cfgNormalize('data_0', False)
```
**Note**: Results might be difference because `G-API` doesn't apply central crop but just do resize to model resolution.

---

`squeezenet1.1.*onnx` - requires scaling to [0,1] and mean/std - onnx documentation is correct.
1. ONNX: apply preprocessing from the documentation: https://github.com/onnx/models/blob/main/vision/classification/imagenet_preprocess.py#L8-L44
2. G-API: Convert image from BGR to RGB and then pass to `apply` as-is with configuring parameters:
```
net = cv.gapi.onnx.params('squeezenet', model_filename)
net.cfgNormalize('data_0', True) // default
net.cfgMeanStd('data_0', [0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
```
**Note**: Results might be difference because `G-API` doesn't apply central crop but just do resize to model resolution.

## 2. Expose Fluid & kernel package-related functionality in Python

* `cv::gapi::combine()`
* `cv::GKernelPackage::size()` (mainly for testing purposes)
* `cv::gapi::imgproc::fluid::kernels()`

Added a test for the above.

## 3. Fixed issues with Python stateful kernel handling

Fixed error message when `outMeta()` of custom python operation fails.

## 4. Fixed various issues in Python tests

1. `test_gapi_streaming.py` - fixed behavior of Desync test to avoid sporadic issues
2. `test_gapi_infer_onnx.py` - fixed model lookup (it was still using the ONNX Zoo layout but was NOT using the proper env var we use to point to one).

### Pull Request Readiness Checklist

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

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
2023-05-30 17:52:17 +03:00
..
cpu Merge pull request #22935 from alalek:gapi_error 2022-12-19 06:05:15 +00:00
fluid Merge pull request #23597 from dmatveev:dm/gapi_onnx_py_integration 2023-05-30 17:52:17 +03:00
gpu Merge pull request #14741 from rgarnov:gapi_fix_includes 2019-06-14 19:27:19 +03:00
infer Merge pull request #23597 from dmatveev:dm/gapi_onnx_py_integration 2023-05-30 17:52:17 +03:00
oak Merge pull request #21775 from luzpaz:typos/gapi 2022-04-13 17:06:37 +00:00
ocl build: fix/eliminate MSVC warnings 2022-12-10 12:19:31 +00:00
own G-API: Fix compilation error in Standalone mode 2023-04-02 17:52:53 +03:00
plaidml Move GKernelPackage to cv namespace 2021-12-24 18:04:11 +03:00
python Fix problem with handleNewStream functionality 2022-09-06 08:52:35 +01:00
render Move GKernelPackage to cv namespace 2021-12-24 18:04:11 +03:00
s11n Merge pull request #22935 from alalek:gapi_error 2022-12-19 06:05:15 +00:00
streaming Merge pull request #22935 from alalek:gapi_error 2022-12-19 06:05:15 +00:00
util Merge pull request #21041 from sivanov-work:gin_gout_concept 2021-12-03 12:30:05 +00:00
core.hpp Backport C-API cleanup (imgproc) from 5.x 2023-01-16 23:29:50 +03:00
garg.hpp Merge pull request #20857 from alexgiving:atrutnev/move_API_samples 2021-11-12 14:17:21 +00:00
garray.hpp Merge pull request #22935 from alalek:gapi_error 2022-12-19 06:05:15 +00:00
gasync_context.hpp G-API: Documentation updates 2021-06-16 01:01:55 +03:00
gcall.hpp Merge pull request #19009 from TolyaTalamanov:at/media-frame-copy 2020-12-11 16:29:34 +00:00
gcommon.hpp Merge pull request #22494 from TolyaTalamanov:at/expose-all-core-to-python 2022-11-08 11:43:38 +00:00
gcompiled_async.hpp Merge pull request #15735 from anton-potapov:gapi_async_documentaion 2019-10-21 22:33:18 +03:00
gcompiled.hpp Enable state initialization params via compile_args 2020-06-25 00:43:12 +03:00
gcompoundkernel.hpp Merge pull request #17163 from AsyaPronina:gcompound_kernel_gmatp_coop 2020-08-25 13:51:43 +00:00
gcomputation_async.hpp Merge pull request #15735 from anton-potapov:gapi_async_documentaion 2019-10-21 22:33:18 +03:00
gcomputation.hpp Merge pull request #20857 from alexgiving:atrutnev/move_API_samples 2021-11-12 14:17:21 +00:00
gframe.hpp Merge pull request #21775 from luzpaz:typos/gapi 2022-04-13 17:06:37 +00:00
gkernel.hpp Merge pull request #23597 from dmatveev:dm/gapi_onnx_py_integration 2023-05-30 17:52:17 +03:00
gmat.hpp G-API: Documentation updates 2021-06-16 01:01:55 +03:00
gmetaarg.hpp G-API: Integrated cv::MediaFrame as I/O type + CPU backend 2020-10-05 20:21:15 +03:00
gopaque.hpp Merge pull request #22935 from alalek:gapi_error 2022-12-19 06:05:15 +00:00
gproto.hpp Merge pull request #20857 from alexgiving:atrutnev/move_API_samples 2021-11-12 14:17:21 +00:00
gscalar.hpp Merge pull request #22494 from TolyaTalamanov:at/expose-all-core-to-python 2022-11-08 11:43:38 +00:00
gstreaming.hpp Merge pull request #21775 from luzpaz:typos/gapi 2022-04-13 17:06:37 +00:00
gtransform.hpp remove redundant semicolons 2021-10-27 20:19:05 +08:00
gtype_traits.hpp Merge pull request #21775 from luzpaz:typos/gapi 2022-04-13 17:06:37 +00:00
gtyped.hpp Merge pull request #20857 from alexgiving:atrutnev/move_API_samples 2021-11-12 14:17:21 +00:00
imgproc.hpp Merge pull request #22735 from TolyaTalamanov:at/expose-all-imgproc-to-python 2022-11-18 15:25:51 +00:00
infer.hpp Merge pull request #22935 from alalek:gapi_error 2022-12-19 06:05:15 +00:00
media.hpp Merge pull request #22935 from alalek:gapi_error 2022-12-19 06:05:15 +00:00
opencv_includes.hpp G-API: Fix compilation error in Standalone mode 2023-04-02 17:52:53 +03:00
operators.hpp Merge pull request #18182 from OrestChura:oc/operators_to_cv 2020-09-02 19:28:10 +00:00
render.hpp Merge pull request #16050 from dmatveev:dm/ocv42_gapi_doc_fixup 2019-12-06 15:36:02 +03:00
rmat.hpp Merge pull request #22935 from alalek:gapi_error 2022-12-19 06:05:15 +00:00
s11n.hpp Merge pull request #22935 from alalek:gapi_error 2022-12-19 06:05:15 +00:00
stereo.hpp Merge pull request #22935 from alalek:gapi_error 2022-12-19 06:05:15 +00:00
video.hpp G-API: Documentation updates 2021-06-16 01:01:55 +03:00