Dictionary-based wrapper of :py:class:`monai.transforms.EnsureType`. Ensure the input data to be a PyTorch Tensor or numpy array, support: `numpy array`, `PyTorch Tensor`, `float`, `int`, `bool`, `string` and `object` keep the original. If passing a dictionary, list or tuple, still
| 491 | |
| 492 | |
| 493 | class EnsureTyped(MapTransform): |
| 494 | """ |
| 495 | Dictionary-based wrapper of :py:class:`monai.transforms.EnsureType`. |
| 496 | |
| 497 | Ensure the input data to be a PyTorch Tensor or numpy array, support: `numpy array`, `PyTorch Tensor`, |
| 498 | `float`, `int`, `bool`, `string` and `object` keep the original. |
| 499 | If passing a dictionary, list or tuple, still return dictionary, list or tuple and recursively convert |
| 500 | every item to the expected data type if `wrap_sequence=False`. |
| 501 | |
| 502 | Note: Currently, we only convert tensor data to numpy array or scalar number in the inverse operation. |
| 503 | |
| 504 | """ |
| 505 | |
| 506 | backend = EnsureType.backend |
| 507 | |
| 508 | def __init__( |
| 509 | self, |
| 510 | keys: KeysCollection, |
| 511 | data_type: str = "tensor", |
| 512 | dtype: Sequence[DtypeLike | torch.dtype] | DtypeLike | torch.dtype = None, |
| 513 | device: torch.device | None = None, |
| 514 | wrap_sequence: bool = True, |
| 515 | track_meta: bool | None = None, |
| 516 | allow_missing_keys: bool = False, |
| 517 | ) -> None: |
| 518 | """ |
| 519 | Args: |
| 520 | keys: keys of the corresponding items to be transformed. |
| 521 | See also: :py:class:`monai.transforms.compose.MapTransform` |
| 522 | data_type: target data type to convert, should be "tensor" or "numpy". |
| 523 | dtype: target data content type to convert, for example: np.float32, torch.float, etc. |
| 524 | It also can be a sequence of dtype, each element corresponds to a key in ``keys``. |
| 525 | device: for Tensor data type, specify the target device. |
| 526 | wrap_sequence: if `False`, then lists will recursively call this function, default to `True`. |
| 527 | E.g., if `False`, `[1, 2]` -> `[tensor(1), tensor(2)]`, if `True`, then `[1, 2]` -> `tensor([1, 2])`. |
| 528 | track_meta: whether to convert to `MetaTensor` when `data_type` is "tensor". |
| 529 | If False, the output data type will be `torch.Tensor`. Default to the return value of `get_track_meta`. |
| 530 | allow_missing_keys: don't raise exception if key is missing. |
| 531 | """ |
| 532 | super().__init__(keys, allow_missing_keys) |
| 533 | self.dtype = ensure_tuple_rep(dtype, len(self.keys)) |
| 534 | self.converter = EnsureType( |
| 535 | data_type=data_type, device=device, wrap_sequence=wrap_sequence, track_meta=track_meta |
| 536 | ) |
| 537 | |
| 538 | def __call__(self, data: Mapping[Hashable, NdarrayOrTensor]) -> dict[Hashable, NdarrayOrTensor]: |
| 539 | d = dict(data) |
| 540 | for key, dtype in self.key_iterator(d, self.dtype): |
| 541 | d[key] = self.converter(d[key], dtype) |
| 542 | return d |
| 543 | |
| 544 | |
| 545 | class ToNumpyd(MapTransform): |
no outgoing calls
searching dependent graphs…