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

查看:72
本文介绍了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(文件路径, 'rb')#y, _, _ = img.split()返回图片类 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_sizeself.upscale_factor = upscale_factorself.transform = 变换self.data_augmentation = data_augmentationself.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):目标 = load_img(self.image_filenames[index])输入 = load_img(os.path.join(self.LR, file))输入,目标,_ = get_patch(输入,目标,self.patch_size,self.upscale_factor)返回输入,目标

但是在编译训练代码时出现以下错误:

文件main_x4.py",第 185 行,在  中火车(模型,时代)文件main_x4.py",第 60 行,在火车中对于迭代,批处理枚举(training_data_loader, 1):文件C:\Users\My_computer\anaconda3\envs\MHAN\lib\site-packages\torch\utils\data\dataloader.py",第 346 行,在 __next__ 中data = self._dataset_fetcher.fetch(index) # 可能会引发 StopIteration文件C:\Users\My_computer\anaconda3\envs\MHAN\lib\site-packages\torch\utils\data\_utils\fetch.py​​",第 44 行,在 fetch 中data = [self.dataset[idx] for idx in possible_batched_index]文件C:\Users\My_computer\anaconda3\envs\MHAN\lib\site-packages\torch\utils\data\_utils\fetch.py​​",第 44 行,在 <listcomp>data = [self.dataset[idx] for idx in possible_batched_index]文件C:\Users\My_computer\Desktop\Compare\MHAN-master\dataset_x4.py",第 91 行,在 __getitem__ 中输入 = 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')

 文件C:\Users\My_computer\anaconda3\envs\MHAN\lib\site-packages\PIL\Image.py",

第 2912 行,打开中fp = builtins.open(文件名,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-主/AID_train/AID_train_HR'self.LR =r'C:/Users/My_computer/Desktop/Compare/MHAN-主/AID_train/AID_train_LR/x4'

请注意r"在字符串前面将它们转换为原始字符串.

I'm very new to python environment. I have tried to compile a super-resolution code for upscaling factor 4 using my own dataset. The low resolution RGB images are kept in "C:/Users/My_computer/Desktop/Compare/MHAN-master/AID_train/AID_train_LR/x4". The code used for image load is shown in below:

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,target,self.patch_size, self.upscale_factor)
    return input, target

But I am getting the following error while the training code is compiled:

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", line 44, 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))
  

File "C:\Users\My_computer\Desktop\Compare\MHAN-master\dataset_x4.py", line 16, in load_img

img = Image.open(filepath).convert('RGB')

  File "C:\Users\My_computer\anaconda3\envs\MHAN\lib\site-packages\PIL\Image.py",

line 2912, in open 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'

As LR images are already in RGB format, so is it necessary to convert to RGB again? Please help me to fix this error

解决方案

'C:/Users/My_computer/Desktop/Compare/MHAN-master/AID_train/AID_train_LR/x4\\9.png'

Your string contains a double backslash at the end of the path, that's why you can't access the directory

use a raw string like

r'yourString'

or review your os.path.join

EDIT:

Try to convert every string into a raw-String, like mentioned above. You are still getting double backslashes, because certain \character combinations are escaped.

These are the escaped characters:

Edit your code to:

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'

Please notice the "r" in front of the string to convert them into raw-Strings.

这篇关于FileNotFoundError: [Errno 2] : 没有这样的文件或目录: 'C:/Users/My_computer/Desktop/Compare/MHAN-master/AID_train/AID_train_LR/x4\\9.png'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆