Model cannot be loaded with torch::jit::load

Hi dear Team
I am trying to load a saved, serialized model in C++ but the model cannot be loaded with error:

open file failed because of errno 2 on fopen: No such file or directory, file path:
Exception raised from RAIIFile at …\caffe2\serialize\ (most recent call first):
00007FFA4A1EA29200007FFA4A1EA230 c10.dll!c10::Error::Error [ @ ]
00007FFA4A1E9D1E00007FFA4A1E9CD0 c10.dll!c10::detail::torchCheckFail [ @ ]
00007FF9DAE717B300007FF9DAE716C0 torch_cpu.dll!caffe2::serialize::FileAdapter::FileAdapter [ @ ]
00007FF9DC09E4EF00007FF9DC09E450 torch_cpu.dll!torch::jit::load [ @ ]
00007FF7811F6B9E00007FF7811F6A40 dcgan.exe!main [c:\users\mskfi\downloads\examples-master\cpp\dcgan\dcgan.cpp @ 24]
00007FF7812251D400007FF7812251A0 dcgan.exe!invoke_main [d:\agent_work\2\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl @ 79]
00007FF78122507E00007FF781224F50 dcgan.exe!__scrt_common_main_seh [d:\agent_work\2\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl @ 288]
00007FF781224F3E00007FF781224F30 dcgan.exe!__scrt_common_main [d:\agent_work\2\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl @ 331]
00007FF78122526900007FF781225260 dcgan.exe!mainCRTStartup [d:\agent_work\2\s\src\vctools\crt\vcstartup\src\startup\exe_main.cpp @ 17]
00007FFA6938703400007FFA69387020 KERNEL32.DLL!BaseThreadInitThunk [ @ ]
00007FFA6ACE265100007FFA6ACE2630 ntdll.dll!RtlUserThreadStart [ @ ]
T _ pG¢B┬☺ ▓h·⌂ ÿ♣ ƒ♣ M¢B┬☺ [­▓h·⌂

Following installation / implementation:
Using the example in Loading a TorchScript Model in C++ — PyTorch Tutorials 1.10.0+cu102 documentation

Installed pytorch with
conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch

Saved the model, as described in the example, with traced_script_module = torch.jit.trace(model, example)

Installed locally CUDA 11.3.1 and respective Cudnn

Used the C++ example in examples/cpp/dcgan at master · pytorch/examples · GitHub

Build based on instructions with cmake -DCMAKE_PREFIX_PATH=“libtorch-win-shared-with-deps-1.10.0+cu113\libtorch” … -G “Visual Studio 15 2017” -A x64
Project compiles and starts

Changed the code to load the model in:

#include <torch/script.h> // One-stop header.

namespace fs = std::experimental::filesystem;

int main(int argc, const char* argv[]) {
if (argc != 2) {
std::cerr << “usage: example-app \n”;
return -1;

torch::jit::script::Module module;
bool ex = fs::exists(argv[1]);
if (ex)
std::cerr << “model exists\n”;
} else {
std::cerr << “model missing\n”;
try {
// Deserialize the ScriptModule from a file using torch::jit::load().
module = torch::jit::load(argv[1]);
} catch (const c10::Error& e) {
std::cerr << “error loading the model\n”;
std::cerr << e.what();
std::cerr << e.msg();
return -1;

std::cout << “ok\n”;

I run the project by calling:
dcgan.exe “<path_to>/”

filesystem::exists is true

The error I get changed from time to time between:
open file failed because of errno 2 on fopen: No such file or directory, file path:
open file failed because of errno 22 on fopen: Invalid argument, file path:

file path: is always empty

Any hints?

Thank you very much


Solved: unfortunately I was just mixing build configurations, so when using the torchlib release package, the projects need to be built in release!

1 Like

Thanks for sharing the hints. Could you please briefly explain the way you’ve modified it? I am currently having the same errors.