Hello,
I want to use a model in a C++ application.
For this I downloaded LibTorch for CPU (2.2.2+cpu) in the Release version.
My CMakeLists.txt is:
cmake_minimum_required(VERSION 3.27)
project(TorchTest)
set(CMAKE_CXX_STANDARD 17)
set(Torch_DIR "C:/Users/hofma/Downloads/libtorch-win-shared-with-deps-2.2.2+cpu/libtorch/share/cmake/Torch")
find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
add_executable(TorchTest main.cpp)
target_link_libraries(TorchTest PRIVATE ${TORCH_LIBRARIES})
I exported the resnet18 model in python
import torch
import torchvision
model = torchvision.models.resnet18()
example = torch.rand(1, 3, 224, 224)
traced_script_module = torch.jit.trace(model, example)
traced_script_module.save("resnet.pt")
print(torch.__version__)
yields 2.2.2+cpu
my C++ MWE is
#include <iostream>
#include "torch/script.h"
int main() {
try
{
torch::jit::script::Module module = torch::jit::load("resnet.pt");
}
catch(const c10::Error& e)
{
std::cerr << "error loading the model\n";
}
return 0;
}
I use MSVC 17 to compile and this is my build command
C:\Users\hofma\AppData\Local\Programs\CLion\bin\cmake\win\x64\bin\cmake.exe -DCMAKE_BUILD_TYPE=Release -DCMAKE_MAKE_PROGRAM=C:/Users/hofma/AppData/Local/Programs/CLion/bin/ninja/win/x64/ninja.exe -G Ninja -S C:\Users\hofma\Documents\CodeTest\TorchTest -B C:\Users\hofma\Documents\CodeTest\TorchTest\cmake-build-release
unfortunately the model is only load when I use the Debug configuration.
Am I missing something to load the model in Release config.
Thanks for helping