Loading raw events using a PyTorch dataloader#

Sometimes we want to load the raw events as fast as possible. In such cases it makes sense to make use of the multiprocessing functionality of a PyTorch dataloader. Wrapping a Tonic dataset without transform with such a dataloader does not work out of the box, because Tonic works with structured numpy arrays and PyTorch cannot convert such data format into tensors. We therefore have to make use of a helper transform as follows.

Note

You can not use batching operations on raw events, because samples all have different lengths.

import tonic

# converts structured numpy arrays to unstructured ones
transform = tonic.transforms.NumpyAsType(int)

nmnist = tonic.datasets.NMNIST("../tutorials/data", train=False, transform=transform)


from torch.utils.data import DataLoader

dataloader = DataLoader(nmnist, shuffle=True, num_workers=2)

events, label = next(iter(dataloader))

print(events)
tensor([[[    12,     19,   3595,      0],
         [    15,     15,   4985,      1],
         [    22,     22,   5337,      1],
         ...,
         [    23,     12, 310013,      1],
         [    15,      7, 310175,      1],
         [    18,      9, 311868,      1]]])