MCPcopy Create free account
hub / github.com/PeizeSun/SparseR-CNN / build_batch_data_loader

Function build_batch_data_loader

detectron2/data/build.py:254–298  ·  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, aspect_ratio_grouping, num_workers): see

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

Source from the content-addressed store, hash-verified

252
253
254def build_batch_data_loader(
255 dataset, sampler, total_batch_size, *, aspect_ratio_grouping=False, num_workers=0
256):
257 """
258 Build a batched dataloader for training.
259
260 Args:
261 dataset (torch.utils.data.Dataset): map-style PyTorch dataset. Can be indexed.
262 sampler (torch.utils.data.sampler.Sampler): a sampler that produces indices
263 total_batch_size, aspect_ratio_grouping, num_workers): see
264 :func:`build_detection_train_loader`.
265
266 Returns:
267 iterable[list]. Length of each list is the batch size of the current
268 GPU. Each element in the list comes from the dataset.
269 """
270 world_size = get_world_size()
271 assert (
272 total_batch_size > 0 and total_batch_size % world_size == 0
273 ), "Total batch size ({}) must be divisible by the number of gpus ({}).".format(
274 total_batch_size, world_size
275 )
276
277 batch_size = total_batch_size // world_size
278 if aspect_ratio_grouping:
279 data_loader = torch.utils.data.DataLoader(
280 dataset,
281 sampler=sampler,
282 num_workers=num_workers,
283 batch_sampler=None,
284 collate_fn=operator.itemgetter(0), # don't batch, but yield individual elements
285 worker_init_fn=worker_init_reset_seed,
286 ) # yield individual mapped dict
287 return AspectRatioGroupedDataset(data_loader, batch_size)
288 else:
289 batch_sampler = torch.utils.data.sampler.BatchSampler(
290 sampler, batch_size, drop_last=True
291 ) # drop_last so the batch always have the same size
292 return torch.utils.data.DataLoader(
293 dataset,
294 num_workers=num_workers,
295 batch_sampler=batch_sampler,
296 collate_fn=trivial_batch_collator,
297 worker_init_fn=worker_init_reset_seed,
298 )
299
300
301def _train_loader_from_config(cfg, *, mapper=None, dataset=None, sampler=None):

Callers 1

Calls 2

get_world_sizeFunction · 0.90

Tested by

no test coverage detected