nrtk-albumentations 2.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of nrtk-albumentations might be problematic. Click here for more details.
- albumentations/__init__.py +21 -0
- albumentations/augmentations/__init__.py +23 -0
- albumentations/augmentations/blur/__init__.py +0 -0
- albumentations/augmentations/blur/functional.py +438 -0
- albumentations/augmentations/blur/transforms.py +1633 -0
- albumentations/augmentations/crops/__init__.py +0 -0
- albumentations/augmentations/crops/functional.py +494 -0
- albumentations/augmentations/crops/transforms.py +3647 -0
- albumentations/augmentations/dropout/__init__.py +0 -0
- albumentations/augmentations/dropout/channel_dropout.py +134 -0
- albumentations/augmentations/dropout/coarse_dropout.py +567 -0
- albumentations/augmentations/dropout/functional.py +1017 -0
- albumentations/augmentations/dropout/grid_dropout.py +166 -0
- albumentations/augmentations/dropout/mask_dropout.py +274 -0
- albumentations/augmentations/dropout/transforms.py +461 -0
- albumentations/augmentations/dropout/xy_masking.py +186 -0
- albumentations/augmentations/geometric/__init__.py +0 -0
- albumentations/augmentations/geometric/distortion.py +1238 -0
- albumentations/augmentations/geometric/flip.py +752 -0
- albumentations/augmentations/geometric/functional.py +4151 -0
- albumentations/augmentations/geometric/pad.py +676 -0
- albumentations/augmentations/geometric/resize.py +956 -0
- albumentations/augmentations/geometric/rotate.py +864 -0
- albumentations/augmentations/geometric/transforms.py +1962 -0
- albumentations/augmentations/mixing/__init__.py +0 -0
- albumentations/augmentations/mixing/domain_adaptation.py +787 -0
- albumentations/augmentations/mixing/domain_adaptation_functional.py +453 -0
- albumentations/augmentations/mixing/functional.py +878 -0
- albumentations/augmentations/mixing/transforms.py +832 -0
- albumentations/augmentations/other/__init__.py +0 -0
- albumentations/augmentations/other/lambda_transform.py +180 -0
- albumentations/augmentations/other/type_transform.py +261 -0
- albumentations/augmentations/pixel/__init__.py +0 -0
- albumentations/augmentations/pixel/functional.py +4226 -0
- albumentations/augmentations/pixel/transforms.py +7556 -0
- albumentations/augmentations/spectrogram/__init__.py +0 -0
- albumentations/augmentations/spectrogram/transform.py +220 -0
- albumentations/augmentations/text/__init__.py +0 -0
- albumentations/augmentations/text/functional.py +272 -0
- albumentations/augmentations/text/transforms.py +299 -0
- albumentations/augmentations/transforms3d/__init__.py +0 -0
- albumentations/augmentations/transforms3d/functional.py +393 -0
- albumentations/augmentations/transforms3d/transforms.py +1422 -0
- albumentations/augmentations/utils.py +249 -0
- albumentations/core/__init__.py +0 -0
- albumentations/core/bbox_utils.py +920 -0
- albumentations/core/composition.py +1885 -0
- albumentations/core/hub_mixin.py +299 -0
- albumentations/core/keypoints_utils.py +521 -0
- albumentations/core/label_manager.py +339 -0
- albumentations/core/pydantic.py +239 -0
- albumentations/core/serialization.py +352 -0
- albumentations/core/transforms_interface.py +976 -0
- albumentations/core/type_definitions.py +127 -0
- albumentations/core/utils.py +605 -0
- albumentations/core/validation.py +129 -0
- albumentations/pytorch/__init__.py +1 -0
- albumentations/pytorch/transforms.py +189 -0
- nrtk_albumentations-2.1.0.dist-info/METADATA +196 -0
- nrtk_albumentations-2.1.0.dist-info/RECORD +62 -0
- nrtk_albumentations-2.1.0.dist-info/WHEEL +4 -0
- nrtk_albumentations-2.1.0.dist-info/licenses/LICENSE +21 -0
|
File without changes
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"""Implementation of the Channel Dropout transform for multi-channel images.
|
|
2
|
+
|
|
3
|
+
This module provides the ChannelDropout transform, which randomly drops (sets to a fill value)
|
|
4
|
+
one or more channels in multi-channel images. This augmentation can help models become more
|
|
5
|
+
robust to missing or corrupted channel information and encourage learning from all available
|
|
6
|
+
channels rather than relying on a subset.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from typing import Annotated, Any
|
|
12
|
+
|
|
13
|
+
import numpy as np
|
|
14
|
+
from albucore import get_num_channels
|
|
15
|
+
from pydantic import AfterValidator
|
|
16
|
+
|
|
17
|
+
from albumentations.core.pydantic import check_range_bounds
|
|
18
|
+
from albumentations.core.transforms_interface import BaseTransformInitSchema, ImageOnlyTransform
|
|
19
|
+
|
|
20
|
+
from .functional import channel_dropout
|
|
21
|
+
|
|
22
|
+
__all__ = ["ChannelDropout"]
|
|
23
|
+
|
|
24
|
+
MIN_DROPOUT_CHANNEL_LIST_LENGTH = 2
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class ChannelDropout(ImageOnlyTransform):
|
|
28
|
+
"""Randomly drop channels in the input image.
|
|
29
|
+
|
|
30
|
+
This transform randomly selects a number of channels to drop from the input image
|
|
31
|
+
and replaces them with a specified fill value. This can improve model robustness
|
|
32
|
+
to missing or corrupted channels.
|
|
33
|
+
|
|
34
|
+
The technique is conceptually similar to:
|
|
35
|
+
- Dropout layers in neural networks, which randomly set input units to 0 during training.
|
|
36
|
+
- CoarseDropout augmentation, which drops out regions in the spatial dimensions of the image.
|
|
37
|
+
|
|
38
|
+
However, ChannelDropout operates on the channel dimension, effectively "dropping out"
|
|
39
|
+
entire color channels or feature maps.
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
channel_drop_range (tuple[int, int]): Range from which to choose the number
|
|
43
|
+
of channels to drop. The actual number will be randomly selected from
|
|
44
|
+
the inclusive range [min, max]. Default: (1, 1).
|
|
45
|
+
fill (float): Pixel value used to fill the dropped channels.
|
|
46
|
+
Default: 0.
|
|
47
|
+
p (float): Probability of applying the transform. Must be in the range
|
|
48
|
+
[0, 1]. Default: 0.5.
|
|
49
|
+
|
|
50
|
+
Raises:
|
|
51
|
+
NotImplementedError: If the input image has only one channel.
|
|
52
|
+
ValueError: If the upper bound of channel_drop_range is greater than or
|
|
53
|
+
equal to the number of channels in the input image.
|
|
54
|
+
|
|
55
|
+
Targets:
|
|
56
|
+
image, volume
|
|
57
|
+
|
|
58
|
+
Image types:
|
|
59
|
+
uint8, float32
|
|
60
|
+
|
|
61
|
+
Examples:
|
|
62
|
+
>>> import numpy as np
|
|
63
|
+
>>> import albumentations as A
|
|
64
|
+
>>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
|
|
65
|
+
>>> transform = A.ChannelDropout(channel_drop_range=(1, 2), fill=128, p=1.0)
|
|
66
|
+
>>> result = transform(image=image)
|
|
67
|
+
>>> dropped_image = result['image']
|
|
68
|
+
>>> assert dropped_image.shape == image.shape
|
|
69
|
+
>>> assert np.any(dropped_image != image) # Some channels should be different
|
|
70
|
+
|
|
71
|
+
Note:
|
|
72
|
+
- The number of channels to drop is randomly chosen within the specified range.
|
|
73
|
+
- Channels are randomly selected for dropping.
|
|
74
|
+
- This transform is not applicable to single-channel (grayscale) images.
|
|
75
|
+
- The transform will raise an error if it's not possible to drop the specified
|
|
76
|
+
number of channels (e.g., trying to drop 3 channels from an RGB image).
|
|
77
|
+
- This augmentation can be particularly useful for training models to be robust
|
|
78
|
+
against missing or corrupted channel data in multi-spectral or hyperspectral imagery.
|
|
79
|
+
|
|
80
|
+
"""
|
|
81
|
+
|
|
82
|
+
class InitSchema(BaseTransformInitSchema):
|
|
83
|
+
channel_drop_range: Annotated[tuple[int, int], AfterValidator(check_range_bounds(1, None))]
|
|
84
|
+
fill: float
|
|
85
|
+
|
|
86
|
+
def __init__(
|
|
87
|
+
self,
|
|
88
|
+
channel_drop_range: tuple[int, int] = (1, 1),
|
|
89
|
+
fill: float = 0,
|
|
90
|
+
p: float = 0.5,
|
|
91
|
+
):
|
|
92
|
+
super().__init__(p=p)
|
|
93
|
+
|
|
94
|
+
self.channel_drop_range = channel_drop_range
|
|
95
|
+
self.fill = fill
|
|
96
|
+
|
|
97
|
+
def apply(self, img: np.ndarray, channels_to_drop: list[int], **params: Any) -> np.ndarray:
|
|
98
|
+
"""Apply channel dropout to the image.
|
|
99
|
+
|
|
100
|
+
Args:
|
|
101
|
+
img (np.ndarray): Image to apply channel dropout to.
|
|
102
|
+
channels_to_drop (list[int]): List of channel indices to drop.
|
|
103
|
+
**params (Any): Additional parameters.
|
|
104
|
+
|
|
105
|
+
Returns:
|
|
106
|
+
np.ndarray: Image with dropped channels.
|
|
107
|
+
|
|
108
|
+
"""
|
|
109
|
+
return channel_dropout(img, channels_to_drop, self.fill)
|
|
110
|
+
|
|
111
|
+
def get_params_dependent_on_data(self, params: dict[str, Any], data: dict[str, Any]) -> dict[str, list[int]]:
|
|
112
|
+
"""Get parameters that depend on input data.
|
|
113
|
+
|
|
114
|
+
Args:
|
|
115
|
+
params (dict[str, Any]): Parameters.
|
|
116
|
+
data (dict[str, Any]): Input data.
|
|
117
|
+
|
|
118
|
+
Returns:
|
|
119
|
+
dict[str, list[int]]: Dictionary with channels to drop.
|
|
120
|
+
|
|
121
|
+
"""
|
|
122
|
+
image = data["image"] if "image" in data else data["images"][0]
|
|
123
|
+
num_channels = get_num_channels(image)
|
|
124
|
+
if num_channels == 1:
|
|
125
|
+
msg = "Images has one channel. ChannelDropout is not defined."
|
|
126
|
+
raise NotImplementedError(msg)
|
|
127
|
+
|
|
128
|
+
if self.channel_drop_range[1] >= num_channels:
|
|
129
|
+
msg = "Can not drop all channels in ChannelDropout."
|
|
130
|
+
raise ValueError(msg)
|
|
131
|
+
num_drop_channels = self.py_random.randint(*self.channel_drop_range)
|
|
132
|
+
channels_to_drop = self.py_random.sample(range(num_channels), k=num_drop_channels)
|
|
133
|
+
|
|
134
|
+
return {"channels_to_drop": channels_to_drop}
|