程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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'?

开发过程中遇到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'的解决方法建议,希望对你解决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 组合被转义。

这些是转义字符:

FileNotFoundError: [Errno 2] : 没有这样的文件或目录: 'C:/Users/My_computer/Desktop/Compare/MHAN-master/AID_train/AID_train_LR/x4\\9.png'

编辑您的代码:

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,请注明来意。