How should I save the model of PyTorch if I want it loadable by OpenCV dnn module

2024/7/4 17:14:51

I train a simple classification model by PyTorch and load it by opencv3.3, but it throw exception and say

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

Model definition

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 xclass 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

This model use Conv2d, ReLU, BatchNorm2d, MaxPool2d and Linear layer only, every layers are supported by opencv3.3

I save it by state_dict

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

Load it by c++ as

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

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

Edit :

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?

Answer

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.

https://en.xdnf.cn/q/73359.html

Related Q&A

Apache Spark ALS - how to perform Live Recommendations / fold-in anonym user

I am using Apache Spark (Pyspark API for Python) ALS MLLIB to develop a service that performs live recommendations for anonym users (users not in the training set) in my site. In my usecase I train th…

python JIRA connection with proxy

Im trying to connect via python-jira using a proxy:server = {"server": "https://ip:port/jira",proxies: {"http": "http://ip:port", "https": "http:/…

How can I iterate over only the first variable of a tuple

In python, when you have a list of tuples, you can iterate over them. For example when you have 3d points then:for x,y,z in points:pass# do something with x y or zWhat if you only want to use the first…

Bottle with Gunicorn

What is the difference between running bottle script like thisfrom bottle import route, run@route(/) def index():return Hello!run(server=gunicorn, host=0.0.0.0, port=8080)with command python app.py and…

Run several python programs at the same time

I have python script run.py:def do(i):# doing something with i, that takes timestart_i = sys.argv[1] end_i = sys.argv[2] for i in range(start_i, end_i):do(i)Then I run this script:python run.py 0 10000…

Using python, what is the most accurate way to auto determine a users current timezone

I have verified that dateutils.tz.tzlocal() does not work on heroku and even if it did, wouldnt it just get the tz from the OS of the computer its on, not necessarly the users?Short of storing a users…

ImportError: cannot import name ParseMode from telegram

I am trying to create a telegram bot. The code i am trying to execute is : from telegram import ParseModeBut it is throwing up this error: ImportError: cannot import name ParseMode from telegram (C:\Pr…

Executing bash with subprocess.Popen

Im trying to write a wrapper for a bash session using python. The first thing I did was just try to spawn a bash process, and then try to read its output. like this:from subprocess import Popen, PIPE b…

Attribute error when attempting to get a value for field

Im working with the django rest framework and the serializer Im trying to use is creating errors. Im trying to do something like https://gist.github.com/anonymous/7463dce5b0bfcf9b6767 but I still get t…

Why did I have problems with alembic migrations

Project structue(only directory with DB migrations):--db_manage:alembic.ini--alembic:env.pyscript.py.makoREADME--versions:#migration filesWhen I try to run command: python db_manage/alembic/env.py, I h…