mirror of
https://github.com/opencv/opencv.git
synced 2026-11-21 00:35:35 -06:00
dnn(onnx): add support for ONNX Compress operator - #30576 ### Description This PR adds support for the ONNX `Compress` operator (opset 9-24) to the OpenCV DNN dynamic engine: - Implements `CompressLayer` in `modules/dnn/src/layers/compress_layer.cpp` supporting dynamic output shapes (`dynamicOutputShapes() { return true; }`). - Implements full ONNX `Compress` semantics: - Slices along a specified axis when the `axis` attribute is provided (supports negative axis indexing via `normalize_axis`). - Flattens the input tensor to 1D before compression when the `axis` attribute is omitted. - Discards elements exceeding condition length when condition length is shorter than target axis dimension (`std::min(dimAxis, condTotal)`). - Handles all-false condition ($K=0$) cleanly with early return after fitting the valid empty output shape (`[..., 0, ...]`). - Enforces continuous tensor invariants with `CV_Assert(data.isContinuous())` and validates `condition` as `CV_Bool` per ONNX spec. - Slicing optimized via OpenCV native `parallel_for_` flattening multi-dimensional iteration domains to 1D (`totalSlices = outer * K`). - Un-denylists and passes all 4 official ONNX conformance tests in `test_onnx_conformance_layer_parser_denylist.inl.hpp`. - Marks `Compress` conformance tests with `SKIP;` in `test_onnx_conformance_layer_filter__openvino.inl.hpp` to ensure OpenVINO CI remains green. - Adds focused Google Test unit tests in `modules/dnn/test/test_layers.cpp` verifying empty output behavior on all-false condition both with and without `axis`. ### Test Results All 4 ONNX conformance tests + 2 all-false unit tests pass locally: ```text [==========] Running 6 tests from 2 test cases. [----------] Global test environment set-up. [----------] 2 tests from Test_Compress [ RUN ] Test_Compress.AllFalse_WithAxis [ OK ] Test_Compress.AllFalse_WithAxis (29 ms) [ RUN ] Test_Compress.AllFalse_WithoutAxis [ OK ] Test_Compress.AllFalse_WithoutAxis (0 ms) [----------] 2 tests from Test_Compress (30 ms total) [----------] 4 tests from Test_ONNX_conformance [ RUN ] Test_ONNX_conformance.Layer_Test/test_compress_0_OCV_CPU [ OK ] Test_ONNX_conformance.Layer_Test/test_compress_0_OCV_CPU (901 ms) [ RUN ] Test_ONNX_conformance.Layer_Test/test_compress_1_OCV_CPU [ OK ] Test_ONNX_conformance.Layer_Test/test_compress_1_OCV_CPU (465 ms) [ RUN ] Test_ONNX_conformance.Layer_Test/test_compress_default_axis_OCV_CPU [ OK ] Test_ONNX_conformance.Layer_Test/test_compress_default_axis_OCV_CPU (456 ms) [ RUN ] Test_ONNX_conformance.Layer_Test/test_compress_negative_axis_OCV_CPU [ OK ] Test_ONNX_conformance.Layer_Test/test_compress_negative_axis_OCV_CPU (559 ms) [----------] 4 tests from Test_ONNX_conformance (2383 ms total) [----------] Global test environment tear-down [==========] 6 tests from 2 test cases ran. (2413 ms total) [ PASSED ] 6 tests. ``` ### Checklist - [x] I have read the [OpenCV contribution guidelines](https://github.com/opencv/opencv/wiki/How_to_contribute). - [x] My code follows the OpenCV coding style guidelines. - [x] I have tested my changes locally and all relevant unit tests pass. - [x] No unrelated files or whitespace changes are included.