# 将事件流数据保留到本地 保存的文件格式为 npy [ # 数组是以未压缩的原始二进制格式保存在扩展名为.npy的文件中 np.save(f"../Tonic_dir/tutorials/data/rand_by_myself/recording{i}.npy", create_random_input()) for i in range(0,10) ]
保存为bin格式
1 2 3 4 5 6
# 将事件流数据保留到本地 文件保存的格式为 bin [ # 数组是以未压缩的原始二进制格式保存在扩展名为.bin的文件中 create_random_input().tofile(f"../Tonic_dir/tutorials/data/rand_by_myself/recording{i}.bin") for i in range(30,40) ]
2.继承Dataset类加载本地的事件流数据
该程序只针对 以npy为后缀的事件流数据,以bin为后缀的数据可以根据文字开头numpy数组的保存:二进制文件链接跳转,修改os.path.join(self.data_dir,f"recording{i}.npy") for i in range(n_recordings)以及events = np.load(self.filenames[index])两个部分,修改为读取bin文件的方式
""" 继承Dataset类加载本地的事件流数据 """ class MyRecordings(Dataset): sensor_size = ( 34, 34, 2, ) # the sensor size of the event camera or the number of channels of the silicon cochlear that was used ordering = ( "xytp" # the order in which your event channels are provided in your recordings )
# replace the strings with your training/testing file locations or pass as an argument # 本地事件流文件路径 self.data_dir = f"../Tonic_dir/tutorials/data/rand_by_myself/Test/0" if train: self.filenames = [ # f"recording{i}.npy" 为 数据流文件 os.path.join(self.data_dir,f"recording{i}.npy") for i in range(n_recordings) ] else: raise NotImplementedError
""" 对本地的事件流数据使用Tonic进行标注 有关网络下载的内容可以删除 Parameters: save_to (string): Location to save files to on disk. train (bool): If True, uses training subset, otherwise testing subset. first_saccade_only (bool): If True, only work with events of the first of three saccades. Results in about a third of the events overall. 如果为True,则只处理三个扫视中的第一个事件。大约三分之一的事件的结果 transform (callable, optional): A callable of transforms to apply to the data. target_transform (callable, optional): A callable of transforms to apply to the targets/labels. 一个可调用的转换应用到目标/标签。 transforms (callable, optional): A callable of transforms that is applied to both data and labels at the same time. 同时应用于数据和标签的转换可调用对象 """ import os from typing import Callable, Optional
import numpy as np
from tonic.dataset import Dataset from tonic.io import read_mnist_file
# os.walk扫描test文件夹下所有的子目录和文件 # 二进制事件流文件的后缀名 --- 根据自己 文件 后缀名进行修改 events_file_end = "bin" # 事件流标签的数据类型的别名 如 NMNIST 的标签为 0到9 为 int label_type = int for path, dirs, files in os.walk(file_path): # 排序 files.sort() # file 是 最底层目录下的 二进制数据流文件 for file in files: if file.endswith(events_file_end): # path 事件流上层目录的路径 # self.data 列表存储了所有的 事件流二进制文件 self.data.append(path + "/" + file) # path 路径的 最后一个名录名称 label_number = label_type(path[-1]) # self.targets 列表存储了所有的 事件流二进制文件 对应的标签 self.targets.append(label_number)
def __getitem__(self, index): """ Returns: a tuple of (events, target) where target is the index of the target class. (events, target)的元组,其中target是目标类的索引 """ # events 得到 self.data 对应 index 索引下的 事件流文件 events = read_mnist_file(self.data[index], dtype=self.dtype) if self.first_saccade_only: events = events[events["t"] < 1e5] # target 得到 self.targets 对应 index 索引下的 事件流文件所对应的标签 target = self.targets[index] if self.transform is not None: events = self.transform(events) if self.target_transform is not None: target = self.target_transform(target) # 对事件流与标签同时进行transform if self.transforms is not None: events, target = self.transforms(events, target) return events, target