opencv: Cannot load a simple model save by PyTorch

os : ubuntu16.04.1 opencv : 3.3 compiler : gcc5.6 python : 3.5

I train and save a very simple cnn model by PyTorch

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

I save it by two solutions

torch.save(net.state_dict(), 'cifar10_model')
torch.save(net, 'cifar10_model.net')

Then load it by c++ api

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);

It always throw exception

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

But the module only use Conv2d, ReLU, BatchNorm2d, MaxPool2d and Linear layer only, every layers are supported by opencv3.3, ENet of torch is more complicated than this one. I am doing something wrong or this is a bug of 3.3?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 16 (6 by maintainers)

Most upvoted comments

@dkurt I restudy your answer, do you mean opencv do not support PyTorch(http://pytorch.org/) but pytorch(https://github.com/hughperkins/pytorch)?If that is the case, please specify it clearly on the document, it is confusing, I believe today most of the people think PyTorch is (https://github.com/pytorch/pytorch) but not pytorch(https://github.com/hughperkins/pytorch).

Thanks for your helps again.

@l-bat, thank you for your ONNX module, but I found it don’t support ONNX model containing LSTM layer.