omniloader.data.datasets¶
Dataset adapters that yield raw sample dicts for OmniLoader.
The concrete datasets here (an HDF5-backed reader and an in-memory tensor
dataset) implement the loose contract OmniLoader expects: __len__ returns the
number of samples and __getitem__ returns a dict of named values. The
DatasetSchema describing what a dataset provides is
supplied alongside it to OmniLoader.
Classes¶
Read samples from a group of an HDF5 file, one sub-group per sample. |
|
In-memory dataset over a dict of stacked tensors, keyed by sample index. |
Module Contents¶
- class omniloader.data.datasets.HDF5Dataset(h5_path: str | pathlib.Path, subset: str, keys: collections.abc.Sequence[str] | None = None, cache_size: int = 0, preload: bool = False)[source]¶
Bases:
torch.utils.data.DatasetRead samples from a group of an HDF5 file, one sub-group per sample.
The file is expected to hold a group named
subset(e.g."train") whose children are per-sample groups, each containing the named value datasets (features, targets, masks, metadata). The file handle is opened lazily on first read so it can be per-process, but an HDF5 handle is not fork-safe: if anything reads a sample (orpreload=True) before aDataLoaderforks its workers, every worker inherits the same open handle and reads silently hang. Usenum_workers > 0only withomniloader.utils.seeding.seed_worker()passed as the DataLoader’sworker_init_fn(OmniLoader’s own loader/config helpers already do this), which clears the handle so each worker reopens its own.- Parameters:
h5_path – Path to the
.h5file.subset – Name of the top-level group to read (e.g.
"train").keys – Optional subset of value names to load per sample. When
None, every dataset in the sample group is loaded.cache_size – If
> 0, keep an in-process LRU cache of up to this many decoded samples to speed up repeated reads.preload – If
True, decode every sample into memory up front (fastest for small datasets; uses the most memory).
- Raises:
KeyError – If
subsetis not present in the file.
- __getitem__(index: int) dict[str, Any][source]¶
Load the sample at
indexas a dict of named values.- Parameters:
index – Position within the subset.
- Returns:
A dict mapping each stored key to a tensor or string.
- __getstate__() dict[str, Any][source]¶
Drop the open file handle and cache when pickling (for workers).
- reset_for_worker() None[source]¶
Drop any inherited file handle and cache so this process reopens its own.
Called by
omniloader.utils.seeding.seed_worker()(the DataLoader’sworker_init_fn) inside each worker, since afork-started worker inherits the parent’s already-open (and not fork-safe) handle directly without going through__getstate__().
- class omniloader.data.datasets.DictTensorDataset(tensors: collections.abc.Mapping[str, torch.Tensor], metadata: collections.abc.Mapping[str, collections.abc.Sequence[Any]] | None = None)[source]¶
Bases:
torch.utils.data.DatasetIn-memory dataset over a dict of stacked tensors, keyed by sample index.
Useful for tests and small experiments: each entry maps a name to a tensor whose leading dimension is the sample axis.
__getitem__returns the per-sample slice of every tensor as a dict.- Parameters:
tensors – Mapping of value name to a tensor of shape
(N, ...)whereNis the number of samples (equal across all tensors).metadata – Optional mapping of metadata name to a per-sample sequence (e.g. dataset name or sample key) of length
N.
- Raises:
ValueError – If the tensors disagree on the number of samples.