python - 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
报告的错误对我来说没有意义,所以我用谷歌搜索了它。起初我以为我没有将feature
from更改numpy.array
为张量,所以我尝试了feature = torch.from_array(feature.copy())
又尝试transforms.TOTensor()
了,但两次尝试都失败了。
解决方案
感谢@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))
推荐阅读
- android - 应用程序更新,只有卸载按钮,Playstore 中没有更多图标或打开按钮
- ios - 如何在 iPhone 的联系人应用程序中获取自定义部分索引标题
- sql-server - T-SQL,如果第一个条件为真,则选择数据,否则从第二列获取最新的
- c++ - 如何使用 MATLAB 编码器将训练有素的神经网络用作函数?
- django-models - 如何将表单的多个输入字段数据存储到django中的单个表(模型)中
- postgresql - 如何使用 PostgreSql 在代码优先方法中自动创建表
- python - 如何获得单词的 TF-IDF 分数?
- java - Spring JPA:OneToMany List 更新导致重复条目
- angular - 在Angular 7中滚动div
- android - 即使手机处于静音/请勿打扰模式也播放声音