From 7c72e7c161b3a1298d7e0e426d75c0c9a9332df2 Mon Sep 17 00:00:00 2001 From: Rob Luke Date: Mon, 21 Jul 2025 11:28:39 +1000 Subject: [PATCH 1/5] Update RandomErasing value type and doc string --- torchvision/transforms/v2/_augment.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/torchvision/transforms/v2/_augment.py b/torchvision/transforms/v2/_augment.py index c6da9aba98b..d91c2fba203 100644 --- a/torchvision/transforms/v2/_augment.py +++ b/torchvision/transforms/v2/_augment.py @@ -25,8 +25,8 @@ class RandomErasing(_RandomApplyTransform): p (float, optional): probability that the random erasing operation will be performed. scale (tuple of float, optional): range of proportion of erased area against input image. ratio (tuple of float, optional): range of aspect ratio of erased area. - value (number or tuple of numbers): erasing value. Default is 0. If a single int, it is used to - erase all pixels. If a tuple of length 3, it is used to erase + value (number, str, or tuple of numbers): erasing value. Default is 0. If a single int, + it is used to erase all pixels. If a tuple of length 3, it is used to erase R, G, B channels respectively. If a str of 'random', erasing each pixel with random values. inplace (bool, optional): boolean to make this transform inplace. Default set to False. @@ -59,7 +59,7 @@ def __init__( p: float = 0.5, scale: Sequence[float] = (0.02, 0.33), ratio: Sequence[float] = (0.3, 3.3), - value: float = 0.0, + value: Union[float, int, str, Sequence[Union[float, int]]] = 0.0, inplace: bool = False, ): super().__init__(p=p) From b09b21d9ee680fee217762460a12a277ec160da6 Mon Sep 17 00:00:00 2001 From: Robert Luke Date: Thu, 24 Jul 2025 10:36:47 +1000 Subject: [PATCH 2/5] lint: fix ufmt error --- torchvision/transforms/v2/_augment.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/torchvision/transforms/v2/_augment.py b/torchvision/transforms/v2/_augment.py index d91c2fba203..e2de589dea2 100644 --- a/torchvision/transforms/v2/_augment.py +++ b/torchvision/transforms/v2/_augment.py @@ -25,7 +25,7 @@ class RandomErasing(_RandomApplyTransform): p (float, optional): probability that the random erasing operation will be performed. scale (tuple of float, optional): range of proportion of erased area against input image. ratio (tuple of float, optional): range of aspect ratio of erased area. - value (number, str, or tuple of numbers): erasing value. Default is 0. If a single int, + value (number, str, or tuple of numbers): erasing value. Default is 0. If a single int, it is used to erase all pixels. If a tuple of length 3, it is used to erase R, G, B channels respectively. If a str of 'random', erasing each pixel with random values. From cf8c1d7df45c21498606074243f6d6b50b7d4112 Mon Sep 17 00:00:00 2001 From: Robert Luke Date: Thu, 24 Jul 2025 10:54:39 +1000 Subject: [PATCH 3/5] Add type hinting --- torchvision/transforms/v2/_augment.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/torchvision/transforms/v2/_augment.py b/torchvision/transforms/v2/_augment.py index e2de589dea2..d1a2a24acee 100644 --- a/torchvision/transforms/v2/_augment.py +++ b/torchvision/transforms/v2/_augment.py @@ -2,7 +2,7 @@ import numbers import warnings from collections.abc import Sequence -from typing import Any, Callable, Optional, Union +from typing import Any, Callable, Optional, Union, List import PIL.Image import torch @@ -78,7 +78,7 @@ def __init__( self.scale = scale self.ratio = ratio if isinstance(value, (int, float)): - self.value = [float(value)] + self.value: Optional[List[float]] = [float(value)] elif isinstance(value, str): self.value = None elif isinstance(value, (list, tuple)): From 7f66d9750e4e12db75ec2433b485407cd3e63b12 Mon Sep 17 00:00:00 2001 From: Robert Luke Date: Thu, 24 Jul 2025 10:55:39 +1000 Subject: [PATCH 4/5] ufmt --- torchvision/transforms/v2/_augment.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/torchvision/transforms/v2/_augment.py b/torchvision/transforms/v2/_augment.py index d1a2a24acee..4155da209fd 100644 --- a/torchvision/transforms/v2/_augment.py +++ b/torchvision/transforms/v2/_augment.py @@ -2,7 +2,7 @@ import numbers import warnings from collections.abc import Sequence -from typing import Any, Callable, Optional, Union, List +from typing import Any, Callable, List, Optional, Union import PIL.Image import torch From 3364931b9204d941fb0aee383ed125facab686c6 Mon Sep 17 00:00:00 2001 From: Robert Luke Date: Thu, 24 Jul 2025 11:11:32 +1000 Subject: [PATCH 5/5] Cleaner version --- torchvision/transforms/v2/_augment.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/torchvision/transforms/v2/_augment.py b/torchvision/transforms/v2/_augment.py index 4155da209fd..52be3c6abb9 100644 --- a/torchvision/transforms/v2/_augment.py +++ b/torchvision/transforms/v2/_augment.py @@ -2,7 +2,7 @@ import numbers import warnings from collections.abc import Sequence -from typing import Any, Callable, List, Optional, Union +from typing import Any, Callable, List, Literal, Optional, Union import PIL.Image import torch @@ -46,6 +46,8 @@ class RandomErasing(_RandomApplyTransform): >>> ]) """ + value: Optional[List[float]] + _v1_transform_cls = _transforms.RandomErasing def _extract_params_for_v1_transform(self) -> dict[str, Any]: @@ -59,7 +61,7 @@ def __init__( p: float = 0.5, scale: Sequence[float] = (0.02, 0.33), ratio: Sequence[float] = (0.3, 3.3), - value: Union[float, int, str, Sequence[Union[float, int]]] = 0.0, + value: Union[float, int, Literal["random"], Sequence[Union[float, int]]] = 0.0, inplace: bool = False, ): super().__init__(p=p) @@ -78,7 +80,7 @@ def __init__( self.scale = scale self.ratio = ratio if isinstance(value, (int, float)): - self.value: Optional[List[float]] = [float(value)] + self.value = [float(value)] elif isinstance(value, str): self.value = None elif isinstance(value, (list, tuple)):