trainingsample 0.2.16__tar.gz → 0.2.17__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. {trainingsample-0.2.16 → trainingsample-0.2.17}/.github/workflows/ci.yml +37 -32
  2. {trainingsample-0.2.16 → trainingsample-0.2.17}/.github/workflows/publish.yml +5 -5
  3. trainingsample-0.2.17/BENCHMARKS.md +137 -0
  4. {trainingsample-0.2.16 → trainingsample-0.2.17}/Cargo.lock +407 -229
  5. {trainingsample-0.2.16 → trainingsample-0.2.17}/Cargo.toml +3 -2
  6. {trainingsample-0.2.16 → trainingsample-0.2.17}/PKG-INFO +1 -1
  7. trainingsample-0.2.17/README.md +180 -0
  8. trainingsample-0.2.17/docs/API_COMPAT_CV2.md +216 -0
  9. {trainingsample-0.2.16 → trainingsample-0.2.17}/docs/BUILDING_STATIC_OPENCV.md +13 -12
  10. {trainingsample-0.2.16 → trainingsample-0.2.17}/pyproject.toml +1 -1
  11. {trainingsample-0.2.16 → trainingsample-0.2.17}/rust-toolchain.toml +0 -1
  12. {trainingsample-0.2.16 → trainingsample-0.2.17}/scripts/build-opencv-static.sh +1 -1
  13. trainingsample-0.2.17/setup-env.sh +304 -0
  14. {trainingsample-0.2.16 → trainingsample-0.2.17}/src/cv_compat.rs +54 -34
  15. {trainingsample-0.2.16 → trainingsample-0.2.17}/src/luminance.rs +37 -33
  16. {trainingsample-0.2.16 → trainingsample-0.2.17}/src/opencv_ops.rs +16 -7
  17. {trainingsample-0.2.16 → trainingsample-0.2.17}/src/python_bindings.rs +64 -80
  18. {trainingsample-0.2.16 → trainingsample-0.2.17}/src/tests.rs +166 -1
  19. {trainingsample-0.2.16 → trainingsample-0.2.17}/tests/test_performance_benchmarks.py +2 -1
  20. {trainingsample-0.2.16 → trainingsample-0.2.17}/tests/test_python_bindings.py +64 -1
  21. trainingsample-0.2.16/BENCHMARKS.md +0 -112
  22. trainingsample-0.2.16/README.md +0 -291
  23. trainingsample-0.2.16/docs/API_COMPAT_CV2.md +0 -357
  24. trainingsample-0.2.16/setup-env.sh +0 -34
  25. {trainingsample-0.2.16 → trainingsample-0.2.17}/.cargo/config.toml +0 -0
  26. {trainingsample-0.2.16 → trainingsample-0.2.17}/.envrc +0 -0
  27. {trainingsample-0.2.16 → trainingsample-0.2.17}/.gitignore +0 -0
  28. {trainingsample-0.2.16 → trainingsample-0.2.17}/.pre-commit-config.yaml +0 -0
  29. {trainingsample-0.2.16 → trainingsample-0.2.17}/.rustfmt.toml +0 -0
  30. {trainingsample-0.2.16 → trainingsample-0.2.17}/Dockerfile +0 -0
  31. {trainingsample-0.2.16 → trainingsample-0.2.17}/LICENSE +0 -0
  32. {trainingsample-0.2.16 → trainingsample-0.2.17}/clippy.toml +0 -0
  33. {trainingsample-0.2.16 → trainingsample-0.2.17}/scripts/repair-wheel.sh +0 -0
  34. {trainingsample-0.2.16 → trainingsample-0.2.17}/scripts/run_competitive_benchmarks.sh +0 -0
  35. {trainingsample-0.2.16 → trainingsample-0.2.17}/src/core.rs +0 -0
  36. {trainingsample-0.2.16 → trainingsample-0.2.17}/src/cropping.rs +0 -0
  37. {trainingsample-0.2.16 → trainingsample-0.2.17}/src/cv_batch_ops.rs +0 -0
  38. {trainingsample-0.2.16 → trainingsample-0.2.17}/src/cv_batch_ops_optimized.rs +0 -0
  39. {trainingsample-0.2.16 → trainingsample-0.2.17}/src/format_conversion_simd.rs +0 -0
  40. {trainingsample-0.2.16 → trainingsample-0.2.17}/src/lib.rs +0 -0
  41. {trainingsample-0.2.16 → trainingsample-0.2.17}/src/loading.rs +0 -0
  42. {trainingsample-0.2.16 → trainingsample-0.2.17}/src/luminance_simd.rs +0 -0
  43. {trainingsample-0.2.16 → trainingsample-0.2.17}/src/true_batch_ops.rs +0 -0
  44. {trainingsample-0.2.16 → trainingsample-0.2.17}/tests/__init__.py +0 -0
  45. {trainingsample-0.2.16 → trainingsample-0.2.17}/tests/comprehensive_tests.rs +0 -0
  46. {trainingsample-0.2.16 → trainingsample-0.2.17}/tests/test_comprehensive.py +0 -0
@@ -32,12 +32,12 @@ jobs:
32
32
  ls -la /usr/lib/llvm-18/lib/libclang.so
33
33
  which llvm-config-18
34
34
 
35
- # Build OpenCV 4.12 from source since Ubuntu 24.04 only has 4.6
36
- echo "Building OpenCV 4.12 from source..."
35
+ # Build OpenCV 4.11 from source since Ubuntu 24.04 only has 4.6
36
+ echo "Building OpenCV 4.11 from source..."
37
37
  cd /tmp
38
- wget -q https://github.com/opencv/opencv/archive/4.12.0.tar.gz
39
- tar -xzf 4.12.0.tar.gz
40
- cd opencv-4.12.0
38
+ wget -q https://github.com/opencv/opencv/archive/4.11.0.tar.gz
39
+ tar -xzf 4.11.0.tar.gz
40
+ cd opencv-4.11.0
41
41
  mkdir build
42
42
  cd build
43
43
  cmake \
@@ -127,12 +127,12 @@ jobs:
127
127
  ls -la /usr/lib/llvm-18/lib/libclang.so
128
128
  which llvm-config-18
129
129
 
130
- # Build OpenCV 4.12 from source since Ubuntu 24.04 only has 4.6
131
- echo "Building OpenCV 4.12 from source..."
130
+ # Build OpenCV 4.11 from source since Ubuntu 24.04 only has 4.6
131
+ echo "Building OpenCV 4.11 from source..."
132
132
  cd /tmp
133
- wget -q https://github.com/opencv/opencv/archive/4.12.0.tar.gz
134
- tar -xzf 4.12.0.tar.gz
135
- cd opencv-4.12.0
133
+ wget -q https://github.com/opencv/opencv/archive/4.11.0.tar.gz
134
+ tar -xzf 4.11.0.tar.gz
135
+ cd opencv-4.11.0
136
136
  mkdir build
137
137
  cd build
138
138
  cmake \
@@ -218,12 +218,12 @@ jobs:
218
218
  ls -la /usr/lib/llvm-18/lib/libclang.so
219
219
  which llvm-config-18
220
220
 
221
- # Build OpenCV 4.12 from source since Ubuntu 24.04 only has 4.6
222
- echo "Building OpenCV 4.12 from source..."
221
+ # Build OpenCV 4.11 from source since Ubuntu 24.04 only has 4.6
222
+ echo "Building OpenCV 4.11 from source..."
223
223
  cd /tmp
224
- wget -q https://github.com/opencv/opencv/archive/4.12.0.tar.gz
225
- tar -xzf 4.12.0.tar.gz
226
- cd opencv-4.12.0
224
+ wget -q https://github.com/opencv/opencv/archive/4.11.0.tar.gz
225
+ tar -xzf 4.11.0.tar.gz
226
+ cd opencv-4.11.0
227
227
  mkdir build
228
228
  cd build
229
229
  cmake \
@@ -298,12 +298,12 @@ jobs:
298
298
  ls -la /usr/lib/llvm-18/lib/libclang.so
299
299
  which llvm-config-18
300
300
 
301
- # Build OpenCV 4.12 from source since Ubuntu 24.04 only has 4.6
302
- echo "Building OpenCV 4.12 from source..."
301
+ # Build OpenCV 4.11 from source since Ubuntu 24.04 only has 4.6
302
+ echo "Building OpenCV 4.11 from source..."
303
303
  cd /tmp
304
- wget -q https://github.com/opencv/opencv/archive/4.12.0.tar.gz
305
- tar -xzf 4.12.0.tar.gz
306
- cd opencv-4.12.0
304
+ wget -q https://github.com/opencv/opencv/archive/4.11.0.tar.gz
305
+ tar -xzf 4.11.0.tar.gz
306
+ cd opencv-4.11.0
307
307
  mkdir build
308
308
  cd build
309
309
  cmake \
@@ -348,19 +348,19 @@ jobs:
348
348
  - name: Install maturin (Linux/macOS)
349
349
  run: pip install maturin[patchelf]
350
350
 
351
- - name: Install OpenCV (macOS)
351
+ - name: Install OpenCV build dependencies (macOS)
352
352
  if: runner.os == 'macOS'
353
353
  run: |
354
- # Install OpenCV and LLVM via Homebrew
355
- brew install opencv llvm
354
+ # Build against a pinned OpenCV version instead of Homebrew's moving target
355
+ brew install llvm cmake nasm pkg-config
356
356
  # Set up LLVM environment for opencv-rust bindings (keg-only installation)
357
357
  echo "LIBCLANG_PATH=/opt/homebrew/opt/llvm/lib" >> $GITHUB_ENV
358
358
  echo "LLVM_CONFIG_PATH=/opt/homebrew/opt/llvm/bin/llvm-config" >> $GITHUB_ENV
359
359
  echo "DYLD_LIBRARY_PATH=/opt/homebrew/opt/llvm/lib:$DYLD_LIBRARY_PATH" >> $GITHUB_ENV
360
- # Set up OpenCV environment
361
- echo "OPENCV_LINK_LIBS=opencv_calib3d,opencv_core,opencv_dnn,opencv_features2d,opencv_flann,opencv_highgui,opencv_imgcodecs,opencv_imgproc,opencv_ml,opencv_objdetect,opencv_photo,opencv_stitching,opencv_video,opencv_videoio" >> $GITHUB_ENV
362
- echo "OPENCV_LINK_PATHS=/opt/homebrew/lib" >> $GITHUB_ENV
363
- echo "OPENCV_INCLUDE_PATHS=/opt/homebrew/include/opencv4" >> $GITHUB_ENV
360
+
361
+ - name: Build static OpenCV (macOS)
362
+ if: runner.os == 'macOS'
363
+ run: ./scripts/build-opencv-static.sh
364
364
 
365
365
  - name: Build wheels (Linux)
366
366
  if: runner.os == 'Linux'
@@ -369,6 +369,11 @@ jobs:
369
369
  - name: Build wheels (macOS)
370
370
  if: runner.os == 'macOS'
371
371
  run: maturin build --release --out dist --no-default-features --features python-bindings,simd,opencv,metal
372
+ env:
373
+ OPENCV_INCLUDE_PATHS: ${{ github.workspace }}/third_party/opencv-static/include/opencv4
374
+ OPENCV_LINK_PATHS: ${{ github.workspace }}/third_party/opencv-static/lib
375
+ OPENCV_LINK_LIBS: static=opencv_world,static=avformat,static=avcodec,static=avfilter,static=swresample,static=swscale,static=avutil,static=jpeg,static=png,static=tiff,static=webp,static=jasper,framework=Accelerate,dylib=c++,framework=OpenCL,z
376
+ OPENCV_DISABLE_PROBES: pkg_config,cmake,vcpkg,vcpkg_cmake
372
377
 
373
378
 
374
379
  - name: Upload wheels
@@ -398,12 +403,12 @@ jobs:
398
403
  echo "LLVM_CONFIG_PATH=/usr/bin/llvm-config-18" >> $GITHUB_ENV
399
404
  echo "LD_LIBRARY_PATH=/usr/lib/llvm-18/lib:$LD_LIBRARY_PATH" >> $GITHUB_ENV
400
405
 
401
- # Build OpenCV 4.12 from source
402
- echo "Building OpenCV 4.12 from source..."
406
+ # Build OpenCV 4.11 from source
407
+ echo "Building OpenCV 4.11 from source..."
403
408
  cd /tmp
404
- wget -q https://github.com/opencv/opencv/archive/4.12.0.tar.gz
405
- tar -xzf 4.12.0.tar.gz
406
- cd opencv-4.12.0
409
+ wget -q https://github.com/opencv/opencv/archive/4.11.0.tar.gz
410
+ tar -xzf 4.11.0.tar.gz
411
+ cd opencv-4.11.0
407
412
  mkdir build
408
413
  cd build
409
414
  cmake \
@@ -49,7 +49,7 @@ jobs:
49
49
  uses: actions/cache@v4
50
50
  with:
51
51
  path: third_party/opencv-static
52
- key: opencv-static-4.12.0-codecs-jasper-ffmpeg-no-itt-no-openjpeg-linux-${{ matrix.os }}-${{ runner.arch }}-${{ matrix.manylinux }}
52
+ key: opencv-static-4.11.0-codecs-jasper-ffmpeg-no-itt-no-openjpeg-linux-${{ matrix.os }}-${{ runner.arch }}-${{ matrix.manylinux }}
53
53
 
54
54
  - name: Install build dependencies
55
55
  run: |
@@ -112,7 +112,7 @@ jobs:
112
112
  # OpenCV static linking configuration
113
113
  OPENCV_INCLUDE_PATHS: ${{ github.workspace }}/third_party/opencv-static/include/opencv4
114
114
  OPENCV_LINK_PATHS: ${{ github.workspace }}/third_party/opencv-static/lib
115
- OPENCV_LINK_LIBS: static=opencv_world,static=avformat,static=avcodec,static=avfilter,static=swresample,static=swscale,static=avutil,static=jpeg,static=png,static=tiff,static=webp,static=z,static=jasper,static=stdc++
115
+ OPENCV_LINK_LIBS: static=opencv_world,static=avformat,static=avcodec,static=avfilter,static=swresample,static=swscale,static=avutil,static=jpeg,static=png,static=tiff,static=webp,static=z,static=jasper,dylib=stdc++
116
116
  OPENCV_DISABLE_PROBES: pkg_config,cmake,vcpkg,vcpkg_cmake
117
117
 
118
118
  - name: Repair wheels
@@ -147,7 +147,7 @@ jobs:
147
147
  uses: actions/cache@v4
148
148
  with:
149
149
  path: third_party/opencv-static
150
- key: opencv-static-4.12.0-codecs-jasper-ffmpeg-no-itt-no-openjpeg-linux-aarch64-${{ runner.arch }}-manylinux_2_39
150
+ key: opencv-static-4.11.0-codecs-jasper-ffmpeg-no-itt-no-openjpeg-linux-aarch64-${{ runner.arch }}-manylinux_2_39
151
151
 
152
152
  - name: Install build dependencies
153
153
  run: |
@@ -194,7 +194,7 @@ jobs:
194
194
  # OpenCV static linking configuration
195
195
  OPENCV_INCLUDE_PATHS: ${{ github.workspace }}/third_party/opencv-static/include/opencv4
196
196
  OPENCV_LINK_PATHS: ${{ github.workspace }}/third_party/opencv-static/lib
197
- OPENCV_LINK_LIBS: static=opencv_world,static=avformat,static=avcodec,static=avfilter,static=swresample,static=swscale,static=avutil,static=jpeg,static=png,static=tiff,static=webp,static=z,static=jasper,static=stdc++
197
+ OPENCV_LINK_LIBS: static=opencv_world,static=avformat,static=avcodec,static=avfilter,static=swresample,static=swscale,static=avutil,static=jpeg,static=png,static=tiff,static=webp,static=z,static=jasper,dylib=stdc++
198
198
  OPENCV_DISABLE_PROBES: pkg_config,cmake,vcpkg,vcpkg_cmake
199
199
 
200
200
  - name: Repair wheels
@@ -229,7 +229,7 @@ jobs:
229
229
  uses: actions/cache@v4
230
230
  with:
231
231
  path: third_party/opencv-static
232
- key: opencv-static-4.12.0-codecs-jasper-ffmpeg-no-itt-no-openjpeg-macos-${{ runner.os }}-${{ runner.arch }}
232
+ key: opencv-static-4.11.0-codecs-jasper-ffmpeg-no-itt-no-openjpeg-macos-${{ runner.os }}-${{ runner.arch }}
233
233
 
234
234
  - name: Install build dependencies
235
235
  run: |
@@ -0,0 +1,137 @@
1
+ # Performance Benchmarks
2
+
3
+ TrainingSample includes benchmarks for common preprocessing operations: crop, resize, luminance, resize-plus-luminance pipelines, and video frame resizing. The benchmarks are meant to catch regressions and provide workload-specific guidance, not to guarantee universal speedups over OpenCV or NumPy.
4
+
5
+ ## Running Benchmarks
6
+
7
+ Use the repository virtual environment when available:
8
+
9
+ ```bash
10
+ .venv/bin/python -m pytest tests/test_performance_benchmarks.py -q -s
11
+ ```
12
+
13
+ To run every Python test and benchmark marker in the repo:
14
+
15
+ ```bash
16
+ .venv/bin/python -m pytest -q
17
+ ```
18
+
19
+ For a fresh source build before measuring:
20
+
21
+ ```bash
22
+ env -u OPENCV_LINK_LIBS -u OPENCV_LINK_PATHS -u OPENCV_INCLUDE_PATHS \
23
+ -u LIBCLANG_PATH -u LLVM_CONFIG_PATH \
24
+ .venv/bin/maturin develop --release
25
+ ```
26
+
27
+ The OpenCV Rust binding needs a discoverable OpenCV and Clang installation. On this development host, stale macOS-style OpenCV and LLVM environment variables had to be unset before the build could probe the system OpenCV installation.
28
+
29
+ ## Current Local Snapshot
30
+
31
+ Last measured command:
32
+
33
+ ```bash
34
+ .venv/bin/python -m pytest tests/test_performance_benchmarks.py -q -s
35
+ ```
36
+
37
+ Environment:
38
+
39
+ - Linux x86_64
40
+ - CPython 3.13
41
+ - NumPy 2.3.4
42
+ - system OpenCV 4.11 via the Rust `opencv` crate
43
+ - release build installed with `maturin develop --release`
44
+
45
+ Point-in-time scenario timings from the benchmark output:
46
+
47
+ | Scenario | Before optimization | After optimization | Comparison after optimization |
48
+ |----------|---------------------|--------------------|-------------------------------|
49
+ | Crop batch, 16 images | 22.9 ms | 0.4 ms | NumPy slicing was still faster because it returns views |
50
+ | Mixed-shape crop, 8 images | 50.2 ms | 3.3 ms | NumPy slicing loop was near-zero because it returns views |
51
+ | Resize, 4 mixed-size images | 4.1 ms | 0.4 ms | OpenCV loop: 2.6 ms |
52
+ | Luminance, 4 mixed-size images | 10.4 ms | 0.6 ms | OpenCV loop: 0.9 ms |
53
+ | Resize + luminance pipeline, 4 images | 5.9 ms | 0.6 ms | OpenCV loop: 2.1 ms |
54
+ | Mixed-shape luminance, 6 images | 78.3 ms | 3.3 ms | NumPy loop: 19.4 ms |
55
+
56
+ Pytest-benchmark means from the same focused run:
57
+
58
+ | Benchmark | Mean |
59
+ |-----------|------|
60
+ | Center crop | 55.2 us |
61
+ | Resize operations | 353.1 us |
62
+ | Luminance calculation | 417.2 us |
63
+ | Crop operations | 583.8 us |
64
+ | Pipeline | 3.44 ms |
65
+ | Video processing | 2.85 ms |
66
+
67
+ A full `pytest -q` run also passed and produced similar benchmark ordering, with normal run-to-run variance.
68
+
69
+ ## What Changed in the Latest Optimization
70
+
71
+ - Owned Rust `ndarray` outputs are transferred into NumPy with `from_owned_array_bound`, avoiding an additional copy in Python-facing result conversion.
72
+ - Contiguous luminance inputs use a channel-sum fast path. Instead of computing weighted luminance per pixel, it sums R, G, and B separately and applies the weights once at the end.
73
+ - Non-contiguous arrays still use the general ndarray path for correctness.
74
+
75
+ ## Benchmark Categories
76
+
77
+ ### Image Operations
78
+
79
+ - `batch_crop_images`
80
+ - `batch_center_crop_images`
81
+ - `batch_random_crop_images`
82
+ - `batch_resize_images`
83
+ - `batch_calculate_luminance`
84
+
85
+ ### Pipeline Operations
86
+
87
+ - resize followed by luminance
88
+ - crop followed by resize
89
+ - mixed input sizes and output sizes
90
+
91
+ ### Video Operations
92
+
93
+ - `batch_resize_videos` with frame batches shaped `(T, H, W, 3)`
94
+
95
+ ## Interpreting Results
96
+
97
+ Use these benchmarks to answer practical questions:
98
+
99
+ - Is a change adding extra Rust-to-NumPy copies?
100
+ - Are contiguous arrays staying on the fast path?
101
+ - Is resize dominated by OpenCV work or Python binding overhead?
102
+ - Does a mixed-shape batch still behave reasonably?
103
+ - Is a video processing change accidentally introducing per-frame Python overhead?
104
+
105
+ Some comparisons need context:
106
+
107
+ - NumPy crop by slicing often returns a view, so it can be much faster than any function that returns owned cropped arrays.
108
+ - Very small images can be dominated by Python call overhead.
109
+ - Large images can be dominated by memory bandwidth rather than arithmetic.
110
+ - OpenCV performance varies by build options, CPU features, and linked libraries.
111
+
112
+ ## Quality Checks
113
+
114
+ The tests validate basic output behavior alongside timing:
115
+
116
+ - Crop outputs have expected shape and match NumPy slicing where ownership differences do not matter.
117
+ - Resize outputs have expected shape and are close to OpenCV output for the configured interpolation.
118
+ - Luminance stays within a small tolerance of NumPy/OpenCV-style references.
119
+ - Non-contiguous arrays are accepted by safe luminance paths and rejected by strict zero-copy crop/resize paths.
120
+
121
+ ## Regression Signals
122
+
123
+ Investigate if a change causes:
124
+
125
+ - Public batch crop to return to multi-millisecond timings for small batches.
126
+ - Luminance on contiguous RGB arrays to lose the channel-sum fast path.
127
+ - Resize benchmarks to add large overhead beyond OpenCV work.
128
+ - Video resizing to scale with per-frame Python object churn.
129
+ - Memory usage to grow unexpectedly for repeated batch calls.
130
+
131
+ ## Future Benchmark Work
132
+
133
+ - Store historical benchmark results by commit and host.
134
+ - Add explicit memory allocation tracking for Python-facing APIs.
135
+ - Separate view-returning crop comparisons from owned-output crop comparisons.
136
+ - Add more video pipeline benchmarks.
137
+ - Document hardware and OpenCV build details in benchmark artifacts.