opencv/modules/gapi/test
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
..
common Merge pull request #22935 from alalek:gapi_error 2022-12-19 06:05:15 +00:00
cpu Merge pull request #21909 from dbudnikov:dbudnikov/reduce_gapi_tests_number 2022-05-15 10:07:01 +00:00
executor build: eliminate build warnings on Ubuntu 20.04/16.04 2022-12-20 06:46:30 +00:00
gpu Merge pull request #21909 from dbudnikov:dbudnikov/reduce_gapi_tests_number 2022-05-15 10:07:01 +00:00
infer Merge pull request #23597 from dmatveev:dm/gapi_onnx_py_integration 2023-05-30 17:52:17 +03:00
internal Fix some clang 14 warnings 2023-02-07 01:19:00 +03:00
oak Merge pull request #20785 from smirnov-alexey:as/oak_backend 2022-01-17 22:56:01 +00:00
own Merge pull request #20922 from alexgiving:atrutnev/align_expect_assert_macros 2021-10-29 16:30:35 +00:00
render Merge pull request #20284 from TolyaTalamanov:at/wrap-render 2021-07-01 09:36:19 +00:00
rmat gapi(test): reduce used amount of memory 2022-05-15 09:41:25 +00:00
s11n Merge pull request #22935 from alalek:gapi_error 2022-12-19 06:05:15 +00:00
streaming Fix some clang 14 warnings 2023-02-07 01:19:00 +03:00
util Merge pull request #20922 from alexgiving:atrutnev/align_expect_assert_macros 2021-10-29 16:30:35 +00:00
gapi_array_tests.cpp Merge pull request #19828 from OrestChura:oc/fix_garray_garray_input 2021-04-01 20:39:31 +00:00
gapi_async_test.cpp Included thread in gapi_async_test.cpp 2022-10-31 12:19:04 +01:00
gapi_basic_hetero_tests.cpp Move GKernelPackage to cv namespace 2021-12-24 18:04:11 +03:00
gapi_compile_args_tests.cpp Move GKernelPackage to cv namespace 2021-12-24 18:04:11 +03:00
gapi_desc_tests.cpp Merge pull request #16995 from mpashchenkov:mp/ocv-gapi-standalone-mat 2020-04-21 20:22:01 +00:00
gapi_fluid_parallel_rois_test.cpp Merge pull request #16745 from Volskig:mp/ocv-gapi-standalone-size 2020-04-02 18:19:45 +00:00
gapi_fluid_resize_test.cpp Fix tolerance for Preproc4lpiTest set 2023-03-24 14:20:22 +03:00
gapi_fluid_roi_test.cpp Merge pull request #16745 from Volskig:mp/ocv-gapi-standalone-size 2020-04-02 18:19:45 +00:00
gapi_fluid_test_kernels.cpp Move GKernelPackage to cv namespace 2021-12-24 18:04:11 +03:00
gapi_fluid_test_kernels.hpp Move GKernelPackage to cv namespace 2021-12-24 18:04:11 +03:00
gapi_fluid_test.cpp Merge pull request #21157 from alexgiving:atrutnev/move_resize 2021-12-29 15:13:43 +00:00
gapi_frame_tests.cpp Merge pull request #21511 from dbudniko:dbudniko/gapi_media_format_gray 2022-02-03 15:20:21 +03:00
gapi_gcompiled_tests.cpp Merge pull request #20184 from sivanov-work:fix_gapi_empty_input 2021-06-10 14:05:46 +03:00
gapi_gcomputation_tests.cpp Merge pull request #19322 from TolyaTalamanov:at/python-callbacks 2021-03-01 15:52:11 +00:00
gapi_gpu_test.cpp Move GKernelPackage to cv namespace 2021-12-24 18:04:11 +03:00
gapi_graph_meta_tests.cpp Merge pull request #20995 from mpashchenkov:mp/ocv-gapi-tdp-skip 2021-11-16 18:27:42 +00:00
gapi_kernel_tests.cpp Merge pull request #19617 from smirnov-alexey:as/extend_kernel_package_api 2021-03-10 15:58:34 +00:00
gapi_mock_kernels.hpp Merge pull request #14741 from rgarnov:gapi_fix_includes 2019-06-14 19:27:19 +03:00
gapi_opaque_tests.cpp Merge pull request #20922 from alexgiving:atrutnev/align_expect_assert_macros 2021-10-29 16:30:35 +00:00
gapi_plaidml_pipelines.cpp Add implementation in case plaidml isn't found 2020-06-22 00:46:41 +03:00
gapi_planar_test.cpp gapi(test): ban and get rid of countNonZero() checks 2020-02-26 14:19:19 +03:00
gapi_sample_pipelines.cpp Merge pull request #21775 from luzpaz:typos/gapi 2022-04-13 17:06:37 +00:00
gapi_scalar_tests.cpp gapi(test): ban and get rid of countNonZero() checks 2020-02-26 14:19:19 +03:00
gapi_smoke_test.cpp gapi(test): ban and get rid of countNonZero() checks 2020-02-26 14:19:19 +03:00
gapi_transform_tests.cpp Move GKernelPackage to cv namespace 2021-12-24 18:04:11 +03:00
gapi_typed_tests.cpp Merge pull request #17871 from OrestChura:oc/typed_GArray_GMat 2020-07-28 14:20:36 +03:00
gapi_util_tests.cpp Merge pull request #21041 from sivanov-work:gin_gout_concept 2021-12-03 12:30:05 +00:00
opencl_kernels_test_gapi.hpp avoid kernel compile error on Arm SBCs 2020-07-06 18:27:19 +09:00
test_main.cpp Merge pull request #20995 from mpashchenkov:mp/ocv-gapi-tdp-skip 2021-11-16 18:27:42 +00:00
test_precomp.hpp Fix building opencv using gcc 11.x #19244 2021-01-02 17:43:11 +00:00