Zip several PyTorch datasets and output data(with the same index) together in a tuple. If the output of single dataset is already a tuple, flatten it and extend to the result. For example: if datasetA returns (img, imgmeta), datasetB returns (seg, segmeta), finally return (img, imgm
| 1270 | |
| 1271 | |
| 1272 | class ZipDataset(Dataset): |
| 1273 | """ |
| 1274 | Zip several PyTorch datasets and output data(with the same index) together in a tuple. |
| 1275 | If the output of single dataset is already a tuple, flatten it and extend to the result. |
| 1276 | For example: if datasetA returns (img, imgmeta), datasetB returns (seg, segmeta), |
| 1277 | finally return (img, imgmeta, seg, segmeta). |
| 1278 | And if the datasets don't have same length, use the minimum length of them as the length |
| 1279 | of ZipDataset. |
| 1280 | If passing slicing indices, will return a PyTorch Subset, for example: `data: Subset = dataset[1:4]`, |
| 1281 | for more details, please check: https://pytorch.org/docs/stable/data.html#torch.utils.data.Subset |
| 1282 | |
| 1283 | Examples:: |
| 1284 | |
| 1285 | >>> zip_data = ZipDataset([[1, 2, 3], [4, 5]]) |
| 1286 | >>> print(len(zip_data)) |
| 1287 | 2 |
| 1288 | >>> for item in zip_data: |
| 1289 | >>> print(item) |
| 1290 | [1, 4] |
| 1291 | [2, 5] |
| 1292 | |
| 1293 | """ |
| 1294 | |
| 1295 | def __init__(self, datasets: Sequence, transform: Callable | None = None) -> None: |
| 1296 | """ |
| 1297 | Args: |
| 1298 | datasets: list of datasets to zip together. |
| 1299 | transform: a callable data transform operates on the zipped item from `datasets`. |
| 1300 | """ |
| 1301 | super().__init__(list(datasets), transform=transform) |
| 1302 | |
| 1303 | def __len__(self) -> int: |
| 1304 | return min(len(dataset) for dataset in self.data) |
| 1305 | |
| 1306 | def _transform(self, index: int): |
| 1307 | |
| 1308 | def to_list(x): |
| 1309 | return list(x) if isinstance(x, (tuple, list)) else [x] |
| 1310 | |
| 1311 | data = [] |
| 1312 | for dataset in self.data: |
| 1313 | data.extend(to_list(dataset[index])) |
| 1314 | |
| 1315 | if self.transform is not None: |
| 1316 | self.transform.map_items = False # Compose object map_items to false so transform is applied to list |
| 1317 | data = self.transform(data) |
| 1318 | # use tuple instead of list as the default collate_fn callback of MONAI DataLoader flattens nested lists |
| 1319 | return tuple(data) |
| 1320 | |
| 1321 | |
| 1322 | class ArrayDataset(Randomizable, _TorchDataset): |
no outgoing calls
searching dependent graphs…