Spaces:
Running
on
Zero
Running
on
Zero
File size: 9,100 Bytes
9b33fca |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
"""Dataloader utility functions."""
from __future__ import annotations
import random
import warnings
from collections.abc import Callable, Sequence
import numpy as np
import torch
from torch.utils.data import (
DataLoader,
Dataset,
RandomSampler,
SequentialSampler,
)
from torch.utils.data.distributed import DistributedSampler
from torch.utils.data.sampler import Sampler
from vis4d.common.distributed import get_rank, get_world_size
from .const import CommonKeys as K
from .data_pipe import DataPipe
from .datasets.base import VideoDataset
from .samplers import AspectRatioBatchSampler, VideoInferenceSampler
from .transforms import compose
from .transforms.to_tensor import ToTensor
from .typing import DictData, DictDataOrList
DEFAULT_COLLATE_KEYS = (
K.seg_masks,
K.extrinsics,
K.intrinsics,
K.depth_maps,
K.optical_flows,
K.categories,
)
def default_collate(
batch: list[DictData],
collate_keys: Sequence[str] = DEFAULT_COLLATE_KEYS,
sensors: Sequence[str] | None = None,
) -> DictData:
"""Default batch collate.
It will concatenate images and stack seg_masks, extrinsics, intrinsics,
and depth_maps. Other keys will be put into a list.
Args:
batch (list[DictData]): List of data dicts.
collate_keys (Sequence[str]): Keys to be collated. Default is
DEFAULT_COLLATE_KEYS.
sensors (Sequence[str] | None): List of sensors to collate. If is not
None will raise an error. Default is None.
Returns:
DictData: Collated data dict.
"""
assert sensors is None, "If specified sensors, use multi_sensor_collate."
data: DictData = {}
for key in batch[0]:
try:
if key == "transforms": # skip transform parameters
continue
if key in [K.images]:
data[key] = torch.cat([b[key] for b in batch])
elif key in collate_keys:
data[key] = torch.stack([b[key] for b in batch], 0)
else:
data[key] = [b[key] for b in batch]
except RuntimeError as e:
raise RuntimeError(f"Error collating key {key}") from e
return data
def multi_sensor_collate(
batch: list[DictData],
collate_keys: Sequence[str] = DEFAULT_COLLATE_KEYS,
sensors: Sequence[str] | None = None,
) -> DictData:
"""Default multi-sensor batch collate.
Args:
batch (list[DictData]): List of data dicts. Each data dict contains
data from multiple sensors.
collate_keys (Sequence[str]): Keys to be collated. Default is
DEFAULT_COLLATE_KEYS.
sensors (Sequence[str] | None): List of sensors to collate. If None,
will raise an error. Default is None.
Returns:
DictData: Collated data dict.
"""
assert (
sensors is not None
), "If not specified sensors, use default_collate."
collated_batch: DictData = {}
# For each sensor, collate the batch. Other keys will be put into a list.
for key in batch[0]:
inner_batch = [b[key] for b in batch]
if key in sensors:
collated_batch[key] = default_collate(inner_batch, collate_keys)
else:
collated_batch[key] = inner_batch
return collated_batch
def default_pipeline(data: list[DictData]) -> list[DictData]:
"""Default data pipeline."""
return compose([ToTensor()])(data)
def build_train_dataloader(
dataset: DataPipe,
samples_per_gpu: int = 1,
workers_per_gpu: int = 1,
batchprocess_fn: Callable[
[list[DictData]], list[DictData]
] = default_pipeline,
collate_fn: Callable[
[list[DictData], Sequence[str]], DictData
] = default_collate,
collate_keys: Sequence[str] = DEFAULT_COLLATE_KEYS,
sensors: Sequence[str] | None = None,
pin_memory: bool = True,
shuffle: bool | None = True,
drop_last: bool = False,
seed: int | None = None,
aspect_ratio_grouping: bool = False,
sampler: Sampler | None = None, # type: ignore
disable_subprocess_warning: bool = False,
) -> DataLoader[DictDataOrList]:
"""Build training dataloader."""
assert isinstance(dataset, DataPipe), "dataset must be a DataPipe"
def _collate_fn_single(data: list[DictData]) -> DictData:
"""Collates data from single view dataset."""
return collate_fn( # type: ignore
batch=batchprocess_fn(data),
collate_keys=collate_keys,
sensors=sensors,
)
def _collate_fn_multi(data: list[list[DictData]]) -> list[DictData]:
"""Collates data from multi view dataset."""
views = []
for view_idx in range(len(data[0])):
view = collate_fn( # type: ignore
batch=batchprocess_fn([d[view_idx] for d in data]),
collate_keys=collate_keys,
sensors=sensors,
)
views.append(view)
return views
def _worker_init_fn(worker_id: int) -> None:
"""Will be called on each worker after seeding and before data loading.
Args:
worker_id (int): Worker id in [0, num_workers - 1].
"""
if seed is not None:
# The seed of each worker equals to
# num_workers * rank + worker_id + user_seed
worker_seed = workers_per_gpu * get_rank() + worker_id + seed
np.random.seed(worker_seed)
random.seed(worker_seed)
torch.manual_seed(worker_seed)
if disable_subprocess_warning and worker_id != 0:
warnings.simplefilter("ignore")
if sampler is None:
if get_world_size() > 1:
assert isinstance(
shuffle, bool
), "When using distributed training, shuffle must be a boolean."
sampler = DistributedSampler(
dataset, shuffle=shuffle, drop_last=drop_last
)
shuffle = False
drop_last = False
elif shuffle:
sampler = RandomSampler(dataset)
shuffle = False
else:
sampler = SequentialSampler(dataset)
batch_sampler = None
if aspect_ratio_grouping:
batch_sampler = AspectRatioBatchSampler(
sampler, batch_size=samples_per_gpu, drop_last=drop_last
)
samples_per_gpu = 1
shuffle = None
drop_last = False
sampler = None
dataloader = DataLoader(
dataset,
batch_size=samples_per_gpu,
num_workers=workers_per_gpu,
collate_fn=(
_collate_fn_multi if dataset.has_reference else _collate_fn_single
),
sampler=sampler,
batch_sampler=batch_sampler,
worker_init_fn=_worker_init_fn,
persistent_workers=workers_per_gpu > 0,
pin_memory=pin_memory,
shuffle=shuffle,
drop_last=drop_last,
)
return dataloader
def build_inference_dataloaders(
datasets: Dataset[DictDataOrList] | list[Dataset[DictDataOrList]],
samples_per_gpu: int = 1,
workers_per_gpu: int = 1,
video_based_inference: bool = False,
batchprocess_fn: Callable[
[list[DictData]], list[DictData]
] = default_pipeline,
collate_fn: Callable[
[list[DictData], Sequence[str]], DictData
] = default_collate,
collate_keys: Sequence[str] = DEFAULT_COLLATE_KEYS,
sensors: Sequence[str] | None = None,
) -> list[DataLoader[DictDataOrList]]:
"""Build dataloaders for test / predict."""
def _collate_fn(data: list[DictData]) -> DictData:
"""Collates data for inference."""
return collate_fn( # type: ignore
batch=batchprocess_fn(data),
collate_keys=collate_keys,
sensors=sensors,
)
if isinstance(datasets, Dataset):
datasets_ = [datasets]
else:
datasets_ = datasets
dataloaders = []
for dataset in datasets_:
sampler: DistributedSampler[list[int]] | None
if get_world_size() > 1:
if video_based_inference:
if isinstance(dataset, DataPipe):
assert (
len(dataset.datasets) == 1
), "DDP Vdieo Inference only support a single dataset."
current_dataset = dataset.datasets[0]
else:
current_dataset = dataset
assert isinstance(
current_dataset, VideoDataset
), "Video based inference needs a VideoDataset."
sampler = VideoInferenceSampler(current_dataset)
else:
sampler = DistributedSampler(dataset)
else:
sampler = None
test_dataloader = DataLoader(
dataset,
batch_size=samples_per_gpu,
num_workers=workers_per_gpu,
sampler=sampler,
shuffle=False,
collate_fn=_collate_fn,
persistent_workers=workers_per_gpu > 0,
)
dataloaders.append(test_dataloader)
return dataloaders
|