Skip to content

Add v2 docs for color transforms #7310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/source/transforms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ Color

ColorJitter
v2.ColorJitter
v2.RandomPhotometricDistort
Grayscale
v2.Grayscale
RandomGrayscale
Expand Down
43 changes: 34 additions & 9 deletions torchvision/transforms/v2/_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def _transform(self, inpt: Any, params: Dict[str, Any]) -> Any:


class RandomGrayscale(_RandomApplyTransform):
"""[BETA] Randomly convert image to grayscale with a probability of p (default 0.1).
"""[BETA] Randomly convert image or videos to grayscale with a probability of p (default 0.1).

.. betastatus:: RandomGrayscale transform

Expand Down Expand Up @@ -85,7 +85,7 @@ def _transform(self, inpt: Any, params: Dict[str, Any]) -> Any:


class ColorJitter(Transform):
"""[BETA] Randomly change the brightness, contrast, saturation and hue of an image.
"""[BETA] Randomly change the brightness, contrast, saturation and hue of an image or video.

.. betastatus:: ColorJitter transform

Expand Down Expand Up @@ -190,6 +190,31 @@ def _transform(self, inpt: Any, params: Dict[str, Any]) -> Any:

# TODO: This class seems to be untested
class RandomPhotometricDistort(Transform):
"""[BETA] Randomly distorts the image or video as used in `SSD: Single Shot
MultiBox Detector <https://arxiv.org/abs/1512.02325>`_.

.. betastatus:: RandomPhotometricDistort transform

This transform relies on :class:`~torchvision.transforms.v2.ColorJitter`
under the hood to adjust the contrast, saturation, hue, brightness, and also
randomly permutes channels.

Args:
brightness (tuple of float (min, max), optional): How much to jitter brightness.
brightness_factor is chosen uniformly from [min, max]. Should be non negative numbers.
contrast tuple of float (min, max), optional): How much to jitter contrast.
contrast_factor is chosen uniformly from [min, max]. Should be non-negative numbers.
saturation (tuple of float (min, max), optional): How much to jitter saturation.
saturation_factor is chosen uniformly from [min, max]. Should be non negative numbers.
hue (tuple of float (min, max), optional): How much to jitter hue.
hue_factor is chosen uniformly from [min, max]. Should have -0.5 <= min <= max <= 0.5.
To jitter hue, the pixel values of the input image has to be non-negative for conversion to HSV space;
thus it does not work if you normalize your image to an interval with negative values,
or use an interpolation that generates negative values before using this function.
p (float, optional) probability each distortion operation (contrast, saturation, ...) to be applied.
Default is 0.5.
"""

_transformed_types = (
datapoints.Image,
PIL.Image.Image,
Expand All @@ -199,10 +224,10 @@ class RandomPhotometricDistort(Transform):

def __init__(
self,
brightness: Tuple[float, float] = (0.875, 1.125),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just to align the signature with the logic below, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes sorry I forgot to comment - this is to align with the signature of ColorJitter

contrast: Tuple[float, float] = (0.5, 1.5),
saturation: Tuple[float, float] = (0.5, 1.5),
hue: Tuple[float, float] = (-0.05, 0.05),
brightness: Tuple[float, float] = (0.875, 1.125),
p: float = 0.5,
):
super().__init__()
Expand Down Expand Up @@ -266,7 +291,7 @@ def _transform(


class RandomEqualize(_RandomApplyTransform):
"""[BETA] Equalize the histogram of the given image randomly with a given probability.
"""[BETA] Equalize the histogram of the given image or video with a given probability.

.. betastatus:: RandomEqualize transform

Expand All @@ -285,7 +310,7 @@ def _transform(self, inpt: Any, params: Dict[str, Any]) -> Any:


class RandomInvert(_RandomApplyTransform):
"""[BETA] Inverts the colors of the given image randomly with a given probability.
"""[BETA] Inverts the colors of the given image or video with a given probability.

.. betastatus:: RandomInvert transform

Expand All @@ -304,7 +329,7 @@ def _transform(self, inpt: Any, params: Dict[str, Any]) -> Any:


class RandomPosterize(_RandomApplyTransform):
"""[BETA] Posterize the image randomly with a given probability by reducing the
"""[BETA] Posterize the image or video with a given probability by reducing the
number of bits for each color channel.

.. betastatus:: RandomPosterize transform
Expand All @@ -329,7 +354,7 @@ def _transform(self, inpt: Any, params: Dict[str, Any]) -> Any:


class RandomSolarize(_RandomApplyTransform):
"""[BETA] Solarize the image randomly with a given probability by inverting all pixel
"""[BETA] Solarize the image or video with a given probability by inverting all pixel
values above a threshold.

.. betastatus:: RandomSolarize transform
Expand All @@ -354,7 +379,7 @@ def _transform(self, inpt: Any, params: Dict[str, Any]) -> Any:


class RandomAutocontrast(_RandomApplyTransform):
"""[BETA] Autocontrast the pixels of the given image randomly with a given probability.
"""[BETA] Autocontrast the pixels of the given image or video with a given probability.

.. betastatus:: RandomAutocontrast transform

Expand All @@ -373,7 +398,7 @@ def _transform(self, inpt: Any, params: Dict[str, Any]) -> Any:


class RandomAdjustSharpness(_RandomApplyTransform):
"""[BETA] Adjust the sharpness of the image randomly with a given probability.
"""[BETA] Adjust the sharpness of the image or video with a given probability.

.. betastatus:: RandomAdjustSharpness transform

Expand Down