| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605 |
- # LICENSE HEADER MANAGED BY add-license-header
- #
- # Copyright 2018 Kornia Team
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- #
- from typing import Optional, Tuple
- import kornia.augmentation as K
- from kornia.augmentation.auto.operations.base import OperationBase
- from kornia.core import Tensor
- from kornia.grad_estimator import STEFunction
- __all__ = [
- "AutoContrast",
- "Brightness",
- "Contrast",
- "Equalize",
- "Gray",
- "HorizontalFlip",
- "Hue",
- "Invert",
- "Posterize",
- "Rotate",
- "Saturate",
- "Sharpness",
- "ShearX",
- "ShearY",
- "Solarize",
- "SolarizeAdd",
- "TranslateX",
- "TranslateY",
- "VerticalFlip",
- ]
- class AutoContrast(OperationBase):
- """Apply auto_contrast operation.
- Args:
- initial_probability: the initial probability. If None, the augmentation will be randomly
- applied according to he augmentation sampling range.
- temperature: temperature for RelaxedBernoulli distribution used during training.
- symmetric_megnitude: if to randomly assign the magnitude as negative or not.
- """
- def __init__(
- self, initial_probability: float = 0.5, temperature: float = 0.1, symmetric_megnitude: bool = False
- ) -> None:
- super().__init__(
- K.RandomAutoContrast(same_on_batch=False, p=initial_probability),
- initial_magnitude=None,
- temperature=temperature,
- symmetric_megnitude=symmetric_megnitude,
- )
- class Brightness(OperationBase):
- """Apply brightness operation.
- Args:
- initial_probability: the initial probability. If None, the augmentation will be randomly
- applied according to he augmentation sampling range.
- initial_magnitude: the initial magnitude.
- magnitude_range: the sampling range for random sampling and clamping the optimized magnitude.
- temperature: temperature for RelaxedBernoulli distribution used during training.
- symmetric_megnitude: if to randomly assign the magnitude as negative or not.
- """
- def __init__(
- self,
- initial_magnitude: Optional[float] = 0.5,
- initial_probability: float = 0.5,
- magnitude_range: Tuple[float, float] = (0.2, 1.8),
- temperature: float = 0.1,
- symmetric_megnitude: bool = False,
- ) -> None:
- super().__init__(
- K.RandomBrightness(magnitude_range, same_on_batch=False, p=initial_probability),
- initial_magnitude=[("brightness_factor", initial_magnitude)],
- temperature=temperature,
- symmetric_megnitude=symmetric_megnitude,
- )
- class Contrast(OperationBase):
- """Apply contrast operation.
- Args:
- initial_probability: the initial probability. If None, the augmentation will be randomly
- applied according to he augmentation sampling range.
- initial_magnitude: the initial magnitude.
- magnitude_range: the sampling range for random sampling and clamping the optimized magnitude.
- temperature: temperature for RelaxedBernoulli distribution used during training.
- symmetric_megnitude: if to randomly assign the magnitude as negative or not.
- """
- def __init__(
- self,
- initial_magnitude: Optional[float] = 0.5,
- initial_probability: float = 0.5,
- magnitude_range: Tuple[float, float] = (0.2, 1.8),
- temperature: float = 0.1,
- symmetric_megnitude: bool = False,
- ) -> None:
- super().__init__(
- K.RandomContrast(magnitude_range, same_on_batch=False, p=initial_probability),
- initial_magnitude=[("contrast_factor", initial_magnitude)],
- temperature=temperature,
- symmetric_megnitude=symmetric_megnitude,
- )
- class Hue(OperationBase):
- """Apply hue operation.
- Args:
- initial_probability: the initial probability. If None, the augmentation will be randomly
- applied according to he augmentation sampling range.
- initial_magnitude: the initial magnitude.
- magnitude_range: the sampling range for random sampling and clamping the optimized magnitude.
- temperature: temperature for RelaxedBernoulli distribution used during training.
- symmetric_megnitude: if to randomly assign the magnitude as negative or not.
- """
- def __init__(
- self,
- initial_magnitude: Optional[float] = 0.0,
- initial_probability: float = 0.5,
- magnitude_range: Tuple[float, float] = (-0.5, 0.5),
- temperature: float = 0.1,
- symmetric_megnitude: bool = False,
- ) -> None:
- super().__init__(
- K.RandomHue(magnitude_range, same_on_batch=False, p=initial_probability),
- initial_magnitude=[("hue_factor", initial_magnitude)],
- temperature=temperature,
- symmetric_megnitude=symmetric_megnitude,
- )
- class Saturate(OperationBase):
- """Apply saturation operation.
- Args:
- initial_probability: the initial probability. If None, the augmentation will be randomly
- applied according to he augmentation sampling range.
- initial_magnitude: the initial magnitude.
- magnitude_range: the sampling range for random sampling and clamping the optimized magnitude.
- temperature: temperature for RelaxedBernoulli distribution used during training.
- symmetric_megnitude: if to randomly assign the magnitude as negative or not.
- """
- def __init__(
- self,
- initial_magnitude: Optional[float] = 0.5,
- initial_probability: float = 0.5,
- magnitude_range: Tuple[float, float] = (0.2, 1.8),
- temperature: float = 0.1,
- symmetric_megnitude: bool = False,
- ) -> None:
- super().__init__(
- K.RandomSaturation(magnitude_range, same_on_batch=False, p=initial_probability),
- initial_magnitude=[("saturation_factor", initial_magnitude)],
- temperature=temperature,
- symmetric_megnitude=symmetric_megnitude,
- )
- # TODO: Equalize cannot update probabilities yet.
- class Equalize(OperationBase):
- """Apply equalize operation.
- Args:
- initial_probability: the initial probability. If None, the augmentation will be randomly
- applied according to he augmentation sampling range.
- temperature: temperature for RelaxedBernoulli distribution used during training.
- Note:
- Equalize cannot update probabilities yet.
- Note:
- STE gradient estimator applied for back propagation.
- """
- def __init__(self, initial_probability: float = 0.5, temperature: float = 0.1) -> None:
- super().__init__(
- K.RandomEqualize(same_on_batch=False, p=initial_probability),
- initial_magnitude=None,
- temperature=temperature,
- symmetric_megnitude=False,
- gradient_estimator=STEFunction,
- )
- class Gray(OperationBase):
- """Apply grayscale operation.
- Args:
- initial_probability: the initial probability. If None, the augmentation will be randomly
- applied according to he augmentation sampling range.
- temperature: temperature for RelaxedBernoulli distribution used during training.
- """
- def __init__(self, initial_probability: float = 0.5, temperature: float = 0.1) -> None:
- super().__init__(
- K.RandomGrayscale(same_on_batch=False, p=initial_probability),
- initial_magnitude=None,
- temperature=temperature,
- symmetric_megnitude=False,
- )
- class Invert(OperationBase):
- """Apply invert operation.
- Args:
- initial_probability: the initial probability. If None, the augmentation will be randomly
- applied according to he augmentation sampling range.
- temperature: temperature for RelaxedBernoulli distribution used during training.
- """
- def __init__(self, initial_probability: float = 0.5, temperature: float = 0.1) -> None:
- super().__init__(
- K.RandomInvert(same_on_batch=False, p=initial_probability),
- initial_magnitude=None,
- temperature=temperature,
- symmetric_megnitude=False,
- )
- class Posterize(OperationBase):
- """Apply posterize operation.
- Args:
- initial_magnitude: the initial magnitude.
- initial_probability: the initial probability. If None, the augmentation will be randomly
- applied according to he augmentation sampling range.
- magnitude_range: the sampling range for random sampling and clamping the optimized magnitude.
- temperature: temperature for RelaxedBernoulli distribution used during training.
- symmetric_megnitude: if to randomly assign the magnitude as negative or not.
- Note:
- STE gradient estimator applied for back propagation.
- """
- @staticmethod
- def _process_magnitude(magnitude: Tensor) -> Tensor:
- return magnitude.long()
- def __init__(
- self,
- initial_magnitude: Optional[float] = 4.0,
- initial_probability: float = 0.5,
- magnitude_range: Tuple[float, float] = (1.0, 8.0),
- temperature: float = 0.1,
- symmetric_megnitude: bool = False,
- ) -> None:
- super().__init__(
- K.RandomPosterize(magnitude_range, same_on_batch=False, p=initial_probability),
- initial_magnitude=[("bits_factor", initial_magnitude)],
- temperature=temperature,
- symmetric_megnitude=symmetric_megnitude,
- magnitude_fn=Posterize._process_magnitude,
- gradient_estimator=STEFunction,
- )
- class Solarize(OperationBase):
- """Apply solarize operation.
- Args:
- initial_magnitude: the initial magnitude.
- initial_probability: the initial probability. If None, the augmentation will be randomly
- applied according to he augmentation sampling range.
- magnitude_range: the sampling range for random sampling and clamping the optimized
- symmetric_megnitude: if to randomly assign the magnitude as negative or not.magnitude.
- temperature: temperature for RelaxedBernoulli distribution used during training.
- Note:
- STE gradient estimator applied for back propagation.
- """
- def __init__(
- self,
- initial_magnitude: Optional[float] = 0.5,
- initial_probability: float = 0.5,
- magnitude_range: Tuple[float, float] = (0.0, 1.0),
- temperature: float = 0.1,
- symmetric_megnitude: bool = False,
- ) -> None:
- super().__init__(
- K.RandomSolarize(magnitude_range, additions=0.0, same_on_batch=False, p=initial_probability),
- initial_magnitude=[("thresholds", initial_magnitude)],
- temperature=temperature,
- symmetric_megnitude=symmetric_megnitude,
- gradient_estimator=STEFunction,
- )
- class SolarizeAdd(OperationBase):
- """Apply solarize-addition operation with a fixed thresholds of 0.5.
- Args:
- initial_magnitude: the initial magnitude.
- initial_probability: the initial probability. If None, the augmentation will be randomly
- applied according to he augmentation sampling range.
- magnitude_range: the sampling range for random sampling and clamping the optimized magnitude.
- temperature: temperature for RelaxedBernoulli distribution used during training.
- symmetric_megnitude: if to randomly assign the magnitude as negative or not.
- Note:
- STE gradient estimator applied for back propagation.
- """
- def __init__(
- self,
- initial_magnitude: Optional[float] = 0.0,
- initial_probability: float = 0.5,
- magnitude_range: Tuple[float, float] = (-0.3, 0.3),
- temperature: float = 0.1,
- symmetric_megnitude: bool = False,
- ) -> None:
- super().__init__(
- K.RandomSolarize(thresholds=0.5, additions=magnitude_range, same_on_batch=False, p=initial_probability),
- initial_magnitude=[("additions", initial_magnitude)],
- temperature=temperature,
- symmetric_megnitude=symmetric_megnitude,
- gradient_estimator=STEFunction,
- )
- class Sharpness(OperationBase):
- """Apply sharpness operation.
- Args:
- initial_magnitude: the initial magnitude.
- initial_probability: the initial probability. If None, the augmentation will be randomly
- applied according to he augmentation sampling range.
- magnitude_range: the sampling range for random sampling and clamping the optimized magnitude.
- temperature: temperature for RelaxedBernoulli distribution used during training.
- symmetric_megnitude: if to randomly assign the magnitude as negative or not.
- """
- def __init__(
- self,
- initial_magnitude: Optional[float] = 0.5,
- initial_probability: float = 0.5,
- magnitude_range: Tuple[float, float] = (0.1, 1.9),
- temperature: float = 0.1,
- symmetric_megnitude: bool = False,
- ) -> None:
- super().__init__(
- K.RandomSharpness(magnitude_range, same_on_batch=False, p=initial_probability),
- initial_magnitude=[("sharpness", initial_magnitude)],
- temperature=temperature,
- symmetric_megnitude=symmetric_megnitude,
- )
- class HorizontalFlip(OperationBase):
- """Apply horizontal flip operation.
- Args:
- initial_probability: the initial probability. If None, the augmentation will be randomly
- applied according to he augmentation sampling range.
- temperature: temperature for RelaxedBernoulli distribution used during training.
- """
- def __init__(self, initial_probability: float = 0.5, temperature: float = 0.1) -> None:
- super().__init__(
- K.RandomHorizontalFlip(same_on_batch=False, p=initial_probability),
- initial_magnitude=None,
- temperature=temperature,
- symmetric_megnitude=False,
- )
- class VerticalFlip(OperationBase):
- """Apply vertical flip operation.
- Args:
- initial_magnitude: the initial magnitude.
- temperature: temperature for RelaxedBernoulli distribution used during training.
- """
- def __init__(self, initial_probability: float = 0.5, temperature: float = 0.1) -> None:
- super().__init__(
- K.RandomVerticalFlip(same_on_batch=False, p=initial_probability),
- initial_magnitude=None,
- temperature=temperature,
- symmetric_megnitude=False,
- )
- class Rotate(OperationBase):
- """Apply rotate operation.
- Args:
- initial_magnitude: the initial magnitude.
- initial_probability: the initial probability. If None, the augmentation will be randomly
- applied according to he augmentation sampling range.
- magnitude_range: the sampling range for random sampling and clamping the optimized magnitude.
- temperature: temperature for RelaxedBernoulli distribution used during training.
- symmetric_megnitude: if to randomly assign the magnitude as negative or not.
- """
- def __init__(
- self,
- initial_magnitude: Optional[float] = 15.0,
- initial_probability: float = 0.5,
- magnitude_range: Tuple[float, float] = (0.0, 30.0),
- temperature: float = 0.1,
- symmetric_megnitude: bool = True,
- ) -> None:
- if symmetric_megnitude and magnitude_range[0] < 0:
- raise ValueError(
- f"Lower bound of {self.__class__.__name__} is a symmetric operation. "
- f"The lower bound must above 0. Got {magnitude_range[0]}."
- )
- super().__init__(
- K.RandomRotation(magnitude_range, same_on_batch=False, p=initial_probability),
- initial_magnitude=[("degrees", initial_magnitude)],
- temperature=temperature,
- symmetric_megnitude=symmetric_megnitude,
- )
- class ShearX(OperationBase):
- """Apply shear operation along x-axis.
- Args:
- initial_magnitude: the initial magnitude.
- initial_probability: the initial probability. If None, the augmentation will be randomly
- applied according to he augmentation sampling range.
- magnitude_range: the sampling range for random sampling and clamping the optimized magnitude.
- temperature: temperature for RelaxedBernoulli distribution used during training.
- symmetric_megnitude: if to randomly assign the magnitude as negative or not.
- """
- @staticmethod
- def _process_magnitude(magnitude: Tensor) -> Tensor:
- # make it sign-agnostic
- return magnitude * 180
- def __init__(
- self,
- initial_magnitude: Optional[float] = 0.1,
- initial_probability: float = 0.5,
- magnitude_range: Tuple[float, float] = (0.0, 0.3),
- temperature: float = 0.1,
- symmetric_megnitude: bool = True,
- ) -> None:
- if symmetric_megnitude and magnitude_range[0] < 0:
- raise ValueError(
- f"Lower bound of {self.__class__.__name__} is a symmetric operation. "
- f"The lower bound must above 0. Got {magnitude_range[0]}."
- )
- super().__init__(
- K.RandomShear(magnitude_range, same_on_batch=False, p=initial_probability, align_corners=True),
- initial_magnitude=[("shear_x", initial_magnitude)],
- temperature=temperature,
- symmetric_megnitude=symmetric_megnitude,
- magnitude_fn=ShearX._process_magnitude,
- )
- class ShearY(OperationBase):
- """Apply shear operation along y-axis.
- Args:
- initial_magnitude: the initial magnitude.
- initial_probability: the initial probability. If None, the augmentation will be randomly
- applied according to he augmentation sampling range.
- magnitude_range: the sampling range for random sampling and clamping the optimized magnitude.
- temperature: temperature for RelaxedBernoulli distribution used during training.
- symmetric_megnitude: if to randomly assign the magnitude as negative or not.
- """
- @staticmethod
- def _process_magnitude(magnitude: Tensor) -> Tensor:
- # make it sign-agnostic
- return magnitude * 180
- def __init__(
- self,
- initial_magnitude: Optional[float] = 0.1,
- initial_probability: float = 0.5,
- magnitude_range: Tuple[float, float] = (0.0, 0.3),
- temperature: float = 0.1,
- symmetric_megnitude: bool = True,
- ) -> None:
- if symmetric_megnitude and magnitude_range[0] < 0:
- raise ValueError(
- f"Lower bound of {self.__class__.__name__} is a symmetric operation. "
- f"The lower bound must above 0. Got {magnitude_range[0]}."
- )
- super().__init__(
- K.RandomShear(
- (0.0, 0.0, magnitude_range[0], magnitude_range[1]),
- same_on_batch=False,
- p=initial_probability,
- align_corners=True,
- ),
- initial_magnitude=[("shear_y", initial_magnitude)],
- temperature=temperature,
- symmetric_megnitude=symmetric_megnitude,
- magnitude_fn=ShearY._process_magnitude,
- )
- class TranslateX(OperationBase):
- """Apply translate operation along x-axis.
- Args:
- initial_magnitude: the initial magnitude.
- initial_probability: the initial probability. If None, the augmentation will be randomly
- applied according to he augmentation sampling range.
- magnitude_range: the sampling range for random sampling and clamping the optimized magnitude.
- temperature: temperature for RelaxedBernoulli distribution used during training.
- symmetric_megnitude: if to randomly assign the magnitude as negative or not.
- """
- def __init__(
- self,
- initial_magnitude: Optional[float] = 0.2,
- initial_probability: float = 0.5,
- magnitude_range: Tuple[float, float] = (0.0, 0.5),
- temperature: float = 0.1,
- symmetric_megnitude: bool = True,
- ) -> None:
- if symmetric_megnitude and magnitude_range[0] < 0:
- raise ValueError(
- f"Lower bound of {self.__class__.__name__} is a symmetric operation. "
- f"The lower bound must above 0. Got {magnitude_range[0]}."
- )
- super().__init__(
- K.RandomTranslate(magnitude_range, same_on_batch=False, p=initial_probability, align_corners=True),
- initial_magnitude=[("translate_x", initial_magnitude)],
- temperature=temperature,
- symmetric_megnitude=symmetric_megnitude,
- )
- class TranslateY(OperationBase):
- """Apply translate operation along y-axis.
- Args:
- initial_magnitude: the initial magnitude.
- initial_probability: the initial probability. If None, the augmentation will be randomly
- applied according to he augmentation sampling range.
- magnitude_range: the sampling range for random sampling and clamping the optimized magnitude.
- temperature: temperature for RelaxedBernoulli distribution used during training.
- symmetric_megnitude: if to randomly assign the magnitude as negative or not.
- """
- def __init__(
- self,
- initial_magnitude: Optional[float] = 0.2,
- initial_probability: float = 0.5,
- magnitude_range: Tuple[float, float] = (0.0, 0.5),
- temperature: float = 0.1,
- symmetric_megnitude: bool = True,
- ) -> None:
- if symmetric_megnitude and magnitude_range[0] < 0:
- raise ValueError(
- f"Lower bound of {self.__class__.__name__} is a symmetric operation. "
- f"The lower bound must above 0. Got {magnitude_range[0]}."
- )
- super().__init__(
- K.RandomTranslate(None, magnitude_range, same_on_batch=False, p=initial_probability, align_corners=True),
- initial_magnitude=[("translate_y", initial_magnitude)],
- temperature=temperature,
- symmetric_megnitude=symmetric_megnitude,
- )
|