openvinotoolkit / open_model_zoo

Pre-trained Deep Learning models and demos (high quality and extremely fast)
https://docs.openvino.ai/latest/model_zoo.html
Apache License 2.0
4.1k stars 1.37k forks source link

Add libtorch in the CMake #1031

Closed cztay closed 4 years ago

cztay commented 4 years ago

Dear all,

I am doing libtorch for LSTM with one of the demo, how can I modify the CMakeList.txt? I tried the demos/CMakeList.txt but I cannot manage to compile. I hope someone could suggest me the method.

Thanks a lot

Wovchena commented 4 years ago

I've never tried libtorch, but according to https://pytorch.org/cppdocs/installing.html#minimal-example it looks that the simple (ugly) solution can be

add

find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

at https://github.com/opencv/open_model_zoo/blob/f9c2ed239724506c855ab0cb91ba84d12040206e/demos/CMakeLists.txt#L210

replace set (CMAKE_CXX_STANDARD 11) with set (CMAKE_CXX_STANDARD 14)

delete if (${CMAKE_CXX_COMPILER_ID} STREQUAL GNU) and its body,

and add target_link_libraries(${IE_SAMPLE_NAME} "${TORCH_LIBRARIES}") to https://github.com/opencv/open_model_zoo/blob/f9c2ed239724506c855ab0cb91ba84d12040206e/demos/CMakeLists.txt#L186

You can add that if (MSVC) from libtorch example after target_link_libraries(${IE_SAMPLE_NAME} "${TORCH_LIBRARIES}").

This should make libtorch available to every demo.

But that's impossible to say where you have stuck without you describing what you did and providing an error message.

It may also be possible to convert the model into Intermediate Representation used by OpenVINO's Inference Engine, so you won't need integrating libtorch.

cztay commented 4 years ago

Thanks for the help

when i add this linetarget_link_libraries(${IE_SAMPLE_NAME} "${TORCH_LIBRARIES}"), the error is the keyword signature for target_link_libraries has been used with...

Then I change that line to target_link_libraries(${IE_SAMPLE_NAME} PRIVATE ${TORCH_LIBRARIES}), and it could be compiled, but the demo still cannot work with libtorch.

I believe that I need to set the CMAKE_PREFIX_PATH but where can I insert it? since I am just using the build_demos.sh, I have change that cmake to cmake -DCMAKE_PREFIX_PATH=~/libtorch -DCMAKE_BUILD_TYPE=Release but it does not work

Regarding the pytorch page, we need to add the prefix at cmake, do you have any idea?

Wovchena commented 4 years ago

CMAKE_PREFIX_PATH helps to find_package(Torch REQUIRED). But if cmake passes for you without setting CMAKE_PREFIX_PATH, than cmake is able to find libtorch even without it. But yes, to set CMAKE_PREFIX_PATH in build_demos.sh you need to replace (cd "$build_dir" && cmake -DCMAKE_BUILD_TYPE=Release "${extra_cmake_opts[@]}" "$DEMOS_PATH") with (cd "$build_dir" && cmake -DCMAKE_PREFIX_PATH=~/libtorch -DCMAKE_BUILD_TYPE=Release "${extra_cmake_opts[@]}" "$DEMOS_PATH")

Again, you can't get a lot of help, unless you specify how you know that "the demo still cannot work with libtorch" and "cmake -DCMAKE_PREFIX_PATH=~/libtorch -DCMAKE_BUILD_TYPE=Release but it does not work".

cztay commented 4 years ago

Thanks for the solution, I have tried out with torch tensor to fit in the model.

the error is coming from the model saving method from python, not the CMake issue. Thanks again your help!