Hello everyone. I’m trying a very basic example, in which I’m loading an MTCNN model (consists of 3 submodules onet, pnet and rnet) and trying to feed forward them. This is the code :
#include <torch/torch.h>
#include <torch/script.h> // One-stop header.
#include <iostream>
#include <memory>
#include <cstdlib>
int main(int argc, const char* argv[])
{
// Create a vector of inputs.
std::vector<torch::jit::IValue> inputs;
inputs.push_back(torch::ones({ 1, 3, 112, 112 }));
torch::jit::script::Module onet;
torch::jit::script::Module pnet;
torch::jit::script::Module rnet;
try
{
std::string mtcnn_onet_path = "G:\\Codes\\python\\MTCNN\\weights_mtcnn_onet.pt";
std::string mtcnn_pnet_path = "G:\\Codes\\python\\MTCNN\\weights_mtcnn_pnet.pt";
std::string mtcnn_rnet_path = "G:\\Codes\\python\\MTCNN\\weights_mtcnn_rnet.pt";
// Deserialize the ScriptModule from a file using torch::jit::load().
onet = torch::jit::load(mtcnn_onet_path);
pnet = torch::jit::load(mtcnn_pnet_path);
rnet = torch::jit::load(mtcnn_rnet_path);
// Execute the model and turn its output into a tensor.
at::Tensor output_o = onet.forward(inputs).toTensor();
at::Tensor output_p = pnet.forward(output_o).toTensor();
at::Tensor output = rnet.forward(output_p).toTensor();
std::cout << output.sizes() << std::endl;
std::cout << "output: " << output[0] << std::endl;
}
catch (const c10::Error& e)
{
std::cerr << "error loading the model\n";
return -1;
}
std::cout << "ok\n";
std::system("pause");
return 0;
}
but I get :
Severity Code Description Project File Line Suppression State
Error C2664 'c10::IValue torch::jit::Module::forward(std::vector<c10::IValue,std::allocator<c10::IValue>>)': cannot convert argument 1 from 'at::Tensor' to 'std::vector<c10::IValue,std::allocator<c10::IValue>>' LibtorchPort G:\Codes\cpp\port\LibtorchPort\LibtorchPort\LibtorchPort.cpp 54
Severity Code Description Project File Line Suppression State
Error C2664 'c10::IValue torch::jit::Module::forward(std::vector<c10::IValue,std::allocator<c10::IValue>>)': cannot convert argument 1 from 'at::Tensor' to 'std::vector<c10::IValue,std::allocator<c10::IValue>>' LibtorchPort G:\Codes\cpp\port\LibtorchPort\LibtorchPort\LibtorchPort.cpp 55
Shouldnt we pass inputs as Tensors like in Python?
I couldnt find any documentation that properly explains this . any help in this regard is appreciated.
Hi, the output_o had been converted back to an at::Tensor, and this is now placed inside pnet.forward().
And error is thrown at this line. output_o needs to be converted to a torch::jit::IValue an then put inside pnet.forward().
The same goes to rnet.forward() as well.
Severity Code Description Project File Line Suppression State
Error C2440 'initializing': cannot convert from 'c10::IValue' to 'std::vector<c10::IValue,std::allocator<c10::IValue>>' LibtorchPort G:\Codes\cpp\port\LibtorchPort\LibtorchPort\LibtorchPort.cpp 52
Severity Code Description Project File Line Suppression State
Error C2440 'initializing': cannot convert from 'c10::IValue' to 'std::vector<c10::IValue,std::allocator<c10::IValue>>' LibtorchPort G:\Codes\cpp\port\LibtorchPort\LibtorchPort\LibtorchPort.cpp 53
Looking at the errors,
the output from the model.forward() will be a torch::jit::IValue.
However the input to a model.forward() will be a std::vector<torch::jit::IValue>
Can you try this ?
auto output_o = onet.forward(inputs);
std::vector<torch::jit::IValue>temp_op;
temp_op.push_back(output_o);
auto output_p = pnet.forward(temp_op);