在C++中运行Detectron2:构建torchscript_mask_rcnn.cpp遇TorchVision错误
在C++中运行Detectron2模型的部署故障排查请求
已完成的部署步骤
- 解压
libtorch-cxx11-abi-shared-with-deps-2.0.0+cpu.zip到/home/hiqbal/Downloads/libtorch,完成LibTorch环境配置 - 安装TorchVision,头文件位于
/usr/local/include/torchvision/ - 通过
tools/deploy/export_model.py将Detectron2模型导出为TorchScript格式,执行命令如下:
./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
模型导出成功,在/deploy/output/目录生成model.tr文件
遇到的具体问题
1. 构建torchscript_mask_rcnn.cpp时CMake报错
执行CMake命令:
~/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.
尝试将TorchVision路径加入CMAKE_PREFIX_PATH(cmake -DCMAKE_PREFIX_PATH=/home/hiqbal/Downloads/libtorch:/usr/local/include/torchvision/),问题仍未解决。
2. TorchVision测试代码编译失败
为验证TorchVision安装,创建测试目录并编写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);
需求
希望解决上述CMake配置和TorchVision编译问题,最终实现Detectron2模型在C中的运行。如果有更简洁可靠的Detectron2 C部署方案及示例步骤,也请提供。
内容的提问来源于stack exchange,提问作者HIq
相关产品推荐
相关产品推荐

