base_augmentation
Base Augmentation API
Classes:
-
BaseAugmentation
–Base augmentation
-
BaseAugmentation1D
–Base 1D augmentation
-
BaseAugmentation2D
–Base 2D augmentation
Functions:
-
tf_keras_map
–Map function for TensorFlow Keras
Classes
BaseAugmentation
BaseAugmentation(seed: int | None = None, auto_vectorize: bool = True, data_format: str | None = None, name: str | None = None, **kwargs)
BaseAugmentation acts as a base class for various custom augmentation layers. This class provides a common interface for augmenting samples and labels. In the future, we will add support for segmentation and bounding boxes.
The only method that needs to be implemented by the subclass is
- augment_sample: Augment a single sample during training.
Optionally, you can implement the following methods:
- augment_label: Augment a single label during training.
- get_random_transformations: Returns a nested structure of random transformations that should be applied to the batch. This is required to have unique transformations for each sample in the batch and maintain the same transformations for samples and labels.
- batch_augment: Augment a batch of samples and labels during training. Needed if layer requires access to all samples (e.g. CutMix).
By default, this method will coerce the input into a batch as well as a nested structure of inputs. If auto_vectorize is set to True, the augment_sample and augment_label methods will be vectorized using keras.ops.vectorized_map. Otherwise, it will use keras.ops.map which runs sequentially.
Parameters:
-
seed
(int | None
, default:None
) –Random seed. Defaults to None.
-
auto_vectorize
(bool
, default:True
) –If True, augment_sample and augment_label methods will be vectorized using keras.ops.vectorized_map. Otherwise, it will use keras.ops.map which runs sequentially. Defaults to True.
-
data_format
(str | None
, default:None
) –Data format. Defaults to None. Will use keras.backend.image_data_format() if None.
-
name
(str | None
, default:None
) –Layer name. Defaults to None.
Source code in neuralspot_edge/layers/preprocessing/base_augmentation.py
Functions
call
This method will serve as the main entry point for the layer. It will handle the input formatting and output formatting.
Parameters:
-
inputs
(NestedTensorValue
) –Inputs to be augmented.
-
training
(bool
, default:True
) –Whether the model is training or not.
Returns:
-
NestedTensorValue
(NestedTensorValue
) –Augmented samples or labels.
Source code in neuralspot_edge/layers/preprocessing/base_augmentation.py
augment_sample
Augment a single sample during training.
Note
This method should be implemented by the subclass.
Args: input(NestedTensorValue): Single sample.
Returns:
-
KerasTensor
(KerasTensor
) –Augmented sample.
Source code in neuralspot_edge/layers/preprocessing/base_augmentation.py
augment_samples
Augment a batch of samples during training.
Parameters:
-
inputs
(NestedTensorValue
) –Batch of samples.
Returns:
-
KerasTensor
(KerasTensor
) –Augmented batch of samples.
Source code in neuralspot_edge/layers/preprocessing/base_augmentation.py
augment_label
Augment a single label during training.
Note
Implement this method if you need to augment labels.
Parameters:
-
inputs
(NestedTensorValue
) –Single label.
Returns:
-
KerasTensor
–keras.KerasTensor: Augmented label.
Source code in neuralspot_edge/layers/preprocessing/base_augmentation.py
augment_labels
Augment a batch of labels during training.
Parameters:
-
inputs
(NestedTensorValue
) –Batch of labels.
Returns:
-
KerasTensor
–keras.KerasTensor: Augmented batch of labels.
Source code in neuralspot_edge/layers/preprocessing/base_augmentation.py
get_random_transformations
Generates random transformations needed for augmenting samples and labels.
Parameters:
Returns:
-
NestedTensorValue
(NestedTensorValue
) –Batch of random transformations.
Note
This method should be implemented by the subclass if the layer requires random transformations.
Source code in neuralspot_edge/layers/preprocessing/base_augmentation.py
batch_augment
Handles processing entire batch of samples and labels in a nested structure. Responsible for calling augment_samples and augment_labels.
Parameters:
-
inputs
(NestedTensorValue
) –Batch of samples and labels.
Returns:
-
NestedTensorValue
(NestedTensorValue
) –Augmented batch of samples and labels.
Source code in neuralspot_edge/layers/preprocessing/base_augmentation.py
compute_output_shape
By default assumes the shape of the input is the same as the output.
Parameters:
Returns:
Note
This method should be implemented by the subclass if the output shape is different from the input shape.
Source code in neuralspot_edge/layers/preprocessing/base_augmentation.py
get_config
Serialize the layer configuration.
Source code in neuralspot_edge/layers/preprocessing/base_augmentation.py
BaseAugmentation1D
BaseAugmentation1D acts as a base class for various custom augmentation layers. This class provides a common interface for augmenting samples and labels. In the future, we will add support for segmentation and 1D bounding boxes.
The only method that needs to be implemented by the subclass is
- augment_sample: Augment a single sample during training.
Optionally, you can implement the following methods:
- augment_label: Augment a single label during training.
- get_random_transformations: Returns a nested structure of random transformations that should be applied to the batch. This is required to have unique transformations for each sample in the batch and maintain the same transformations for samples and labels.
- batch_augment: Augment a batch of samples and labels during training. Needed if layer requires access to all samples (e.g. CutMix).
By default, this method will coerce the input into a batch as well as a nested structure of inputs. If auto_vectorize is set to True, the augment_sample and augment_label methods will be vectorized using keras.ops.vectorized_map. Otherwise, it will use keras.ops.map which runs sequentially.
Example:
class NormalizeLayer1D(BaseAugmentation1D):
def __init__(self, **kwargs):
...
def augment_sample(self, inputs):
sample = inputs["data"]
mu = keras.ops.mean()
std = keras.ops.std()
return (sample - mu) / (std + self.epsilon)
x = np.random.rand(100, 3)
lyr = NormalizeLayer(...)
y = lyr(x, training=True)
Source code in neuralspot_edge/layers/preprocessing/base_augmentation.py
Functions
BaseAugmentation2D
BaseAugmentation2D acts as a base class for various custom augmentation layers. This class provides a common interface for augmenting samples and labels. In the future, we will add support for segmentation and 1D bounding boxes.
The only method that needs to be implemented by the subclass is
- augment_sample: Augment a single sample during training.
Optionally, you can implement the following methods:
- augment_label: Augment a single label during training.
- get_random_transformations: Returns a nested structure of random transformations that should be applied to the batch. This is required to have unique transformations for each sample in the batch and maintain the same transformations for samples and labels.
- batch_augment: Augment a batch of samples and labels during training. Needed if layer requires access to all samples (e.g. CutMix).
By default, this method will coerce the input into a batch as well as a nested structure of inputs. If auto_vectorize is set to True, the augment_sample and augment_label methods will be vectorized using keras.ops.vectorized_map. Otherwise, it will use keras.ops.map which runs sequentially.
Example:
class NormalizeLayer2D(BaseAugmentation2D):
def __init__(self, name=None, **kwargs):
...
def augment_sample(self, inputs):
sample = inputs["data"]
mu = keras.ops.mean()
std = keras.ops.std()
return (sample - mu) / (std + self.epsilon)
x = np.random.rand(32, 32, 3)
lyr = NormalizeLayer(...)
y = lyr(x, training=True)