Torch Vision C++ 接口错误“未知内置操作:torchvision::nms”

Posted

技术标签:

【中文标题】Torch Vision C++ 接口错误“未知内置操作:torchvision::nms”【英文标题】:Torch Vision C++ interface error "Unknown builtin op: torchvision::nms" 【发布时间】:2021-04-18 15:45:24 【问题描述】:

我正在尝试在 jit(使用脚本)中为 FasterRCNN 的 torchscript 运行一个脚本。

我安装了 CUDA 10.1、兼容的 cudnn、LibTorch (C++) 1.7.1 和 Torch Vision 0.8.2

我遵循了 torchscript 和 vision 中的说明,我有以下几点:

--- CMakeLists.txt ---

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(load_and_run_model_proj)

list(APPEND CMAKE_PREFIX_PATH "/home/fstrati/libtorch_shared_cuda_10.1/libtorch")
list(APPEND CMAKE_PREFIX_PATH "/opt/vision_0.8.2")

find_package(Torch REQUIRED)
find_package(TorchVision REQUIRED)

add_executable(load_and_run_model src/load_and_run_model.cpp)
# target_link_libraries(load_and_run_model "$TORCH_LIBRARIES")

target_link_libraries(load_and_run_model PUBLIC TorchVision::TorchVision)

set_property(TARGET load_and_run_model PROPERTY CXX_STANDARD 14)

--- CMakeLists.txt ---

--- src/load_and_run_model.cpp ---

#include <torch/script.h> // One-stop header.
#include <torchvision/vision.h>
#include <torchvision/nms.h>

#include <iostream>
#include <memory>

int main(int argc, const char* argv[])

    if (argc != 2)
    
        std::cerr << "usage: example-app <path-to-exported-script-module>\n";
        return -1;
    

    torch::jit::script::Module module;
    try
    
        // Deserialize the ScriptModule from a file using torch::jit::load().
        module = torch::jit::load(argv[1]);
    
    catch (const c10::Error& e)
    
        std::cerr << e.what() << std::endl;
        std::cerr << "error loading the model\n";
        return -1;
    

    std::cout << "ok\n";
    return 0;

--- src/load_and_run_model.cpp ---

我编译和链接很好。

当我尝试使用跟踪脚本运行它时,TorchCcript for fasterRCNN 使用 jit.script 创建 我收到以下错误:

terminate called after throwing an instance of 'torch::jit::ErrorReport'
  what():  
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.
:
  File "C:\Users\andre\anaconda3\envs\pytorch\lib\site-packages\torchvision\ops\boxes.py", line 42
    """
    _assert_has_ops()
    return torch.ops.torchvision.nms(boxes, scores, iou_threshold)
           ~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
Serialized   File "code/__torch__/torchvision/ops/boxes.py", line 93
  _42 = __torch__.torchvision.extension._assert_has_ops
  _43 = _42()
  _44 = ops.torchvision.nms(boxes, scores, iou_threshold)
        ~~~~~~~~~~~~~~~~~~~ <--- HERE
  return _44
'nms' is being compiled since it was called from 'batched_nms'
  File "C:\Users\andre\anaconda3\envs\pytorch\lib\site-packages\torchvision\ops\boxes.py", line 88
        offsets = idxs.to(boxes) * (max_coordinate + torch.tensor(1).to(boxes))
        boxes_for_nms = boxes + offsets[:, None]
        keep = nms(boxes_for_nms, scores, iou_threshold)
               ~~~ <--- HERE
        return keep
Serialized   File "code/__torch__/torchvision/ops/boxes.py", line 50
    _18 = torch.slice(offsets, 0, 0, 9223372036854775807, 1)
    boxes_for_nms = torch.add(boxes, torch.unsqueeze(_18, 1), alpha=1)
    keep = __torch__.torchvision.ops.boxes.nms(boxes_for_nms, scores, iou_threshold, )
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
    _11 = keep
  return _11
'batched_nms' is being compiled since it was called from 'RegionProposalNetwork.filter_proposals'
Serialized   File "code/__torch__/torchvision/models/detection/rpn.py", line 64
    _11 = __torch__.torchvision.ops.boxes.clip_boxes_to_image
    _12 = __torch__.torchvision.ops.boxes.remove_small_boxes
    _13 = __torch__.torchvision.ops.boxes.batched_nms
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
    num_images = (torch.size(proposals))[0]
    device = ops.prim.device(proposals)
'RegionProposalNetwork.filter_proposals' is being compiled since it was called from 'RegionProposalNetwork.forward'
  File "C:\Users\andre\anaconda3\envs\pytorch\lib\site-packages\torchvision\models\detection\rpn.py", line 344
        proposals = self.box_coder.decode(pred_bbox_deltas.detach(), anchors)
        proposals = proposals.view(num_images, -1, 4)
        boxes, scores = self.filter_proposals(proposals, objectness, images.image_sizes, num_anchors_per_level)
                        ~~~~~~~~~~~~~~~~~~~~~ <--- HERE

        losses = 
Serialized   File "code/__torch__/torchvision/models/detection/rpn.py", line 37
    proposals = (self.box_coder).decode(torch.detach(pred_bbox_deltas0), anchors, )
    proposals0 = torch.view(proposals, [num_images, -1, 4])
    _8 = (self).filter_proposals(proposals0, objectness0, images.image_sizes, num_anchors_per_level, )
                                                                              ~~~~~~~~~~~~~~~~~~~~~ <--- HERE
    boxes, scores, = _8
    losses = annotate(Dict[str, Tensor], )

关于如何解决此错误的任何想法或建议:似乎操作员 nms 未注册。

顺便说一下torchvision的master分支cuda没有编译所以报错 用于torch vision的标签v0.8.2。

【问题讨论】:

【参考方案1】:

在研究了这个问题后,我想到了 这个在 master 中的提交不在 火炬视觉v0.8.2:

https://github.com/pytorch/vision/pull/2798/commits/fb893e7ba390d1b668efb4b84b3376cf634bd043

将提交应用到 v0.8.2 解决了这个问题: 现在操作员已在 torch 脚本中正确注册

【讨论】:

以上是关于Torch Vision C++ 接口错误“未知内置操作:torchvision::nms”的主要内容,如果未能解决你的问题,请参考以下文章

fastai.vision 导入错误:如何修复导入错误以便我可以使用 ImageDataBunch.from_folder?

使用 C++ 的隐马尔可夫模型 [关闭]

如何使用 torch.hub.load 加载本地模型?

如何使用 torch Dataloader 获取具有相同类的图片?

Vision 和 CoreML – CGImagePropertyOrientation 需要错误的类型

pytorch1.0 用torch script导出保存模型