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.

Files changed (62) hide show
  1. albumentations/__init__.py +21 -0
  2. albumentations/augmentations/__init__.py +23 -0
  3. albumentations/augmentations/blur/__init__.py +0 -0
  4. albumentations/augmentations/blur/functional.py +438 -0
  5. albumentations/augmentations/blur/transforms.py +1633 -0
  6. albumentations/augmentations/crops/__init__.py +0 -0
  7. albumentations/augmentations/crops/functional.py +494 -0
  8. albumentations/augmentations/crops/transforms.py +3647 -0
  9. albumentations/augmentations/dropout/__init__.py +0 -0
  10. albumentations/augmentations/dropout/channel_dropout.py +134 -0
  11. albumentations/augmentations/dropout/coarse_dropout.py +567 -0
  12. albumentations/augmentations/dropout/functional.py +1017 -0
  13. albumentations/augmentations/dropout/grid_dropout.py +166 -0
  14. albumentations/augmentations/dropout/mask_dropout.py +274 -0
  15. albumentations/augmentations/dropout/transforms.py +461 -0
  16. albumentations/augmentations/dropout/xy_masking.py +186 -0
  17. albumentations/augmentations/geometric/__init__.py +0 -0
  18. albumentations/augmentations/geometric/distortion.py +1238 -0
  19. albumentations/augmentations/geometric/flip.py +752 -0
  20. albumentations/augmentations/geometric/functional.py +4151 -0
  21. albumentations/augmentations/geometric/pad.py +676 -0
  22. albumentations/augmentations/geometric/resize.py +956 -0
  23. albumentations/augmentations/geometric/rotate.py +864 -0
  24. albumentations/augmentations/geometric/transforms.py +1962 -0
  25. albumentations/augmentations/mixing/__init__.py +0 -0
  26. albumentations/augmentations/mixing/domain_adaptation.py +787 -0
  27. albumentations/augmentations/mixing/domain_adaptation_functional.py +453 -0
  28. albumentations/augmentations/mixing/functional.py +878 -0
  29. albumentations/augmentations/mixing/transforms.py +832 -0
  30. albumentations/augmentations/other/__init__.py +0 -0
  31. albumentations/augmentations/other/lambda_transform.py +180 -0
  32. albumentations/augmentations/other/type_transform.py +261 -0
  33. albumentations/augmentations/pixel/__init__.py +0 -0
  34. albumentations/augmentations/pixel/functional.py +4226 -0
  35. albumentations/augmentations/pixel/transforms.py +7556 -0
  36. albumentations/augmentations/spectrogram/__init__.py +0 -0
  37. albumentations/augmentations/spectrogram/transform.py +220 -0
  38. albumentations/augmentations/text/__init__.py +0 -0
  39. albumentations/augmentations/text/functional.py +272 -0
  40. albumentations/augmentations/text/transforms.py +299 -0
  41. albumentations/augmentations/transforms3d/__init__.py +0 -0
  42. albumentations/augmentations/transforms3d/functional.py +393 -0
  43. albumentations/augmentations/transforms3d/transforms.py +1422 -0
  44. albumentations/augmentations/utils.py +249 -0
  45. albumentations/core/__init__.py +0 -0
  46. albumentations/core/bbox_utils.py +920 -0
  47. albumentations/core/composition.py +1885 -0
  48. albumentations/core/hub_mixin.py +299 -0
  49. albumentations/core/keypoints_utils.py +521 -0
  50. albumentations/core/label_manager.py +339 -0
  51. albumentations/core/pydantic.py +239 -0
  52. albumentations/core/serialization.py +352 -0
  53. albumentations/core/transforms_interface.py +976 -0
  54. albumentations/core/type_definitions.py +127 -0
  55. albumentations/core/utils.py +605 -0
  56. albumentations/core/validation.py +129 -0
  57. albumentations/pytorch/__init__.py +1 -0
  58. albumentations/pytorch/transforms.py +189 -0
  59. nrtk_albumentations-2.1.0.dist-info/METADATA +196 -0
  60. nrtk_albumentations-2.1.0.dist-info/RECORD +62 -0
  61. nrtk_albumentations-2.1.0.dist-info/WHEEL +4 -0
  62. nrtk_albumentations-2.1.0.dist-info/licenses/LICENSE +21 -0
File without changes
@@ -0,0 +1,180 @@
1
+ """Lambda transform module for creating custom user-defined transformations.
2
+
3
+ This module provides a flexible transform class that allows users to define their own
4
+ custom transformation functions for different targets (image, mask, keypoints, bboxes).
5
+ It's particularly useful for implementing custom logic that isn't available in the
6
+ standard transforms.
7
+
8
+ The Lambda transform accepts different callable functions for each target type and
9
+ applies them when the transform is executed. This allows for maximum flexibility
10
+ while maintaining compatibility with the Albumentations pipeline structure.
11
+
12
+ Key features:
13
+ - Apply different custom functions to different target types
14
+ - Compatible with all Albumentations pipeline features
15
+ - Support for all image types and formats
16
+ - Ability to handle any number of channels
17
+ - Warning system for lambda expressions and multiprocessing compatibility
18
+
19
+ Note that using actual lambda expressions (rather than named functions) can cause
20
+ issues with multiprocessing, as lambdas cannot be properly pickled.
21
+ """
22
+
23
+ from __future__ import annotations
24
+
25
+ import warnings
26
+ from types import LambdaType
27
+ from typing import Any, Callable
28
+
29
+ import numpy as np
30
+
31
+ from albumentations.augmentations.pixel import functional as fpixel
32
+ from albumentations.core.transforms_interface import NoOp
33
+ from albumentations.core.utils import format_args
34
+
35
+ __all__ = ["Lambda"]
36
+
37
+
38
+ class Lambda(NoOp):
39
+ """A flexible transformation class for using user-defined transformation functions per targets.
40
+ Function signature must include **kwargs to accept optional arguments like interpolation method, image size, etc:
41
+
42
+ Args:
43
+ image (Callable[..., Any] | None): Image transformation function.
44
+ mask (Callable[..., Any] | None): Mask transformation function.
45
+ keypoints (Callable[..., Any] | None): Keypoints transformation function.
46
+ bboxes (Callable[..., Any] | None): BBoxes transformation function.
47
+ p (float): probability of applying the transform. Default: 1.0.
48
+
49
+ Targets:
50
+ image, mask, bboxes, keypoints, volume, mask3d
51
+
52
+ Image types:
53
+ uint8, float32
54
+
55
+ Number of channels:
56
+ Any
57
+
58
+ """
59
+
60
+ def __init__(
61
+ self,
62
+ image: Callable[..., Any] | None = None,
63
+ mask: Callable[..., Any] | None = None,
64
+ keypoints: Callable[..., Any] | None = None,
65
+ bboxes: Callable[..., Any] | None = None,
66
+ name: str | None = None,
67
+ p: float = 1.0,
68
+ ):
69
+ super().__init__(p=p)
70
+
71
+ self.name = name
72
+ self.custom_apply_fns = dict.fromkeys(("image", "mask", "keypoints", "bboxes"), fpixel.noop)
73
+ for target_name, custom_apply_fn in {
74
+ "image": image,
75
+ "mask": mask,
76
+ "keypoints": keypoints,
77
+ "bboxes": bboxes,
78
+ }.items():
79
+ if custom_apply_fn is not None:
80
+ if isinstance(custom_apply_fn, LambdaType) and custom_apply_fn.__name__ == "<lambda>":
81
+ warnings.warn(
82
+ "Using lambda is incompatible with multiprocessing. "
83
+ "Consider using regular functions or partial().",
84
+ stacklevel=2,
85
+ )
86
+
87
+ self.custom_apply_fns[target_name] = custom_apply_fn
88
+
89
+ def apply(self, img: np.ndarray, **params: Any) -> np.ndarray:
90
+ """Apply the Lambda transform to the input image.
91
+
92
+ Args:
93
+ img (np.ndarray): The input image to apply the Lambda transform to.
94
+ **params (Any): Additional parameters (not used in this transform).
95
+
96
+ Returns:
97
+ np.ndarray: The image with the applied Lambda transform.
98
+
99
+ """
100
+ fn = self.custom_apply_fns["image"]
101
+ return fn(img, **params)
102
+
103
+ def apply_to_mask(self, mask: np.ndarray, **params: Any) -> np.ndarray:
104
+ """Apply the Lambda transform to the input mask.
105
+
106
+ Args:
107
+ mask (np.ndarray): The input mask to apply the Lambda transform to.
108
+ **params (Any): Additional parameters (not used in this transform).
109
+
110
+ Returns:
111
+ np.ndarray: The mask with the applied Lambda transform.
112
+
113
+ """
114
+ fn = self.custom_apply_fns["mask"]
115
+ return fn(mask, **params)
116
+
117
+ def apply_to_bboxes(self, bboxes: np.ndarray, **params: Any) -> np.ndarray:
118
+ """Apply the Lambda transform to the input bounding boxes.
119
+
120
+ Args:
121
+ bboxes (np.ndarray): The input bounding boxes to apply the Lambda transform to.
122
+ **params (Any): Additional parameters (not used in this transform).
123
+
124
+ Returns:
125
+ np.ndarray: The bounding boxes with the applied Lambda transform.
126
+
127
+ """
128
+ fn = self.custom_apply_fns["bboxes"]
129
+ return fn(bboxes, **params)
130
+
131
+ def apply_to_keypoints(self, keypoints: np.ndarray, **params: Any) -> np.ndarray:
132
+ """Apply the Lambda transform to the input keypoints.
133
+
134
+ Args:
135
+ keypoints (np.ndarray): The input keypoints to apply the Lambda transform to.
136
+ **params (Any): Additional parameters (not used in this transform).
137
+
138
+ Returns:
139
+ np.ndarray: The keypoints with the applied Lambda transform.
140
+
141
+ """
142
+ fn = self.custom_apply_fns["keypoints"]
143
+ return fn(keypoints, **params)
144
+
145
+ @classmethod
146
+ def is_serializable(cls) -> bool:
147
+ """Check if the Lambda transform is serializable.
148
+
149
+ Returns:
150
+ bool: True if the transform is serializable, False otherwise.
151
+
152
+ """
153
+ return False
154
+
155
+ def to_dict_private(self) -> dict[str, Any]:
156
+ """Convert the Lambda transform to a dictionary.
157
+
158
+ Returns:
159
+ dict[str, Any]: The dictionary representation of the transform.
160
+
161
+ """
162
+ if self.name is None:
163
+ msg = (
164
+ "To make a Lambda transform serializable you should provide the `name` argument, "
165
+ "e.g. `Lambda(name='my_transform', image=<some func>, ...)`."
166
+ )
167
+ raise ValueError(msg)
168
+ return {"__class_fullname__": self.get_class_fullname(), "__name__": self.name}
169
+
170
+ def __repr__(self) -> str:
171
+ """Return the string representation of the Lambda transform.
172
+
173
+ Returns:
174
+ str: The string representation of the Lambda transform.
175
+
176
+ """
177
+ state = {"name": self.name}
178
+ state.update(self.custom_apply_fns.items()) # type: ignore[arg-type]
179
+ state.update(self.get_base_init_args())
180
+ return f"{self.__class__.__name__}({format_args(state)})"
@@ -0,0 +1,261 @@
1
+ """Transforms for type conversion between float and other data types.
2
+
3
+ This module provides transform classes for converting image data types, primarily
4
+ for converting between floating point and integer representations. These transforms
5
+ are useful for preprocessing before neural network input (ToFloat) and for converting
6
+ network outputs back to standard image formats (FromFloat).
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ from typing import Any
12
+
13
+ import numpy as np
14
+ from albucore import (
15
+ from_float,
16
+ get_max_value,
17
+ to_float,
18
+ )
19
+ from pydantic import (
20
+ model_validator,
21
+ )
22
+ from typing_extensions import Literal, Self
23
+
24
+ from albumentations.core.transforms_interface import (
25
+ BaseTransformInitSchema,
26
+ ImageOnlyTransform,
27
+ )
28
+
29
+ __all__ = [
30
+ "FromFloat",
31
+ "ToFloat",
32
+ ]
33
+
34
+
35
+ class ToFloat(ImageOnlyTransform):
36
+ """Convert the input image to a floating-point representation.
37
+
38
+ This transform divides pixel values by `max_value` to get a float32 output array
39
+ where all values lie in the range [0, 1.0]. It's useful for normalizing image data
40
+ before feeding it into neural networks or other algorithms that expect float input.
41
+
42
+ Args:
43
+ max_value (float | None): The maximum possible input value. If None, the transform
44
+ will try to infer the maximum value by inspecting the data type of the input image:
45
+ - uint8: 255
46
+ - uint16: 65535
47
+ - uint32: 4294967295
48
+ - float32: 1.0
49
+ Default: None.
50
+ p (float): Probability of applying the transform. Default: 1.0.
51
+
52
+ Targets:
53
+ image, volume
54
+
55
+ Image types:
56
+ uint8, uint16, uint32, float32
57
+
58
+ Returns:
59
+ np.ndarray: Image in floating point representation, with values in range [0, 1.0].
60
+
61
+ Note:
62
+ - If the input image is already float32 with values in [0, 1], it will be returned unchanged.
63
+ - For integer types (uint8, uint16, uint32), the function will scale the values to [0, 1] range.
64
+ - The output will always be float32, regardless of the input type.
65
+ - This transform is often used as a preprocessing step before applying other transformations
66
+ or feeding the image into a neural network.
67
+
68
+ Raises:
69
+ TypeError: If the input image data type is not supported.
70
+
71
+ Examples:
72
+ >>> import numpy as np
73
+ >>> import albumentations as A
74
+ >>>
75
+ # Convert uint8 image to float
76
+ >>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
77
+ >>> transform = A.ToFloat(max_value=None)
78
+ >>> float_image = transform(image=image)['image']
79
+ >>> assert float_image.dtype == np.float32
80
+ >>> assert 0 <= float_image.min() <= float_image.max() <= 1.0
81
+ >>>
82
+ # Convert uint16 image to float with custom max_value
83
+ >>> image = np.random.randint(0, 4096, (100, 100, 3), dtype=np.uint16)
84
+ >>> transform = A.ToFloat(max_value=4095)
85
+ >>> float_image = transform(image=image)['image']
86
+ >>> assert float_image.dtype == np.float32
87
+ >>> assert 0 <= float_image.min() <= float_image.max() <= 1.0
88
+
89
+ See Also:
90
+ FromFloat: The inverse operation, converting from float back to the original data type.
91
+
92
+ """
93
+
94
+ class InitSchema(BaseTransformInitSchema):
95
+ max_value: float | None
96
+
97
+ def __init__(
98
+ self,
99
+ max_value: float | None = None,
100
+ p: float = 1.0,
101
+ ):
102
+ super().__init__(p=p)
103
+ self.max_value = max_value
104
+
105
+ def apply(self, img: np.ndarray, **params: Any) -> np.ndarray:
106
+ """Apply the ToFloat transform to the input image.
107
+
108
+ Args:
109
+ img (np.ndarray): The input image to apply the ToFloat transform to.
110
+ **params (Any): Additional parameters (not used in this transform).
111
+
112
+ Returns:
113
+ np.ndarray: The image with the applied ToFloat transform.
114
+
115
+ """
116
+ return to_float(img, self.max_value)
117
+
118
+ def apply_to_images(self, images: np.ndarray, **params: Any) -> np.ndarray:
119
+ """Apply the ToFloat transform to the input batch of images.
120
+
121
+ Args:
122
+ images (np.ndarray): The input batch of images to apply the ToFloat transform to.
123
+ **params (Any): Additional parameters (not used in this transform).
124
+
125
+ Returns:
126
+ np.ndarray: The batch of images with the applied ToFloat transform.
127
+
128
+ """
129
+ return to_float(images, self.max_value)
130
+
131
+ def apply_to_volume(self, volume: np.ndarray, **params: Any) -> np.ndarray:
132
+ """Apply the ToFloat transform to the input volume.
133
+
134
+ Args:
135
+ volume (np.ndarray): The input volume to apply the ToFloat transform to.
136
+ **params (Any): Additional parameters (not used in this transform).
137
+
138
+ Returns:
139
+ np.ndarray: The volume with the applied ToFloat transform.
140
+
141
+ """
142
+ return self.apply_to_images(volume, **params)
143
+
144
+ def apply_to_volumes(self, volumes: np.ndarray, **params: Any) -> np.ndarray:
145
+ """Apply the ToFloat transform to the input volumes.
146
+
147
+ Args:
148
+ volumes (np.ndarray): The input volumes to apply the ToFloat transform to.
149
+ **params (Any): Additional parameters (not used in this transform).
150
+
151
+ Returns:
152
+ np.ndarray: The volumes with the applied ToFloat transform.
153
+
154
+ """
155
+ return self.apply_to_images(volumes, **params)
156
+
157
+
158
+ class FromFloat(ImageOnlyTransform):
159
+ """Convert an image from floating point representation to the specified data type.
160
+
161
+ This transform is designed to convert images from a normalized floating-point representation
162
+ (typically with values in the range [0, 1]) to other data types, scaling the values appropriately.
163
+
164
+ Args:
165
+ dtype (str): The desired output data type. Supported types include 'uint8', 'uint16',
166
+ 'uint32'. Default: 'uint8'.
167
+ max_value (float | None): The maximum value for the output dtype. If None, the transform
168
+ will attempt to infer the maximum value based on the dtype.
169
+ Default: None.
170
+ p (float): Probability of applying the transform. Default: 1.0.
171
+
172
+ Targets:
173
+ image, volume
174
+
175
+ Image types:
176
+ float32, float64
177
+
178
+ Note:
179
+ - This is the inverse transform for ToFloat.
180
+ - Input images are expected to be in floating point format with values in the range [0, 1].
181
+ - For integer output types (uint8, uint16, uint32), the function will scale the values
182
+ to the appropriate range (e.g., 0-255 for uint8).
183
+ - For float output types (float32, float64), the values will remain in the [0, 1] range.
184
+ - The transform uses the `from_float` function internally, which ensures output values
185
+ are within the valid range for the specified dtype.
186
+
187
+ Examples:
188
+ >>> import numpy as np
189
+ >>> import albumentations as A
190
+ >>> transform = A.FromFloat(dtype='uint8', max_value=None, p=1.0)
191
+ >>> image = np.random.rand(100, 100, 3).astype(np.float32) # Float image in [0, 1] range
192
+ >>> result = transform(image=image)
193
+ >>> uint8_image = result['image']
194
+ >>> assert uint8_image.dtype == np.uint8
195
+ >>> assert uint8_image.min() >= 0 and uint8_image.max() <= 255
196
+
197
+ """
198
+
199
+ class InitSchema(BaseTransformInitSchema):
200
+ dtype: Literal["uint8", "uint16", "uint32"]
201
+ max_value: float | None
202
+
203
+ @model_validator(mode="after")
204
+ def _update_max_value(self) -> Self:
205
+ if self.max_value is None:
206
+ self.max_value = get_max_value(np.dtype(self.dtype))
207
+
208
+ return self
209
+
210
+ def __init__(
211
+ self,
212
+ dtype: Literal["uint8", "uint16", "uint32"] = "uint8",
213
+ max_value: float | None = None,
214
+ p: float = 1.0,
215
+ ):
216
+ super().__init__(p=p)
217
+ self.dtype = dtype
218
+ self.max_value = max_value
219
+
220
+ def apply(self, img: np.ndarray, **params: Any) -> np.ndarray:
221
+ """Apply the FromFloat transform to the input image.
222
+
223
+ Args:
224
+ img (np.ndarray): The input image to apply the FromFloat transform to.
225
+ **params (Any): Additional parameters (not used in this transform).
226
+
227
+ Returns:
228
+ np.ndarray: The image with the applied FromFloat transform.
229
+
230
+ """
231
+ return from_float(img, np.dtype(self.dtype), self.max_value)
232
+
233
+ def apply_to_images(self, images: np.ndarray, **params: Any) -> np.ndarray:
234
+ """Apply the FromFloat transform to the input images.
235
+
236
+ Args:
237
+ images (np.ndarray): The input images to apply the FromFloat transform to.
238
+ **params (Any): Additional parameters (not used in this transform).
239
+
240
+ """
241
+ return from_float(images, np.dtype(self.dtype), self.max_value)
242
+
243
+ def apply_to_volume(self, volume: np.ndarray, **params: Any) -> np.ndarray:
244
+ """Apply the FromFloat transform to the input volume.
245
+
246
+ Args:
247
+ volume (np.ndarray): The input volume to apply the FromFloat transform to.
248
+ **params (Any): Additional parameters (not used in this transform).
249
+
250
+ """
251
+ return self.apply_to_images(volume, **params)
252
+
253
+ def apply_to_volumes(self, volumes: np.ndarray, **params: Any) -> np.ndarray:
254
+ """Apply the FromFloat transform to the input volumes.
255
+
256
+ Args:
257
+ volumes (np.ndarray): The input volumes to apply the FromFloat transform to.
258
+ **params (Any): Additional parameters (not used in this transform).
259
+
260
+ """
261
+ return self.apply_to_images(volumes, **params)
File without changes