如何为多输出回归问题编写 Pytorch Dataset 类和 DataLoader?
pytorch 357
原文标题 :How to write Pytorch Dataset class and DataLoader for a multi-output regression problem?
我正在使用基于事件的数据(包括时间戳、x 和 y 坐标以及极性)来解决神经形态问题。但是,我的问题是,当我尝试预测多个输出时,如何创建 Dataset 和正确的 DataLoader?具体来说,我试图预测速度矢量的 x 分量、y 分量和 z 分量。这是我当前的自定义数据集类的样子:
import os
import ast
import torch
import tonic
import torchvision
import numpy as np
import pandas as pd
import tonic.transforms as transforms
from torch.utils.data import DataLoader
class SyntheticRecording(tonic.Dataset):
"""
Synthetic event camera recordings dataset.
"""
def __init__(self, csv_file):
super(SyntheticRecording, self).__init__()
self.csv_file = csv_file
df = pd.read_csv(self.csv_file, index_col = False)
self.events = df['Events'] # Select only last column of dataframe
self.target = df[['Vel_x', 'Vel_y', 'Vel_z']] # Select every column except last column of dataframe
assert(self.target.shape[0] == len(self.events))
self.sensor_size = (1920, 1080, 2)
"""
Retrieve the index i to get the ith sample from the dataset. Apply the appropriate transformations.
"""
def __getitem__(self, index):
list_ = ast.literal_eval(self.events[index])
t = []
x = []
y = []
p = []
for e in list_:
t.append(e[0] * 1e6) # Convert to microseconds
x.append(e[1])
y.append(e[2])
p.append(e[3])
events = tonic.io.make_structured_array(x, y, t, p) # Ordering is xytp now
# Denoise removes isolated, one-off events
frame_transform = transforms.Compose([transforms.Denoise(filter_time = 10000),
transforms.ToFrame(sensor_size = self.sensor_size,
time_window = 1000)
])
transformed_frames = frame_transform(events)
vel_x = np.array(self.target.loc[index][0]).astype('float')
vel_y = np.array(self.target.loc[index][1]).astype('float')
vel_z = np.array(self.target.loc[index][2]).astype('float')
sample = {'frames': transformed_frames,
'vel_x': vel_x,
'vel_y': vel_y,
'vel_z': vel_z}
return sample
这就是我尝试创建 DataLoader 的方式:
batch_size = 16
trainloader = DataLoader(sr, batch_size = batch_size, collate_fn = tonic.collation.PadTensors(), shuffle = True, drop_last = True)
每当我尝试遍历帧和目标值(3 个值)时,都会收到以下错误:
for frames, targets in trainloader:
print(frames.shape)
print(targets.shape)
frames, targets = next(iter(trainloader))
结果:ValueError: too many values to unpack (expected 2)
创建可以处理多预测回归问题的 DataLoader 的正确方法是什么?
编辑:
我使用它作为我的资源来尝试处理多输出部分:https://medium.com/jdsc-tech-blog/multioutput-cnn-in-pytorch-c5f702d4915f