augmentation
sleap_nn.data.augmentation
¶
This module implements data pipeline blocks for augmentation operations.
Classes:
Name | Description |
---|---|
RandomUniformNoise |
Data transformer for applying random uniform noise to input images. |
Functions:
Name | Description |
---|---|
apply_geometric_augmentation |
Apply kornia geometric augmentation on image and instances. |
apply_intensity_augmentation |
Apply kornia intensity augmentation on image and instances. |
RandomUniformNoise
¶
Bases: IntensityAugmentationBase2D
Data transformer for applying random uniform noise to input images.
This is a custom Kornia augmentation inheriting from IntensityAugmentationBase2D
.
Uniform noise within (min_val, max_val) is applied to the entire input image.
Note: Inverse transform is not implemented and re-applying the same transformation in the example below does not work when included in an AugmentationSequential class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
noise
|
Tuple[float, float]
|
2-tuple (min_val, max_val); 0.0 <= min_val <= max_val <= 1.0. |
required |
p
|
float
|
probability for applying an augmentation. This param controls the augmentation probabilities element-wise for a batch. |
0.5
|
p_batch
|
float
|
probability for applying an augmentation to a batch. This param controls the augmentation probabilities batch-wise. |
1.0
|
same_on_batch
|
bool
|
apply the same transformation across the batch. |
False
|
keepdim
|
bool
|
whether to keep the output shape the same as input |
False
|
Examples:
>>> rng = torch.manual_seed(0)
>>> img = torch.rand(1, 1, 2, 2)
>>> RandomUniformNoise(min_val=0., max_val=0.1, p=1.)(img)
tensor([[[[0.9607, 0.5865],
[0.2705, 0.5920]]]])
To apply the exact augmentation again, you may take the advantage of the previous parameter state: >>> input = torch.rand(1, 3, 32, 32) >>> aug = RandomUniformNoise(min_val=0., max_val=0.1, p=1.) >>> (aug(input) == aug(input, params=aug._params)).all() tensor(True)
Ref: kornia.augmentation._2d.intensity.gaussian_noise
<https://kornia.readthedocs.io/en/latest/_modules/kornia/augmentation/_2d/intensity/gaussian_noise.html#RandomGaussianNoise>
_.
Methods:
Name | Description |
---|---|
__init__ |
Initialize the class. |
apply_transform |
Compute the uniform noise, add, and clamp output. |
Source code in sleap_nn/data/augmentation.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
|
__init__(noise, p=0.5, p_batch=1.0, clip_output=True, same_on_batch=False, keepdim=False)
¶
Initialize the class.
Source code in sleap_nn/data/augmentation.py
apply_transform(input, params, flags, transform=None)
¶
Compute the uniform noise, add, and clamp output.
Source code in sleap_nn/data/augmentation.py
apply_geometric_augmentation(image, instances, rotation_min=-15.0, rotation_max=15.0, scale_min=0.9, scale_max=1.1, translate_width=0.02, translate_height=0.02, affine_p=0.0, erase_scale_min=0.0001, erase_scale_max=0.01, erase_ratio_min=1, erase_ratio_max=1, erase_p=0.0, mixup_lambda_min=0.01, mixup_lambda_max=0.05, mixup_p=0.0)
¶
Apply kornia geometric augmentation on image and instances.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image
|
Tensor
|
Input image. Shape: (n_samples, C, H, W) |
required |
instances
|
Tensor
|
Input keypoints. (n_samples, n_instances, n_nodes, 2) or (n_samples, n_nodes, 2) |
required |
rotation_min
|
Optional[float]
|
Minimum rotation angle in degrees. Default: -15.0. |
-15.0
|
rotation_max
|
Optional[float]
|
Maximum rotation angle in degrees. Default: 15.0. |
15.0
|
scale_min
|
Optional[float]
|
Minimum scaling factor for isotropic scaling. Default: 0.9. |
0.9
|
scale_max
|
Optional[float]
|
Maximum scaling factor for isotropic scaling. Default: 1.1. |
1.1
|
translate_width
|
Optional[float]
|
Maximum absolute fraction for horizontal translation. Default: 0.02. |
0.02
|
translate_height
|
Optional[float]
|
Maximum absolute fraction for vertical translation. Default: 0.02. |
0.02
|
affine_p
|
float
|
Probability of applying random affine transformations. Default: 0.0. |
0.0
|
erase_scale_min
|
Optional[float]
|
Minimum value of range of proportion of erased area against input image. Default: 0.0001. |
0.0001
|
erase_scale_max
|
Optional[float]
|
Maximum value of range of proportion of erased area against input image. Default: 0.01. |
0.01
|
erase_ratio_min
|
Optional[float]
|
Minimum value of range of aspect ratio of erased area. Default: 1. |
1
|
erase_ratio_max
|
Optional[float]
|
Maximum value of range of aspect ratio of erased area. Default: 1. |
1
|
erase_p
|
float
|
Probability of applying random erase. Default: 0.0. |
0.0
|
mixup_lambda_min
|
Optional[float]
|
Minimum mixup strength value. Default: 0.01. |
0.01
|
mixup_lambda_max
|
Optional[float]
|
Maximum mixup strength value. Default: 0.05. |
0.05
|
mixup_p
|
float
|
Probability of applying random mixup v2. Default: 0.0. |
0.0
|
Returns:
Type | Description |
---|---|
Tuple[Tensor]
|
Returns tuple: (image, instances) with augmentation applied. |
Source code in sleap_nn/data/augmentation.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
|
apply_intensity_augmentation(image, instances, uniform_noise_min=0.0, uniform_noise_max=0.04, uniform_noise_p=0.0, gaussian_noise_mean=0.02, gaussian_noise_std=0.004, gaussian_noise_p=0.0, contrast_min=0.5, contrast_max=2.0, contrast_p=0.0, brightness_min=1.0, brightness_max=1.0, brightness_p=0.0)
¶
Apply kornia intensity augmentation on image and instances.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image
|
Tensor
|
Input image. Shape: (n_samples, C, H, W) |
required |
instances
|
Tensor
|
Input keypoints. (n_samples, n_instances, n_nodes, 2) or (n_samples, n_nodes, 2) |
required |
uniform_noise_min
|
Optional[float]
|
Minimum value for uniform noise (uniform_noise_min >=0). |
0.0
|
uniform_noise_max
|
Optional[float]
|
Maximum value for uniform noise (uniform_noise_max <=1). |
0.04
|
uniform_noise_p
|
float
|
Probability of applying random uniform noise. |
0.0
|
gaussian_noise_mean
|
Optional[float]
|
The mean of the gaussian distribution. |
0.02
|
gaussian_noise_std
|
Optional[float]
|
The standard deviation of the gaussian distribution. |
0.004
|
gaussian_noise_p
|
float
|
Probability of applying random gaussian noise. |
0.0
|
contrast_min
|
Optional[float]
|
Minimum contrast factor to apply. Default: 0.5. |
0.5
|
contrast_max
|
Optional[float]
|
Maximum contrast factor to apply. Default: 2.0. |
2.0
|
contrast_p
|
float
|
Probability of applying random contrast. |
0.0
|
brightness_min
|
Optional[float]
|
Minimum brightness factor to apply. Default: 1.0. |
1.0
|
brightness_max
|
Optional[float]
|
Maximum brightness factor to apply. Default: 1.0. |
1.0
|
brightness_p
|
float
|
Probability of applying random brightness. |
0.0
|
Returns:
Type | Description |
---|---|
Tuple[Tensor]
|
Returns tuple: (image, instances) with augmentation applied. |