PytorchStreamReader failed locating file constants.pkl: file not found

Hello,

I want to use YOLOv5 for object detection Using C++ and libtorch. I’m using Windows 10, and VS 2019.

However, the following code:

#include <torch/script.h>
#include <torch/torch.h>

int main(int argc, char* argv[]) try
{
    torch::jit::script::Module module;
    try
    {
        // Deserialize the ScriptModule from a file using torch::jit::load().
        module = torch::jit::load("C:/Users/hedey/source/repos/Road_Defects_Detector/YOLOv5s.pt");
    }
    catch (const c10::Error& e)
    {
        std::cerr << "error loading the model\n" << e.msg();
        std::system("pause");
    }
    return 0;
} 

is causing the following error:

error loading the model
PytorchStreamReader failed locating file constants.pkl: file not foundPress any key to continue . . .

I think this issue is related LibTorch cannot load PyTorch exported model · Issue #47917 · pytorch/pytorch · GitHub

Maybe you are trying to load a non-TorchScripted model.

1 Like

You’re correct @mhbassel. I converting to torch script via tracing as described in this link, and it worked. Thanks a lot.

2 Likes