Pytorch自定义数据集:ValueError:给定numpy数组的某些步幅为负

时间:2019-08-16 00:50:27

标签: python pytorch

我写了一个自定义的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

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

2 个答案:

答案 0 :(得分:1)

由于@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))

答案 1 :(得分:0)

在某些情况下,您可能需要 numpy.ascontiguousarray 返回内存中的连续数组(ndim >= 1)(C 顺序)。然后,您可以使用 torch.from_numpy()

点击 this link 了解更多信息。

祝你好运。