Dictionary-based transform to convert a dictionary to MetaTensor. If input is `{"a": torch.Tensor, "a_meta_dict": dict, "b": ...}`, then output will have the form `{"a": MetaTensor, "b": MetaTensor}`.
| 87 | |
| 88 | |
| 89 | class ToMetaTensord(MapTransform, InvertibleTransform): |
| 90 | """ |
| 91 | Dictionary-based transform to convert a dictionary to MetaTensor. |
| 92 | |
| 93 | If input is `{"a": torch.Tensor, "a_meta_dict": dict, "b": ...}`, then output will |
| 94 | have the form `{"a": MetaTensor, "b": MetaTensor}`. |
| 95 | """ |
| 96 | |
| 97 | backend = [TransformBackends.TORCH, TransformBackends.NUMPY, TransformBackends.CUPY] |
| 98 | |
| 99 | def __call__(self, data: Mapping[Hashable, NdarrayOrTensor]) -> dict[Hashable, NdarrayOrTensor]: |
| 100 | d = dict(data) |
| 101 | for key in self.key_iterator(d): |
| 102 | self.push_transform(d, key) |
| 103 | im = d[key] |
| 104 | meta = d.pop(PostFix.meta(key), None) |
| 105 | transforms = d.pop(PostFix.transforms(key), None) |
| 106 | im = MetaTensor(im, meta=meta, applied_operations=transforms) # type: ignore |
| 107 | d[key] = im |
| 108 | return d |
| 109 | |
| 110 | def inverse(self, data: Mapping[Hashable, NdarrayOrTensor]) -> dict[Hashable, NdarrayOrTensor]: |
| 111 | d = dict(data) |
| 112 | for key in self.key_iterator(d): |
| 113 | # check transform |
| 114 | _ = self.get_most_recent_transform(d, key) |
| 115 | # do the inverse |
| 116 | im: MetaTensor = d[key] # type: ignore |
| 117 | d.update(im.as_dict(key)) |
| 118 | # Remove the applied transform |
| 119 | self.pop_transform(d, key) |
| 120 | return d |
| 121 | |
| 122 | |
| 123 | FromMetaTensorD = FromMetaTensorDict = FromMetaTensord |
no outgoing calls
searching dependent graphs…