Hex Augmentations¶
Geometric Transformations¶
flyvision.datasets.augmentation.hex.HexRotate ¶
Bases: Augmentation
Rotate a sequence of regular hex-lattices by multiple of 60 degree.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
extent |
int
|
Extent of the regular hexagonal grid in columns. |
required |
n_rot |
int
|
Number of 60 degree rotations. 0-5. |
0
|
p_rot |
float
|
Probability of rotating. If None, no rotation is performed. |
0.5
|
Attributes:
Name | Type | Description |
---|---|---|
extent |
int
|
Extent of the regular hexagonal grid in columns. |
n_rot |
int
|
Number of 60 degree rotations. 0-5. |
p_rot |
float
|
Probability of rotating. |
permutation_indices |
dict
|
Cached indices for rotation. |
rotation_matrices |
dict
|
Cached rotation matrices for rotation. |
Source code in flyvision/datasets/augmentation/hex.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
|
rotate ¶
rotate(seq)
Rotate a sequence on a regular hexagonal lattice.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seq |
Tensor
|
Sequence of shape (frames, dims, hexals). |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Rotated sequence of the same shape as seq. |
Source code in flyvision/datasets/augmentation/hex.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
|
transform ¶
transform(seq, n_rot=None)
Rotate a sequence on a regular hexagonal lattice.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seq |
Tensor
|
Sequence of shape (frames, dims, hexals). |
required |
n_rot |
Optional[int]
|
Optional number of rotations to apply. |
None
|
Returns:
Type | Description |
---|---|
Tensor
|
Rotated sequence of the same shape as seq. |
Source code in flyvision/datasets/augmentation/hex.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
|
set_or_sample ¶
set_or_sample(n_rot=None)
Set or sample the number of rotations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_rot |
Optional[int]
|
Number of rotations to set. If None, sample randomly. |
None
|
Source code in flyvision/datasets/augmentation/hex.py
95 96 97 98 99 100 101 102 103 104 105 106 107 |
|
flyvision.datasets.augmentation.hex.HexFlip ¶
Bases: Augmentation
Flip a sequence of regular hex-lattices across one of three hex-axes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
extent |
int
|
Extent of the regular hexagonal grid. |
required |
axis |
int
|
Flipping axis. 0 corresponds to no flipping. |
0
|
p_flip |
float
|
Probability of flipping. If None, no flipping is performed. |
0.5
|
flip_axes |
List[int]
|
List of valid flipping axes. Can contain 0, 1, 2, 3. |
[0, 1, 2, 3]
|
Attributes:
Name | Type | Description |
---|---|---|
extent |
int
|
Extent of the regular hexagonal grid. |
axis |
int
|
Flipping axis. |
p_flip |
float
|
Probability of flipping. |
flip_axes |
List[int]
|
List of valid flipping axes. |
permutation_indices |
dict
|
Cached indices for flipping. |
rotation_matrices |
dict
|
Cached rotation matrices for flipping. |
Note
This is to avoid redundant transformations from rotation and flipping. For example, flipping across the 1st axis is equivalent to rotating by 240 degrees and flipping across the 2nd axis.
Source code in flyvision/datasets/augmentation/hex.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 206 207 208 209 |
|
flip ¶
flip(seq)
Flip a sequence on a regular hexagonal lattice.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seq |
Tensor
|
Sequence of shape (frames, dims, hexals). |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Flipped sequence of the same shape as seq. |
Source code in flyvision/datasets/augmentation/hex.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
|
transform ¶
transform(seq, axis=None)
Flip a sequence on a regular hexagonal lattice.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seq |
Tensor
|
Sequence of shape (frames, dims, hexals). |
required |
axis |
Optional[int]
|
Optional flipping axis to apply. |
None
|
Returns:
Type | Description |
---|---|
Tensor
|
Flipped sequence of the same shape as seq. |
Source code in flyvision/datasets/augmentation/hex.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
|
set_or_sample ¶
set_or_sample(axis=None)
Set or sample the flipping axis.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
axis |
Optional[int]
|
Flipping axis to set. If None, sample randomly. |
None
|
Source code in flyvision/datasets/augmentation/hex.py
197 198 199 200 201 202 203 204 205 206 207 208 209 |
|
Intensity Transformations¶
flyvision.datasets.augmentation.hex.ContrastBrightness ¶
Bases: Augmentation
Contrast transformation.
The transformation is described as:
pixel = max(0, contrast_factor * (pixel - 0.5) + 0.5
+ contrast_factor * brightness_factor)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
contrast_factor |
Optional[float]
|
Contrast factor. |
None
|
brightness_factor |
Optional[float]
|
Brightness factor. |
None
|
contrast_std |
float
|
Standard deviation of the contrast factor. |
0.2
|
brightness_std |
float
|
Standard deviation of the brightness factor. |
0.1
|
Attributes:
Name | Type | Description |
---|---|---|
contrast_std |
float
|
Standard deviation of the contrast factor. |
brightness_std |
float
|
Standard deviation of the brightness factor. |
contrast_factor |
float
|
Contrast factor. |
brightness_factor |
float
|
Brightness factor. |
Source code in flyvision/datasets/augmentation/hex.py
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 284 285 286 287 |
|
transform ¶
transform(seq)
Apply the transformation to a sequence.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seq |
Tensor
|
Input sequence. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Transformed sequence. |
Source code in flyvision/datasets/augmentation/hex.py
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
|
set_or_sample ¶
set_or_sample(contrast_factor=None, brightness_factor=None)
Set or sample contrast and brightness factors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
contrast_factor |
Optional[float]
|
Contrast factor to set. If None, sample randomly. |
None
|
brightness_factor |
Optional[float]
|
Brightness factor to set. If None, sample randomly. |
None
|
Source code in flyvision/datasets/augmentation/hex.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
|
flyvision.datasets.augmentation.hex.PixelNoise ¶
Bases: Augmentation
Pixelwise gaussian noise.
The transformation is described as:
pixel = pixel + N(0, std)
It biases the signal to noise ratio: high for light, low for dark pixels.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
std |
float
|
Standard deviation of the gaussian noise. |
0.08
|
Attributes:
Name | Type | Description |
---|---|---|
std |
float
|
Standard deviation of the gaussian noise. |
Source code in flyvision/datasets/augmentation/hex.py
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
|
transform ¶
transform(seq)
Apply the transformation to a sequence.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seq |
Tensor
|
Input sequence. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Transformed sequence. |
Source code in flyvision/datasets/augmentation/hex.py
310 311 312 313 314 315 316 317 318 319 320 321 322 |
|
set_or_sample ¶
set_or_sample(std=None)
Set or sample the standard deviation of the gaussian noise.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
std |
Optional[float]
|
Standard deviation of the gaussian noise to set. If None, no change is made. |
None
|
Source code in flyvision/datasets/augmentation/hex.py
324 325 326 327 328 329 330 331 332 333 |
|
flyvision.datasets.augmentation.hex.GammaCorrection ¶
Bases: Augmentation
Gamma correction.
The transformation is described as:
pixel = pixel ** gamma
Gamma > 1 increases the contrast, gamma < 1 decreases the contrast.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
gamma |
float
|
Gamma value. |
1
|
std |
Optional[float]
|
Standard deviation of the gamma value. |
None
|
Attributes:
Name | Type | Description |
---|---|---|
gamma |
float Gamma value. |
|
std |
Optional[float] Standard deviation of the gamma value. |
Source code in flyvision/datasets/augmentation/hex.py
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
|
transform ¶
transform(seq)
Apply the transformation to a sequence.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seq |
Tensor
|
Input sequence. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Transformed sequence. |
Source code in flyvision/datasets/augmentation/hex.py
361 362 363 364 365 366 367 368 369 370 371 372 |
|
set_or_sample ¶
set_or_sample(gamma=None)
Set or sample the gamma value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
gamma |
Optional[float]
|
Gamma value to set. If None, sample randomly. |
None
|
Source code in flyvision/datasets/augmentation/hex.py
379 380 381 382 383 384 385 386 387 |
|
Temporal Augmentations¶
flyvision.datasets.augmentation.temporal.Interpolate ¶
Bases: Augmentation
Interpolate a sequence to a target framerate.
Attributes:
Name | Type | Description |
---|---|---|
original_framerate |
int
|
The original framerate of the sequence. |
target_framerate |
float
|
The target framerate after interpolation. |
mode |
str
|
The interpolation mode. |
align_corners |
bool | None
|
Alignment of corners for interpolation. |
Source code in flyvision/datasets/augmentation/temporal.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
|
transform ¶
transform(sequence, dim=0)
Resample the sequence along the specified dimension.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sequence |
Tensor
|
Sequence to resample of ndim == 3. |
required |
dim |
int
|
Dimension along which to resample. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: Resampled sequence. |
Raises:
Type | Description |
---|---|
AssertionError
|
If the input sequence is not 3D. |
Source code in flyvision/datasets/augmentation/temporal.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
|
piecewise_constant_indices ¶
piecewise_constant_indices(length)
Return indices to sample from a sequence with piecewise constant interpolation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
length |
int
|
Length of the original sequence. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: Indices for piecewise constant interpolation. |
Source code in flyvision/datasets/augmentation/temporal.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
|
flyvision.datasets.augmentation.temporal.CropFrames ¶
Bases: Augmentation
Crop frames from a sequence.
Attributes:
Name | Type | Description |
---|---|---|
n_frames |
int
|
Number of frames to crop. |
all_frames |
bool
|
Whether to return all frames. |
start |
int
|
Starting frame for cropping. |
random |
bool
|
Whether to use random cropping. |
Source code in flyvision/datasets/augmentation/temporal.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 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 |
|
transform ¶
transform(sequence, dim=0)
Crop the sequence along the specified dimension.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sequence |
Tensor
|
Sequence to crop of shape (…, n_frames, …). |
required |
dim |
int
|
Dimension along which to crop. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: Cropped sequence. |
Raises:
Type | Description |
---|---|
ValueError
|
If n_frames is greater than the total sequence length. |
Source code in flyvision/datasets/augmentation/temporal.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
|
set_or_sample ¶
set_or_sample(start=None, total_sequence_length=None)
Set or sample the starting frame for cropping.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start |
int | None
|
Starting frame for cropping. |
None
|
total_sequence_length |
int | None
|
Total length of the sequence. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If n_frames is greater than the total sequence length. |
Source code in flyvision/datasets/augmentation/temporal.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
|
Base Class¶
flyvision.datasets.augmentation.augmentation.Augmentation ¶
Base class for data augmentation operations.
This class provides a framework for implementing various data augmentation
techniques. Subclasses should override the transform
method to implement
specific augmentation logic.
Attributes:
Name | Type | Description |
---|---|---|
augment |
bool
|
Flag to enable or disable augmentation. |
Source code in flyvision/datasets/augmentation/augmentation.py
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
|
__call__ ¶
__call__(seq, *args, **kwargs)
Apply augmentation to the input sequence if enabled.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seq |
Sequence[Any]
|
Input sequence to be augmented. |
required |
*args |
Any
|
Additional positional arguments. |
()
|
**kwargs |
Any
|
Additional keyword arguments. |
{}
|
Returns:
Type | Description |
---|---|
Sequence[Any]
|
Augmented sequence if augmentation is enabled, otherwise the original sequence. |
Source code in flyvision/datasets/augmentation/augmentation.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
transform ¶
transform(seq, *args, **kwargs)
Apply the augmentation transformation to the input sequence.
This method should be overridden by subclasses to implement specific augmentation logic.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seq |
Sequence[Any]
|
Input sequence to be transformed. |
required |
*args |
Any
|
Additional positional arguments. |
()
|
**kwargs |
Any
|
Additional keyword arguments. |
{}
|
Returns:
Type | Description |
---|---|
Sequence[Any]
|
Transformed sequence. |
Source code in flyvision/datasets/augmentation/augmentation.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
|
set_or_sample ¶
set_or_sample(*args)
Set or sample augmentation parameters.
This method can be used to set fixed parameters or sample random parameters for the augmentation process.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args |
Any
|
Arguments for setting or sampling parameters. |
()
|
Source code in flyvision/datasets/augmentation/augmentation.py
52 53 54 55 56 57 58 59 60 61 62 |
|
Utils¶
flyvision.datasets.augmentation.utils ¶
rotation_matrix ¶
rotation_matrix(angle_in_rad, three_d=False)
Generate a rotation matrix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
angle_in_rad |
float
|
Rotation angle in radians. |
required |
three_d |
bool
|
If True, generate a 3D rotation matrix. |
False
|
Returns:
Type | Description |
---|---|
Tensor
|
Rotation matrix as a torch.Tensor. |
Source code in flyvision/datasets/augmentation/utils.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
rotation_permutation_index ¶
rotation_permutation_index(extent, n_rot)
Calculate rotation permutation indices for hex coordinates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
extent |
int
|
Extent of the regular hexagonal grid. |
required |
n_rot |
int
|
Number of 60-degree rotations. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Permutation indices as a torch.Tensor. |
Source code in flyvision/datasets/augmentation/utils.py
37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
rotate_Nx60 ¶
rotate_Nx60(u, v, n)
Rotate hex coordinates by multiples of 60 degrees.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
u |
ndarray
|
U coordinates of hex grid. |
required |
v |
ndarray
|
V coordinates of hex grid. |
required |
n |
int
|
Number of 60-degree rotations. |
required |
Returns:
Type | Description |
---|---|
Tuple[ndarray, ndarray]
|
Tuple of rotated U and V coordinates. |
Source code in flyvision/datasets/augmentation/utils.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
|
flip_matrix ¶
flip_matrix(angle_in_rad, three_d=False)
Generate a flip matrix for mirroring over a line.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
angle_in_rad |
float
|
Angle of the flip axis in radians. |
required |
three_d |
bool
|
If True, generate a 3D flip matrix. |
False
|
Returns:
Type | Description |
---|---|
Tensor
|
Flip matrix as a torch.Tensor. |
Note
Source code in flyvision/datasets/augmentation/utils.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
|
flip_permutation_index ¶
flip_permutation_index(extent, axis)
Get indices used to flip the sequence.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
extent |
int
|
Extent of the regular hexagonal grid. |
required |
axis |
Literal[1, 2, 3]
|
Axis to flip across (1, 2, or 3). |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Permutation indices as a torch.Tensor. |
Raises:
Type | Description |
---|---|
ValueError
|
If axis is not in [1, 2, 3]. |
Source code in flyvision/datasets/augmentation/utils.py
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 |
|