Skip to content

Commit a4db799

Browse files
leoll2Copilot
andauthored
Upgrade numpy to 2.x in OTX (#4970)
Co-authored-by: Copilot <[email protected]>
1 parent 5f22cf8 commit a4db799

File tree

11 files changed

+116
-95
lines changed

11 files changed

+116
-95
lines changed

library/pyproject.toml

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ dependencies = [
5151
"onnxscript==0.5.3",
5252
"nncf==2.17.0",
5353
"anomalib[core]==1.1.3",
54-
"numpy<2.0",
54+
"imgaug==0.4.0", # for anomalib
55+
"numpy>2.0",
5556
"tensorboardx==2.6.4"
5657
]
5758

@@ -114,6 +115,14 @@ otx = "otx.cli:main"
114115
Documentation = "https://open-edge-platform.github.io/training_extensions/"
115116
Repository = "https://github.com/open-edge-platform/training_extensions/"
116117

118+
[tool.uv]
119+
conflicts = [
120+
[
121+
{ extra = "xpu" },
122+
{ extra = "cuda" },
123+
],
124+
]
125+
117126
[tool.uv.sources]
118127
torch = [
119128
{ index = "torch-xpu", extra = "xpu"},
@@ -126,6 +135,10 @@ torchvision = [
126135
{ index = "torch-xpu", extra = "xpu"},
127136
{ index = "torch-cuda", extra = "cuda"},
128137
]
138+
# Anomalib depends on imgaug, which is abandoned and depends on an old version of numpy.
139+
# To avoid dependency resolution issues, we install imgaug from a maintained fork.
140+
# For details: https://github.com/aleju/imgaug/issues/859
141+
imgaug = { git = "https://github.com/imaug/imaug/", rev = "a7ca1b04d44700d075a23f14a97f86285ba3b33f" }
129142

130143
[[tool.uv.index]]
131144
name = "torch-cuda"
@@ -285,12 +298,6 @@ ignore = [
285298

286299
"E731", # Do not assign a `lambda` expression, use a `def`
287300
"TD003", # Missing issue link on the line following this TODO
288-
289-
# TODO (someone): Update legacy np.random.{method_name} call with np.random.Generator
290-
# Legacy (MT19964) and new (PCG64) random generators use different pseudo-random number generators.
291-
# (https://numpy.org/doc/stable/reference/random/legacy.html#legacy)
292-
# For now, remaining legacy random methods is required until numpy version is explicitly updated in OTX.
293-
"NPY002", # Replace legacy np.random.{method_name} call with np.random.Generator
294301
]
295302

296303
# Allow autofix for all enabled rules (when `--fix`) is provided.

library/src/otx/backend/native/exporter/exportable_code/demo/demo_package/visualizers/visualizer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,12 @@ def __init__(
204204
self.color_map = self._create_color_map()
205205

206206
def _create_color_map(self) -> np.ndarray:
207+
rng = np.random.default_rng(42)
207208
classes = self.color_palette[:, ::-1] # RGB to BGR
208209
color_map = np.zeros((256, 1, 3), dtype=np.uint8)
209210
classes_num = len(classes)
210211
color_map[:classes_num, 0, :] = classes
211-
color_map[classes_num:, 0, :] = np.random.uniform(0, 255, size=(256 - classes_num, 3))
212+
color_map[classes_num:, 0, :] = rng.uniform(0, 255, size=(256 - classes_num, 3))
212213
return color_map
213214

214215
def _apply_color_map(self, input_2d_mask: np.ndarray) -> np.ndarray:

library/src/otx/backend/native/models/detection/detectors/detection_transformer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def __init__(
6767
)
6868
)
6969
self.optimizer_configuration = optimizer_configuration
70+
self.rng = np.random.default_rng(42)
7071

7172
def generate_scales(self, input_size: int, base_size_repeat: int = 3) -> list[int]:
7273
"""Generates scales for multi-scale training."""
@@ -84,7 +85,7 @@ def _forward_features(self, images: Tensor, targets: dict[str, Any] | None = Non
8485
def forward(self, images: Tensor, targets: dict[str, Any] | None = None) -> dict[str, Tensor] | Tensor:
8586
"""Forward pass of the model."""
8687
if self.multi_scale and self.training:
87-
sz = int(np.random.choice(self.multi_scale))
88+
sz = int(self.rng.choice(self.multi_scale))
8889
images = nn.functional.interpolate(images, size=[sz, sz])
8990

9091
output = self._forward_features(images, targets)

0 commit comments

Comments
 (0)