Skip to main content

DataLoader

Struct DataLoader 

Source
pub struct DataLoader<Src, S: Sampler, C, T = Identity> { /* private fields */ }
Expand description

High-performance DataLoader with a PyTorch-like interface.

The dataset fetches items; the loader applies its Transform to each one (T, Identity unless set with transform), then collates them with C. Src holds the dataset: SyncSource for a Dataset, AsyncSource for an AsyncDataset (feature async).

Implementations§

Source§

impl<D: Dataset> DataLoader<SyncSource<D>, SequentialSampler, VecCollator>

Source

pub fn builder( dataset: D, ) -> DataLoaderBuilder<SyncSource<D>, SequentialSampler, VecCollator>

Create a DataLoaderBuilder starting with SequentialSampler and VecCollator as defaults.

Source§

impl<Src, S, C, T> DataLoader<Src, S, C, T>
where Src: LoaderSource, S: Sampler, T: Transform<Src::Item>, C: Collator<T::Output>,

Source

pub fn iter(&mut self) -> DataLoaderIter<Src, C, T> ⓘ

Start one epoch of iteration.

Advances the sampler (hence &mut self); the returned iterator does not borrow the loader.

Source

pub fn iter_with_indices(&mut self) -> IndexedIter<Src, C, T> ⓘ

Start one epoch, like iter, whose batches come with the dataset indices of their items: (indices, batch), indices[k] being the index of the batch’s k-th item as the sampler drew it. A failed batch is still one step of the epoch: the next batch gets the next indices.

use dataloader_rs::{DataLoader, Dataset, RandomSampler, error::Result};

struct Squares;

impl Dataset for Squares {
    type Item = usize;
    fn get(&self, index: usize) -> Result<usize> { Ok(index * index) }
    fn len(&self) -> usize { 10 }
}

let mut loader = DataLoader::builder(Squares)
    .batch_size(4)
    .sampler(RandomSampler::new(7))
    .build();
for batch in loader.iter_with_indices() {
    let (indices, squares) = batch.unwrap();
    assert!(indices.iter().zip(squares).all(|(i, s)| i * i == s));
}
Source

pub fn dataset(&self) -> &Src::Dataset

Reference to the underlying dataset.

Source

pub fn transform(&self) -> &T

Reference to the transform.

Source

pub fn collator(&self) -> &C

Reference to the collator.

Source

pub fn len(&self) -> usize

Total number of samples in the dataset.

Source

pub fn batch_len(&self) -> usize

Total number of batches for one epoch with current batch settings.

Counts what the sampler yields, so a sharding or subsampling sampler (e.g. DistributedSampler) is accounted for.

Source

pub fn is_empty(&self) -> bool

Returns true when the dataset is empty.

Trait Implementations§

Source§

impl<Src, S, C, T> IntoIterator for &mut DataLoader<Src, S, C, T>
where Src: LoaderSource, S: Sampler, T: Transform<Src::Item>, C: Collator<T::Output>,

Source§

type Item = Result<<C as Collator<<T as Transform<<Src as Sealed>::Item>>::Output>>::Batch, Box<dyn Error + Sync + Send>>

The type of the elements being iterated over.
Source§

type IntoIter = DataLoaderIter<Src, C, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<Src, S, C, T = Identity> !RefUnwindSafe for DataLoader<Src, S, C, T>

§

impl<Src, S, C, T = Identity> !UnwindSafe for DataLoader<Src, S, C, T>

§

impl<Src, S, C, T> Forget for DataLoader<Src, S, C, T>

§

impl<Src, S, C, T> Freeze for DataLoader<Src, S, C, T>
where Src: Freeze, BatchSampler<S>: Freeze, Arc<T>: Freeze, Arc<C>: Freeze,

§

impl<Src, S, C, T> Send for DataLoader<Src, S, C, T>
where Src: Send, BatchSampler<S>: Send, Arc<T>: Send, Arc<C>: Send,

§

impl<Src, S, C, T> Sync for DataLoader<Src, S, C, T>
where Src: Sync, BatchSampler<S>: Sync, Arc<T>: Sync, Arc<C>: Sync,

§

impl<Src, S, C, T> Unpin for DataLoader<Src, S, C, T>
where Src: Unpin, BatchSampler<S>: Unpin, Arc<T>: Unpin, Arc<C>: Unpin,

§

impl<Src, S, C, T> UnsafeUnpin for DataLoader<Src, S, C, T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.