在 C++ 中运行 detectron2。构建工具/部署/torchscript_mask_rcnn.cpp 时遇到 TorchVision 错误

问题描述 投票:0回答:0

我想在 C++ 中运行 detectron2 模型。我正在使用 readme 中提到的步骤。我采取了以下步骤:

  1. 我通过在 /home/hiqbal/Downloads/libtorch 中解压缩libtorch-cxx11-abi-shared-with-deps-2.0.0+cpu.zip 来设置
    libtorch
  2. 按照 here 的步骤安装 torch vision。这将安装 torchVision
    /usr/local/include/torchvision/
    .
  3. 使用/tools/deploy/export_model.py 将模型转换为
    .tr
    。用于导出模型的命令是:
./export_model.py --config-file ../../configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml \
    --output ./output --export-method tracing --format torchscript \
    MODEL.WEIGHTS /home/pathto/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl \
    MODEL.DEVICE CPU

模型导出成功,model.tr创建在/deploy/output/

  1. 然后构建
    tools/deploy/torchscript_mask_rcnn.cpp 
    ,我使用了以下命令:
cmake -DCMAKE_PREFIX_PATH=/home/hiqbal/Downloads/libtorch

上面的命令产生了以下错误:

~/detectron2/tools/deploy$ cmake -DCMAKE_PREFIX_PATH=/home/hiqbal/Downloads/libtorch
CMake Warning:
  No source or binary directory provided.  Both will be assumed to be the
  same as the current working directory, but note that this warning will
  become a fatal error in future CMake releases.


-- Caffe2: Found gflags with new-style gflags target.
-- Caffe2: Found glog with new-style glog target.
-- Caffe2: Found protobuf with old-style protobuf targets.
-- Caffe2: Protobuf version 3.12.4
-- Caffe2: Found gflags with new-style gflags target.
-- Caffe2: Found glog with new-style glog target.
-- Caffe2: Found protobuf with old-style protobuf targets.
-- Caffe2: Protobuf version 3.12.4
-- Configuring done (0.0s)
CMake Error in CMakeLists.txt:
  Imported target "TorchVision::TorchVision" includes non-existent path

    "/usr/lib/include"

  in its INTERFACE_INCLUDE_DIRECTORIES.  Possible reasons include:

  * The path was deleted, renamed, or moved to another location.

  * An install or uninstall procedure did not complete successfully.

  * The installation package was faulty and references files it does not
  provide.

-- Generating done (0.0s)
CMake Generate step failed.  Build files cannot be regenerated correctly.

只是为了它,我尝试了

cmake -DCMAKE_PREFIX_PATH=/home/hiqbal/Downloads/libtorch:/usr/local/include/torchvision/
但没有运气。


为了测试 torchVision,我创建了一个

test
目录并创建了 main.cpp

main.cpp

#include <torch/torch.h>
// #include <torchvision/vision.h>
#include </usr/local/include/torchvision/vision.h>

int main() {
  // Load an image from a file
  auto img = torch::vision::image::ReadImage("/home/hiqbal/Downloads/sample_abc.jpg");

  // Convert image to tensor
  auto tensor = torch::vision::image::ToTensor(img);

  // Normalize image tensor
  auto mean = torch::tensor({0.485, 0.456, 0.406});
  auto std = torch::tensor({0.229, 0.224, 0.225});
  auto normalized = torch::vision::transforms::Normalize(mean, std)(tensor);

  // Apply a transformation
  auto transformed = torch::vision::transforms::Resize(256)(normalized);

  // Save the transformed image
  torch::vision::image::WriteImage("transformed.jpg", transformed);

  return 0;
}

和 CMakeLists.txt

cmake_minimum_required(VERSION 3.0)
# project name
project(debugfunc)

# define path to the libtorch extracted folder
set(CMAKE_PREFIX_PATH /home/hiqbal/Downloads/libtorch)

find_package(Torch REQUIRED)
find_package(TorchVision REQUIRED)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

add_executable(${PROJECT_NAME} main.cpp)
target_compile_features(${PROJECT_NAME} PUBLIC cxx_range_for)
target_link_libraries(${PROJECT_NAME} TorchVision::TorchVision)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 14)

构建此代码会产生以下错误:

/usr/local/include/torchvision/vision.h:10:40: warning: ‘_register_ops’ initialized and declared ‘extern’
   10 | extern "C" VISION_INLINE_VARIABLE auto _register_ops = &cuda_version;
      |                                        ^~~~~~~~~~~~~
/home/hiqbal/testcode/main.cpp: In function ‘int main()’:
/home/hiqbal/testcode/main.cpp:19:21: error: ‘torch::vision’ has not been declared
   19 |   auto img = torch::vision::image::ReadImage("/home/hiqbal/Downloads/sample_abc.jpg");
      |                     ^~~~~~
/home/hiqbal/testcode/main.cpp:22:24: error: ‘torch::vision’ has not been declared
   22 |   auto tensor = torch::vision::image::ToTensor(img);
      |                        ^~~~~~
/home/hiqbal/testcode/main.cpp:27:28: error: ‘torch::vision’ has not been declared
   27 |   auto normalized = torch::vision::transforms::Normalize(mean, std)(tensor);
      |                            ^~~~~~
/home/hiqbal/testcode/main.cpp:30:29: error: ‘torch::vision’ has not been declared
   30 |   auto transformed = torch::vision::transforms::Resize(256)(normalized);
      |                             ^~~~~~
/home/hiqbal/testcode/main.cpp:33:10: error: ‘torch::vision’ has not been declared
   33 |   torch::vision::image::WriteImage("transformed.jpg", transformed);

我真的很感激帮助解决这个障碍。 目的是在 C++ 中运行 detectron2 模型。如果有人有更好的方法以及所有示例代码和步骤,那将很有帮助。 谢谢。

c++ tensorflow cmake torchvision detectron
© www.soinside.com 2019 - 2024. All rights reserved.