写下来让自己有个参考
读取dataloader输出时字典的时候找不到正确的键
但是把出错的路径打印出来发现也没有问题
解决方法:
Windows文件分隔符默认为"“,由于是转义符,通常开发中写为”\“,注意,Windows也可以识别”/"分隔符
而linux只能识别"/"
意思是你在windows下对数据进行预处理,构建dataloader中返回的路径是不能被linux识别的
一个例子
class dataloader(Dataset):
def __getitem__(self, index):
filepath = self.files[index % len(self.files)]
filename = filepath.split('\\')[-1]
#filename = filepath.split('/')[-1](linux用这行)
img = self.transform(Image.open(filepath))
label = self.annotations[filename]
label = torch.FloatTensor(np.array(label))
return img, label
解决办法:
self.files = [file.replace(‘\’, ‘/’) for file in self.files]
另一个例子
class dataloader(Dataset):
......
def __getitem__(self, index):
left_image = Image.open(self.file_names[index]).convert('RGB')
image_name = self.file_names[index].replace(self.img_folder, "")
orig_attributes = self.attributes[image_name]
transformed_attributes = self.get_transformed_attributes(image_name)
......
直接image_name = os.path.basename(self.file_names[index])即可