Newbees
Newbees

Reputation: 43

FileNotFoundError: [Errno 2] :No such file or directory: 'C:/Users/My_computer/Desktop/Compare/MHAN-master/AID_train/AID_train_LR/x4\\9.png'

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

Upvotes: 1

Views: 1565

Answers (1)

ValentinB
ValentinB

Reputation: 79

'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:

[1]: https://i.sstatic.net/5UdXQ.png

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.

Upvotes: 3

Related Questions