Dictionary-based transform to convert MetaTensor to a dictionary. If input is `{"a": MetaTensor, "b": MetaTensor}`, then output will have the form `{"a": torch.Tensor, "a_meta_dict": dict, "a_transforms": list, "b": ...}`.
| 40 | |
| 41 | |
| 42 | class FromMetaTensord(MapTransform, InvertibleTransform): |
| 43 | """ |
| 44 | Dictionary-based transform to convert MetaTensor to a dictionary. |
| 45 | |
| 46 | If input is `{"a": MetaTensor, "b": MetaTensor}`, then output will |
| 47 | have the form `{"a": torch.Tensor, "a_meta_dict": dict, "a_transforms": list, "b": ...}`. |
| 48 | """ |
| 49 | |
| 50 | backend = [TransformBackends.TORCH, TransformBackends.NUMPY, TransformBackends.CUPY] |
| 51 | |
| 52 | def __init__( |
| 53 | self, keys: KeysCollection, data_type: Sequence[str] | str = "tensor", allow_missing_keys: bool = False |
| 54 | ): |
| 55 | """ |
| 56 | Args: |
| 57 | keys: keys of the corresponding items to be transformed. |
| 58 | See also: :py:class:`monai.transforms.compose.MapTransform` |
| 59 | data_type: target data type to convert, should be "tensor" or "numpy". |
| 60 | allow_missing_keys: don't raise exception if key is missing. |
| 61 | """ |
| 62 | super().__init__(keys, allow_missing_keys) |
| 63 | self.as_tensor_output = tuple(d == "tensor" for d in ensure_tuple_rep(data_type, len(self.keys))) |
| 64 | |
| 65 | def __call__(self, data: Mapping[Hashable, NdarrayOrTensor]) -> dict[Hashable, NdarrayOrTensor]: |
| 66 | d = dict(data) |
| 67 | for key, t in self.key_iterator(d, self.as_tensor_output): |
| 68 | im: MetaTensor = d[key] # type: ignore |
| 69 | d.update(im.as_dict(key, output_type=torch.Tensor if t else np.ndarray)) |
| 70 | self.push_transform(d, key) |
| 71 | return d |
| 72 | |
| 73 | def inverse(self, data: Mapping[Hashable, NdarrayOrTensor]) -> dict[Hashable, NdarrayOrTensor]: |
| 74 | d = dict(data) |
| 75 | for key in self.key_iterator(d): |
| 76 | # check transform |
| 77 | _ = self.get_most_recent_transform(d, key) |
| 78 | # do the inverse |
| 79 | im = d[key] |
| 80 | meta = d.pop(PostFix.meta(key), None) |
| 81 | transforms = d.pop(PostFix.transforms(key), None) |
| 82 | im = MetaTensor(im, meta=meta, applied_operations=transforms) # type: ignore |
| 83 | d[key] = im |
| 84 | # Remove the applied transform |
| 85 | self.pop_transform(d, key) |
| 86 | return d |
| 87 | |
| 88 | |
| 89 | class ToMetaTensord(MapTransform, InvertibleTransform): |
no outgoing calls
searching dependent graphs…