How can I read pytorch model file via cv2.dnn.readNetFromTorch()?

Viewed 1273

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

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.

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++.

Related