Follow-up fixes for numpy2 compatibility - #3508
Merged
Merged
Conversation
Add NumPy 2.0 compatibility for PyTorch snapshot loading by registering the correct scalar reconstruction helper and keeping NumPy float metrics loadable without changing weights_only. If registration fails, emit a warning explaining the fallback and the impact on snapshot loading.
Add explicit float typing and return values for `Assembly.affinity`, and cast the affinity value before storing it in PyTorch pose tensors. This keeps the inference path type-consistent and avoids integer zero values when no links are present.
Expand the snapshot-loading helper docstring to explain why NumPy reconstructors must be allowlisted for legacy metrics, note the NumPy version split more clearly, and mention that newer Torch releases may make the workaround unnecessary.
- opencv was used but never declared as a dependency - numba .054 is not compatible with our numpy lower bound now
Correct the `numba` dependency in `pyproject.toml` from `>=0.6` to `>=0.60` so the declared minimum version matches the intended release series.
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Three moderate review findings remain unresolved.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR improves NumPy 2 compatibility for inference, snapshot loading, and dependencies.
Changes:
- Normalizes assembly affinities to Python floats.
- Adds NumPy 1.x/2.x safe-global import support.
- Updates Numba, OpenCV, and lockfile dependencies.
- Adds affinity regression tests.
File summaries
| File | Summary |
|---|---|
uv.lock |
Updates locked dependency metadata. |
tests/pose_estimation_pytorch/models/predictors/test_paf.py |
Adds affinity regression tests; moderate finding (3 votes): the test does not reproduce the production failure. |
pyproject.toml |
Updates Numba and OpenCV requirements; moderate finding (1 vote): OpenCV providers may conflict. |
deeplabcut/pose_estimation_pytorch/runners/base.py |
Adds NumPy import fallback; moderate finding (1 vote): missing focused regression coverage. |
deeplabcut/pose_estimation_pytorch/models/predictors/paf_predictor.py |
Safely assigns affinity values to tensors. |
deeplabcut/core/inferenceutils.py |
Normalizes Assembly.affinity to float. |
Review details
Suppressed comments (2)
deeplabcut/pose_estimation_pytorch/runners/base.py:227
- The new NumPy 2 import path and fallback are the compatibility fix, but the runner tests do not exercise either path or verify that a legacy snapshot containing a NumPy scalar loads with
weights_only=True. Add a focused regression test for the supported NumPy paths and the safe-global registration so this import can’t silently regress.
try:
from numpy._core.multiarray import scalar # NumPy >= 2.0
except ImportError:
from numpy.core.multiarray import scalar # NumPy < 2.0
pyproject.toml:44
opencv-python-headlessandopencv-pythoninstall the samecv2package, but thetfextras pullopencv-pythonthroughimgaugandfmpose3dpulls it directly (as reflected inuv.lock). Installingdeeplabcut[tf]or[fmpose3d]therefore resolves both variants, making the resulting OpenCV build depend on installation order; make the providers mutually exclusive or standardize on one variant and regenerate the lockfile.
"opencv-python-headless>=4.10.0.84",
- Files reviewed: 5/6 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
C-Achard
marked this pull request as ready for review
September 11, 2026 10:28
This was referenced Sep 11, 2026
2 tasks
deruyter92
added this pull request to stack #3515
September 14, 2026 08:52
deruyter92
approved these changes
Sep 14, 2026
deruyter92
left a comment
Collaborator
There was a problem hiding this comment.
Minor suggestions. Thanks for the fix!!
Co-authored-by: Jaap de Ruyter van Steveninck <32810691+deruyter92@users.noreply.github.com>
deruyter92
removed this pull request from stack #3515
September 14, 2026 11:38
deruyter92
added this pull request to stack #3516
September 14, 2026 11:44
deruyter92
added a commit
that referenced
this pull request
Sep 14, 2026
C-Achard
added a commit
that referenced
this pull request
Sep 14, 2026
* release upper bounds for numpy and matplotlib * keep np upper bound for tensorflow extras * add numpy-1 to canary lane * Fix intelligent testing matrix JSON Add the missing comma between the `matplotlib` and `numpy-1` matrix entries in `.github/workflows/intelligent-testing.yml` so the GitHub Actions matrix parses correctly. * Expand intelligent test matrix Broaden the intelligent-testing workflow to include strict dependency-resolution checks for Python 3.10 and 3.12, plus NumPy 1/2 and Matplotlib upgrade variants. This improves compatibility coverage and validates resolver behavior beyond the previous narrower matrix. * Adjust matplotlib and TF numpy constraints Restore exclusion of matplotlib 3.7 and 3.7.1 from the base dependencies, and drop the redundant numpy cap from the `tf-cu12` extra because TensorFlow 2.18 already declares the required numpy range. * Update uv.lock * Fix missing ceiling in tf * Update uv.lock * update pyproject.toml and uv.lock. Only declare np upperbound when required * Restore numba/opencv floors from #3508 after restack --------- Co-authored-by: C-Achard <cyril.achard@epfl.ch>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Preparation for #3499, which removes the numpy<2 and matplotlib<3.9 upper bounds.
Changes
Without, poses[i, j, :, 4] = assembly.affinity raises TypeError: can't assign a numpy.float32 to a torch.FloatTensor under NumPy 2, on the default multi-animal inference path.
numpy.core became numpy._core in NumPy 2, so the import failed. Now tries both module paths and logs a warning.
numba 0.54 requires numpy<1.21, below our own numpy>=1.22.4. 0.60 is also the first release supporting NumPy 2.
cv2 is imported across deeplabcut/ but was not declared, it arrived only transitively via albumentations, imgaug and fmpose3d, so which build you got depended on which extras you installed.