From f4fe5e45a0e7248abe2ff45129c68e128731c8ea Mon Sep 17 00:00:00 2001 From: Connor Baker Date: Thu, 3 Jul 2025 20:42:49 +0000 Subject: [PATCH] opencv.passthru.tests.opencv4-tests: refactor and fix CUDA tests Signed-off-by: Connor Baker --- pkgs/development/libraries/opencv/tests.nix | 174 +++++++++++++------- 1 file changed, 110 insertions(+), 64 deletions(-) diff --git a/pkgs/development/libraries/opencv/tests.nix b/pkgs/development/libraries/opencv/tests.nix index ca4c9631dc51..040554a6683b 100644 --- a/pkgs/development/libraries/opencv/tests.nix +++ b/pkgs/development/libraries/opencv/tests.nix @@ -1,85 +1,131 @@ { - opencv4, - testDataSrc, - stdenv, - lib, - runCommand, - gst_all_1, - runAccuracyTests, - runPerformanceTests, enableGStreamer, enableGtk2, enableGtk3, + gst_all_1, + lib, + opencv4, + runAccuracyTests, + runCommand, + runPerformanceTests, + stdenv, + testDataSrc, + writableTmpDirAsHomeHook, xvfb-run, }: let - testNames = - [ + inherit (lib) getExe optionals optionalString; + inherit (opencv4.passthru) cudaSupport; + inherit (stdenv.hostPlatform) isAarch64 isDarwin; +in +runCommand "opencv4-tests" + { + __structuredAttrs = true; + strictDeps = true; + + nativeBuildInputs = + [ writableTmpDirAsHomeHook ] + ++ optionals enableGStreamer ( + with gst_all_1; + [ + gstreamer + gst-plugins-base + gst-plugins-good + ] + ); + + ignoredTests = + [ + "AsyncAPICancelation/cancel*" + "Photo_CalibrateDebevec.regression" + ] + ++ optionals cudaSupport [ + # opencv4-tests> /build/source/modules/photo/test/test_denoising.cuda.cpp:115: Failure + # opencv4-tests> The max difference between matrices "bgr_gold" and "dbgr" is 2 at (339, 486), which exceeds "1", where "bgr_gold" at (339, 486) evaluates to (182, 239, 239), "dbgr" at (339, 486) evaluates to (184, 239, 239), "1" evaluates to 1 + # opencv4-tests> [ FAILED ] CUDA_FastNonLocalMeans.Regression (48 ms) + "CUDA_FastNonLocalMeans.Regression" + ]; + + inherit runAccuracyTests; + + accuracyTestNames = + [ + "calib3d" + "core" + "features2d" + "flann" + "imgcodecs" + "imgproc" + "ml" + "objdetect" + "photo" + "stitching" + "video" + #"videoio" # - a lot of GStreamer warnings and failed tests + #"dnn" #- some caffe tests failed, probably because github workflow also downloads additional models + ] + ++ optionals (!isAarch64 && enableGStreamer) [ "gapi" ] + ++ optionals (enableGtk2 || enableGtk3) [ "highgui" ]; + + inherit runPerformanceTests; + + performanceTestNames = [ "calib3d" "core" "features2d" - "flann" "imgcodecs" "imgproc" - "ml" "objdetect" "photo" "stitching" "video" - #"videoio" # - a lot of GStreamer warnings and failed tests - #"dnn" #- some caffe tests failed, probably because github workflow also downloads additional models - ] - ++ lib.optionals (!stdenv.hostPlatform.isAarch64 && enableGStreamer) [ "gapi" ] - ++ lib.optionals (enableGtk2 || enableGtk3) [ "highgui" ]; - perfTestNames = [ - "calib3d" - "core" - "features2d" - "imgcodecs" - "imgproc" - "objdetect" - "photo" - "stitching" - "video" - ] ++ lib.optionals (!stdenv.hostPlatform.isAarch64 && enableGStreamer) [ "gapi" ]; - testRunner = lib.optionalString (!stdenv.hostPlatform.isDarwin) "${lib.getExe xvfb-run} -a "; - testsPreparation = '' - touch $out + ] ++ optionals (!isAarch64 && enableGStreamer) [ "gapi" ]; + + testRunner = optionalString (!isDarwin) "${getExe xvfb-run} -a "; + + requiredSystemFeatures = optionals cudaSupport [ "cuda" ]; + } + '' + set -euo pipefail + # several tests want a write access, so we have to copy files - tmpPath="$(mktemp -d "/tmp/opencv_extra_XXXXXX")" - cp -R ${testDataSrc} $tmpPath/opencv_extra - chmod -R +w $tmpPath/opencv_extra - export OPENCV_TEST_DATA_PATH="$tmpPath/opencv_extra/testdata" + nixLog "Preparing test data" + cp -R "${testDataSrc}" "$HOME/opencv_extra" + chmod -R +w "$HOME/opencv_extra" + export OPENCV_TEST_DATA_PATH="$HOME/opencv_extra/testdata" export OPENCV_SAMPLES_DATA_PATH="${opencv4.package_tests}/samples/data" # ignored tests because of gtest error - "Test code is not available due to compilation error with GCC 11" # ignore test due to numerical instability - export GTEST_FILTER="-AsyncAPICancelation/cancel*:Photo_CalibrateDebevec.regression" - ''; - accuracyTests = lib.optionalString runAccuracyTests '' - ${builtins.concatStringsSep "\n" ( - map ( - test: - "${testRunner}${opencv4.package_tests}/opencv_test_${test} --test_threads=$NIX_BUILD_CORES --gtest_filter=$GTEST_FILTER" - ) testNames - )} - ''; - performanceTests = lib.optionalString runPerformanceTests '' - ${builtins.concatStringsSep "\n" ( - map ( - test: - "${testRunner}${opencv4.package_tests}/opencv_perf_${test} --perf_impl=plain --perf_min_samples=10 --perf_force_samples=10 --perf_verify_sanity --skip_unstable=1 --gtest_filter=$GTEST_FILTER" - ) perfTestNames - )} - ''; -in -runCommand "opencv4-tests" { - nativeBuildInputs = lib.optionals enableGStreamer ( - with gst_all_1; - [ - gstreamer - gst-plugins-base - gst-plugins-good - ] - ); -} (testsPreparation + accuracyTests + performanceTests) + if [[ -n ''${ignoredTests+x} ]]; then + export GTEST_FILTER="-$(concatStringsSep ":" ignoredTests)" + nixLog "Using GTEST_FILTER: $GTEST_FILTER" + fi + + if [[ -n $runAccuracyTests ]]; then + nixLog "Running accuracy tests" + for testName in "''${accuracyTestNames[@]}"; do + nixLog "Running accuracy test: $testName" + ''${testRunner}${opencv4.package_tests}/opencv_test_''${testName} \ + --test_threads=$NIX_BUILD_CORES + done + nixLog "Finished running accuracy tests" + fi + + if [[ -n $runPerformanceTests ]]; then + nixLog "Running performance tests" + for testName in "''${performanceTestNames[@]}"; do + nixLog "Running performance test: $testName" + ''${testRunner}${opencv4.package_tests}/opencv_perf_''${testName} \ + --perf_impl=plain \ + --perf_min_samples=10 \ + --perf_force_samples=10 \ + --perf_verify_sanity \ + --skip_unstable=1 + done + nixLog "Finished running performance tests" + fi + + nixLog "Finished running tests" + touch "$out" + ''