From 0670bc1ed38a8680954b38b52baf93f47f7e2278 Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Mon, 3 Jun 2024 11:17:48 +0100 Subject: [PATCH 1/2] 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 2/2] 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")