FileNotFoundError: [Errno 2] No such file or directory: 'data/images/00018.png'

Hey! I am trying to execute a github repo LOHO for HairStyle Transfer.
But its getting pretty problematic.
This is the link to my colab file : Google Colaboratory

This is the repo that I am trying to execute : GitHub - dukebw/LOHO: Demo code for "LOHO: Latent Optimization of Hairstyles via Orthogonalization".

Error :

[7]
!python /content/LOHO/loho.py
!python /content/LOHO/loho.py
Traceback (most recent call last):
File “/content/LOHO/loho.py”, line 99, in
image_files[‘I_1_path’], image_files[‘M_1_path’], size=resize, normalize=1
File “/content/LOHO/datasets/ffhq.py”, line 77, in process_image
img = Image.open(img_path).convert(“RGB”)
File “/usr/local/lib/python3.7/site-packages/PIL/Image.py”, line 2912, in open
fp = builtins.open(filename, “rb”)
FileNotFoundError: [Errno 2] No such file or directory: ‘data/images/00018.png’

Please help , Thank you

1 Like

Hi Saakshi,

You are getting this error:

FileNotFoundError: [Errno 2] No such file or directory: 'data/images/00018.png'

Isn’t that error message clear enough? There is no such file by that
name in the current directory. Either the file is misspelled or missing,
or one of the directories is misspelled or missing. Or your working
directory is not where you think it is, and you are not in the directory
you expected.

If you used os.chdir to change working directories, that could be the
problem.

Check that you are executing the code from the correct directory, on the
correct machine. Check that you haven’t misspelled the file name.
Remember that on Linux, file names are case-sensitive: 00018.png is not
the same as 00018.PNG.

Check that you are running under the correct user with the correct
permissions to see inside the directories – sometimes, if you don’t
have correct permissions, you might get “file not found”.

Do you need help with these?

1 Like

I somehow manage to get the path right.
But now I am getting this "Runtime Error"

Setting up Perceptual loss…
Loading model from: /content/LOHO/networks/lpips/weights/v0.1/vgg.pth
…[net-lin [vgg]] initialized
…Done
Setting up Perceptual loss…
Loading model from: /content/LOHO/networks/lpips/weights/v0.1/vgg.pth
…[net-lin [vgg]] initialized
…Done
Constructing DeepLabv3+ model…
Number of classes: 20
Output stride: 16
Number of Input Channels: 3
Traceback (most recent call last):
File “/content/LOHO/loho.py”, line 180, in
state_dict = torch.load(graphonomy_model_path)
File “/usr/local/lib/python3.7/site-packages/torch/serialization.py”, line 593, in load
return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)
File “/usr/local/lib/python3.7/site-packages/torch/serialization.py”, line 779, in _legacy_load
deserialized_objects[key]._set_from_file(f, offset, f_should_read_directly)
RuntimeError: unexpected EOF, expected 1362420 more bytes. The file might be corrupted.

1 Like

You “somehow” got the path right? What did you do, try random paths
until it worked? Please, if you are having problems with specific tasks,
you can ask for help.

“How do I find out the path to my file?” is a good question to ask, if
you don’t know how.

Anyway, moving on…

The error message looks fairly complete and self-explanatory to
me. It says:

RuntimeError: unexpected EOF, expected 1362420 more bytes.
The file might be corrupted.

So the file might be corrupted. Are you sure the file is good? How do
you know it is a good file? Where did you get it from?

This does not seem to be the same file as before. Earlier, your file was
a PNG image: 00018.png, if I remember correctly. Now you are unpickling
a model from a “.pth” file:

Loading model from: /content/LOHO/networks/lpips/weights/v0.1/vgg.pth

Where did this file come from? Did you create it?

One thing which would be very useful: in this script:

/content/LOHO/loho.py

line 180 says:

state_dict = torch.load(graphonomy_model_path)

but we don’t know what the graphonomy_model_path is. Can you put a line
immediately before line 180, saying:

print(graphonomy_model_path)

That will tell us what file is being used.

It looks like you are using the “torch” third-party library, and it is
trying to unpickle some file. You need to identify which file it is
using, and make sure it is the correct file. You need to do this
debugging yourself – I don’t have access to your code or data files or
the library you are using.

Maybe you are trying to use the wrong data file. Maybe the file is
corrupt. Maybe the library is buggy. It is hard to say what is
happening. Can you provide the smallest possible example that fails?

http://www.sscce.org/

That might help us to track down the cause of the fault.

1 Like

This was not about the path, the images in the dataset provided were in ‘.jgp’ but i don’t know why the code was looking for '.png ’ files. Is there any way to change this like with code ? which could have been used here ??

Also, I printed the print(graphonomy_model_path)
I got the path as /content/drive/MyDrive/inference.pth

if you want to access the code here is the link : [GitHub - saakshi077/LOHO: Demo code for "LOHO: Latent Optimization of Hairstyles via Orthogonalization".]
Link to the colab file : Google Colaboratory

1 Like

Hi,

I’m sorry, your question confuses me:

“”"
This was not about the path, the images in the dataset provided were in
‘.jgp’ but i don’t know why the code was looking for '.png ’ files. Is
there any way to change this like with code ? which could have been used
here ??
“”"

You said you fixed it. So you have already changed it with code. Or
maybe I don’t understand what you have done, or your question.

It is hard to help you when I don’t understand your questions.

You printed the graphonomy_model_path, thank you, and got:

/content/drive/MyDrive/inference.pth

Who provided that file? Your program is reporting that the file is
corrupt. Is it possible that it is corrupt, or that you are using the
wrong file?

If you are on a Linux system, it might help to run:

file /content/drive/MyDrive/inference.pth

and see what sort of file it is.

1 Like

When you open a file with the name “filename.ext”; you are telling the open() function that your file is in the current working directory . This is called a relative path.

file = open('filename.ext') //relative path

In the above code, you are not giving the full path to a file to the open() function, just its name - a relative path. The error “FileNotFoundError: [Errno 2] No such file or directory” is telling you that there is no file of that name in the working directory. So, try using the exact, or absolute path.

file = open(r'C:\path\to\your\filename.ext') //absolute path

In the above code, all of the information needed to locate the file is contained in the path string - absolute path.

If the user does not pass the full path to the file (on Unix type systems this means a path that starts with a slash), the python file path is interpreted relatively to the current working directory. The current working directory usually is the directory in which you started the program. In order to make this work, the directory containing the python executable must be in the PATH, a so-called environment variable that contains directories that are automatically used for searching executables when you enter a command. In any case, if your Python script file and your data input file are not in the same directory, you always have to specify either a relative path between them or you have to use an absolute path for one of them.

2 Likes

Thanks. It helped

1 Like

If the script is in the same directory as the file to open, then get the script directory at startup via script_dir = os.path.dirname(os.path.abspath(__file__)). Then join the script directory with the filename via filepath = os.path.join(script_dir, "filename.ext").

The script directory is generally not the same as the current working directory. Never assume it is.

1 Like