Your ad could be here - Reach CV/ML engineers
Contact for advertisingContactalbumentations.augmentations.dropout.transforms
Transform classes for dropout-based augmentations. This module contains transform classes for various dropout techniques used in image augmentation. It provides the base dropout class and specialized implementations like PixelDropout. These transforms randomly remove or modify pixels, channels, or regions in images, which can help models become more robust to occlusions and missing information.
Members
- classBaseDropout
- classPixelDropout
BaseDropoutclass
Base class for dropout-style transformations. This class provides common functionality for various dropout techniques, including applying cutouts to images and masks.
Parameters
Name | Type | Default | Description |
---|---|---|---|
fill | One of:
| - | Value to fill dropped regions. |
fill_mask | One of:
| - | Value to fill dropped regions in the mask. If None, the mask is not modified. |
p | float | - | Probability of applying the transform. |
Examples
>>> import numpy as np
>>> import albumentations as A
>>>
>>> # Example of a custom dropout transform inheriting from BaseDropout
>>> class CustomDropout(A.BaseDropout):
... def __init__(self, num_holes_range=(4, 8), hole_size_range=(10, 20), *args, **kwargs):
... super().__init__(*args, **kwargs)
... self.num_holes_range = num_holes_range
... self.hole_size_range = hole_size_range
...
... def get_params_dependent_on_data(self, params, data):
... img = data["image"]
... height, width = img.shape[:2]
...
... # Generate random holes
... num_holes = self.py_random.randint(*self.num_holes_range)
... hole_sizes = self.py_random.randint(*self.hole_size_range, size=num_holes)
...
... holes = []
... for i in range(num_holes):
... # Random position for each hole
... x1 = self.py_random.randint(0, max(1, width - hole_sizes[i]))
... y1 = self.py_random.randint(0, max(1, height - hole_sizes[i]))
... x2 = min(width, x1 + hole_sizes[i])
... y2 = min(height, y1 + hole_sizes[i])
... holes.append([x1, y1, x2, y2])
...
... # Return holes and random seed
... return {
... "holes": np.array(holes) if holes else np.empty((0, 4), dtype=np.int32),
... "seed": self.py_random.integers(0, 100000)
... }
>>>
>>> # Prepare sample data
>>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
>>> mask = np.random.randint(0, 2, (100, 100), dtype=np.uint8)
>>> bboxes = np.array([[0.1, 0.1, 0.4, 0.4], [0.6, 0.6, 0.9, 0.9]])
>>>
>>> # Create a transform with custom dropout
>>> transform = A.Compose([
... CustomDropout(
... num_holes_range=(3, 6), # Generate 3-6 random holes
... hole_size_range=(5, 15), # Holes of size 5-15 pixels
... fill=0, # Fill holes with black
... fill_mask=1, # Fill mask holes with 1
... p=1.0 # Always apply for this example
... )
... ], bbox_params=A.BboxParams(format='yolo', min_visibility=0.3))
>>>
>>> # Apply the transform
>>> transformed = transform(image=image, mask=mask, bboxes=bboxes)
>>>
>>> # Get the transformed data
>>> dropout_image = transformed["image"] # Image with random holes filled with 0
>>> dropout_mask = transformed["mask"] # Mask with same holes filled with 1
>>> dropout_bboxes = transformed["bboxes"] # Bboxes filtered by visibility threshold
PixelDropoutclass
Drops random pixels from the image. This transform randomly sets pixels in the image to a specified value, effectively "dropping out" those pixels. It can be applied to both the image and its corresponding mask.
Parameters
Name | Type | Default | Description |
---|---|---|---|
dropout_prob | float | 0.01 | Probability of dropping out each pixel. Should be in the range [0, 1]. Default: 0.01 |
per_channel | bool | False | If True, the dropout mask will be generated independently for each channel. If False, the same dropout mask will be applied to all channels. Default: False |
drop_value | One of:
| 0 | Value to assign to the dropped pixels. If None, the value will be randomly sampled for each application: - For uint8 images: Random integer in [0, 255] - For float32 images: Random float in [0, 1] If a single number, that value will be used for all dropped pixels. If a sequence, it should contain one value per channel. Default: 0 |
mask_drop_value | One of:
| None | Value to assign to dropped pixels in the mask. If None, the mask will remain unchanged. If a single number, that value will be used for all dropped pixels in the mask. If a sequence, it should contain one value per channel. Default: None |
p | float | 0.5 | Probability of applying the transform. Should be in the range [0, 1]. Default: 0.5 |
Examples
>>> import numpy as np
>>> import albumentations as A
>>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
>>> mask = np.random.randint(0, 2, (100, 100), dtype=np.uint8)
>>> transform = A.PixelDropout(dropout_prob=0.1, per_channel=True, p=1.0)
>>> result = transform(image=image, mask=mask)
>>> dropped_image, dropped_mask = result['image'], result['mask']
Notes
- When applied to bounding boxes, this transform may cause some boxes to have zero area if all pixels within the box are dropped. Such boxes will be removed. - When applied to keypoints, keypoints that fall on dropped pixels will be removed if the keypoint processor is configured to remove invisible keypoints.