Include could not find load file: /path/build/TorchVisionTargets.cmake

username: lezwon
cooked: <p>Hi,<br>
I am trying to run vision c++ api with torch. I have added the following in in my <code>CMakeLists.txt</code>.</p>
<pre><code class="lang-auto">find_package(TorchVision REQUIRED)
target_link_libraries(my-target PUBLIC TorchVision::TorchVision)
</code></pre>
<p>But when call cmake I get:</p>
<pre><code class="lang-auto">ubuntu@pc:~/pytorch_cpp/build$ cmake -DCMAKE_PREFIX_PATH="/home/ubuntu/pytorch_cpp/build/libtorch;/home/ubuntu/pytorch_cpp/build/vision" ..
-- Caffe2: CUDA detected: 10.0
-- Caffe2: CUDA nvcc is: /usr/local/cuda/bin/nvcc
-- Caffe2: CUDA toolkit directory: /usr/local/cuda
-- Caffe2: Header version is: 10.0
-- Found cuDNN: v7.5.1  (include: /usr/local/cuda/include, library: /usr/local/cuda/lib64/libcudnn.so)
-- Autodetected CUDA architecture(s):  5.2 5.2
-- Added CUDA NVCC flags for: -gencode;arch=compute_52,code=sm_52
CMake Error at build/vision/build/TorchVisionConfig.cmake:50 (include):
  include could not find load file:

    /home/ubuntu/pytorch_cpp/build/vision/build/TorchVisionTargets.cmake
Call Stack (most recent call first):
  CMakeLists.txt:10 (find_package)


CMake Error at build/vision/build/TorchVisionConfig.cmake:59 (set_target_properties):
  set_target_properties Can not find target to add properties to:
  TorchVision::TorchVision
Call Stack (most recent call first):
  CMakeLists.txt:10 (find_package)


-- Configuring incomplete, errors occurred!
See also "/home/ubuntu/pytorch_cpp/build/CMakeFiles/CMakeOutput.log".
See also "/home/ubuntu/pytorch_cpp/build/CMakeFiles/CMakeError.log".
</code></pre>
<p>I checked <code>/home/ubuntu/pytorch_cpp/build/vision/build/</code> for <code>TorchVisionTargets.cmake</code> and its not present. I called <code>cmake</code> in the directory, but it was not created. I’m not sure how to go around this issue as I’m pretty new to LibTorch. I’m trying to run the RCNN-FPN model from detectron2 on torchscript. If I run it without vision I get the error:<br>
<code>Unknown builtin op: torchvision::nms. Could not find any similar ops to torchvision::nms. This op may not exist or may not be currently supported in TorchScript.</code></p>
<p>I need some guidance on how to set up vision in c++. There are my files:</p>
<p>CMakeLists.txt</p>
<pre><code class="lang-auto">cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(example)
#set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD 14)
find_package(Torch REQUIRED)


add_executable(main main.cpp)
target_link_libraries(main ${TORCH_LIBRARIES})
</code></pre>
<p>main.cpp</p>
<pre><code class="lang-auto">#include &lt;torch/torch.h&gt;
#include &lt;torch/script.h&gt;
#include &lt;torchvision/vision.h&gt;

int main()
{
    torch::Device device(torch::kCPU);
    torch::Tensor tensor = torch::zeros({2, 2});
    std::cout &lt;&lt; tensor &lt;&lt; std::endl;


    if (torch::cuda::is_available()) {
        std::cout &lt;&lt; "CUDA is available! " &lt;&lt; std::endl;
        device = torch::kCUDA;
    }

    torch::Tensor test_gpu_tensor = tensor.to(device);

    std::cout &lt;&lt; test_gpu_tensor &lt;&lt; std::endl;


    torch::jit::script::Module module;
    module = torch::jit::load("/home/ubuntu/pytorch_cpp/script_model.pt");
    module.to(device);
    return 0;
}
</code></pre>
<p>Directory Structure:</p>
<pre><code class="lang-auto">├── CMakeLists.txt
├── build
│   ├── CMakeCache.txt
│   ├── CMakeFiles [14 entries exceeds filelimit, not opening dir]
│   ├── Makefile
│   ├── cmake_install.cmake
│   ├── detect_cuda_compute_capabilities.cpp
│   ├── detect_cuda_version.cc
│   ├── libtorch
│   │   ├── bin
│   │   ├── build-hash
│   │   ├── build-version
│   │   ├── include
│   │   ├── lib
│   │   └── share
│   ├── main
│   └── vision
          └── build
├── main.cpp
└── script_model.pt
</code></pre>
<p>References:</p><aside class="onebox githubfolder">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="16" height="16">
      <a href="https://github.com/pytorch/vision#c-api" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>
  <article class="onebox-body">
    <img src="https://avatars.githubusercontent.com/u/21003710?s=400&amp;amp;v=4" class="thumbnail onebox-full-image" width="60" height="60">

<h3><a href="https://github.com/pytorch/vision#c-api" target="_blank" rel="noopener nofollow ugc">pytorch/vision - C++ API</a></h3>


  <p><span class="label1">Datasets, Transforms and Models specific to Computer Vision</span></p>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
<aside class="onebox githubfolder">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="16" height="16">
      <a href="https://github.com/ollewelin/Installing-and-Test-PyTorch-C-API-on-Ubuntu-with-GPU-enabled" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>
  <article class="onebox-body">
    <img src="https://avatars.githubusercontent.com/u/22187201?s=400&amp;amp;v=4" class="thumbnail onebox-full-image" width="60" height="60">

<h3><a href="https://github.com/ollewelin/Installing-and-Test-PyTorch-C-API-on-Ubuntu-with-GPU-enabled" target="_blank" rel="noopener nofollow ugc">ollewelin/Installing-and-Test-PyTorch-C-API-on-Ubuntu-with-GPU-enabled</a></h3>


  <p><span class="label1">Installing and Test PyTorch C++ API on Ubuntu with GPU enabled</span></p>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>


username: icewired-yy
cooked: <p>Hello, I have the same problem with you, and I have solved this problem right now.<br>
The torchvisionTarget.cmake is in the CMakeFiles/Export/[some random code]/. But I don’t know why this file is in that place. And my solution does not need it.<br>
Since you have built the torchvision successfully, you can easily find the torchvision.lib and torchvision.dll in the build/. And you can just link these two file into your project like:</p>
<ol>
<li>Add include_directories(path/to/vision-project/torchvision/csrc) so that you can find the torchvision header easily in your project.</li>
<li>Add ‘target_link_libraries(your_target path/to/your/torchvision.lib)’<br>
And then build your project. You also need to move the ‘torchvision.dll’ to the directory where your target executable file placed.</li>
</ol>
<p>I’m also a noob in C++, and this problem has confused my for a few days. This solution can pass the C++ example in vision. Hope these information can help you.</p>

username: George_Polya
cooked: <p>Thank you for your solution. But it doesn’t work for me..</p>
<p>where is the torchvision.lib and torchvision.dll?</p>
<pre><code class="lang-auto">root@18ac223ec29f:/workspace/vision-0.20.0# tree -L 2
.
|-- CITATION.cff
|-- CMakeLists.txt
|-- CODE_OF_CONDUCT.md
|-- CONTRIBUTING.md
|-- CONTRIBUTING_MODELS.md
|-- LICENSE
|-- MANIFEST.in
|-- README.md
|-- android
|   |-- README.md
|   |-- build.gradle
|   |-- gradle
|   |-- gradle.properties
|   |-- gradle_scripts
|   |-- gradlew
|   |-- gradlew.bat
|   |-- ops
|   |-- settings.gradle
|   `-- test_app
|-- benchmarks
|   `-- encoding_decoding.py
|-- bin
|   |-- convert-caffe2-to-onnx
|   |-- convert-onnx-to-caffe2
|   |-- f2py
|   |-- isympy
|   |-- proton
|   |-- proton-viewer
|   |-- torchfrtrace
|   `-- torchrun
|-- build
|   |-- CMakeCache.txt
|   |-- CMakeFiles
|   |-- Makefile
|   |-- TorchVisionConfig.cmake
|   |-- TorchVisionConfigVersion.cmake
|   |-- bdist.linux-x86_64
|   |-- cmake_install.cmake
|   |-- detect_cuda_compute_capabilities.cu
|   |-- detect_cuda_version.cc
|   |-- install_manifest.txt
|   |-- lib.linux-x86_64-3.10
|   |-- libtorchvision.so
|   `-- temp.linux-x86_64-3.10
|-- cmake
|   |-- TorchVisionConfig.cmake.in
|   `-- iOS.cmake
|-- dist
|   `-- torchvision-0.20.0a0-py3.10-linux-x86_64.egg
|-- docs
|   |-- Makefile
|   |-- make.bat
|   |-- requirements.txt
|   `-- source
|-- examples
|   |-- cpp
|   `-- python
|-- gallery
|   |-- README.rst
|   |-- assets
|   |-- others
|   `-- transforms
|-- hubconf.py
|-- install
|   |-- bin
|   `-- lib
|-- ios
|   |-- CMakeLists.txt
|   |-- LibTorchvision.podspec
|   |-- README.md
|   |-- VisionTestApp
|   `-- build_ios.sh
|-- lib
|   `-- python3.10
|-- maintainer_guide.md
|-- mypy.ini
|-- packaging
|   |-- cut_release.sh
|   |-- post_build_script.sh
|   |-- pre_build_script.sh
|   |-- torchvision
|   |-- vs2019
|   |-- wheel
|   `-- windows
|-- pyproject.toml
|-- pytest.ini
|-- references
|   |-- classification
|   |-- depth
|   |-- detection
|   |-- optical_flow
|   |-- segmentation
|   |-- similarity
|   `-- video_classification
|-- scripts
|   |-- README.rst
|   |-- collect_model_urls.py
|   |-- download_model_urls.py
|   |-- fbcode_to_main_sync.sh
|   `-- release_notes
|-- setup.cfg
|-- setup.py
|-- test
|   |-- _utils_internal.py
|   |-- assets
|   |-- common_extended_utils.py
|   |-- common_utils.py
|   |-- conftest.py
|   |-- cpp
|   |-- datasets_utils.py
|   |-- expect
|   |-- optests_failures_dict.json
|   |-- preprocess-bench.py
|   |-- smoke_test.py
|   |-- test_architecture_ops.py
|   |-- test_backbone_utils.py
|   |-- test_datasets.py
|   |-- test_datasets_download.py
|   |-- test_datasets_samplers.py
|   |-- test_datasets_utils.py
|   |-- test_datasets_video_utils.py
|   |-- test_datasets_video_utils_opt.py
|   |-- test_extended_models.py
|   |-- test_functional_tensor.py
|   |-- test_image.py
|   |-- test_internal_utils.py
|   |-- test_internet.py
|   |-- test_io.py
|   |-- test_io_opt.py
|   |-- test_models.py
|   |-- test_models_detection_anchor_utils.py
|   |-- test_models_detection_negative_samples.py
|   |-- test_models_detection_utils.py
|   |-- test_onnx.py
|   |-- test_ops.py
|   |-- test_transforms.py
|   |-- test_transforms_tensor.py
|   |-- test_transforms_v2.py
|   |-- test_transforms_v2_utils.py
|   |-- test_transforms_video.py
|   |-- test_tv_tensors.py
|   |-- test_utils.py
|   |-- test_video_gpu_decoder.py
|   |-- test_video_reader.py
|   `-- test_videoapi.py
|-- torchvision
|   |-- _C.so
|   |-- __init__.py
|   |-- __pycache__
|   |-- _internally_replaced_utils.py
|   |-- _meta_registrations.py
|   |-- _utils.py
|   |-- csrc
|   |-- datasets
|   |-- extension.py
|   |-- image.so
|   |-- io
|   |-- models
|   |-- ops
|   |-- transforms
|   |-- tv_tensors
|   |-- utils.py
|   |-- version.py
|   `-- video_reader.so
|-- torchvision.egg-info
|   |-- PKG-INFO
|   |-- SOURCES.txt
|   |-- dependency_links.txt
|   |-- not-zip-safe
|   |-- requires.txt
|   `-- top_level.txt
`-- version.txt
</code></pre>