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

Implement Numpy 2.0 support in Python bindings #25455

Open
asmorkalov opened this issue Apr 19, 2024 · 4 comments
Open

Implement Numpy 2.0 support in Python bindings #25455

asmorkalov opened this issue Apr 19, 2024 · 4 comments

Comments

@asmorkalov
Copy link
Contributor

Describe the feature and motivation

Note that numpy 2.0.0rc1 is now publicly available: numpy/numpy#24300 (comment)

Additional context

No response

@mshabunin
Copy link
Contributor

I've checked builds with recent NumPy versions on Ubuntu 22, it seems that with 2.x everything just works 🙂 See my experimental repository: https://github.com/mshabunin/opencv-numpy-check

The only modification I made was in Python tests - NumPy 2.x doesn't support float_:

diff --git a/modules/python/test/test_misc.py b/modules/python/test/test_misc.py
index bcd3152699..08ab04d53d 100644
--- a/modules/python/test/test_misc.py
+++ b/modules/python/test/test_misc.py
@@ -451,7 +451,7 @@ class Arguments(NewOpenCVTests):
         try_to_convert = partial(self._try_to_convert, cv.utils.dumpFloat)
         min_float, max_float = get_limits(ctypes.c_float)
         for convertible in (2, -13, 1.24, np.float32(32.45), float(32), np.double(12.23),
-                            np.float32(-12.3), np.float64(3.22), np.float_(-1.5), min_float,
+                            np.float32(-12.3), np.float64(3.22), min_float,
                             max_float, np.inf, -np.inf, float('Inf'), -float('Inf'),
                             np.double(np.inf), np.double(-np.inf), np.double(float('Inf')),
                             np.double(-float('Inf'))):
@@ -495,7 +495,7 @@ class Arguments(NewOpenCVTests):
         min_float, max_float = get_limits(ctypes.c_float)
         min_double, max_double = get_limits(ctypes.c_double)
         for convertible in (2, -13, 1.24, np.float32(32.45), float(2), np.double(12.23),
-                            np.float32(-12.3), np.float64(3.22), np.float_(-1.5), min_float,
+                            np.float32(-12.3), np.float64(3.22), min_float,
                             max_float, min_double, max_double, np.inf, -np.inf, float('Inf'),
                             -float('Inf'), np.double(np.inf), np.double(-np.inf),
                             np.double(float('Inf')), np.double(-float('Inf'))):

OpenCV built with NumPy 2.x works with NumPy 1.x (not vice versa). I haven't checked NumPy versions older than 1.21.5 though. See https://github.com/mshabunin/opencv-numpy-check/blob/d2e5f9b4ccd7c689263b5992c5036b60a2c716b6/run.sh#L44-L48

@asmorkalov
Copy link
Contributor Author

@what about backward compatibility with 1.x? D we need extra opencv-python packages for it?

@mshabunin
Copy link
Contributor

Theoretically builds with 2.x should work with 1.x too, our python tests pass with this configuration and this is what NumPy developers recommend (run OpenCV built with 1.x in environment with NumPy 2.x):

ImportError: 
A module that was compiled using NumPy 1.x cannot be run in
NumPy 2.0.0rc1 as it may crash. To support both 1.x and 2.x
versions of NumPy, modules must be compiled with NumPy 2.0.
Some module may need to rebuild instead e.g. with 'pybind11>=2.12'.

If you are a user of the module, the easiest solution will be to
downgrade to 'numpy<2' or try to upgrade the affected module.
We expect that some modules will need time to support NumPy 2.

I think that we should:

  • produce experimental builds with NumPy 2.0.0rc1
  • perform thorough compatibility checks on platforms supported by opencv-python (up to oldest NumPy available or the one we currenly use for binary packages)
  • wait for official 2.0.0 release and switch to it

@mshabunin
Copy link
Contributor

Created #25541 with test fixes for 2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants