Skip to content

Commit f5eb6c6

Browse files
authored
Revert add array to opencv (#43)
* Updated benchmark result * Refactor * Refactor
1 parent 8b309b4 commit f5eb6c6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+1085
-175
lines changed

albucore/functions.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,15 @@ def multiply(img: np.ndarray, value: ValueType, inplace: bool = False) -> np.nda
215215
def add_opencv(img: np.ndarray, value: np.ndarray | float) -> np.ndarray:
216216
value = prepare_value_opencv(img, value, "add")
217217

218-
if img.dtype == np.uint8:
219-
if isinstance(value, (int, float)) and value < 0:
220-
return cv2.add(img.astype(np.float32), value)
221-
if isinstance(value, np.ndarray) and value.dtype != np.uint8:
222-
return cv2.add(img.astype(np.float32), value.astype(np.float32))
218+
# Convert to float32 if:
219+
# 1. uint8 image with negative scalar value
220+
# 2. uint8 image with non-uint8 array value
221+
needs_float = img.dtype == np.uint8 and (
222+
(isinstance(value, (int, float)) and value < 0) or (isinstance(value, np.ndarray) and value.dtype != np.uint8)
223+
)
223224

224-
return cv2.add(img, value)
225+
if needs_float:
226+
return cv2.add(img.astype(np.float32), value if isinstance(value, (int, float)) else value.astype(np.float32))
225227

226228
return cv2.add(img, value)
227229

@@ -248,8 +250,6 @@ def add_vector(img: np.ndarray, value: np.ndarray, inplace: bool) -> np.ndarray:
248250

249251
@clipped
250252
def add_array(img: np.ndarray, value: np.ndarray) -> np.ndarray:
251-
if img.dtype == value.dtype:
252-
return add_array_simsimd(img, value)
253253
return add_opencv(img, value)
254254

255255

benchmark/albucore_benchmark/results/float32_1/AddArray.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ Number of images: 500
2020

2121
| | albucore | lut | opencv | numpy | simsimd |
2222
|:---------|:-----------------|:------|:-----------------|:-----------------|:-----------------|
23-
| AddArray | 1586.61 ± 162.91 | N/A | 1789.76 ± 205.71 | 1656.58 ± 111.87 | 1519.24 ± 199.93 |
23+
| AddArray | 1854.77 ± 415.86 | N/A | 1852.88 ± 121.24 | 1459.54 ± 375.42 | 1654.07 ± 126.99 |

benchmark/albucore_benchmark/results/float32_1/AddConstant.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ Number of images: 500
1818

1919
## Performance (images/second)
2020

21-
| | albucore | lut | opencv | numpy | simsimd |
22-
|:------------|:-----------------|:------|:-----------------|:-----------------|:-----------------|
23-
| AddConstant | 2070.64 ± 213.81 | N/A | 2004.65 ± 181.20 | 1861.08 ± 224.83 | 1332.33 ± 149.68 |
21+
| | albucore | lut | opencv | numpy | simsimd |
22+
|:------------|:-----------------|:------|:----------------|:----------------|:-----------------|
23+
| AddConstant | 1888.37 ± 106.49 | N/A | 1954.72 ± 42.99 | 1861.73 ± 74.98 | 1219.05 ± 189.20 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Benchmark Results: AddVector
2+
3+
Number of images: 500
4+
5+
## CPU Information
6+
7+
- CPU: Apple M1 Pro
8+
- Frequency: Current: 3228.00 MHz, Min: 600.00 MHz, Max: 3228.00 MHz
9+
- Physical cores: 10
10+
- Total cores: 10
11+
12+
## Package Versions
13+
14+
| Python | albucore | opencv-python-headless | numpy | torchvision |
15+
|:--------------------------------------|:-----------|:-------------------------|:--------|:--------------|
16+
| 3.9.20 (main, Oct 3 2024, 02:24:59) | 0.0.19 | 4.10.0.84 | 2.0.2 | 0.19.1 |
17+
| [Clang 14.0.6 ] | | | | |
18+
19+
## Performance (images/second)
20+
21+
| | albucore | lut | opencv | numpy | simsimd |
22+
|:----------|:-----------------|:------|:----------------|:----------------|:----------|
23+
| AddVector | 1933.94 ± 176.48 | N/A | 1802.99 ± 47.72 | 1876.27 ± 73.40 | N/A |

benchmark/albucore_benchmark/results/float32_1/AddWeighted.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ Number of images: 500
1818

1919
## Performance (images/second)
2020

21-
| | albucore | lut | opencv | numpy | simsimd |
22-
|:------------|:-----------------|:------|:-----------------|:----------------|:-----------------|
23-
| AddWeighted | 1613.50 ± 154.15 | N/A | 1410.60 ± 134.12 | 1257.10 ± 78.80 | 1534.87 ± 100.77 |
21+
| | albucore | lut | opencv | numpy | simsimd |
22+
|:------------|:----------------|:------|:----------------|:-----------------|:----------------|
23+
| AddWeighted | 1522.93 ± 45.94 | N/A | 1378.03 ± 37.98 | 1134.65 ± 176.15 | 1555.68 ± 37.54 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Benchmark Results: FromFloat
2+
3+
Number of images: 500
4+
5+
## CPU Information
6+
7+
- CPU: Apple M1 Pro
8+
- Frequency: Current: 3228.00 MHz, Min: 600.00 MHz, Max: 3228.00 MHz
9+
- Physical cores: 10
10+
- Total cores: 10
11+
12+
## Package Versions
13+
14+
| Python | albucore | opencv-python-headless | numpy | torchvision |
15+
|:--------------------------------------|:-----------|:-------------------------|:--------|:--------------|
16+
| 3.9.20 (main, Oct 3 2024, 02:24:59) | 0.0.19 | 4.10.0.84 | 2.0.2 | 0.19.1 |
17+
| [Clang 14.0.6 ] | | | | |
18+
19+
## Performance (images/second)
20+
21+
| | albucore | lut | opencv | numpy | simsimd |
22+
|:----------|:----------------|:------|:---------------|:---------------|:----------|
23+
| FromFloat | 1260.47 ± 41.56 | N/A | 712.18 ± 88.37 | 734.41 ± 58.55 | N/A |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Benchmark Results: HorizontalFlip
2+
3+
Number of images: 500
4+
5+
## CPU Information
6+
7+
- CPU: Apple M1 Pro
8+
- Frequency: Current: 3228.00 MHz, Min: 600.00 MHz, Max: 3228.00 MHz
9+
- Physical cores: 10
10+
- Total cores: 10
11+
12+
## Package Versions
13+
14+
| Python | albucore | opencv-python-headless | numpy | torchvision |
15+
|:--------------------------------------|:-----------|:-------------------------|:--------|:--------------|
16+
| 3.9.20 (main, Oct 3 2024, 02:24:59) | 0.0.19 | 4.10.0.84 | 2.0.2 | 0.19.1 |
17+
| [Clang 14.0.6 ] | | | | |
18+
19+
## Performance (images/second)
20+
21+
| | albucore | lut | opencv | numpy | simsimd |
22+
|:---------------|:------------------|:------|:-----------------|:-----------------|:----------|
23+
| HorizontalFlip | 4655.65 ± 1173.34 | N/A | 1665.88 ± 304.01 | 1802.95 ± 195.86 | N/A |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Benchmark Results: MultiplyAdd
2+
3+
Number of images: 500
4+
5+
## CPU Information
6+
7+
- CPU: Apple M1 Pro
8+
- Frequency: Current: 3228.00 MHz, Min: 600.00 MHz, Max: 3228.00 MHz
9+
- Physical cores: 10
10+
- Total cores: 10
11+
12+
## Package Versions
13+
14+
| Python | albucore | opencv-python-headless | numpy | torchvision |
15+
|:--------------------------------------|:-----------|:-------------------------|:--------|:--------------|
16+
| 3.9.20 (main, Oct 3 2024, 02:24:59) | 0.0.19 | 4.10.0.84 | 2.0.2 | 0.19.1 |
17+
| [Clang 14.0.6 ] | | | | |
18+
19+
## Performance (images/second)
20+
21+
| | albucore | lut | opencv | numpy | simsimd |
22+
|:------------|:---------------|:------|:----------------|:-----------------|:----------|
23+
| MultiplyAdd | 629.51 ± 81.55 | N/A | 445.55 ± 178.66 | 1733.97 ± 110.31 | N/A |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Benchmark Results: MultiplyArray
2+
3+
Number of images: 500
4+
5+
## CPU Information
6+
7+
- CPU: Apple M1 Pro
8+
- Frequency: Current: 3228.00 MHz, Min: 600.00 MHz, Max: 3228.00 MHz
9+
- Physical cores: 10
10+
- Total cores: 10
11+
12+
## Package Versions
13+
14+
| Python | albucore | opencv-python-headless | numpy | torchvision |
15+
|:--------------------------------------|:-----------|:-------------------------|:--------|:--------------|
16+
| 3.9.20 (main, Oct 3 2024, 02:24:59) | 0.0.19 | 4.10.0.84 | 2.0.2 | 0.19.1 |
17+
| [Clang 14.0.6 ] | | | | |
18+
19+
## Performance (images/second)
20+
21+
| | albucore | lut | opencv | numpy | simsimd |
22+
|:--------------|:---------------|:------|:--------------|:---------------|:----------|
23+
| MultiplyArray | 485.21 ± 13.04 | N/A | 511.79 ± 3.88 | 446.52 ± 32.21 | N/A |

benchmark/albucore_benchmark/results/float32_1/MultiplyConstant.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ Number of images: 500
1818

1919
## Performance (images/second)
2020

21-
| | albucore | lut | opencv | numpy | simsimd |
22-
|:-----------------|:-----------------|:------|:----------------|:-----------------|:-----------------|
23-
| MultiplyConstant | 1867.99 ± 277.36 | N/A | 1473.50 ± 46.66 | 1910.56 ± 184.89 | 1595.79 ± 156.64 |
21+
| | albucore | lut | opencv | numpy | simsimd |
22+
|:-----------------|:----------------|:------|:----------------|:----------------|:-----------------|
23+
| MultiplyConstant | 1878.75 ± 49.01 | N/A | 1416.15 ± 79.34 | 1840.54 ± 78.93 | 1525.54 ± 242.47 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Benchmark Results: MultiplyVector
2+
3+
Number of images: 500
4+
5+
## CPU Information
6+
7+
- CPU: Apple M1 Pro
8+
- Frequency: Current: 3228.00 MHz, Min: 600.00 MHz, Max: 3228.00 MHz
9+
- Physical cores: 10
10+
- Total cores: 10
11+
12+
## Package Versions
13+
14+
| Python | albucore | opencv-python-headless | numpy | torchvision |
15+
|:--------------------------------------|:-----------|:-------------------------|:--------|:--------------|
16+
| 3.9.20 (main, Oct 3 2024, 02:24:59) | 0.0.19 | 4.10.0.84 | 2.0.2 | 0.19.1 |
17+
| [Clang 14.0.6 ] | | | | |
18+
19+
## Performance (images/second)
20+
21+
| | albucore | lut | opencv | numpy | simsimd |
22+
|:---------------|:----------------|:------|:----------------|:----------------|:----------|
23+
| MultiplyVector | 1815.01 ± 79.45 | N/A | 1719.36 ± 94.23 | 1882.30 ± 56.89 | N/A |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Benchmark Results: Normalize
2+
3+
Number of images: 500
4+
5+
## CPU Information
6+
7+
- CPU: Apple M1 Pro
8+
- Frequency: Current: 3228.00 MHz, Min: 600.00 MHz, Max: 3228.00 MHz
9+
- Physical cores: 10
10+
- Total cores: 10
11+
12+
## Package Versions
13+
14+
| Python | albucore | opencv-python-headless | numpy | torchvision |
15+
|:--------------------------------------|:-----------|:-------------------------|:--------|:--------------|
16+
| 3.9.20 (main, Oct 3 2024, 02:24:59) | 0.0.19 | 4.10.0.84 | 2.0.2 | 0.19.1 |
17+
| [Clang 14.0.6 ] | | | | |
18+
19+
## Performance (images/second)
20+
21+
| | albucore | lut | opencv | numpy | simsimd |
22+
|:----------|:-----------------|:------|:---------------|:----------------|:----------|
23+
| Normalize | 1368.75 ± 159.20 | N/A | 490.46 ± 60.55 | 801.62 ± 112.00 | N/A |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Benchmark Results: NormalizeMinMax
2+
3+
Number of images: 500
4+
5+
## CPU Information
6+
7+
- CPU: Apple M1 Pro
8+
- Frequency: Current: 3228.00 MHz, Min: 600.00 MHz, Max: 3228.00 MHz
9+
- Physical cores: 10
10+
- Total cores: 10
11+
12+
## Package Versions
13+
14+
| Python | albucore | opencv-python-headless | numpy | torchvision |
15+
|:--------------------------------------|:-----------|:-------------------------|:--------|:--------------|
16+
| 3.9.20 (main, Oct 3 2024, 02:24:59) | 0.0.19 | 4.10.0.84 | 2.0.2 | 0.19.1 |
17+
| [Clang 14.0.6 ] | | | | |
18+
19+
## Performance (images/second)
20+
21+
| | albucore | lut | opencv | numpy | simsimd |
22+
|:----------------|:-----------------|:------|:----------------|:----------------|:----------|
23+
| NormalizeMinMax | 3388.75 ± 168.43 | N/A | 1534.00 ± 43.98 | 1626.95 ± 76.15 | N/A |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Benchmark Results: NormalizeMinMaxPerChannel
2+
3+
Number of images: 500
4+
5+
## CPU Information
6+
7+
- CPU: Apple M1 Pro
8+
- Frequency: Current: 3228.00 MHz, Min: 600.00 MHz, Max: 3228.00 MHz
9+
- Physical cores: 10
10+
- Total cores: 10
11+
12+
## Package Versions
13+
14+
| Python | albucore | opencv-python-headless | numpy | torchvision |
15+
|:--------------------------------------|:-----------|:-------------------------|:--------|:--------------|
16+
| 3.9.20 (main, Oct 3 2024, 02:24:59) | 0.0.19 | 4.10.0.84 | 2.0.2 | 0.19.1 |
17+
| [Clang 14.0.6 ] | | | | |
18+
19+
## Performance (images/second)
20+
21+
| | albucore | lut | opencv | numpy | simsimd |
22+
|:--------------------------|:-----------------|:------|:-----------------|:----------------|:----------|
23+
| NormalizeMinMaxPerChannel | 3393.35 ± 254.25 | N/A | 1313.99 ± 276.22 | 1623.50 ± 73.98 | N/A |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Benchmark Results: NormalizePerImage
2+
3+
Number of images: 500
4+
5+
## CPU Information
6+
7+
- CPU: Apple M1 Pro
8+
- Frequency: Current: 3228.00 MHz, Min: 600.00 MHz, Max: 3228.00 MHz
9+
- Physical cores: 10
10+
- Total cores: 10
11+
12+
## Package Versions
13+
14+
| Python | albucore | opencv-python-headless | numpy | torchvision |
15+
|:--------------------------------------|:-----------|:-------------------------|:--------|:--------------|
16+
| 3.9.20 (main, Oct 3 2024, 02:24:59) | 0.0.19 | 4.10.0.84 | 2.0.2 | 0.19.1 |
17+
| [Clang 14.0.6 ] | | | | |
18+
19+
## Performance (images/second)
20+
21+
| | albucore | lut | opencv | numpy | simsimd |
22+
|:------------------|:---------------|:------|:---------------|:----------------|:----------|
23+
| NormalizePerImage | 953.93 ± 12.09 | N/A | 681.00 ± 45.06 | 784.77 ± 145.08 | N/A |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Benchmark Results: NormalizePerImagePerChannel
2+
3+
Number of images: 500
4+
5+
## CPU Information
6+
7+
- CPU: Apple M1 Pro
8+
- Frequency: Current: 3228.00 MHz, Min: 600.00 MHz, Max: 3228.00 MHz
9+
- Physical cores: 10
10+
- Total cores: 10
11+
12+
## Package Versions
13+
14+
| Python | albucore | opencv-python-headless | numpy | torchvision |
15+
|:--------------------------------------|:-----------|:-------------------------|:--------|:--------------|
16+
| 3.9.20 (main, Oct 3 2024, 02:24:59) | 0.0.19 | 4.10.0.84 | 2.0.2 | 0.19.1 |
17+
| [Clang 14.0.6 ] | | | | |
18+
19+
## Performance (images/second)
20+
21+
| | albucore | lut | opencv | numpy | simsimd |
22+
|:----------------------------|:----------------|:------|:---------------|:---------------|:----------|
23+
| NormalizePerImagePerChannel | 917.51 ± 122.85 | N/A | 681.85 ± 29.76 | 787.58 ± 97.78 | N/A |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Benchmark Results: PowerConstant
2+
3+
Number of images: 500
4+
5+
## CPU Information
6+
7+
- CPU: Apple M1 Pro
8+
- Frequency: Current: 3228.00 MHz, Min: 600.00 MHz, Max: 3228.00 MHz
9+
- Physical cores: 10
10+
- Total cores: 10
11+
12+
## Package Versions
13+
14+
| Python | albucore | opencv-python-headless | numpy | torchvision |
15+
|:--------------------------------------|:-----------|:-------------------------|:--------|:--------------|
16+
| 3.9.20 (main, Oct 3 2024, 02:24:59) | 0.0.19 | 4.10.0.84 | 2.0.2 | 0.19.1 |
17+
| [Clang 14.0.6 ] | | | | |
18+
19+
## Performance (images/second)
20+
21+
| | albucore | lut | opencv | numpy | simsimd |
22+
|:--------------|:----------------|:------|:----------------|:---------------|:----------|
23+
| PowerConstant | 1186.78 ± 22.07 | N/A | 1159.67 ± 88.09 | 626.00 ± 47.36 | N/A |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Benchmark Results: ToFloat
2+
3+
Number of images: 500
4+
5+
## CPU Information
6+
7+
- CPU: Apple M1 Pro
8+
- Frequency: Current: 3228.00 MHz, Min: 600.00 MHz, Max: 3228.00 MHz
9+
- Physical cores: 10
10+
- Total cores: 10
11+
12+
## Package Versions
13+
14+
| Python | albucore | opencv-python-headless | numpy | torchvision |
15+
|:--------------------------------------|:-----------|:-------------------------|:--------|:--------------|
16+
| 3.9.20 (main, Oct 3 2024, 02:24:59) | 0.0.19 | 4.10.0.84 | 2.0.2 | 0.19.1 |
17+
| [Clang 14.0.6 ] | | | | |
18+
19+
## Performance (images/second)
20+
21+
| | albucore | lut | opencv | numpy | simsimd |
22+
|:--------|-----------:|------:|---------:|--------:|----------:|
23+
| ToFloat | nan | nan | nan | nan | nan |

0 commit comments

Comments
 (0)