如果我希望通过OpenCV dnn模块加载PyTorch的模型,应该如何保存 [英] How should I save the model of PyTorch if I want it loadable by OpenCV dnn module
问题描述
我通过PyTorch训练了一个简单的分类模型,并通过opencv3.3加载了它,但是它抛出异常并说
I train a simple classification model by PyTorch and load it by opencv3.3, but it throw exception and say
OpenCV错误:readObject,文件中未实现功能/功能(不支持的Lua类型) /home/ramsus/Qt/3rdLibs/opencv/modules/dnn/src/torch/torch_importer.cpp, 797行 /home/ramsus/Qt/3rdLibs/opencv/modules/dnn/src/torch/torch_importer.cpp:797: 错误:(-213)函数readObject中不支持的Lua类型
OpenCV Error: The function/feature is not implemented (Unsupported Lua type) in readObject, file /home/ramsus/Qt/3rdLibs/opencv/modules/dnn/src/torch/torch_importer.cpp, line 797 /home/ramsus/Qt/3rdLibs/opencv/modules/dnn/src/torch/torch_importer.cpp:797: error: (-213) Unsupported Lua type in function readObject
模型定义
class conv_block(nn.Module):
def __init__(self, in_filter, out_filter, kernel):
super(conv_block, self).__init__()
self.conv1 = nn.Conv2d(in_filter, out_filter, kernel, 1, (kernel - 1)//2)
self.batchnorm = nn.BatchNorm2d(out_filter)
self.maxpool = nn.MaxPool2d(2, 2)
def forward(self, x):
x = self.conv1(x)
x = self.batchnorm(x)
x = F.relu(x)
x = self.maxpool(x)
return x
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = conv_block(3, 6, 3)
self.conv2 = conv_block(6, 16, 3)
self.fc1 = nn.Linear(16 * 8 * 8, 120)
self.bn1 = nn.BatchNorm1d(120)
self.fc2 = nn.Linear(120, 84)
self.bn2 = nn.BatchNorm1d(84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = x.view(x.size()[0], -1)
x = F.relu(self.bn1(self.fc1(x)))
x = F.relu(self.bn2(self.fc2(x)))
x = self.fc3(x)
return x
此模型仅使用Conv2d,ReLU,BatchNorm2d,MaxPool2d和Linear图层,opencv3.3支持每个图层
This model use Conv2d, ReLU, BatchNorm2d, MaxPool2d and Linear layer only, every layers are supported by opencv3.3
我通过state_dict保存了
I save it by state_dict
torch.save(net.state_dict(), 'cifar10_model')
通过c ++加载为
std::string const model_file("/home/some_folder/cifar10_model");
std::cout<<"read net from torch"<<std::endl;
dnn::Net net = dnn::readNetFromTorch(model_file);
我想我以错误的方式保存模型,为了使用OpenCV加载而保存PyTorch模型的正确方法是什么?谢谢
I guess I save the model with the wrong way, what is the proper way to save the model of PyTorch in order to load using OpenCV? Thanks
我使用另一种方式保存模型,但也无法加载
I use another way to save the model, but it cannot be loaded either
torch.save(net, 'cifar10_model.net')
这是一个错误吗?还是我做错了什么?
Is this a bug?Or I am doing something wrong?
推荐答案
我找到了答案,opencv3.3不支持PyTorch( https://github.com/hughperkins/pytorch ),这是一个很大的惊喜,我不知道还有其他版本的pytorch(看起来像一个已死的项目,很长一段时间都没有更新),希望他们能提到他们在Wiki上支持哪种pytorch.
I found the answer, opencv3.3 do not support PyTorch (https://github.com/pytorch/pytorch) but pytorch (https://github.com/hughperkins/pytorch), it is a big surprise, I never know there are another version of pytorch exist(looks like a dead project, long time haven't updated), I hope they could mention which pytorch they support on wiki.
这篇关于如果我希望通过OpenCV dnn模块加载PyTorch的模型,应该如何保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!