I'm trying to load and use a model from Python into c++ using libtorch. The program compiles correctly but I get Illegal instruction (core dumped) using forward on Input.
That's the code:
void test(vector<module_type>& model){
//pseudo input
vector<torch::jit::IValue> inputs;
inputs.push_back(torch::ones({1, 3, 224, 224}));
//ERROR IS HERE
at::Tensor output = model[0].forward(inputs).toTensor();
cout << output << endl;
}
int main(int argc, char *argv[]) {
if (argc == 2){
cout << argv[1] << endl;
}
else {
cerr << "no path of model is given" << endl;
return -1;
}
// test
module_type module = torch::jit::load(argv[1]);
vector<module_type> modul;
modul.push_back(module);
test(modul);
}
CMake:
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(main)
find_package(Torch REQUIRED)
add_executable(main main.cpp)
target_link_libraries(main "${TORCH_LIBRARIES}")
set_property(TARGET main PROPERTY CXX_STANDARD 11)