Execute function on the input dataset and leverage the output to act as a new Dataset. It can be used to load / fetch the basic dataset items, like the list of `image, label` paths. Or chain together to execute more complicated logic, like `partition_dataset`, `resample_datalist`, etc.
| 110 | |
| 111 | |
| 112 | class DatasetFunc(Dataset): |
| 113 | """ |
| 114 | Execute function on the input dataset and leverage the output to act as a new Dataset. |
| 115 | It can be used to load / fetch the basic dataset items, like the list of `image, label` paths. |
| 116 | Or chain together to execute more complicated logic, like `partition_dataset`, `resample_datalist`, etc. |
| 117 | The `data` arg of `Dataset` will be applied to the first arg of callable `func`. |
| 118 | Usage example:: |
| 119 | |
| 120 | data_list = DatasetFunc( |
| 121 | data="path to file", |
| 122 | func=monai.data.load_decathlon_datalist, |
| 123 | data_list_key="validation", |
| 124 | base_dir="path to base dir", |
| 125 | ) |
| 126 | # partition dataset for every rank |
| 127 | data_partition = DatasetFunc( |
| 128 | data=data_list, |
| 129 | func=lambda **kwargs: monai.data.partition_dataset(**kwargs)[torch.distributed.get_rank()], |
| 130 | num_partitions=torch.distributed.get_world_size(), |
| 131 | ) |
| 132 | dataset = Dataset(data=data_partition, transform=transforms) |
| 133 | |
| 134 | Args: |
| 135 | data: input data for the func to process, will apply to `func` as the first arg. |
| 136 | func: callable function to generate dataset items. |
| 137 | kwargs: other arguments for the `func` except for the first arg. |
| 138 | |
| 139 | """ |
| 140 | |
| 141 | def __init__(self, data: Any, func: Callable, **kwargs) -> None: |
| 142 | super().__init__(data=None, transform=None) # type: ignore |
| 143 | self.src = data |
| 144 | self.func = func |
| 145 | self.kwargs = kwargs |
| 146 | self.reset() |
| 147 | |
| 148 | def reset(self, data: Any | None = None, func: Callable | None = None, **kwargs): |
| 149 | """ |
| 150 | Reset the dataset items with specified `func`. |
| 151 | |
| 152 | Args: |
| 153 | data: if not None, execute `func` on it, default to `self.src`. |
| 154 | func: if not None, execute the `func` with specified `kwargs`, default to `self.func`. |
| 155 | kwargs: other arguments for the `func` except for the first arg. |
| 156 | |
| 157 | """ |
| 158 | src = self.src if data is None else data |
| 159 | self.data = self.func(src, **self.kwargs) if func is None else func(src, **kwargs) |
| 160 | |
| 161 | |
| 162 | class PersistentDataset(Dataset): |
no outgoing calls
searching dependent graphs…