Build a dataloader for object detection with some default features. This interface is experimental. Args: dataset (list or torch.utils.data.Dataset): a list of dataset dicts, or a map-style pytorch dataset. They can be obtained by using :func:`DatasetCat
(
dataset, *, mapper, sampler=None, total_batch_size, aspect_ratio_grouping=True, num_workers=0
)
| 339 | # TODO can allow dataset as an iterable or IterableDataset to make this function more general |
| 340 | @configurable(from_config=_train_loader_from_config) |
| 341 | def build_detection_train_loader( |
| 342 | dataset, *, mapper, sampler=None, total_batch_size, aspect_ratio_grouping=True, num_workers=0 |
| 343 | ): |
| 344 | """ |
| 345 | Build a dataloader for object detection with some default features. |
| 346 | This interface is experimental. |
| 347 | |
| 348 | Args: |
| 349 | dataset (list or torch.utils.data.Dataset): a list of dataset dicts, |
| 350 | or a map-style pytorch dataset. They can be obtained by using |
| 351 | :func:`DatasetCatalog.get` or :func:`get_detection_dataset_dicts`. |
| 352 | mapper (callable): a callable which takes a sample (dict) from dataset and |
| 353 | returns the format to be consumed by the model. |
| 354 | When using cfg, the default choice is ``DatasetMapper(cfg, is_train=True)``. |
| 355 | sampler (torch.utils.data.sampler.Sampler or None): a sampler that |
| 356 | produces indices to be applied on ``dataset``. |
| 357 | Default to :class:`TrainingSampler`, which coordinates a random shuffle |
| 358 | sequence across all workers. |
| 359 | total_batch_size (int): total batch size across all workers. Batching |
| 360 | simply puts data into a list. |
| 361 | aspect_ratio_grouping (bool): whether to group images with similar |
| 362 | aspect ratio for efficiency. When enabled, it requires each |
| 363 | element in dataset be a dict with keys "width" and "height". |
| 364 | num_workers (int): number of parallel data loading workers |
| 365 | |
| 366 | Returns: |
| 367 | torch.utils.data.DataLoader: a dataloader. Each output from it is a |
| 368 | ``list[mapped_element]`` of length ``total_batch_size / num_workers``, |
| 369 | where ``mapped_element`` is produced by the ``mapper``. |
| 370 | """ |
| 371 | if isinstance(dataset, list): |
| 372 | dataset = DatasetFromList(dataset, copy=False) |
| 373 | if mapper is not None: |
| 374 | dataset = MapDataset(dataset, mapper) |
| 375 | if sampler is None: |
| 376 | sampler = TrainingSampler(len(dataset)) |
| 377 | assert isinstance(sampler, torch.utils.data.sampler.Sampler) |
| 378 | return build_batch_data_loader( |
| 379 | dataset, |
| 380 | sampler, |
| 381 | total_batch_size, |
| 382 | aspect_ratio_grouping=aspect_ratio_grouping, |
| 383 | num_workers=num_workers, |
| 384 | ) |
| 385 | |
| 386 | |
| 387 | def _test_loader_from_config(cfg, dataset_name, mapper=None): |
no test coverage detected