MCPcopy Create free account
hub / github.com/TRI-ML/dd3d / build_batch_data_loader

Function build_batch_data_loader

tridet/data/build.py:106–149  ·  view source on GitHub ↗

Build a batched dataloader for training. Args: dataset (torch.utils.data.Dataset): map-style PyTorch dataset. Can be indexed. sampler (torch.utils.data.sampler.Sampler): a sampler that produces indices total_batch_size (int): total batch size across GPUs. as

(dataset, sampler, total_batch_size, *, aspect_ratio_grouping=False, num_workers=0)

Source from the content-addressed store, hash-verified

104
105
106def build_batch_data_loader(dataset, sampler, total_batch_size, *, aspect_ratio_grouping=False, num_workers=0):
107 """
108 Build a batched dataloader for training.
109
110 Args:
111 dataset (torch.utils.data.Dataset): map-style PyTorch dataset. Can be indexed.
112 sampler (torch.utils.data.sampler.Sampler): a sampler that produces indices
113 total_batch_size (int): total batch size across GPUs.
114 aspect_ratio_grouping (bool): whether to group images with similar
115 aspect ratio for efficiency. When enabled, it requires each
116 element in dataset be a dict with keys "width" and "height".
117 num_workers (int): number of parallel data loading workers
118
119 Returns:
120 iterable[list]. Length of each list is the batch size of the current
121 GPU. Each element in the list comes from the dataset.
122 """
123 world_size = comm.get_world_size()
124 assert (
125 total_batch_size > 0 and total_batch_size % world_size == 0
126 ), "Total batch size ({}) must be divisible by the number of gpus ({}).".format(total_batch_size, world_size)
127
128 batch_size = total_batch_size // world_size
129 if aspect_ratio_grouping:
130 data_loader = torch.utils.data.DataLoader(
131 dataset,
132 sampler=sampler,
133 num_workers=num_workers,
134 batch_sampler=None,
135 collate_fn=operator.itemgetter(0), # don't batch, but yield individual elements
136 worker_init_fn=worker_init_reset_seed,
137 ) # yield individual mapped dict
138 return AspectRatioGroupedDataset(data_loader, batch_size)
139 else:
140 batch_sampler = torch.utils.data.sampler.BatchSampler(
141 sampler, batch_size, drop_last=True
142 ) # drop_last so the batch always have the same size
143 return torch.utils.data.DataLoader(
144 dataset,
145 num_workers=num_workers,
146 batch_sampler=batch_sampler,
147 collate_fn=trivial_batch_collator,
148 worker_init_fn=worker_init_reset_seed,
149 )
150
151
152def trivial_batch_collator(batch):

Callers 1

build_train_dataloaderFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected