I'm trying to run a script in jit(using script) in torchscript for FasterRCNN.
I installed CUDA 10.1, compatible cudnn, LibTorch (C++) 1.7.1 and Torch Vision 0.8.2
I followed the instructions in both torchscript and vision and I have the following:
--- 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 ---
and
--- 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 ---
I compile & link fine.
When I try to run it with traced script TorchCcript for fasterRCNN creates with jit.script I get the following error:
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], {})
Any ideas or suggestion on how to cure this error: seems like the operator nms is not registered.
By the way the master branch of torchvision with cuda is not compiling so I report the error for the tag v0.8.2 of torch vision.