MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / ZipDataset

Class ZipDataset

monai/data/dataset.py:1272–1319  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

1270
1271
1272class 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
1322class ArrayDataset(Randomizable, _TorchDataset):

Callers 5

test_zipdatasetMethod · 0.90
test_valueMethod · 0.90
test_slicingMethod · 0.90
test_sequenceMethod · 0.90
__init__Method · 0.85

Calls

no outgoing calls

Tested by 4

test_zipdatasetMethod · 0.72
test_valueMethod · 0.72
test_slicingMethod · 0.72
test_sequenceMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…