首页 > 解决方案 > Pytorch 自定义数据集:ValueError:给定 numpy 数组的某些步幅为负

问题描述

我编写了一个自定义的 pytorch 数据集,但遇到了一个似乎很难理解的错误。

我的自定义数据集,

class data_from_xlsx(Dataset):
    def __init__(self, xlsx_fp, path_col, class_cols_list):
        self.xlsx_file = pd.read_excel(xlsx_fp)
        self.path_col = path_col
        self.class_cols_list = class_cols_list

    def __len__(self):
        return get_xlsx_length(self.xlsx_file)

    def __getitem__(self, index):
        file_path = cols_from_xlsx(self.xlsx_file, index, 1, self.path_col) 
        feature = load_nii_file(file_path) # get 3D volume (x, y, z) 
        feature = np.expand_dims(feature, axis=0) # add channel (c, x, y, z)
        label = cols_from_xlsx(self.xlsx_file, index, 1, self.class_cols_list) # get label
        return feature, label.astype(np.bool)


def main():
dataset = data_from_xlsx("train.xlsx", "file_path", ["pos", "neg"], transformations, aug=True)
    data_loader = DataLoader(dataset, batch_size=4, shuffle=True)

    for (f, l) in data_loader:
        print("f shape", f.shape)
        print("l shape", l.shape)

跑的时候报错main()

 File "d:\pytorch\lib\site-packages\torch\utils\data\dataloader.py", line 346, in __next__
    data = self.dataset_fetcher.fetch(index)  # may raise StopIteration
  File "d:\pytorch\lib\site-packages\torch\utils\data\_utils\fetch.py", line 47, in fetch
    return self.collate_fn(data)
  File "d:\pytorch\lib\site-packages\torch\utils\data\_utils\collate.py", line 80, in default_collate
    return [default_collate(samples) for samples in transposed]
  File "d:\pytorch\lib\site-packages\torch\utils\data\_utils\collate.py", line 80, in <listcomp>
    return [default_collate(samples) for samples in transposed]
  File "d:\pytorch\lib\site-packages\torch\utils\data\_utils\collate.py", line 65, in default_collate
    return default_collate([torch.as_tensor(b) for b in batch])
  File "d:\pytorch\lib\site-packages\torch\utils\data\_utils\collate.py", line 65, in <listcomp>
    return default_collate([torch.as_tensor(b) for b in batch])
ValueError: some of the strides of a given numpy array are negative. This is currently not supported, but will be added in future release

报告的错误对我来说没有意义,所以我用谷歌搜索了它。起初我以为我没有将featurefrom更改numpy.array为张量,所以我尝试了feature = torch.from_array(feature.copy())又尝试transforms.TOTensor()了,但两次尝试都失败了。

标签: pythonpytorch

解决方案


感谢@jodag 和@UsmanAli 的建议,我通过返回解决了这个问题torch.from_numpy(feature.copy())torch.tensor(label.astype(np.bool)) 所以整个事情应该是,

class data_from_xlsx(Dataset):
    def __init__(self, xlsx_fp, path_col, class_cols_list):
        self.xlsx_file = pd.read_excel(xlsx_fp)
        self.path_col = path_col
        self.class_cols_list = class_cols_list

    def __len__(self):
        return get_xlsx_length(self.xlsx_file)

    def __getitem__(self, index):
        file_path = cols_from_xlsx(self.xlsx_file, index, 1, self.path_col) 
        feature = load_nii_file(file_path) # get 3D volume (x, y, z) 
        feature = np.expand_dims(feature, axis=0) # add channel (c, x, y, z)
        label = cols_from_xlsx(self.xlsx_file, index, 1, self.class_cols_list) # get label
        return torch.from_numpy(feature.copy()), torch.tensor(label.astype(np.bool))

推荐阅读