Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

dmatveev
Copy link
Contributor

@dmatveev dmatveev commented May 9, 2023

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)
  1. 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

  • 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 another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the 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

Copy link
Contributor Author

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

Commented on the questionable parts in rev. 0

Comment on lines 1165 to 1170
// Adding const input is necessary because the definition of input_names
// includes const input.
// FIXME(DM): This is actually questionable!
for (const auto& a : pp.const_inputs)
{
pp.input_names.push_back(a.first);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@TolyaTalamanov @mpashchenkov this may require your review

Copy link
Contributor

Choose a reason for hiding this comment

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

That's weird, const inputs must be set explicitly via cfgConstInputs maybe @mpashchenkov knows details...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe it came with #22017, I am wondering what's the right approach there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed this code for now, and tests pass. Propose to keep it as-is.

@dmatveev dmatveev changed the title G-API: Integration branch for ONNX & Python-related changes WIP: G-API: Integration branch for ONNX & Python-related changes May 9, 2023
@dmatveev dmatveev self-assigned this May 9, 2023
dmatveev and others added 2 commits May 9, 2023 20:56
- cv::gapi::combine() is now exposed as cv.gapi.combine();
- GKernelPackage::size() was exposed in Python for testing purposes;
- Fluid `imgproc` kernel package was exposed as cv.gapi.imgproc.fluid.kernels().
* Removed PyObjectHolder usage
* Throw understandable error message
* Added tests
const cv::Scalar &m,
const cv::Scalar &s);
GAPI_WRAP
PyParams& cfgNormalize(const std::string &layer_name, bool flag);
Copy link
Contributor

Choose a reason for hiding this comment

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

I propose we disable normalization at all if cfgMeanStd is provided because using these two is very confusing...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wait, shouldn't we in fact enable normalization when we pass mean/std?

Copy link
Contributor

Choose a reason for hiding this comment

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

Discussed locally, quick summary:

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)
  1. 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @TolyaTalamanov

It seems with the exposed knobs we cover most of the cases so can go forward in the current state

@dmatveev dmatveev added this to the 4.8.0 milestone May 23, 2023
@dmatveev
Copy link
Contributor Author

@TolyaTalamanov I see you fixed stateful kernels here, please update the PR description accordingly (add a new chapter please).
If you don't have permissions then send the text to me please.

Thanks!

dmatveev added 3 commits May 26, 2023 23:53
0) Fixed compilation warnings on onnx.hpp
1) Explicitly disabled normalization in C++ & Python tests which use squeezenet1.0-9.onnx
2) Aligned the Python ONNX test to take model from ONNX Zoo (rely on the same
   test environment used for C++ ONNX tests)
@dmatveev dmatveev marked this pull request as ready for review May 27, 2023 00:11
@dmatveev dmatveev changed the title WIP: G-API: Integration branch for ONNX & Python-related changes G-API: Integration branch for ONNX & Python-related changes May 27, 2023
@dmatveev dmatveev force-pushed the dm/gapi_onnx_py_integration branch from 5f0158f to e5b7f3a Compare May 29, 2023 10:58
Copy link
Contributor

@TolyaTalamanov TolyaTalamanov 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! 👍

@dmatveev
Copy link
Contributor Author

@asmorkalov can you please merge this one? Thanks!

@asmorkalov asmorkalov merged commit fc5d412 into opencv:4.x May 30, 2023
@asmorkalov asmorkalov mentioned this pull request May 31, 2023
thewoz pushed a commit to thewoz/opencv that referenced this pull request Jan 4, 2024
…tion

G-API: Integration branch for ONNX & Python-related changes opencv#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
thewoz pushed a commit to thewoz/opencv that referenced this pull request May 29, 2024
…tion

G-API: Integration branch for ONNX & Python-related changes opencv#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
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.

4 participants