gpt4 book ai didi

python - "IsADirectoryError: [Errno 21] Is a directory: "是一个文件

转载 作者:太空狗 更新时间:2023-10-30 02:15:10 33 4
gpt4 key购买 nike

我已经将数据分成测试集和训练集到不同的文件夹中。现在我需要加载患者数据。每个病人有8张图像。

def load_dataset(root_dir, split):
"""
load the data set numpy arrays saved by the preprocessing script
:param root_dir: path to input data
:param split: defines whether to load the training or test set
:return: data: dictionary containing one dictionary ({'data', 'seg', 'pid'}) per patient
"""
in_dir = os.path.join(root_dir, split)
data_paths = [os.path.join(in_dir, f) for f in os.listdir(in_dir)]
data_and_seg_arr = [np.load(ii, mmap_mode='r') for ii in data_paths]
pids = [ii.split('/')[-1].split('.')[0] for ii in data_paths]
data = OrderedDict()
for ix, pid in enumerate(pids):
data[pid] = {'data': data_and_seg_arr[ix][..., 0], 'seg': data_and_seg_arr[ix][..., 1], 'pid': pid}
return data

但是,错误说:

File "/home/zhe/Research/Seg/heart_seg/data_loader.py", line 61, in load_dataset
data_and_seg_arr = [np.load(ii, mmap_mode='r') for ii in data_paths]
File "/home/zhe/Research/Seg/heart_seg/data_loader.py", line 61, in <listcomp>
data_and_seg_arr = [np.load(ii, mmap_mode='r') for ii in data_paths]
File "/home/zhe/anaconda3/envs/tf_env/lib/python3.6/site-packages/numpy/lib/npyio.py", line 372, in load
fid = open(file, "rb")
IsADirectoryError: [Errno 21] Is a directory: './data/preprocessed_data/train/Patient009969'

已经是文件名,不是目录。谢谢!

最佳答案

看来 ./data/preprocessed_data/train/Patient009969 是一个目录,而不是一个文件。

os.listdir() 返回文件和目录。

也许尝试使用 os.walk() 代替。它将文件和目录分开处理,并且可以在子目录内部递归以迭代的方式查找更多文件:

data_paths = [os.path.join(pth, f) 
[for pth, dirs, files in os.walk(in_dir) for f in files]

关于python - "IsADirectoryError: [Errno 21] Is a directory: "是一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52338706/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com