Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import and inference INT8 quantized TFLite model #23409

Merged
merged 10 commits into from Apr 24, 2023

Conversation

dkurt
Copy link
Member

@dkurt dkurt commented Mar 25, 2023

Pull Request Readiness Checklist

  • Support quantized TFLite models
  • Enable fused activations (FP32, INT8)

Merge with extra: opencv/opencv_extra#1048

res

on the image, green boxes are from TFLite and red boxes from OpenCV

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

@asmorkalov
Copy link
Contributor

Ubuntu 20.04:

 [  PASSED  ] 2652 tests.
[  FAILED  ] 2 tests, listed below:
[  FAILED  ] Test_TFLite.max_unpooling
[  FAILED  ] Test_TFLite.quant

@dkurt dkurt marked this pull request as ready for review April 13, 2023 07:10
@dkurt dkurt force-pushed the dnn_tflite_quant branch 2 times, most recently from 8a10784 to 166890b Compare April 13, 2023 08:15
@opencv-alalek opencv-alalek self-requested a review April 19, 2023 11:36
@opencv-alalek opencv-alalek added this to the 4.8.0 milestone Apr 19, 2023
@@ -56,25 +56,32 @@ class TFLiteImporter {
// Wrap TFLite Tensor to OpenCV Mat without data copying
Mat parseTensor(const Tensor& tensor);

typedef void (TFLiteImporter::*TFLiteImporterNodeParser)(const Operator&, const std::string&, LayerParams&);
typedef void (TFLiteImporter::*TFLiteImporterNodeParser)(const Operator&, const std::string&, LayerParams&, LayerParams&);
Copy link
Contributor

Choose a reason for hiding this comment

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

This should not be a common change.

Many handlers just don't use activParams at all.

Compiler warnings are disabled in dnn module, many warnings are really generated here for that change.


// Connect layer to inputs
int i = 0;
for (int idx : layerInputs) {
std::vector<DataLayout> inpLayouts;
Copy link
Contributor

Choose a reason for hiding this comment

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

Problem of handlers API is here.
We should not have any heavy postprocessing after the handler call.

Post-processing should be done in the handler itself (because handler defines post-processing steps).
Provide a method for registration of DNN layers to be called from handlers.

See ONNX importer implementation for reference.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks! I reworked both fused activations logic and created an addLayer method. Here is a graph for EfficientDet I used to verify that activations were added correctly: efficientdet.dot (efficientdet.pdf). Unfortunately, for this model they work as identity function and even with unconnected activations, test passes.

Copy link
Contributor

@opencv-alalek opencv-alalek left a comment

Choose a reason for hiding this comment

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

Well done 👍 Thank you!

@opencv-alalek opencv-alalek self-assigned this Apr 24, 2023
@asmorkalov asmorkalov merged commit aa57833 into opencv:4.x Apr 24, 2023
19 checks passed
@dkurt
Copy link
Member Author

dkurt commented Apr 24, 2023

@opencv-alalek, thanks for the review!

@dkurt dkurt deleted the dnn_tflite_quant branch April 24, 2023 10:45
@asmorkalov asmorkalov mentioned this pull request May 31, 2023
thewoz pushed a commit to thewoz/opencv that referenced this pull request Jan 4, 2024
Import and inference INT8 quantized TFLite model opencv#23409

### Pull Request Readiness Checklist

* Support quantized TFLite models
* Enable fused activations (FP32, INT8)

**Merge with extra**: opencv/opencv_extra#1048

![res](https://user-images.githubusercontent.com/25801568/231433201-566b4bd6-ccff-462c-9e74-adbdcdf3648b.png)

on the image, green boxes are from TFLite and red boxes from OpenCV

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.

None yet

3 participants