ToSparseTensor#

class tonic.transforms.ToSparseTensor(sensor_size: Tuple[int, int, int], time_window: Optional[float] = None, event_count: Optional[int] = None, n_time_bins: Optional[int] = None, n_event_bins: Optional[int] = None, overlap: float = 0, include_incomplete: bool = False)[source]#

PyTorch sparse tensor drop-in replacement for ToFrame. See https://pytorch.org/docs/stable/sparse.html for details about sparse tensors. The dense shape of the tensor will be (TCWH) and can be inflated by calling to_dense(). You need to have PyTorch installed for this transformation. Under the hood this transform calls ToFrame() with the same parameters, converts to a pytorch tensor and calls to_sparse().

Parameters:
  • sensor_size (Tuple[int, int, int]) – a 3-tuple of x,y,p for sensor_size. If omitted, the sensor size is calculated for that sample. However, do use this feature sparingly as when not all pixels fire in a sample, this might cause issues with batching/ stacking tensors further down the line.

  • time_window (float) – time window length for one frame. Use the same time unit as timestamps in the event recordings. Good if you want temporal consistency in your training, bad if you need some visual consistency for every frame if the recording’s activity is not consistent.

  • event_count (int) – number of events per frame. Good for training CNNs which do not care about temporal consistency.

  • n_time_bins (int) – fixed number of frames, sliced along time axis. Good for generating a pre-determined number of frames which might help with batching.

  • n_event_bins (int) – fixed number of frames, sliced along number of events in the recording. Good for generating a pre-determined number of frames which might help with batching.

  • overlap (float) – overlap between frames defined either in time units, number of events or number of bins between 0 and 1.

  • include_incomplete (bool) – if True, includes overhang slice when time_window or event_count is specified. Not valid for bin_count methods.

Example

>>> from tonic.transforms import ToSparseTensor
>>> transform1 = ToSparseTensor(time_window=10000, overlap=300, include_incomplete=True)
>>> transform2 = ToSparseTensor(event_count=3000, overlap=100, include_incomplete=True)
>>> transform3 = ToSparseTensor(n_time_bins=100, overlap=0.1)