1

I am able to save a PyTorch custom model? (it can work any PyTorch version above 1.0)

However, I am not able to read the saved model. I am trying to read it via cv2.dnn.readNetFromTorch() so as to use the model in Opencv framework (4.1.0).

I saved the PyTorch model with different methods as follows to see whether this difference reacts to the reading function of cv2.dnn.

        torch.save(model.state_dict(), '/home/aktaseren/people-opencv/pidxx.pt')
        torch.save(model.state_dict(), '/home/aktaseren/people-opencv/pidxx.t7')
        torch.save(model, '/home/aktaseren/people-opencv/pidxx.t7')
        torch.save(model, '/home/aktaseren/people-opencv/pidxx.pth')

None of these saved file can be readable via cv2.dnn.readNetFromTorch().

The error I am getting is always the same on this issue, which is below.

cv2.error: OpenCV(4.1.0) ../modules/dnn/src/torch/torch_importer.cpp:1022: error: (-213:The function/feature is not implemented) Unsupported Lua type in function 'readObject'

Do you have any idea how to solve this issue?

2 Answers 2

1

OpenCV documentation states can only read in torch7 framework format. There is no mention of .pt or .pth saved by pytorch.

This post mentions pytorch does not save as .t7.

.t7 was used in Torch7 and is not used in PyTorch. If I’m not mistaken the file extension does not change the behavior of torch.save.

An alternate method is to export the model as onnx, then read the model in opencv using readNetFromONNX.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I got it now that I cannot save a PyTorch model with .t7 format in order to read it by cv2.dnn.readNetFromTorch. I also understand the differences now between Torch and PyTorch frameworks (stackoverflow.com/questions/44371560/…) According to official tutorials of saving/loading models in PyTorch, I can only save PyTorch models with .pt or pth format that can't be readable by cv2 (pytorch.org/tutorials/beginner/saving_loading_models.html). As you suggested, I will have a look at the ONXX option and update you here.
0

Yes, we have tested these methods (saving the model as .pt or .pth). And we couldn't load these model files by using opencv readNetFromTorch. We should use LibTorch or ONNX as the intermediate model file to be read from C++.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.