0
# Augmentation Transforms
1
2
Extensive augmentation transforms including spatial augmentations (affine, elastic deformation, flipping) and intensity augmentations with medical imaging-specific artifacts (motion, ghosting, bias field, spike artifacts). These transforms increase dataset diversity and improve model robustness.
3
4
## Capabilities
5
6
### Spatial Augmentation
7
8
Spatial augmentation transforms that modify the geometric properties of medical images while maintaining anatomical realism and medical validity.
9
10
#### Flipping Transforms
11
12
Random and deterministic flipping along specified axes, commonly used for data augmentation in medical imaging.
13
14
```python { .api }
15
class Flip(SpatialTransform):
16
"""
17
Deterministic image flipping along specified axes.
18
19
Parameters:
20
- axes: Axes to flip (int, tuple, or sequence)
21
"""
22
def __init__(self, axes: Union[int, tuple[int, ...]]): ...
23
24
class RandomFlip(SpatialTransform):
25
"""
26
Random image flipping with specified probability.
27
28
Parameters:
29
- axes: Axes that can be flipped (default: (0,) for left-right)
30
- flip_probability: Probability of flipping each axis
31
"""
32
def __init__(
33
self,
34
axes: TypeTuple = (0,),
35
flip_probability: float = 0.5
36
): ...
37
```
38
39
#### Affine Transforms
40
41
Affine transformations including rotation, translation, scaling, and shearing, essential for geometric data augmentation.
42
43
```python { .api }
44
class Affine(SpatialTransform):
45
"""
46
Deterministic affine transformation.
47
48
Parameters:
49
- scales: Scaling factors per axis
50
- degrees: Rotation angles in degrees per axis
51
- translation: Translation in voxels per axis
52
- isotropic: Whether to use isotropic scaling
53
- center: Center of rotation ('image' or 'origin')
54
- default_pad_value: Padding value for out-of-bounds regions
55
"""
56
def __init__(
57
self,
58
scales: TypeRangeFloat = None,
59
degrees: TypeRangeFloat = None,
60
translation: TypeRangeFloat = None,
61
isotropic: bool = False,
62
center: str = 'image',
63
default_pad_value: float = 0,
64
**kwargs
65
): ...
66
67
class RandomAffine(SpatialTransform):
68
"""
69
Random affine transformation.
70
71
Parameters:
72
- scales: Range of scaling factors (tuple of min, max)
73
- degrees: Range of rotation degrees per axis
74
- translation: Range of translation in voxels
75
- isotropic: Whether to use isotropic scaling
76
- center: Center of rotation
77
- default_pad_value: Padding value for out-of-bounds regions
78
"""
79
def __init__(
80
self,
81
scales: TypeRangeFloat = None,
82
degrees: TypeRangeFloat = None,
83
translation: TypeRangeFloat = None,
84
isotropic: bool = False,
85
center: str = 'image',
86
default_pad_value: float = 0,
87
**kwargs
88
): ...
89
```
90
91
#### Elastic Deformation
92
93
Non-linear elastic deformations that simulate realistic tissue deformations and anatomical variations.
94
95
```python { .api }
96
class ElasticDeformation(SpatialTransform):
97
"""
98
Deterministic elastic deformation.
99
100
Parameters:
101
- num_control_points: Number of control points for B-spline grid
102
- max_displacement: Maximum displacement in voxels
103
- locked_borders: Whether to lock image borders
104
"""
105
def __init__(
106
self,
107
num_control_points: TypeTuple = 7,
108
max_displacement: TypeRangeFloat = 7.5,
109
locked_borders: int = 2
110
): ...
111
112
class RandomElasticDeformation(SpatialTransform):
113
"""
114
Random elastic deformation.
115
116
Parameters:
117
- num_control_points: Number of control points for deformation grid
118
- max_displacement: Maximum displacement range
119
- locked_borders: Number of border voxels to keep fixed
120
"""
121
def __init__(
122
self,
123
num_control_points: TypeTuple = 7,
124
max_displacement: TypeRangeFloat = 7.5,
125
locked_borders: int = 2
126
): ...
127
```
128
129
#### Combined Transforms
130
131
```python { .api }
132
class AffineElasticDeformation(SpatialTransform):
133
"""
134
Combined affine and elastic deformation.
135
136
Applies affine transformation followed by elastic deformation
137
for realistic anatomical variations.
138
"""
139
def __init__(
140
self,
141
scales: TypeRangeFloat = None,
142
degrees: TypeRangeFloat = None,
143
translation: TypeRangeFloat = None,
144
num_control_points: TypeTuple = 7,
145
max_displacement: TypeRangeFloat = 7.5,
146
**kwargs
147
): ...
148
149
class RandomAffineElasticDeformation(SpatialTransform):
150
"""Random combined affine and elastic deformation."""
151
def __init__(
152
self,
153
scales: TypeRangeFloat = None,
154
degrees: TypeRangeFloat = None,
155
translation: TypeRangeFloat = None,
156
num_control_points: TypeTuple = 7,
157
max_displacement: TypeRangeFloat = 7.5,
158
**kwargs
159
): ...
160
161
class RandomAnisotropy(SpatialTransform):
162
"""
163
Random anisotropic scaling to simulate resolution variations.
164
165
Parameters:
166
- axes: Axes along which to apply anisotropic scaling
167
- downsampling: Range of downsampling factors
168
"""
169
def __init__(
170
self,
171
axes: tuple[int, ...] = (0, 1, 2),
172
downsampling: TypeRangeFloat = (1.5, 5)
173
): ...
174
```
175
176
Usage example:
177
178
```python
179
import torchio as tio
180
181
# Spatial augmentation pipeline
182
spatial_augmentation = tio.Compose([
183
tio.RandomFlip(axes=(0,), flip_probability=0.5),
184
tio.RandomAffine(
185
scales=(0.9, 1.1),
186
degrees=(-10, 10),
187
translation=(-5, 5)
188
),
189
tio.RandomElasticDeformation(
190
num_control_points=7,
191
max_displacement=7.5
192
),
193
])
194
195
subject = tio.Subject(
196
t1=tio.ScalarImage('t1.nii.gz'),
197
seg=tio.LabelMap('segmentation.nii.gz')
198
)
199
200
augmented = spatial_augmentation(subject)
201
```
202
203
### Intensity Augmentation
204
205
Intensity augmentations that modify pixel/voxel intensities to simulate acquisition variations, artifacts, and improve model robustness to intensity variations.
206
207
#### Noise Augmentation
208
209
```python { .api }
210
class Noise(IntensityTransform):
211
"""
212
Deterministic noise addition.
213
214
Parameters:
215
- std: Standard deviation of Gaussian noise
216
"""
217
def __init__(self, std: float): ...
218
219
class RandomNoise(IntensityTransform):
220
"""
221
Random Gaussian noise addition.
222
223
Parameters:
224
- std: Range of standard deviation values (float or tuple)
225
"""
226
def __init__(self, std: TypeRangeFloat = (0, 0.25)): ...
227
```
228
229
#### Blurring
230
231
```python { .api }
232
class Blur(IntensityTransform):
233
"""
234
Deterministic Gaussian blurring.
235
236
Parameters:
237
- std: Standard deviation for Gaussian kernel
238
"""
239
def __init__(self, std: TypeRangeFloat): ...
240
241
class RandomBlur(IntensityTransform):
242
"""
243
Random Gaussian blurring.
244
245
Parameters:
246
- std: Range of standard deviation values
247
"""
248
def __init__(self, std: TypeRangeFloat = (0, 2)): ...
249
```
250
251
#### Gamma Correction
252
253
```python { .api }
254
class Gamma(IntensityTransform):
255
"""
256
Deterministic gamma correction.
257
258
Parameters:
259
- log_gamma: Log of gamma value for correction
260
"""
261
def __init__(self, log_gamma: float): ...
262
263
class RandomGamma(IntensityTransform):
264
"""
265
Random gamma correction.
266
267
Parameters:
268
- log_gamma: Range of log gamma values
269
"""
270
def __init__(self, log_gamma: TypeRangeFloat = (-0.3, 0.3)): ...
271
```
272
273
#### Medical Imaging Specific Artifacts
274
275
TorchIO provides specialized augmentations that simulate realistic medical imaging artifacts commonly encountered in MRI and other modalities.
276
277
```python { .api }
278
class Motion(IntensityTransform):
279
"""
280
Deterministic motion artifacts simulation.
281
282
Simulates patient motion during MRI acquisition.
283
284
Parameters:
285
- degrees: Rotation angles for motion simulation
286
- translation: Translation distances for motion
287
- num_transforms: Number of motion transforms to apply
288
"""
289
def __init__(
290
self,
291
degrees: TypeRangeFloat = 10,
292
translation: TypeRangeFloat = 10,
293
num_transforms: int = 2
294
): ...
295
296
class RandomMotion(IntensityTransform):
297
"""
298
Random motion artifacts simulation.
299
300
Parameters:
301
- degrees: Range of rotation angles
302
- translation: Range of translation distances
303
- num_transforms: Range of number of transforms
304
"""
305
def __init__(
306
self,
307
degrees: TypeRangeFloat = 10,
308
translation: TypeRangeFloat = 10,
309
num_transforms: TypeRangeFloat = 2
310
): ...
311
312
class BiasField(IntensityTransform):
313
"""
314
Deterministic bias field simulation.
315
316
Simulates MRI bias field inhomogeneity.
317
318
Parameters:
319
- coefficients: Polynomial coefficients for bias field
320
"""
321
def __init__(self, coefficients: TypeRangeFloat): ...
322
323
class RandomBiasField(IntensityTransform):
324
"""
325
Random bias field simulation.
326
327
Parameters:
328
- coefficients: Range of polynomial coefficients
329
"""
330
def __init__(self, coefficients: TypeRangeFloat = 0.5): ...
331
332
class Ghosting(IntensityTransform):
333
"""
334
Deterministic ghosting artifacts simulation.
335
336
Simulates MRI ghosting artifacts from periodic motion.
337
338
Parameters:
339
- num_ghosts: Number of ghost repetitions
340
- axes: Axes along which ghosting occurs
341
- intensity: Intensity of ghost artifacts
342
"""
343
def __init__(
344
self,
345
num_ghosts: int,
346
axes: tuple[int, ...],
347
intensity: float
348
): ...
349
350
class RandomGhosting(IntensityTransform):
351
"""
352
Random ghosting artifacts simulation.
353
354
Parameters:
355
- num_ghosts: Range of number of ghosts
356
- axes: Axes along which ghosting can occur
357
- intensity: Range of ghost intensities
358
"""
359
def __init__(
360
self,
361
num_ghosts: tuple[int, int] = (4, 10),
362
axes: tuple[int, ...] = (0, 1, 2),
363
intensity: TypeRangeFloat = (0.5, 1)
364
): ...
365
366
class Spike(IntensityTransform):
367
"""
368
Deterministic spike artifacts simulation.
369
370
Simulates k-space spike artifacts in MRI.
371
372
Parameters:
373
- num_spikes: Number of spikes to add
374
- intensity: Intensity of spikes
375
"""
376
def __init__(
377
self,
378
num_spikes: int,
379
intensity: float
380
): ...
381
382
class RandomSpike(IntensityTransform):
383
"""
384
Random spike artifacts simulation.
385
386
Parameters:
387
- num_spikes: Range of number of spikes
388
- intensity: Range of spike intensities
389
"""
390
def __init__(
391
self,
392
num_spikes: tuple[int, int] = (1, 3),
393
intensity: TypeRangeFloat = (1, 3)
394
): ...
395
```
396
397
#### Swap and Label Manipulation
398
399
```python { .api }
400
class Swap(IntensityTransform):
401
"""
402
Deterministic axis swapping.
403
404
Parameters:
405
- patch_size: Size of patches to swap
406
- num_iterations: Number of swap iterations
407
"""
408
def __init__(
409
self,
410
patch_size: int = 15,
411
num_iterations: int = 100
412
): ...
413
414
class RandomSwap(IntensityTransform):
415
"""
416
Random axis swapping for data augmentation.
417
418
Parameters:
419
- patch_size: Range of patch sizes for swapping
420
- num_iterations: Range of number of iterations
421
"""
422
def __init__(
423
self,
424
patch_size: TypeRangeFloat = 15,
425
num_iterations: TypeRangeFloat = 100
426
): ...
427
428
class LabelsToImage(IntensityTransform):
429
"""
430
Convert label map to intensity image.
431
432
Parameters:
433
- label_key: Name of label image in subject
434
- used_labels: Labels to convert (None for all)
435
"""
436
def __init__(
437
self,
438
label_key: str,
439
used_labels: list[int] = None
440
): ...
441
442
class RandomLabelsToImage(IntensityTransform):
443
"""
444
Randomly convert labels to intensity image.
445
446
Parameters:
447
- label_key: Name of label image in subject
448
- used_labels: Labels that can be converted
449
"""
450
def __init__(
451
self,
452
label_key: str,
453
used_labels: list[int] = None
454
): ...
455
```
456
457
Usage example:
458
459
```python
460
# Medical imaging specific augmentation
461
medical_augmentation = tio.Compose([
462
tio.RandomNoise(std=(0, 0.1)),
463
tio.RandomBlur(std=(0, 1)),
464
tio.RandomGamma(log_gamma=(-0.3, 0.3)),
465
tio.RandomMotion(degrees=2, translation=2),
466
tio.RandomBiasField(coefficients=0.5),
467
tio.RandomGhosting(num_ghosts=(2, 6), intensity=(0.5, 1)),
468
tio.RandomSpike(num_spikes=(1, 3), intensity=(1, 3)),
469
])
470
471
subject = tio.Subject(
472
t1=tio.ScalarImage('t1.nii.gz'),
473
t2=tio.ScalarImage('t2.nii.gz')
474
)
475
476
# Apply medical imaging artifacts
477
augmented = medical_augmentation(subject)
478
```
479
480
### Complete Augmentation Pipeline
481
482
Example of a comprehensive augmentation pipeline combining spatial and intensity transforms:
483
484
```python
485
# Complete augmentation pipeline
486
augmentation_pipeline = tio.Compose([
487
# Spatial augmentation
488
tio.RandomFlip(axes=(0,)),
489
tio.RandomAffine(
490
scales=(0.9, 1.1),
491
degrees=(-5, 5),
492
translation=(-5, 5),
493
isotropic=False
494
),
495
tio.RandomElasticDeformation(
496
num_control_points=(4, 8),
497
max_displacement=(1, 7.5)
498
),
499
500
# Intensity augmentation
501
tio.RandomNoise(std=(0, 0.05)),
502
tio.RandomBlur(std=(0, 0.5)),
503
tio.RandomGamma(log_gamma=(-0.2, 0.2)),
504
505
# Medical imaging artifacts
506
tio.RandomMotion(
507
degrees=(0, 2),
508
translation=(0, 2),
509
num_transforms=(1, 3)
510
),
511
tio.RandomBiasField(coefficients=(0, 0.3)),
512
tio.RandomGhosting(intensity=(0.5, 0.8)),
513
])
514
515
# Apply to subject
516
augmented_subject = augmentation_pipeline(subject)
517
```