大佬教程收集整理的这篇文章主要介绍了FileNotFoundError: [Errno 2] : 没有这样的文件或目录: 'C:/Users/My_computer/Desktop/Compare/MHAN-master/AID_train/AID_train_LR/x4\\9.png',大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我对 python 环境很陌生。我曾尝试使用我自己的数据集编译用于放大因子 4 的超分辨率代码。低分辨率 RGB 图像保存在“C:/Users/My_computer/Desktop/Compare/MHAN-master/AID_Train/AID_Train_LR/x4”中。用于图片加载的代码如下所示:
def load_img(filepath):
img = Image.open(filepath).convert('RGB')
#img = Image.open(filepath,'rb')
#y,_,_ = img.split()
return img
class DatasetFromFolder(data.Dataset):
def __init__(self,image_dir,lr_dir,patch_size,upscale_factor,data_augmentation,transform=NonE):
super(DatasetFromFolder,self).__init__()
self.image_filenames = [join(image_dir,X) for x in Listdir(image_dir) if is_image_file(X)]
self.patch_size = patch_size
self.upscale_factor = upscale_factor
self.transform = transform
self.data_augmentation = data_augmentation
self.HR ='C://Users//My_computer//Desktop//Compare//MHAN-master//AID_Train//AID_Train_HR'
self.LR ='C://Users//My_computer//Desktop//Compare//MHAN-master//AID_Train//AID_Train_LR//x4'
def __getitem__(self,indeX):
target = load_img(self.image_filenames[index])
input = load_img(os.path.join(self.LR,filE))
input,target,_ = get_patch(input,self.patch_size,self.upscale_factor)
return input,target
但是在编译训练代码时出现以下错误:
file "main_x4.py",line 185,in <module>
Train(model,epoch)
file "main_x4.py",line 60,in Train
for iteration,batch in enumerate(Training_data_loader,1):
file "C:\Users\My_computer\anaconda3\envs\MHAN\lib\site-packages\torch\utils\data\DataLoader.py",line 346,in __next__
data = self._dataset_fetcher.fetch(indeX) # may raise stopiteration
file "C:\Users\My_computer\anaconda3\envs\MHAN\lib\site-packages\torch\utils\data\_utils\fetch.py",line 44,in fetch
data = [self.dataset[IDx] for IDx in possibly_batched_index]
file "C:\Users\My_computer\anaconda3\envs\MHAN\lib\site-packages\torch\utils\data\_utils\fetch.py",in <Listcomp>
data = [self.dataset[IDx] for IDx in possibly_batched_index]
file "C:\Users\My_computer\Desktop\Compare\MHAN-master\dataset_x4.py",line 91,in __getitem__
input = load_img(os.path.join(self.LR,filE))
文件“C:\Users\My_computer\Desktop\Compare\MHAN-master\dataset_x4.py”, 第 16 行,在 load_img
img = Image.open(filepath).convert('RGB')
file "C:\Users\My_computer\anaconda3\envs\MHAN\lib\site-packages\PIL\Image.py",
第 2912 行,打开中 fp = builTins.open(filename,"rb")
fileNotFoundError: [Errno 2] No such file or directory: 'C://Users//My_computer//Desktop//Compare//MHAN-master//AID_Train//AID_Train_LR//x4\\9.png'
LR图片已经是RGB格式了,还需要再转RGB吗? 请帮我解决这个错误
'C:/Users/My_computer/Desktop/Compare/MHAN-master/AID_Train/AID_Train_LR/x4\\9.png'
您的字符串在路径末尾包含双反斜杠,这就是您无法访问目录的原因
使用像这样的原始字符串
r'yourString'
或查看您的 os.path.join
编辑:
尝试将每个字符串转换为原始字符串,如上所述。你仍然得到双反斜杠,因为某些 \character 组合被转义。
这些是转义字符:
编辑您的代码:
self.HR =r'C:/Users/My_computer/Desktop/Compare/MHAN-
master/AID_Train/AID_Train_HR'
self.LR =r'C:/Users/My_computer/Desktop/Compare/MHAN-
master/AID_Train/AID_Train_LR/x4'
请注意字符串前面的“r”以将它们转换为原始字符串。
以上是大佬教程为你收集整理的FileNotFoundError: [Errno 2] : 没有这样的文件或目录: 'C:/Users/My_computer/Desktop/Compare/MHAN-master/AID_train/AID_train_LR/x4\\9.png'全部内容,希望文章能够帮你解决FileNotFoundError: [Errno 2] : 没有这样的文件或目录: 'C:/Users/My_computer/Desktop/Compare/MHAN-master/AID_train/AID_train_LR/x4\\9.png'所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。