MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / DataLoader

Class DataLoader

imperative/python/megengine/data/dataloader.py:89–245  ·  view source on GitHub ↗

r"""Data loader. Combines a dataset and a sampler, and provides a convenient way to iterate on a given dataset. The process is as follows: .. mermaid:: :align: center flowchart LR Dataset.__len__ -- Sampler --> Indices batch_size -- Sampler --> Indices

Source from the content-addressed store, hash-verified

87
88
89class DataLoader:
90 r"""Data loader. Combines a dataset and a sampler, and provides a convenient way
91 to iterate on a given dataset. The process is as follows:
92
93 .. mermaid::
94 :align: center
95
96 flowchart LR
97 Dataset.__len__ -- Sampler --> Indices
98 batch_size -- Sampler --> Indices
99 Indices -- Dataset.__getitem__ --> Samples
100 Samples -- Transform + Collator --> mini-batch
101
102 See :ref:`data-guide` for more details.
103
104 Args:
105 dataset: dataset from which to load the minibatch.
106 sampler: defines the strategy to sample data from the dataset.
107 If ``None``, it will sequentially sample from the dataset one by one.
108 transform: defined the transforming strategy for a sampled batch.
109 collator: defined the merging strategy for a transformed batch.
110 num_workers: the number of sub-process to load, transform and collate
111 the batch. ``0`` means using single-process. Default: 0
112 timeout: if positive, means the timeout value(second) for collecting a
113 batch from workers. Default: 0
114 preload: whether to enable the preloading strategy of the dataloader.
115 When enabling, the dataloader will preload one batch to the device memory to speed up the whole training process.
116 parallel_stream: whether to splitting workload across all workers when dataset is streamdataset and num_workers > 0.
117 When enabling, each worker will collect data from different dataset in order to speed up the whole loading process.
118 See ref:`streamdataset-example` for more details
119
120 Examples:
121 >>> import megengine.data as data
122 >>> dataset = CustomDataset() # doctest: +SKIP
123 >>> dataloader = DataLoader(dataset) # doctest: +SKIP
124 >>> for batch_data in DataLoader: # doctest: +SKIP
125 >>> print(batch_data.shape) # doctest: +SKIP
126
127 .. admonition:: The effect of enabling preload
128 :class: warning
129
130 * All elements in :class:`map`, :class:`list`, and :class:`tuple` will be converted to :class:`~.Tensor` by preloading,
131 and you will get :class:`~.Tensor` instead of the original Numpy array or Python built-in data structrure.
132 * Tensors' host2device copy and device kernel execution will be overlapped,
133 which will improve the training speed at the cost of **higher device memory usage** (due to one more batch data on device memory).
134 This feature saves more time when your NN training time is short or your machine's host PCIe bandwidth for each device is low.
135 """
136
137 def __init__(
138 self,
139 dataset: Dataset,
140 sampler: Sampler = None,
141 transform: Transform = None,
142 collator: Collator = None,
143 num_workers: int = 0,
144 timeout: int = 0,
145 preload: bool = False,
146 parallel_stream: bool = False,

Calls

no outgoing calls