MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / RandFlip

Class RandFlip

monai/transforms/spatial/array.py:1439–1489  ·  view source on GitHub ↗

Randomly flips the image along axes. Preserves shape. See numpy.flip for additional details. https://docs.scipy.org/doc/numpy/reference/generated/numpy.flip.html This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic ` for more informat

Source from the content-addressed store, hash-verified

1437
1438
1439class RandFlip(RandomizableTransform, InvertibleTransform, LazyTransform):
1440 """
1441 Randomly flips the image along axes. Preserves shape.
1442 See numpy.flip for additional details.
1443 https://docs.scipy.org/doc/numpy/reference/generated/numpy.flip.html
1444
1445 This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>`
1446 for more information.
1447
1448 Args:
1449 prob: Probability of flipping.
1450 spatial_axis: Spatial axes along which to flip over. Default is None.
1451 lazy: a flag to indicate whether this transform should execute lazily or not.
1452 Defaults to False
1453 """
1454
1455 backend = Flip.backend
1456
1457 def __init__(self, prob: float = 0.1, spatial_axis: Sequence[int] | int | None = None, lazy: bool = False) -> None:
1458 RandomizableTransform.__init__(self, prob)
1459 LazyTransform.__init__(self, lazy=lazy)
1460 self.flipper = Flip(spatial_axis=spatial_axis, lazy=lazy)
1461
1462 @LazyTransform.lazy.setter # type: ignore
1463 def lazy(self, val: bool):
1464 self.flipper.lazy = val
1465 self._lazy = val
1466
1467 def __call__(self, img: torch.Tensor, randomize: bool = True, lazy: bool | None = None) -> torch.Tensor:
1468 """
1469 Args:
1470 img: channel first array, must have shape: (num_channels, H[, W, ..., ]),
1471 randomize: whether to execute `randomize()` function first, default to True.
1472 lazy: a flag to indicate whether this transform should execute lazily or not
1473 during this call. Setting this to False or True overrides the ``lazy`` flag set
1474 during initialization for this call. Defaults to None.
1475 """
1476 if randomize:
1477 self.randomize(None)
1478 lazy_ = self.lazy if lazy is None else lazy
1479 out = self.flipper(img, lazy=lazy_) if self._do_transform else img
1480 out = convert_to_tensor(out, track_meta=get_track_meta())
1481 self.push_transform(out, replace=True, lazy=lazy_)
1482 return out
1483
1484 def inverse(self, data: torch.Tensor) -> torch.Tensor:
1485 transform = self.pop_transform(data)
1486 if not transform[TraceKeys.DO_TRANSFORM]:
1487 return data
1488 data.applied_operations.append(transform[TraceKeys.EXTRA_INFO]) # type: ignore
1489 return self.flipper.inverse(data)
1490
1491
1492class RandAxisFlip(RandomizableTransform, InvertibleTransform, LazyTransform):

Callers 8

run_training_testFunction · 0.90
test_decollate.pyFile · 0.90
test_invalid_inputsMethod · 0.90
test_correct_resultsMethod · 0.90
test_invertMethod · 0.90

Calls

no outgoing calls

Tested by 6

run_training_testFunction · 0.72
test_invalid_inputsMethod · 0.72
test_correct_resultsMethod · 0.72
test_invertMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…