I serialize my python model to a file and load it in c++. However, I get the following error
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
15:23:44: The program has unexpectedly finished.
Hope someone can help me out.
Here is my python model
import torch
class MyModule(torch.jit.ScriptModule):
def __init__(self, N, M):
super(MyModule, self).__init__()
self.weight = torch.nn.Parameter(torch.rand(N, M))
@torch.jit.script_method
def forward(self, input):
if bool(input.sum() > 0):
output = self.weight.mv(input)
else:
output = self.weight + input
return output
my_script_module = MyModule(10, 10)
my_script_module.save("mymodule.pt")
Here is my c++ code:
#include <iostream>
#include <torch/torch.h>
#include <torch/serialize.h>
#include <torch/script.h>
#include <memory>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
const char* module_name = "/home/ssm/ssj/github/pytorchCPP/example-tracking/test_pytorch/mymodule.pt";
std::shared_ptr<torch::jit::script::Module> module = torch::jit::load(module_name);
assert(module != nullptr);
std::vector<torch::jit::IValue> inputs;
inputs.push_back(torch::ones({10, 10}));
auto output = module->forward(inputs).toTensor();
return 0;
}
Python packages as follows:
certifi==2018.10.15
cffi==1.11.5
mkl-fft==1.0.6
mkl-random==1.0.1
numpy==1.15.4
opencv-python==3.4.3.18
Pillow==5.3.0
protobuf==3.6.1
pycparser==2.19
six==1.11.0
tensorboardX==1.4
torch==1.0.0.dev20181119
torchvision-nightly==0.2.1
tqdm==4.26.0
The pytorch cpp library is downloaded from the official site which matches the configure for my computer.