minerva.transforms.transform¶
Classes¶
Cast the input data to the specified data type. |
|
This class is a base class for all transforms. Transforms is just a |
|
This class is a base class for all transforms. Transforms is just a |
|
Crop an image to a given output size, with optional padding and bounding-box support. |
|
Flip the input data along the specified axis. |
|
This class is a base class for all transforms. Transforms is just a |
|
This class is a base class for all transforms. Transforms is just a |
|
This class is a dummy transform that does nothing. It is useful when |
|
This class is a base class for all transforms. Transforms is just a |
|
This class is a base class for all transforms. Transforms is just a |
|
This class is a base class for all transforms. Transforms is just a |
|
This class is a base class for all transforms. Transforms is just a |
|
Zeroes entries of a tensor according to the sign of Perlin noise. Seed for the noise generator given by torch.randint |
|
This class is a base class for all transforms. Transforms is just a |
|
This class is a base class for all transforms. Transforms is just a |
|
This class is a base class for all transforms. Transforms is just a |
|
This class is a base class for all transforms. Transforms is just a |
|
This class is a base class for all transforms. Transforms is just a |
|
Remove single-dimensional entries from the shape of an array. |
|
Apply a sequence of transforms to a single sample of data and return the |
|
Reorder the axes of numpy arrays. |
|
Add a new axis to the input data at the specified position. |
|
This class is a base class for all transforms. Transforms is just a |
Module Contents¶
- class minerva.transforms.transform.CastTo(dtype)[source]¶
Bases:
_TransformCast the input data to the specified data type.
Cast the input data to the specified data type.
Parameters¶
- dtypetype
The data type to which the input data will be cast.
- __call__(x)[source]¶
Cast the input data to the specified data type.
- Parameters:
x (numpy.ndarray)
- Return type:
numpy.ndarray
- dtype¶
- Parameters:
dtype (Union[type, str])
- class minerva.transforms.transform.ColorJitter(brightness=1.0, contrast=1.0, saturation=1.0, hue=0.0)[source]¶
Bases:
_TransformThis class is a base class for all transforms. Transforms is just a fancy word for a function that takes an input and returns an output. The input and output can be anything. However, transforms operates over a single sample of data and does not require any additional information to perform the transformation. The __call__ method should be overridden in subclasses to define the transformation logic.
Applies fixed adjustments to brightness, contrast, saturation, and hue to an input image.
Parameters¶
- brightnessfloat, optional
Fixed factor for brightness adjustment. A value of 1.0 means no change. Defaults to 1.0.
- contrastfloat, optional
Fixed factor for contrast adjustment. A value of 1.0 means no change. Defaults to 1.0.
- saturationfloat, optional
Fixed factor for saturation adjustment. A value of 1.0 means no change. Defaults to 1.0.
- huefloat, optional
Fixed degree shift for hue adjustment, in the range [-180, 180]. Defaults to 0.0.
Returns¶
- np.ndarray
The transformed image with fixed brightness, contrast, saturation, and hue adjustments applied.
- __call__(image)[source]¶
Implement the transformation logic in this method. Usually, the transformation is applied on a single sample of data.
- Parameters:
image (numpy.ndarray)
- Return type:
numpy.ndarray
- brightness = 1.0¶
- contrast = 1.0¶
- hue = 0.0¶
- saturation = 1.0¶
- Parameters:
brightness (float)
contrast (float)
saturation (float)
hue (float)
- class minerva.transforms.transform.ContrastiveTransform(transform)[source]¶
Bases:
_TransformThis class is a base class for all transforms. Transforms is just a fancy word for a function that takes an input and returns an output. The input and output can be anything. However, transforms operates over a single sample of data and does not require any additional information to perform the transformation. The __call__ method should be overridden in subclasses to define the transformation logic.
A transformation wrapper that applies the same transform twice to create contrastive pairs.
This transform is commonly used in contrastive learning approaches where you need two different augmented versions of the same input. It takes a base transformation and applies it twice independently to the same input, potentially producing different results if the underlying transform includes randomness.
Parameters¶
- transform_Transform
The base transformation to be applied twice. This should be a callable that takes a numpy array and returns a transformed numpy array.
- __call__(x)[source]¶
Implement the transformation logic in this method. Usually, the transformation is applied on a single sample of data.
- Parameters:
x (numpy.ndarray)
- Return type:
Tuple
- transform¶
- Parameters:
transform (_Transform)
- class minerva.transforms.transform.Crop(output_size, pad_mode='reflect', coords=(0, 0), bbox=None)[source]¶
Bases:
_TransformCrop an image to a given output size, with optional padding and bounding-box support.
Expects inputs in CHW (C, H, W) or 2D (H, W) format.
Parameters¶
- output_sizeTuple[int, int]
Desired output size as (height, width).
- pad_modestr, optional
Padding mode used if output size is larger than input size. Defaults to ‘reflect’. Valid modes include: ‘constant’, ‘edge’, ‘linear_ramp’, ‘maximum’, ‘mean’, ‘median’, ‘minimum’, ‘reflect’, ‘symmetric’, ‘wrap’, ‘empty’.
- coordsTuple[float, float], optional
Top-left coordinates for the crop box as (row, col). Values must go from 0 to 1 indicating the relative position on where the new top-left corner can be set, taking in consideration the new size. Defaults to (0, 0) which corresponds to the top-left corner of the image.
- bboxOptional[Tuple[int, int, int, int]], optional
If provided, crops the image to the bounding box defined by (y1, y2, x1, x2). If this parameter is set, the coords parameter is ignored. Defaults to None.
- __call__(image)[source]¶
Crop the image to the configured output size.
Parameters¶
- imagenp.ndarray
Input array in CHW (C, H, W) or 2D (H, W) format.
Returns¶
- np.ndarray
Cropped array. Shape is (C, new_h, new_w) for 3D input or (new_h, new_w) for 2D input. Padded symmetrically if the output size exceeds the input size.
- Parameters:
image (numpy.ndarray)
- Return type:
numpy.ndarray
- bbox = None¶
- coords = (0, 0)¶
- output_size¶
- pad_mode = 'reflect'¶
- Parameters:
output_size (Tuple[int, int])
pad_mode (str)
coords (Tuple[float, float])
bbox (Optional[Tuple[int, int, int, int]])
- class minerva.transforms.transform.Flip(axis=1)[source]¶
Bases:
_TransformFlip the input data along the specified axis.
Flip the input data along the specified axis.
Parameters¶
- axisint | List[int], optional
One or more axis to flip the input data along, by default 1 (horizontal). If a list of axis is provided, the input data is flipped along all the specified axis in the order they are provided.
- __call__(x)[source]¶
Flip the input data along the specified axis. if axis is an integer, the input data is flipped along the specified axis. if axis is a list of integers, the input data is flipped along all the specified axis in the order they are provided. The input must have the same, or less, number of dimensions as the length of the list of axis.
- Parameters:
x (numpy.ndarray)
- Return type:
numpy.ndarray
- axis = 1¶
- Parameters:
axis (Union[int, List[int]])
- class minerva.transforms.transform.Gradient(direction)[source]¶
Bases:
_TransformThis class is a base class for all transforms. Transforms is just a fancy word for a function that takes an input and returns an output. The input and output can be anything. However, transforms operates over a single sample of data and does not require any additional information to perform the transformation. The __call__ method should be overridden in subclasses to define the transformation logic.
- direction:
0 -> Gradient along the x-axis (width) 1 -> Gradient along the y-axis (height)
- Parameters:
direction (int)
- __call__(x)[source]¶
Implement the transformation logic in this method. Usually, the transformation is applied on a single sample of data.
- direction¶
- directions¶
- class minerva.transforms.transform.GrayScale(method='luminosity')[source]¶
Bases:
_TransformThis class is a base class for all transforms. Transforms is just a fancy word for a function that takes an input and returns an output. The input and output can be anything. However, transforms operates over a single sample of data and does not require any additional information to perform the transformation. The __call__ method should be overridden in subclasses to define the transformation logic.
Converts an image to grayscale using the specified method.
Parameters¶
- method{‘average’, ‘luminosity’}, optional
The method to compute grayscale: - ‘average’: (R + G + B) / 3 - ‘luminosity’: 0.299R + 0.587G + 0.114B Defaults to ‘luminosity’.
- __call__(image)[source]¶
Applies grayscale conversion to the input RGB image.
Parameters¶
- imagenp.ndarray
Input image in RGB format with shape (H, W, 3).
Returns¶
- np.ndarray
Grayscale image with shape (H, W, 3) where all channels are equal.
- Parameters:
image (numpy.ndarray)
- Return type:
numpy.ndarray
- method = 'luminosity'¶
- Parameters:
method (Literal['average', 'luminosity'])
- class minerva.transforms.transform.Identity[source]¶
Bases:
_TransformThis class is a dummy transform that does nothing. It is useful when you want to skip a transform in a pipeline.
- class minerva.transforms.transform.Indexer(index)[source]¶
Bases:
_TransformThis class is a base class for all transforms. Transforms is just a fancy word for a function that takes an input and returns an output. The input and output can be anything. However, transforms operates over a single sample of data and does not require any additional information to perform the transformation. The __call__ method should be overridden in subclasses to define the transformation logic.
This transform extracts a single channel from a multi-channel image.
Parameters¶
- indexint
The index of the channel to extract.
- __call__(x)[source]¶
Implement the transformation logic in this method. Usually, the transformation is applied on a single sample of data.
- Parameters:
x (numpy.ndarray)
- Return type:
numpy.ndarray
- index¶
- Parameters:
index (int)
- class minerva.transforms.transform.Normalize(mean, std, to_rgb=False, normalize_labels=False)[source]¶
Bases:
_TransformThis class is a base class for all transforms. Transforms is just a fancy word for a function that takes an input and returns an output. The input and output can be anything. However, transforms operates over a single sample of data and does not require any additional information to perform the transformation. The __call__ method should be overridden in subclasses to define the transformation logic.
Normalize the input data using the provided means and standard deviations. I assumes the data shape is (C, H, W)
Parameters¶
- meanList[float]
List of means for each channel.
- stdList[float]
List of standard deviations for each channel.
- to_rgbbool, optional
Convert grayscale images to RGB format, by default False.
- normalize_labelsbool, optional
Normalize label images, by default False.
- __call__(data)[source]¶
Implement the transformation logic in this method. Usually, the transformation is applied on a single sample of data.
- mean¶
- normalize_labels = False¶
- std¶
- to_rgb = False¶
- class minerva.transforms.transform.PadCrop(target_h_size, target_w_size, padding_mode='reflect', seed=None, constant_values=0)[source]¶
Bases:
_TransformThis class is a base class for all transforms. Transforms is just a fancy word for a function that takes an input and returns an output. The input and output can be anything. However, transforms operates over a single sample of data and does not require any additional information to perform the transformation. The __call__ method should be overridden in subclasses to define the transformation logic.
Transforms image and pads or crops it to the target size. If the target size is larger than the input size, the image is padded, else, the image is cropped. The same happens for both height and width. The padding mode can be specified, as well as the seed for the random number generator.
For padding, the padding is applied symmetrically on both sides of the image, thus, image will be centered in the padded image. For cropping, the crop is applied from a random position in the image.
Image is expected to be in C x H x W, or H x W format.
Parameters¶
- target_h_sizeint
Desired height size.
- target_w_sizeint
Desired width size.
- padding_modestr, optional
The padding mode to use, by default “reflect”
- seedint, optional
The seed for the random number generator. It is used to generate the random crop position. By default, None.
- constant_valuesint, optional
If padding mode is ‘constant’, the value to use for padding. By default 0.
- __call__(x)[source]¶
Implement the transformation logic in this method. Usually, the transformation is applied on a single sample of data.
- Parameters:
x (numpy.ndarray)
- Return type:
numpy.ndarray
- constant_values = 0¶
- padding_mode = 'reflect'¶
- rng¶
- seed = None¶
- target_h_size¶
- target_w_size¶
- Parameters:
target_h_size (int)
target_w_size (int)
padding_mode (str)
seed (Optional[int])
constant_values (int)
- class minerva.transforms.transform.Padding(target_h_size, target_w_size, padding_mode='reflect', padding_value=0, mask_padding_value=255)[source]¶
Bases:
_TransformThis class is a base class for all transforms. Transforms is just a fancy word for a function that takes an input and returns an output. The input and output can be anything. However, transforms operates over a single sample of data and does not require any additional information to perform the transformation. The __call__ method should be overridden in subclasses to define the transformation logic.
A transform that pads images and masks to reach target dimensions.
This transform automatically pads input arrays to the specified target dimensions using different padding strategies. It intelligently determines whether the input is an image or mask based on the data type and applies appropriate padding values. For unsigned integer arrays (typically masks), it uses the mask padding value, while for other arrays (typically images), it uses the regular padding value.
Parameters¶
- target_h_sizeint
Target height in pixels after padding.
- target_w_sizeint
Target width in pixels after padding.
- padding_modestr, default=”reflect”
Padding mode to use. Options include “reflect”, “constant”, “edge”, “wrap”. When using “constant”, the padding values specified below will be used.
- padding_valueint, default=0
Padding value to use for image data when padding_mode is “constant”. Only used for non-unsigned integer arrays.
- mask_padding_valueint, default=255
Padding value to use for mask data when padding_mode is “constant”. Only used for unsigned integer arrays. Commonly set to 255 to represent ignore class in segmentation masks.
- __call__(x)[source]¶
Implement the transformation logic in this method. Usually, the transformation is applied on a single sample of data.
- Parameters:
x (numpy.ndarray)
- Return type:
numpy.ndarray
- mask_padding_value = 255¶
- padding_mode = 'reflect'¶
- padding_value = 0¶
- target_h_size¶
- target_w_size¶
- Parameters:
target_h_size (int)
target_w_size (int)
padding_mode (str)
padding_value (int)
mask_padding_value (int)
- class minerva.transforms.transform.PerlinMasker(octaves, scale=1)[source]¶
Bases:
_TransformZeroes entries of a tensor according to the sign of Perlin noise. Seed for the noise generator given by torch.randint
Zeroes entries of a tensor according to the sign of Perlin noise. Seed for the noise generator given by torch.randint
Parameters¶
- octaves: int
Level of detail for the Perlin noise generator
- scale: float = 1
Optionally rescale the Perlin noise. Default is 1 (no rescaling)
- __call__(x)[source]¶
Zeroes entries of a tensor according to the sign of Perlin noise.
Parameters¶
- x: np.ndarray
The tensor whose entries to zero.
- Parameters:
x (numpy.ndarray)
- Return type:
numpy.ndarray
- octaves¶
- scale = 1¶
- Parameters:
octaves (int)
scale (float)
- class minerva.transforms.transform.Repeat(axis, n_repetitions)[source]¶
Bases:
_TransformThis class is a base class for all transforms. Transforms is just a fancy word for a function that takes an input and returns an output. The input and output can be anything. However, transforms operates over a single sample of data and does not require any additional information to perform the transformation. The __call__ method should be overridden in subclasses to define the transformation logic.
This transform repeats the input data along the specified axis.
Parameters¶
- axisint
The axis along which to repeat the input data.
- n_repetitionsint
The number of repetitions.
- __call__(x)[source]¶
Implement the transformation logic in this method. Usually, the transformation is applied on a single sample of data.
- Parameters:
x (numpy.ndarray)
- Return type:
numpy.ndarray
- axis¶
- n_repetitions¶
- Parameters:
axis (int)
n_repetitions (int)
- class minerva.transforms.transform.Reshape(shape)[source]¶
Bases:
_TransformThis class is a base class for all transforms. Transforms is just a fancy word for a function that takes an input and returns an output. The input and output can be anything. However, transforms operates over a single sample of data and does not require any additional information to perform the transformation. The __call__ method should be overridden in subclasses to define the transformation logic.
Reshape the input data to the specified shape.
Parameters¶
- shapeTuple[int, …]
The target shape for the input data.
- __call__(x)[source]¶
Implement the transformation logic in this method. Usually, the transformation is applied on a single sample of data.
- Parameters:
x (numpy.ndarray)
- Return type:
numpy.ndarray
- shape¶
- Parameters:
shape (Tuple[int, Ellipsis])
- class minerva.transforms.transform.Resize(target_h_size, target_w_size, keep_aspect_ratio=False, detect_mask=False)[source]¶
Bases:
_TransformThis class is a base class for all transforms. Transforms is just a fancy word for a function that takes an input and returns an output. The input and output can be anything. However, transforms operates over a single sample of data and does not require any additional information to perform the transformation. The __call__ method should be overridden in subclasses to define the transformation logic.
A transformation class for resizing images with optional aspect ratio preservation.
This transform can resize images to specified target dimensions either by direct resizing or by preserving the aspect ratio. When preserving aspect ratio, the transform scales the image using the smaller of the two scaling factors to ensure the image fits within the target dimensions without distortion.
Parameters¶
- target_h_sizeint
Target height for the resized image in pixels.
- target_w_sizeint
Target width for the resized image in pixels.
- keep_aspect_ratiobool, default=False
If True, preserves the original aspect ratio by scaling with the smaller scaling factor. If False, directly resizes to target dimensions which may distort the image.
- detect_maskbool, default=False
If True, uses nearest neighbor interpolation for resizing masks (integer types).
- __call__(x)[source]¶
Implement the transformation logic in this method. Usually, the transformation is applied on a single sample of data.
- Parameters:
x (numpy.ndarray)
- Return type:
numpy.ndarray
- detect_mask = False¶
- keep_aspect_ratio = False¶
- target_h_size¶
- target_w_size¶
- Parameters:
target_h_size (int)
target_w_size (int)
keep_aspect_ratio (bool)
detect_mask (bool)
- class minerva.transforms.transform.Rotation(degrees)[source]¶
Bases:
_TransformThis class is a base class for all transforms. Transforms is just a fancy word for a function that takes an input and returns an output. The input and output can be anything. However, transforms operates over a single sample of data and does not require any additional information to perform the transformation. The __call__ method should be overridden in subclasses to define the transformation logic.
Rotates an image by a specified angle using reflection padding.
Parameters¶
- degreesfloat
Angle in degrees to rotate the image counterclockwise.
Notes¶
Accepts input with shape (H, W) or (H, W, C), where C can be any number of channels.
For multi-channel images, the same transformation is applied to all channels.
Uses OpenCV’s warpAffine with reflection padding.
- __call__(image)[source]¶
Implement the transformation logic in this method. Usually, the transformation is applied on a single sample of data.
- Parameters:
image (numpy.ndarray)
- Return type:
numpy.ndarray
- degrees¶
- Parameters:
degrees (float)
- class minerva.transforms.transform.Solarize(threshold=128)[source]¶
Bases:
_TransformThis class is a base class for all transforms. Transforms is just a fancy word for a function that takes an input and returns an output. The input and output can be anything. However, transforms operates over a single sample of data and does not require any additional information to perform the transformation. The __call__ method should be overridden in subclasses to define the transformation logic.
Solarizes the image by inverting pixel values above a specified threshold.
Parameters¶
- thresholdint, optional
Intensity threshold for inversion, default is 128.
Returns¶
- np.ndarray
Solarized image with inverted pixel values above threshold.
- __call__(image)[source]¶
Implement the transformation logic in this method. Usually, the transformation is applied on a single sample of data.
- Parameters:
image (numpy.ndarray)
- Return type:
numpy.ndarray
- threshold = 128¶
- Parameters:
threshold (int)
- class minerva.transforms.transform.Squeeze(axis)[source]¶
Bases:
_TransformRemove single-dimensional entries from the shape of an array.
Remove single-dimensional entries from the shape of an array.
Parameters¶
- axisint
The position of the axis to be removed.
- __call__(x)[source]¶
Remove single-dimensional entries from the shape of an array.
- Parameters:
x (numpy.ndarray)
- Return type:
numpy.ndarray
- axis¶
- Parameters:
axis (int)
- class minerva.transforms.transform.TransformPipeline(transforms)[source]¶
Bases:
_TransformApply a sequence of transforms to a single sample of data and return the transformed data.
Apply a sequence of transforms to a single sample of data and return the transformed data.
Parameters¶
- transformsList[_Transform]
A list of transforms to be applied to the input data.
- __add__(other)[source]¶
Add a transform to the pipeline.
- Parameters:
other (_Transform)
- Return type:
- __call__(x)[source]¶
Apply a sequence of transforms to a single sample of data and return the transformed data.
- Parameters:
x (Any)
- Return type:
Any
- __radd__(other)[source]¶
Add a transform to the pipeline.
- Parameters:
other (_Transform)
- Return type:
- transforms¶
- Parameters:
transforms (Sequence[_Transform])
- class minerva.transforms.transform.Transpose(axes)[source]¶
Bases:
_TransformReorder the axes of numpy arrays.
Reorder the axes of numpy arrays.
Parameters¶
- axesint
The order of the new axes
- __call__(x)[source]¶
Reorder the axes of numpy arrays.
- Parameters:
x (numpy.ndarray)
- Return type:
numpy.ndarray
- axes¶
- Parameters:
axes (Sequence[int])
- class minerva.transforms.transform.Unsqueeze(axis, label_only=False)[source]¶
Bases:
_TransformAdd a new axis to the input data at the specified position.
Add a new axis to the input data at the specified position.
Parameters¶
- axisint
The position of the new axis to be added.
- label_onlybool, optional
If True, the transform will only apply to the label (second element) of the input tuple. If False, it will apply to the entire input data, by default False.
- __call__(x)[source]¶
Add a new axis to the input data at the specified position.
- Parameters:
x (numpy.ndarray)
- axis¶
- label_only = False¶
- Parameters:
axis (int)
label_only (bool)
- class minerva.transforms.transform._Transform[source]¶
This class is a base class for all transforms. Transforms is just a fancy word for a function that takes an input and returns an output. The input and output can be anything. However, transforms operates over a single sample of data and does not require any additional information to perform the transformation. The __call__ method should be overridden in subclasses to define the transformation logic.