vision icon indicating copy to clipboard operation
vision copied to clipboard

CMake config file to link target

Open facug91 opened this issue 5 years ago • 9 comments

There is no TorchvisionConfig.cmake file generated, or FindTorchvision.cmake, so that we can link the target from CMake. Should there be some, or the library is supposed to be linked in some other way? If there should be one, I could work on a PR to add it.

facug91 avatar Jun 24 '19 16:06 facug91

cc @ShahriarSS

fmassa avatar Jun 24 '19 16:06 fmassa

Hi @facug91 I'm looking into this.

ShahriarRezghi avatar Jun 25 '19 07:06 ShahriarRezghi

@facug91 This is a minimal example of usage:

CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)

project(testvision)
add_executable(${PROJECT_NAME} "main.cpp")

find_package(Torch REQUIRED)
target_link_libraries(${PROJECT_NAME} torchvision)
target_link_libraries(${PROJECT_NAME} PUBLIC "${TORCH_LIBRARIES}")

main.cpp:

#include <torchvision/vision.h>

int main()
{
    vision::models::AlexNet network;
    network->forward(torch::rand({1, 3, 224, 224}));
    return 0;
}

But having a cmake config file would be a good thing. Can you work on a PR adding it?

ShahriarRezghi avatar Jun 25 '19 18:06 ShahriarRezghi

Is that CMake script working for you? Because torchvision target shouldn't exist, Torch doesn't create it.

What I could do is work on a PR to add a cmake config file, so that with a new line find_package(TorchVision REQUIRED), the target_link_libraries(${PROJECT_NAME} torchvision) should work.

facug91 avatar Jun 26 '19 14:06 facug91

Yes it is working because libtorchvision.so is just another shared library and you cal link to it with target_link_libraries. I think it would be better if a config and a version file would exist. You can start working on it and I'll help in any way I can.

ShahriarRezghi avatar Jun 26 '19 19:06 ShahriarRezghi

@ShahriarSS How can I get libtorchvision.so ? I am configuring cmake for torchvision. cmake generated TorchVisionTargets.cmake and TorchVisiontargets-release.cmake files in which libtorchvision.so is been referred. Its not provided by the torchvision package that I installed. I am getting the following error:

CMake Error at /path/libtorch/share/cmake/TorchVision/vision/build/TorchVisionTargets.cmake:70 (message): The imported target "TorchVision::TorchVision" references the file

 "path/libtorch/share/cmake/lib/libtorchvision.so"

but this file does not exist. Possible reasons include:

  • The file was deleted, renamed, or moved to another location.

  • An install or uninstall procedure did not complete successfully.

  • The installation package was faulty and contained

How should I solve it? I am using non-GPU machine.

Pnikam avatar May 30 '21 11:05 Pnikam

Hi @Pnikam You can build and install torchvision's C++ API from source. The instructions are in the README file.

ShahriarRezghi avatar May 31 '21 05:05 ShahriarRezghi

I have followed all the steps given. I was able to configure it successfully. But when I try to configure it for detectron2 models, I am getting the error: CMake Error at /home/admin1/Vision2/vision/build/TorchVisionConfig.cmake:50 (include): include could not find load file:

/home/admin1/Vision2/vision/build/TorchVisionTargets.cmake

Call Stack (most recent call first): CMakeLists.txt:9 (find_package) ``

CMake Error at /home/admin1/Vision2/vision/build/TorchVisionConfig.cmake:58 (target_link_libraries): Cannot specify link libraries for target "TorchVision::TorchVision" which is not built by this project. Call Stack (most recent call first): CMakeLists.txt:9 (find_package)

Configuring incomplete, errors occurred!`

TorchVisionTargets.cmake file is: `# TorchVisionConfig.cmake ####### --------------------

####### Exported targets:: Vision

####### Expanded from @PACKAGE_INIT@ by configure_package_config_file() ####### ####### Any changes to this file will be overwritten by the next CMake run #### ####### The input file was TorchVisionConfig.cmake.in ########

get_filename_component(PACKAGE_PREFIX_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../" ABSOLUTE)

macro(set_and_check _var _file) set(${_var} "${_file}") if(NOT EXISTS "${_file}") message(FATAL_ERROR "File or directory ${_file} referenced by variable ${_var} does not exist !") endif() endmacro()

macro(check_required_components _NAME) foreach(comp ${${_NAME}_FIND_COMPONENTS}) if(NOT ${NAME}${comp}_FOUND) if(${_NAME}FIND_REQUIRED${comp}) set(${_NAME}_FOUND FALSE) endif() endif() endforeach() endmacro()

####################################################################################

set(PN TorchVision)

######location of include/torchvision set(${PN}_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/include")

set(${PN}_LIBRARY "") set(${PN}DEFINITIONS USING${PN})

check_required_components(${PN})

if(NOT (CMAKE_VERSION VERSION_LESS 3.0)) #####----------------------------------------------------------------------------- #####Don't include targets if this file is being picked up by another #####project which has already built this as a subproject #####----------------------------------------------------------------------------- if(NOT TARGET ${PN}::TorchVision) include("${CMAKE_CURRENT_LIST_DIR}/${PN}Targets.cmake")

if(NOT TARGET torch_library) find_package(Torch REQUIRED) endif() if(NOT TARGET Python3::Python) find_package(Python3 COMPONENTS Development) endif() target_link_libraries(TorchVision::TorchVision INTERFACE ${TORCH_LIBRARIES} Python3::Python)

if(OFF) target_compile_definitions(TorchVision::TorchVision INTERFACE WITH_CUDA) endif()

endif() endif()`

TorchVisionTargets.cmake file has been generated by cmake at CmakeFiles/Export/share/cmake/TorchVision. As it was not found, I copied it to the same folder as TorchVisionConfig.cmake. Now I am getting the above error that libtorchvisio.so is not found.

Pnikam avatar May 31 '21 06:05 Pnikam

having exactly the same problem as above

ashBabu avatar Aug 31 '22 09:08 ashBabu

+1 on the problems as above

mark1ng123 avatar Oct 30 '22 15:10 mark1ng123