torch.load(path),FileNotFoundError: [Errno 2] No such file or directory:

nikiguo93: <p>I download a model and try to use torch.load(path),but an error occurs, No such file or directory… how to solve the problem?<br>
FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\<em><strong>\Desktop\ Gemma文献\feature__inversion\moco\moco\moco_v1_200ep_pretrain.pth’<br>
[Finished in 7.0s with exit code 1]<br>
[shell_cmd: python -u "C:\Users*</strong>\Desktop\pytorchlearning\ModelParamLearning.py"]<br>
[dir: C:\Users*</em>**\Desktop\pytorchlearning]<br>
[path: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\libnvvp;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\MATLAB\R2017b\runtime\win64;C:\Program Files\MATLAB\R2017b\bin;C:\Program Files\Git\cmd;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files\SlikSvn\bin;C:\Program Files\NVIDIA Corporation\Nsight Compute 2020.2.0;C:\Program Files (x86)\NVIDIA C</p>

ptrblck: <p>Based on the error message your specified path is wrong and the file cannot be found at this location, so you would need to fix the path.</p>

Kapil_Rana: <p>The file location format is not correct. In windows you can’t do it like it.<br>
1st solution will be, save the model relative to the code directory. then don’t need to give full path like c:.</p>
<p>2nd<br>
Try path like this C:/Users/myName/My Documents. “/” instead of “”.</p>
<p>3rd<br>
Compose a path like raw string<br>
model_loc = r"C:\Users\myName\My Documents"</p>
<p>I feel first method is easier and simple.</p>

lovelmark: <aside class="quote no-group" data-username="nikiguo93" data-post="1" data-topic="118532">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/nikiguo93/48/30791_2.png" class="avatar"> nikiguo93:</div>
<blockquote>
<p>FileNotFoundError: [Errno 2] No such file or directory:</p>
</blockquote>
</aside>
<p>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 <a href="http://net-informations.com/python/file/open.htm" rel="noopener nofollow ugc">python file</a> 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.</p>
<p>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.</p>

ume_habiba: <p>I want to load weights of trained model which is save on local machine . For this i i’m using custom function but getting the following error<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/8/686bb3fcd7028a789016bc9c389dbf3acf75153a.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/686bb3fcd7028a789016bc9c389dbf3acf75153a" title="1.PNG"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/6/8/686bb3fcd7028a789016bc9c389dbf3acf75153a_2_690x397.jpeg" alt="1.PNG" data-base62-sha1="eTKpoU36sQxKl6VvwU5Z2agdAKK" width="690" height="397" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/6/8/686bb3fcd7028a789016bc9c389dbf3acf75153a_2_690x397.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/6/8/686bb3fcd7028a789016bc9c389dbf3acf75153a_2_1035x595.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/6/8/686bb3fcd7028a789016bc9c389dbf3acf75153a_2_1380x794.jpeg 2x" data-dominant-color="2F2824"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">1.PNG</span><span class="informations">1920×1106 163 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
Can you please tell me what is this error?</p>

ptrblck: <p><code>fname</code> seems to be an empty string which is why the <code>torch.load</code> operation fails. Make sure to pass a valid file name and rerun your script.</p>

ume_habiba: <p>Hi,<br>
Thanks for replying<br>
The first figure is the model from which I want to load the weights .<br>
2nd is the trained model and 3rd is the error line.<br>
How can I load the features from a pretrained model using a custom function? Can you please help me ?</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/d/2dca0fe87a269a4b3e5766f56e6833cb3977794b.png" data-download-href="https://discuss.pytorch.org/uploads/default/2dca0fe87a269a4b3e5766f56e6833cb3977794b" title="model.PNG"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/2/d/2dca0fe87a269a4b3e5766f56e6833cb3977794b_2_621x500.png" alt="model.PNG" data-base62-sha1="6x4oWtWt1BEtzg8bLnZNI3cSjEv" width="621" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/2/d/2dca0fe87a269a4b3e5766f56e6833cb3977794b_2_621x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/2/d/2dca0fe87a269a4b3e5766f56e6833cb3977794b_2_931x750.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/2/d/2dca0fe87a269a4b3e5766f56e6833cb3977794b_2_1242x1000.png 2x" data-dominant-color="F1F0E8"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">model.PNG</span><span class="informations">1550×1247 50.4 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/c/ac160bb9cded9c3d192ffc087692bbca2e2ddc89.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/ac160bb9cded9c3d192ffc087692bbca2e2ddc89" title="205.PNG.jpg"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/a/c/ac160bb9cded9c3d192ffc087692bbca2e2ddc89_2_690x372.jpeg" alt="205.PNG.jpg" data-base62-sha1="oylmbvVdIlzdIHfMUHeiMqgPQvf" width="690" height="372" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/a/c/ac160bb9cded9c3d192ffc087692bbca2e2ddc89_2_690x372.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/a/c/ac160bb9cded9c3d192ffc087692bbca2e2ddc89_2_1035x558.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/a/c/ac160bb9cded9c3d192ffc087692bbca2e2ddc89_2_1380x744.jpeg 2x" data-dominant-color="272B24"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">205.PNG.jpg</span><span class="informations">2807×1515 679 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/6/36dbbe92e30995067ed9c4bc0755c5ec56b623c4.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/36dbbe92e30995067ed9c4bc0755c5ec56b623c4" title="trained.PNG.jpg"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/6/36dbbe92e30995067ed9c4bc0755c5ec56b623c4_2_689x429.jpeg" alt="trained.PNG.jpg" data-base62-sha1="7PiAbDiQbfKLNK1zByRKafArKNC" width="689" height="429" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/6/36dbbe92e30995067ed9c4bc0755c5ec56b623c4_2_689x429.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/3/6/36dbbe92e30995067ed9c4bc0755c5ec56b623c4_2_1033x643.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/3/6/36dbbe92e30995067ed9c4bc0755c5ec56b623c4_2_1378x858.jpeg 2x" data-dominant-color="242721"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">trained.PNG.jpg</span><span class="informations">2569×1600 681 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

ptrblck: <p>Somewhere in your script you would most likely need to create a variable with the path to the model <code>state_dict</code>, which currently seems to be empty. Check your code where the model is loaded and make sure you are properly defining the path to the needed files.</p>
<p>It’s generally better to post code snippet, which would be easier to debug, as I cannot help using your screenshots.</p>

ume_habiba: <p>Here is the link to the github repository which I’m following.<br>
<a href="https://github.com/thilinicooray/context-aware-reasoning-for-sr" rel="noopener nofollow ugc">https://github.com/thilinicooray/context-aware-reasoning-for-sr</a></p>
<p>I’m trying to run the <a href="https://github.com/thilinicooray/context-aware-reasoning-for-sr/blob/master/main_top_down_query_context.py" rel="noopener nofollow ugc">main_top_down_query_context.py</a></p>