From c0d048a4b7f3119e7ff962a9c1a259bcacbfb9e0 Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Fri, 31 May 2024 10:25:10 +0100 Subject: [PATCH 1/4] test against numpy 2.0 --- .github/scripts/unittest.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/scripts/unittest.sh b/.github/scripts/unittest.sh index da8a06928ea..673a068c04b 100755 --- a/.github/scripts/unittest.sh +++ b/.github/scripts/unittest.sh @@ -7,6 +7,10 @@ set -euo pipefail # Activate conda environment eval "$($(which conda) shell.bash hook)" && conda deactivate && conda activate ci +echo '::group::Install numpy nightly' +pip install "numpy==2.0.0rc2" +python -c "import numpy; print(numpy.__version__)" + echo '::group::Install testing utilities' # TODO: remove the <8 constraint on pytest when https://github.com/pytorch/vision/issues/8238 is closed pip install --progress-bar=off "pytest<8" pytest-mock pytest-cov expecttest!=0.2.0 requests From 0670bc1ed38a8680954b38b52baf93f47f7e2278 Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Mon, 3 Jun 2024 11:17:48 +0100 Subject: [PATCH 2/4] Make adjust_hue() work with numpy 2.0 --- torchvision/transforms/_functional_pil.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/torchvision/transforms/_functional_pil.py b/torchvision/transforms/_functional_pil.py index 277848224ac..46f928f541a 100644 --- a/torchvision/transforms/_functional_pil.py +++ b/torchvision/transforms/_functional_pil.py @@ -109,9 +109,13 @@ def adjust_hue(img: Image.Image, hue_factor: float) -> Image.Image: h, s, v = img.convert("HSV").split() np_h = np.array(h, dtype=np.uint8) - # uint8 addition take cares of rotation across boundaries with np.errstate(over="ignore"): - np_h += np.uint8(hue_factor * 255) + shift = int(hue_factor * 255) + if shift > 0: + np_h += shift + else: + np_h -= abs(shift) + h = Image.fromarray(np_h, "L") img = Image.merge("HSV", (h, s, v)).convert(input_mode) From fb4fb59d6acb154361d93828aa36c68eacb7d0c9 Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Mon, 3 Jun 2024 11:33:00 +0100 Subject: [PATCH 3/4] Use better way from warning --- torchvision/transforms/_functional_pil.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/torchvision/transforms/_functional_pil.py b/torchvision/transforms/_functional_pil.py index 46f928f541a..527879bb6f1 100644 --- a/torchvision/transforms/_functional_pil.py +++ b/torchvision/transforms/_functional_pil.py @@ -109,12 +109,8 @@ def adjust_hue(img: Image.Image, hue_factor: float) -> Image.Image: h, s, v = img.convert("HSV").split() np_h = np.array(h, dtype=np.uint8) - with np.errstate(over="ignore"): - shift = int(hue_factor * 255) - if shift > 0: - np_h += shift - else: - np_h -= abs(shift) + # This will over/underflow, as desired + np_h += np.array(hue_factor * 255).astype(np.uint8) h = Image.fromarray(np_h, "L") From 569dad761923172f6ed584eb48db10553b18f1ca Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Mon, 3 Jun 2024 13:39:13 +0100 Subject: [PATCH 4/4] empty