spaic.IO package

Submodules

spaic.IO.Dataloader module

Created on 2020/8/12 @project: SPAIC @filename: Dataloader @author: Hong Chaofei @contact: hongchf@gmail.com @description: 定义数据导入模块

spaic.IO.Dataloader.default_collate(batch)[source]
class spaic.IO.Dataloader.Dataloader(dataset, batch_size=1, shuffle=False, sampler=None, batch_sampler=None, collate_fn=None, drop_last=False)[source]

Bases: object

sampler的作用是生成一系列的index 而batch_sampler则是将sampler生成的indices打包分组,得到一个又一个batch的index

try_fetch()[source]

spaic.IO.Dataset module

class spaic.IO.Dataset.Dataset(**kwargs)[source]

Bases: object

All datasets that represent a map from keys to data samples should subclass it. All subclasses should overwrite __getitem__(), supporting fetching a data sample for a given key. Subclasses should also overwrite __len__(), which is expected to return the size of the sample dataset.

class spaic.IO.Dataset.CustomDataset(data=None, label=None)[source]

Bases: Dataset

自定义数据集: 个人采集的实值数据

class spaic.IO.Dataset.CustomSpikeDataset(spike_times=None, neuron_ids=None, label=None)[source]

Bases: Dataset

自定义数据集: 编码后的脉冲数据(仅支持[spike_time, neuron_ids]表示)

class spaic.IO.Dataset.SpecifiedDataset(image_file, label_file)[source]

Bases: Dataset

labels load from json file

class spaic.IO.Dataset.cifar10(root, is_train=True)[source]

Bases: Dataset

files = {'test_dataset': 'test_batch', 'train_dataset1': 'data_batch_1', 'train_dataset2': 'data_batch_2', 'train_dataset3': 'data_batch_3', 'train_dataset4': 'data_batch_4', 'train_dataset5': 'data_batch_5'}
property dataset_folder
property data_dict
property is_train
load_cifar10_batch(folder_path, batch_id)[source]
class spaic.IO.Dataset.ImageNet(root, is_train=True)[source]

Bases: Dataset

files = {'train_dataset': 'ILSVRC2012_img_train.tar', 'val_dataset': 'ILSVRC2012_img_val.tar', 'val_label': 'ILSVRC2012_devkit_t12.tar.gz'}
property dataset_folder
property data_dict
property is_train
untar_train_tar(train_tar)[source]

untar images from train_tar and save in corresponding folders organize like: /train

/n01440764

images

/n01443537

images

move_val_img(val_dir, devkit_dir)[source]

move val_img to correspongding folders. val_id(start from 1) -> ILSVRC_ID(start from 1) -> WIND organize like: /val

/n01440764

images

/n01443537

images

class spaic.IO.Dataset.MNIST(root, is_train=True)[source]

Bases: Dataset

A 10-class multi-class classfication Args: root (string): Root directory of dataset where MNIST/processed/training.pt

and MNIST/processed/test.pt exist.

is_train (bool, optional): If True, creates dataset from training.pt,

otherwise from test.pt.

class_number = 10
maxNum = 784
resources = ['http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz', 'http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz', 'http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz', 'http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz']
files = {'test_images': 't10k-images-idx3-ubyte', 'test_labels': 't10k-labels-idx1-ubyte', 'train_images': 'train-images-idx3-ubyte', 'train_labels': 'train-labels-idx1-ubyte'}
property dataset_folder
property class_to_idx
property data_dict
property is_train
download()[source]
class spaic.IO.Dataset.FashionMNIST(root, is_train=True)[source]

Bases: Dataset

A 10-class multi-class classfication

class_number = 10
maxNum = 784
resources = ['http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz', 'http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz', 'http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz', 'http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz']
files = {'test_images': 't10k-images-idx3-ubyte', 'test_labels': 't10k-labels-idx1-ubyte', 'train_images': 'train-images-idx3-ubyte', 'train_labels': 'train-labels-idx1-ubyte'}
property dataset_folder
property class_to_idx
property data_dict
property is_train
class spaic.IO.Dataset.PathMNIST(root, is_train=True)[source]

Bases: Dataset

A 9-class multi-class classfication

resources = 'https://drive.google.com/drive/folders/1Tl_SP-ffDQg-jDG_EWPlWKgZTmGbvFXU'
class_number = 9
maxNum = 2352
property data_dict
property is_train
class spaic.IO.Dataset.OctMNIST(root, is_train=True)[source]

Bases: Dataset

A 4-class multi-class classfication

resources = 'https://drive.google.com/drive/folders/1Tl_SP-ffDQg-jDG_EWPlWKgZTmGbvFXU'
class_number = 4
maxNum = 784
property data_dict
property is_train
class spaic.IO.Dataset.RWCP10(root, is_train=True, **kwargs)[source]

Bases: Dataset

classes = {'bells5': 5, 'bottle1': 9, 'buzzer': 6, 'cymbals': 3, 'horn': 4, 'kara': 7, 'metal15': 8, 'phone4': 2, 'ring': 0, 'whistle1': 1}
class_number = 10
property data_dict
property is_train
class spaic.IO.Dataset.MNISTVoices(root, is_train=True, **kwargs)[source]

Bases: Dataset

Used to load any type of 0-9 audio dataset Class number: 10

class_number = 10
classes = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
property data_dict
property is_train
class spaic.IO.Dataset.TIDIGITS(root, is_train=True, **kwargs)[source]

Bases: Dataset

Used to load any type of 0-9 and oh audio dataset Class number: 11

class_number = 11
classes = {'eight': 8, 'five': 5, 'four': 4, 'nine': 9, 'oh': 10, 'one': 1, 'seven': 7, 'six': 6, 'three': 3, 'two': 2, 'zero': 0}
property data_dict
property is_train
class spaic.IO.Dataset.SHD(root, is_train=True, **kwargs)[source]

Bases: Dataset

Spiking Heidelberg Digits Dataset max spiking time: 136.9 ms max neuron num: 700 Class number: 20 number of train samples: 8156 number of test samples: 2264

class_number = 20
maxNum = 700
Time = 1.37
files = {'test_dataset': 'shd_test.h5', 'train_dataset': 'shd_train.h5'}
property dataset_folder
property data_dict
property is_train
class spaic.IO.Dataset.SSC(root, is_train=True, **kwargs)[source]

Bases: Dataset

Spiking Speech Command Dataset max spiking time: 99.95ms max neuron num: 700 Class number: 35 number of train samples: 75466 number of test samples: 30363

class_number = 35
maxNum = 700
Time = 1
files = {'test_dataset': 'ssc_test.h5', 'train_dataset': 'ssc_train.h5', 'valid_dataset': 'ssc_valid.h5'}
property dataset_folder
property data_dict
property is_train
class spaic.IO.Dataset.DVS128Gesture(dataset_root, is_train=True, data_type='event', event_cut=None, frames_number=None, split_by=None, duration=None, time_step=None)[source]

Bases: Dataset

参考 https://spikingjelly.readthedocs.io/zh_CN/latest/clock_driven/13_neuromorphic_datasets.html

Parameters:
  • dataset_root

  • is_train

  • data_type

  • event_cut – 事件太长需要切分,分割事件的时间长度,ms

  • frames_number

  • split_by

  • duration

resources = 'https://ibm.ent.box.com/s/3hiq58ww1pbbjrinh367ykfdf60xsfm8/folder/50167556794'
scale = 0.001
resize = 1
class_number = 11
maxNum = 16384
origin_maxTime = 18457
maxTime = 1000
maxFrameValue = 65.0
files = {'test_dataset': 'trials_to_test.txt', 'train_dataset': 'trials_to_train.txt'}
calculate_max_time()[source]

读取数据集,计算事件的最大持续时间 Returns:

calculate_max_value_from_frame()[source]
calculate_max_time_from_npz()[source]

读取数据集,计算事件的最大持续时间 Returns:

split_event_to_short(spiking)[source]
property dataset_folder
property data_dict
property is_train
static split_aedat_files_to_np(fname: str, aedat_file: str, csv_file: str, output_dir: str, event_split=None)[source]
create_events_np_files(events_np_root: str, event_split=None)[source]
Parameters:

events_np_root (str) – Root directory path which saves events files in the npz format

This function defines how to convert the origin binary data in extract_root to npz format and save converted files in events_np_root. :param event_split: 事件划分成几份

get_H_W()[source]
Returns:

A tuple (H, W), where H is the height of the data and W` is the weight of the data. For example, this function returns ``(128, 128) for the DVS128 Gesture dataset.

Return type:

tuple

spaic.IO.Environment module

Created on 2020/8/12 @project: SPAIC @filename: Environment @author: Hong Chaofei @contact: hongchf@gmail.com

@description: 定义强化学习的环境交互模块

class spaic.IO.Environment.BaseEnvironment[source]

Bases: ABC

Abstract environment class.

abstract step(action: int)[source]

Abstract method for step().

Parameters:

action (int) – action to take in environment.

abstract reset()[source]

Abstract method for reset().

abstract render()[source]

Abstract method for render().

abstract seed(seed)[source]

Abstract method for seed().

abstract close()[source]

Abstract method for close().

class spaic.IO.Environment.GymEnvironment(name: str, **kwargs)[source]

Bases: BaseEnvironment

Wrapper the OpenAI gym environments.

Initializes the environment wrapper. This class makes the assumption that the OpenAI gym environment will provide an image of format HxW as an observation.

Parameters:
  • name (str) – The name of an OpenAI gym environment.

  • encoding (str) – The key of encoding class which is used to encode observations into spike trains.

Variables:
  • max_prob (float) – Maximum spiking probability.

  • clip_rewards (bool) – Whether or not to use np.sign of rewards.

  • binary (bool) – Whether to convert the image to binary

step(action)[source]

Wrapper around the OpenAI gym environment step() function.

Parameters:

action (int) – Action to take in the environment.

Returns:

Observation, reward, done flag, and information dictionary.

reset()[source]

Wrapper around the OpenAI gym environment reset() function.

Returns:

Observation from the environment.

render(mode)[source]

Wrapper around the OpenAI gym environment render() function.

seed(seed)[source]

Wrapper around the OpenAI gym environment render() function.

close()[source]

Wrapper around the OpenAI gym environment close() function.

spaic.IO.Pipeline module

Created on 2020/8/17 @project: SPAIC @filename: IO @author: Hong Chaofei @contact: hongchf@gmail.com

@description:

class spaic.IO.Pipeline.Pipline(**kwargs)[source]

Bases: object

abstract update_step(batch, **kwargs)[source]

Perform a pass of the network given the input batch.

Parameters:

batch – The current batch. This could be anything as long as the subclass agrees upon the format in some way.

Returns:

Any output that is need for recording purposes.

class spaic.IO.Pipeline.RLPipeline(network, environment, time=None, **kwargs)[source]

Bases: Pipline

env_step()[source]

Single step of the environment which includes rendering, getting and performing the action, and accumulating/delaying rewards.

Returns:

An OpenAI gym compatible tuple (next_state, reward, done).

update_step(gym_batch, **kwargs)[source]

Run a single iteration of the network and update it and the reward list when done.

Parameters:

gym_batch (tuple) – An OpenAI gym compatible tuple (next_state, reward, done).

reset_pipeline()[source]

Reset the pipeline.

class spaic.IO.Pipeline.Transition(state, action, next_state, reward)

Bases: tuple

Create new instance of Transition(state, action, next_state, reward)

action

Alias for field number 1

next_state

Alias for field number 2

reward

Alias for field number 3

state

Alias for field number 0

class spaic.IO.Pipeline.ReplayMemory(capacity)[source]

Bases: object

push(*args)[source]

Save a transition

sample(batch_size)[source]

spaic.IO.SignalGenerator module

Created on 2020/10/23 @project: SPAIC @filename: SignalGenerator @author: Hong Chaofei @contact: hongchf@gmail.com

@description:

class spaic.IO.SignalGenerator.SigGenerator[source]

Bases: Pipline

Generate signals such as sin/cos wave, random process…

spaic.IO.sampler module

class spaic.IO.sampler.Sampler(data_source)[source]

Bases: object

Base class for all Samplers.

Every Sampler subclass has to provide an __iter__() method, providing a way to iterate over indices of dataset elements, and a __len__() method that returns the length of the returned iterators.

class spaic.IO.sampler.SequentialSampler(data_source)[source]

Bases: Sampler

Samples elements sequentially, always in the same order.

Parameters:

data_source (Dataset) – dataset to sample from

class spaic.IO.sampler.RandomSampler(data_source, replacement=False, num_samples=None)[source]

Bases: Sampler

Samples elements randomly. If without replacement, then sample from a shuffled dataset. If with replacement, then user can specify num_samples to draw.

Parameters:
  • data_source (Dataset) – dataset to sample from

  • replacement (bool) – samples are drawn on-demand with replacement if True, default=``False``

  • num_samples (int) – number of samples to draw, default=`len(dataset)`. This argument is supposed to be specified only when replacement is True.

property num_samples
class spaic.IO.sampler.BatchSampler(sampler, batch_size, drop_last)[source]

Bases: Sampler

Wraps another sampler to yield a mini-batch of indices.

Parameters:
  • sampler (Sampler or Iterable) – Base sampler. Can be any iterable object

  • batch_size (int) – Size of mini-batch.

  • drop_last (bool) – If True, the sampler will drop the last batch if its size would be less than batch_size

Example

>>> list(BatchSampler(SequentialSampler(range(10)), batch_size=3, drop_last=False))
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
>>> list(BatchSampler(SequentialSampler(range(10)), batch_size=3, drop_last=True))
[[0, 1, 2], [3, 4, 5], [6, 7, 8]]

spaic.IO.utils module

@author: Yuan Mengwen @contact: mwyuan94@gmail.com @project: PyCharm @filename: utils.py @time:2021/4/1 14:33 @description:

spaic.IO.utils.wav_file_resample(file_path, dest_sample=16000.0)[source]

对WAV文件进行resample的操作 :param file_path: 需要进行resample操作的wav文件的路径 :param dest_sample: 目标采样率

Returns:

降采样后的数据 dest_sample: 目标采样率

Return type:

resampled

spaic.IO.utils.wav_file_cut(file_path, signal_num=16000.0)[source]

对WAV文件进行裁剪操作 :param file_path: 需要进行resample操作的wav文件的路径 :param signal_num: 目标数据数量

Returns:

裁剪后的数据

Return type:

cropped_data

spaic.IO.utils.save_mfcc_feature(root, npz_name, sample_rate=16000.0, signal_num=16000.0, class_labels=None, **kwargs)[source]
spaic.IO.utils.save_kp_feature(root=None, npz_name=None, sample_rate=16000.0, class_labels=None, **kwargs)[source]
spaic.IO.utils.load_kp_data(root, filename)[source]
spaic.IO.utils.load_mfcc_data(root, filename)[source]
spaic.IO.utils.dataset_split(source_root, target_root, ratio, is_shuffle)[source]
spaic.IO.utils.reclassification(source_root, target_root, class_num, perperson_perclass_samplenum)[source]

将按录音者分类的digit语音数据集重保存为按录的音频的类别分类

spaic.IO.utils.datasetAlignment(source, maxNum)[source]

Zeros are padded to each sample in the dataset according to the value of maxNum :param source: samples of dataset :type source: ndarray :param maxNum: The length of longest sample :type maxNum: int

Returns:

The data after padding

spaic.IO.utils.batchAlignment(source)[source]
spaic.IO.utils.fetchGmSpectrogram(sig, fs=16000.0, window_size=0.016, stride=0.008, kernels_num=32, freq_min=20, log=False, show=False)[source]
spaic.IO.utils.extractKeyPoints(gmspec, Dr=13, Dc=13, significance_level=3)[source]
spaic.IO.utils.get_Max(data)[source]

get the maximum number of data :param data (): can be spiking time or neuron ids

Returns:

spaic.IO.utils.RGBtoGray(image)[source]

Converts RGB image into gray image.

Parameters:

image – RGB image.

Returns:

Gray image.

spaic.IO.utils.GraytoBinary(image)[source]

Converts input image into black and white (binary)

Parameters:

image – Gray image.

Returns:

Binary image.

spaic.IO.utils.reshape(image, shape)[source]

Scale the image to (x, y).

Args:

image: Image to be rescaled. shape: Changed shape

Returns:

Re-scaled image.

spaic.IO.utils.im2col(img, kh, kw, stride, padding='same')[source]
Parameters:
  • img – 4D array

  • kh – kernel_height

  • kw – kernel_width

  • stride

  • padding

Returns:

spaic.IO.utils.un_tar(file_name, output_root)[source]
spaic.IO.utils.load_aedat_v3(file_name: str)[source]
Parameters:

file_name (str) – path of the aedat v3 file

Returns:

a dict whose keys are [‘t’, ‘x’, ‘y’, ‘p’] and values are numpy.ndarray

This function is written by referring to https://gitlab.com/inivation/dv/dv-python . It can be used for DVS128 Gesture.

spaic.IO.utils.create_same_directory_structure(source_dir: str, target_dir: str) None[source]
Parameters:
  • source_dir (str) – Path of the directory that be copied from

  • target_dir (str) – Path of the directory that be copied to

Returns:

None

Create the same directory structure in target_dir with that of source_dir.

spaic.IO.utils.integrate_events_file_to_frames_file_by_fixed_frames_number(events_np_file: str, output_dir: str, split_by: str, frames_num: int, H: int, W: int, print_save: bool = False) None[source]
Parameters:
  • events_np_file (str) – path of the events np file

  • output_dir (str) – output directory for saving the frames

  • split_by (str) – ‘time’ or ‘number’

  • frames_num (int) – the number of frames

  • H (int) – the height of frame

  • W (int) – the weight of frame

  • print_save (bool) – If True, this function will print saved files’ paths.

Returns:

None

Integrate a events file to frames by fixed frames number and save it. See cal_fixed_frames_number_segment_index and integrate_events_segment_to_frame for more details.

spaic.IO.utils.integrate_events_by_fixed_frames_number(events: dict, split_by: str, frames_num: int, H: int, W: int) ndarray[source]
Parameters:
  • events (Dict) – a dict whose keys are [‘t’, ‘x’, ‘y’, ‘p’] and values are numpy.ndarray

  • split_by (str) – ‘time’ or ‘number’

  • frames_num (int) – the number of frames

  • H (int) – the height of frame

  • W (int) – the weight of frame

Returns:

frames

Return type:

np.ndarray

Integrate events to frames by fixed frames number. See cal_fixed_frames_number_segment_index and integrate_events_segment_to_frame for more details.

spaic.IO.utils.cal_fixed_frames_number_segment_index(events_t: ndarray, split_by: str, frames_num: int) tuple[source]
Parameters:
  • events_t (numpy.ndarray) – events’ t

  • split_by (str) – ‘time’ or ‘number’

  • frames_num (int) – the number of frames

Returns:

a tuple (j_l, j_r)

Return type:

tuple

Denote frames_num as \(M\), if split_by is 'time', then .. math:

\Delta T & = [\frac{t_{N-1} - t_{0}}{M}] \\
j_{l} & = \mathop{\arg\min}\limits_{k} \{t_{k} | t_{k} \geq t_{0} + \Delta T \cdot j\} \\
j_{r} & = \begin{cases} \mathop{\arg\max}\limits_{k} \{t_{k} | t_{k} < t_{0} + \Delta T \cdot (j + 1)\} + 1, & j <  M - 1 \cr N, & j = M - 1 \end{cases}

If split_by is 'number', then .. math:

j_{l} & = [\frac{N}{M}] \cdot j \\
j_{r} & = \begin{cases} [\frac{N}{M}] \cdot (j + 1), & j <  M - 1 \cr N, & j = M - 1 \end{cases}
spaic.IO.utils.integrate_events_segment_to_frame(events: dict, H: int, W: int, j_l: int = 0, j_r: int = -1) ndarray[source]
param events:

a dict whose keys are [‘t’, ‘x’, ‘y’, ‘p’] and values are numpy.ndarray

type events:

Dict

param H:

height of the frame

type H:

int

param W:

weight of the frame

type W:

int

param j_l:

the start index of the integral interval, which is included

type j_l:

int

param j_r:

the right index of the integral interval, which is not included

type j_r:

return:

frames

rtype:

np.ndarray

Denote a two channels frame as \(F\) and a pixel at \((p, x, y)\) as \(F(p, x, y)\), the pixel value is integrated from the events data whose indices are in \([j_{l}, j_{r})\):

\[F(p, x, y) &= \sum_{i = j_{l}}^{j_{r} - 1} \mathcal{I}_{p, x, y}(p_{i}, x_{i}, y_{i})\]

where \(\lfloor \cdot floor\) is the floor operation, \(\mathcal{I}_{p, x, y}(p_{i}, x_{i}, y_{i})\) is an indicator function and it equals 1 only when \((p, x, y) = (p_{i}, x_{i}, y_{i})\).

spaic.IO.utils.integrate_events_file_to_frames_file_by_fixed_duration(events_np_file: str, output_dir: str, duration: int, H: int, W: int, print_save: bool = False) None[source]
Parameters:
  • events_np_file (str) – path of the events np file

  • output_dir (str) – output directory for saving the frames

  • duration (int) – the time duration of each frame

  • H (int) – the height of frame

  • W (int) – the weight of frame

  • print_save (bool) – If True, this function will print saved files’ paths.

Returns:

None

Integrate events to frames by fixed time duration of each frame.

spaic.IO.utils.integrate_events_by_fixed_duration(events: dict, duration: int, H: int, W: int) ndarray[source]
Parameters:
  • events (Dict) – a dict whose keys are [‘t’, ‘x’, ‘y’, ‘p’] and values are numpy.ndarray

  • duration (int) – the time duration of each frame

  • H (int) – the height of frame

  • W (int) – the weight of frame

Returns:

frames

Return type:

np.ndarray

Integrate events to frames by fixed time duration of each frame.

Module contents

Created on 2020/8/11 @project: SPAIC @author: Hong Chaofei @contact: hongchf@gmail.com

@description: