pvio.torch_tools — PyTorch Integration
Dataset and DataLoader for efficient parallel frame loading with PyTorch.
pvio.torch_tools.VideoCollectionDataset
VideoCollectionDataset(
videos: list[Video],
*,
transform: Callable | None = None,
use_cached_video_metadata: bool = True,
n_frame_counting_workers: int = -1,
progress_bar: bool | None = None,
)
Bases: IterableDataset
Iterable dataset that yields frames from a list of Video objects.
Each frame is yielded as a CHW float32 tensor with values in [0, 1].
Call .assign_workers() (done automatically by VideoCollectionDataLoader)
before iterating.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
videos
|
list[Video]
|
Video objects to iterate. Videos are set up here;
do not call |
required |
transform
|
Callable | None
|
Applied to each tensor before yielding.
The following are already applied before the transform: (i)
numpy array → torch tensor, (ii) HWC → CHW layout, and
(iii) uint8 |
None
|
use_cached_video_metadata
|
bool
|
Use cached metadata for EncodedVideo objects if available. Set to False to force re-reading. |
True
|
n_frame_counting_workers
|
int
|
Parallel workers for pre-loading EncodedVideo metadata. -1 uses all available cores. |
-1
|
progress_bar
|
bool | None
|
Show a progress bar during metadata loading. If None, shows one only when stderr is a TTY. |
None
|
Source code in src/pvio/torch_tools.py
assign_workers
Distribute frames across workers for balanced parallel loading.
Frames are assigned in contiguous ranges within each video to minimise
seeking overhead. If the per-worker frame count would fall below
min_frames_per_worker, the effective worker count is reduced accordingly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_loading_workers
|
int
|
Number of workers to distribute frames across. |
required |
min_frames_per_worker
|
int
|
Lower bound on frames per worker. Workers below this threshold are merged to avoid excessive overhead. |
300
|
Source code in src/pvio/torch_tools.py
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 | |
pvio.torch_tools.VideoCollectionDataLoader
VideoCollectionDataLoader(
dataset: VideoCollectionDataset,
min_frames_per_worker: int = 300,
**kwargs: Any,
)
Bases: DataLoader
DataLoader for VideoCollectionDataset with automatic worker assignment.
Each worker is assigned a contiguous range of frames to minimise seeking overhead. Batch dicts contain:
frames: Tensor of shape (batch_size, C, H, W)video_indices: list of int — index into the videos listframe_indices: list of int — virtual frame index within that video
Custom batch_sampler and collate_fn are not supported.
Wrap a VideoCollectionDataset in a DataLoader with automatic worker assignment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
VideoCollectionDataset
|
The dataset to load from. |
required |
min_frames_per_worker
|
int
|
Minimum frames per worker; the effective worker count is reduced if this threshold would otherwise be breached. |
300
|
**kwargs
|
Any
|
Forwarded to torch.utils.data.DataLoader. |
{}
|
Source code in src/pvio/torch_tools.py
pvio.torch_tools.SimpleVideoCollectionLoader
SimpleVideoCollectionLoader(
videos: list[Path | str | Video],
*,
transform: Callable | None = None,
buffer_size: int = 64,
frame_id_regex: str
| Pattern
| None = "frame\\D*(\\d+)(?!\\d)",
use_cached_video_metadata: bool = True,
n_frame_counting_workers: int = -1,
progress_bar: bool | None = None,
min_frames_per_worker: int = 300,
device: str | None = None,
**kwargs: Any,
)
Bases: VideoCollectionDataLoader
Create a VideoCollectionDataset and VideoCollectionDataLoader in one call.
Each entry in videos may be a pre-constructed :class:Video object
or a path (str / Path). Paths pointing to files become
:class:EncodedVideo; paths pointing to directories become
:class:ImageDirVideo.
The decode workflow is selected automatically: on a machine with a CUDA
GPU, file-backed videos decode on the GPU (NVDEC) and iteration runs in
the main process (num_workers is forced to 0, since CUDA cannot be
used in forked workers); on a CPU-only machine, decoding uses the
requested number of CPU workers as before. Pass device="cpu" to opt
out and keep multi-worker CPU decoding even when a GPU is present.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
videos
|
list[Path | str | Video]
|
Video sources. |
required |
transform
|
Callable | None
|
Applied to each CHW float32 frame tensor before it is yielded. |
None
|
buffer_size
|
int
|
Decode-buffer size forwarded to :class: |
64
|
frame_id_regex
|
str | Pattern | None
|
Regex forwarded to :class: |
'frame\\D*(\\d+)(?!\\d)'
|
use_cached_video_metadata
|
bool
|
Use cached metadata when available. Set
to |
True
|
n_frame_counting_workers
|
int
|
Workers for parallel metadata loading.
|
-1
|
progress_bar
|
bool | None
|
Show a progress bar during metadata loading.
Defaults to |
None
|
min_frames_per_worker
|
int
|
Minimum frames per worker; see
:meth: |
300
|
device
|
str | None
|
Decode device for file-backed videos, forwarded to
:class: |
None
|
**kwargs
|
Any
|
Forwarded to :class: |
{}
|
Source code in src/pvio/torch_tools.py
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 | |