core#

Copyright 2025 Apple Inc.

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

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.

class parameterized_transforms.core.AtomicTransform(*args, **kwargs)#

Bases: Transform

Defines the base for an atomic transform.

The initializer.

Parameters:
  • tx_mode – The mode of the transform. DEFAULT: TransformMode.CASCADE.

  • args – The arguments for the underlying transform.

  • kwargs – The keyword arguments for the underlying transform.

abstract apply_transform(img: Tensor | Image, params: Tuple[int | float, ...], **kwargs) Tensor | Image#

Augments given data point using given parameters.

Parameters:
  • img – The data point to be augmented.

  • params – The parameters to be used for augmentation.

Returns:

The augmented image.

cascade_transform(img: Tensor | Image, params: Tuple[int | float, ...]) Tuple[Tensor | Image, Tuple[int | float, ...]]#

Input the data point and previous parameters, generate the parameters for the current transform, augment previous parameters with current parameters, and return the augmented parameters.

Parameters:
  • img – The data point to be augmented.

  • params – The parameters for the augmentations so far.

Returns:

The tuple of augmented data point and the params.

This method consists of 5 steps– 1. Generate local raw parameters for the transform. 2. Generate augmentation from the data point and local raw parameters. 3. Post-process the local raw parameters to get processed parameters. 4. Attach processed parameters to those input to this method. 5. Return augmented data point and combined processed parameters.

consume_transform(img: Tensor | Image, params: Tuple[int | float, ...]) Tuple[Tensor | Image, Tuple[int | float, ...]]#

Input the data point and parameters, extract the required number of parameters, performs the required transforms, and returns the augmented data point along with remaining parameters.

Parameters:
  • img – The data point to be augmented.

  • params – The parameters remaining from the augmentations so far.

Returns:

The tuple of augmented data point and remaining parameters.

This method consists of 4 steps– 1. Detach processed parameters from the input parameters. 2. Pre-process the processed parameters to get local raw parameters. 3. Generate augmentation from the data point and local raw parameters. 4. Return augmented data point along with remaining parameters.

abstract extract_params(params: Tuple[int | float, ...]) Tuple[Tuple[int | float, ...], Tuple[int | float, ...]]#

Chunks the input parameters into two sets; the first required for the augmentation of the current data and the second to pass on to the next augmentations.

Parameters:

params – The parameters remaining from the augmentations so far.

Returns:

The tuple of the local and subsequent parameters.

abstract get_raw_params(img: Tensor | Image) Tuple[int | float, ...]#

Generates the raw parameters used to augment current data point.

Parameters:

img – The data point to be augmented.

Returns:

Current raw parameters to augment the data point.

abstract post_process_params(img: Tensor | Image, params: Tuple[int | float, ...]) Tuple[int | float, ...]#

Post-processes the parameters of augmentations before outputting.

Parameters:
  • img – The data point to be augmented.

  • params – The raw local parameters to be post-processed.

Returns:

The post-processed parameters.

abstract pre_process_params(img: Tensor | Image, params: Tuple[int | float, ...]) Tuple[int | float, ...]#

Pre-processes the parameters of augmentations.

Parameters:
  • img – The data point to be augmented.

  • params – The parameters from which to extract local parameters.

Returns:

The pre-processed parameters ready for their usage.

class parameterized_transforms.core.ComposingTransform(*args, **kwargs)#

Bases: Transform

Defines the base for a composing transform. These transforms are intended to input one or more underlying transforms and use their functionality to achieve the intended composing behavior.

The initializer.

Parameters:
  • args – The arguments for the underlying transform.

  • kwargs – The keyword arguments for the underlying transform.

The implementing subclass should define transforms and tx_mode.

Parameters:
  • transforms – The list, tuple, or any other iterable object that can produce all the component transforms in the correct order.

  • tx_mode – The mode of the transform.

abstract apply_cascade_transform(img: Tensor | Image, params: Tuple[int | float, ...], **kwargs) Tuple[Tensor | Image, Tuple[int | float, ...]]#

Augments given data point using given parameters.

Parameters:
  • img – The data point to be augmented.

  • params – The parameters to be used for augmentation.

Returns:

The augmented image and corresponding augmentation parameters.

Here, the returned augmentation parameters are the ones generated by the application of the core transforms in self.transforms.

abstract apply_consume_transform(img: Tensor | Image, params: Tuple[int | float, ...], aug_params: Tuple[int | float, ...]) Tuple[Tensor | Image, Tuple[int | float, ...]]#

Applies the transform with given parameters by consuming them.

Parameters:
  • img – The data point to be augmented.

  • params – The parameters remaining from the augmentations so far.

  • aug_params – The parameters from composition of parameterized transforms.

Returns:

The augmented image with the remaining parameters.

cascade_transform(img: Tensor | Image, params: Tuple[int | float, ...]) Tuple[Tensor | Image, Tuple[int | float, ...]]#

Inputs the data point and previous parameters, generates the parameters for the current transform, augments the data point with the current parameters, concatenates previous parameters with the current and augmentation parameters, and returns the concatenated parameters.

Parameters:
  • img – The data point to be augmented.

  • params – The previously collected parameters.

Returns:

The augmented data point and the corresponding parameters appended to the previously collected params.

This method consists of 5 steps– 1. Generate raw parameters for the composing transform. 2. Generate the augmentation from the data point and local parameters

to get augmentation and corresponding augmentation parameters.

  1. Post-process the raw local and augmentation parameters.

  2. Attach processed versions of the local parameters and the

    augmentation parameters to those input to this method.

  3. Return the augmented data point and combined processed parameters.

consume_transform(img: Tensor | Image, params: Tuple[int | float, ...]) Tuple[Tensor | Image, Tuple[int | float, ...]]#

Inputs the data point and parameters, extracts the required number of parameters, performs the required transforms, and returns the augmented data point along with remaining parameters.

Parameters:
  • img – The data point to be augmented.

  • params – The parameters remaining from the augmentations so far.

Returns:

The tuple of augmented data point and remaining parameters.

This method consists of 4 steps– 1. Detach local processed parameters from the input parameters. 2. Pre-process the local post-processed parameters. 3. Generate augmentation from the data point and local parameters. 4. Return augmented data point along with the remaining parameters.

abstract extract_params(params: Tuple[int | float, ...]) Tuple[Tuple[int | float, ...], Tuple[int | float, ...]]#

Chunks the input parameters into two sets; the first required for the augmentation of the current data and the second to pass on to the next augmentations.

Parameters:

params – The parameters remaining from the augmentations so far.

Returns:

The tuple of the local and subsequent parameters.

abstract get_raw_params(img: Tensor | Image) Tuple[int | float, ...]#

Generates the raw parameters used to augment current data point.

Parameters:

img – The data point to be augmented.

Returns:

Current raw parameters to augment the data point.

abstract post_process_params(img: Tensor | Image, params: Tuple[int | float, ...], aug_params: Tuple[int | float, ...]) Tuple[Tuple[int | float, ...], Tuple[int | float, ...]]#

Post-processes the local and augmentation parameters.

Parameters:
  • img – The data point to be augmented.

  • params – The raw local parameters to be post-processed.

  • aug_params – The processed params from the composition of the core transforms of this composing transform.

Returns:

The post-processed local and augmentation parameters.

abstract pre_process_params(img: Tensor | Image, params: Tuple[int | float, ...]) Tuple[Tuple[int | float, ...], Tuple[int | float, ...]]#

Pre-processes the parameters of augmentations.

Parameters:
  • img – The data point to be augmented.

  • params – The parameters from which to extract local parameters.

Returns:

The pre-processed parameters ready for their usage.

class parameterized_transforms.core.DefaultParamsMode(value)#

Bases: Enum

Defines the enum for the default parameters mode for certain transforms. In these transforms, there are multiple parameter settings that can lead to the result of returning the same image.

For instance, in ColorJitter transform, we can get the default parameters for perturbing brightness, contrast, saturation, and hue, and apply these perturbations in ANY order to retain the same image.

In such cases, we might want to have stochasticity during training and a fixed behavior during testing. This enum can help in such cases.

RANDOMIZED = 'RANDOMIZED'#
UNIQUE = 'UNIQUE'#
class parameterized_transforms.core.Transform(*args, tx_mode: str | TransformMode = TransformMode.CASCADE, **kwargs)#

Bases: ABC

Defines the core base for all transforms.

The initializer.

Parameters:
  • tx_mode – The mode of the transform as a keyword only argument. DEFAULT: TransformMode.CASCADE.

  • args – The arguments for the underlying transform.

  • kwargs – The keyword arguments for the underlying transform.

abstract cascade_transform(img: Tensor | Image, params: Tuple[int | float, ...]) Tuple[Tensor | Image, Tuple[int | float, ...]]#

Inputs the data point and previous parameters, generates the parameters for the current transform, augments previous parameters with current parameters, and returns the augmented parameters.

Parameters:
  • img – The data point to be augmented.

  • params – The parameters for the augmentations so far.

Returns:

The tuple of augmented data point and the params.

This method consists of 5 steps– 1. Generate local raw parameters for the transform. 2. Generate augmentation from the data point and local raw parameters. 3. Post-process the local raw parameters to get processed parameters. 4. Attach processed parameters to those input to this method. 5. Return augmented data point and combined processed parameters.

static concat_params(*params) Tuple[int | float, ...]#

Concatenates a given tuple of parameters into one.

Parameters:

params – The tuple of parameters, each of which is a tuple.

Returns:

The concatenated parameters as the answer.

abstract consume_transform(img: Tensor | Image, params: Tuple[int | float, ...]) Tuple[Tensor | Image, Tuple[int | float, ...]]#

Input the data point and parameters, extract the required number of parameters, performs the required transforms, and returns the augmented data point along with remaining parameters.

Parameters:
  • img – The data point to be augmented.

  • params – The parameters remaining from the augmentations so far.

Returns:

The tuple of augmented data point and remaining parameters.

This method consists of 4 steps– 1. Detach processed parameters from the input parameters. 2. Pre-process the processed parameters to get local raw parameters. 3. Generate augmentation from the data point and local raw parameters. 4. Return augmented data point along with remaining parameters.

static data_to_tx_mode(tx_mode: str | TransformMode) TransformMode#

Convert the given transform mode data into its enum. `

param tx_mode:

The input mode of the transform.

returns:

The cleaned mode.

If the mode is NOT found, we raise a not-implemented error.

abstract get_default_params(img: Tensor | Image, processed: bool = True) Tuple[int | float, ...]#

Returns the parameters for preserving the input data information.

Parameters:
  • img – The data point to be augmented.

  • processed – Whether we want the processed default parameters.

Returns:

The no-augmentation params for the class.

property name: str#

Returns the name of the transform.

Returns:

The name of the transform.

set_param_count() int#

Returns the total number of processed parameters generated by the transform under consideration.

Returns:

The number of parameters for this transform.

class parameterized_transforms.core.TransformMode(value)#

Bases: Enum

Defines the enum for the mode of the transform. Currently, the transforms can either have CASCADE or COMPOSE mode.

CASCADE = 'CASCADE'#
CONSUME = 'CONSUME'#