【问题标题】:FileNotFoundError: [Errno 2] :No such file or directory: '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'
【发布时间】:2021-05-17 13:22:41
【问题描述】:

我对 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,target,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", 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))
  

文件 "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吗? 请帮我解决这个错误

【问题讨论】:

标签: python pytorch python-imaging-library


【解决方案1】:

'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”,以便将它们转换为原始字符串。

【讨论】:

  • 请查看修改后的问题,其中我提到了 os.path 详细信息。
  • 我已经更新了我的答案,您仍然遇到同样的问题,因为您无法访问实际路径
  • 您好@ValentinB,我已根据您的建议更改了代码。但仍然出现此错误:SyntaxError: EOL while scanning string literal
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-03
  • 2021-08-24
  • 2021-03-07
相关资源
最近更新 更多