I’ve been trying to get a FRCNN model trained in Python to be “torchscripted” and saved off so that it can be loaded into C++ using libtorch. To reduce the situation to a fairly simple example, I’m now just trying to use torchvision’s pretrained model to get my custom model out of the picture. I’m still running into problems when loading the scripted model in libtorch complaining about nms being an “Unknown builtin op”.
Here’s the reduced python code:
import torch
import torchvision
print(f'Torch version: {torch.__version__}')
print(f'Torchvision version: {torchvision.__version__}')
tvfrcnnModel = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained = True)
scriptTvfrcnnModel = torch.jit.script(tvfrcnnModel)
torch.jit.save(scriptTvfrcnnModel, 'torchvision_frcnn_scripted.pt')
print(f'Torchvision pretrained frcnn scripted and saved')
that outputs the following:
Torch version: 1.10.0a0+gitf69cf3c
Torchvision version: 0.11.0a0+9275cc6
Torchvision pretrained frcnn scripted and saved
Here’s the C++ code, then, that attempts to load the scripted model:
//According to: https://github.com/pytorch/vision/#c-api
// In order to get the torchvision operators registered with
// torch (eg. for the JIT), all you need to do is to ensure
// that you #include <torchvision/vision.h> in your project.
#include <torchvision/vision.h>
#include <torch/torch.h>
#include <torch/script.h>
#include <iostream>
#include <fstream>
#include <string>
int main()
{
string scriptedModelFname = "torchvision_frcnn_scripted.pt";
ifstream scriptedModelF;
scriptedModelF.open(scriptedModelFname);
torch::jit::script::Module tvfcrnnModel;
tvfcrnnModel = torch::jit::load(scriptedModelF);
scriptedModelF.close();
return 0;
}
When I run this program, though, it reports:
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 "/usr/local/lib64/python3.6/site-packages/torchvision-0.11.0a0+9275cc6-py3.6-linux-x86_64.egg/torchvision/ops/boxes.py", line 35
"""
_assert_has_ops()
return torch.ops.torchvision.nms(boxes, scores, iou_threshold)
~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
...
I’ve seen a small handful of similar questions out there (e.g. Unknown builtin op: torchvision::nms when loading scripted FasterRCNN · Issue #48932 · pytorch/pytorch · GitHub) but I haven’t found any that seem to come to a resolution. Is torchscripting a FRCNN model with nm something that is supposed to be supported currently, and if so, are there are any suggestions as to what might be going wrong here?
Thanks!