I was trying to load a torchscript version of mask-rcnn into c++ but unfortunately this failed. Here is the error message / stacktrace I got:
./run_model ../torchscript.pt
terminate called after throwing an instance of 'torch::jit::script::ErrorReport'
what():
Unknown builtin op: torchvision::_new_empty_tensor_op.
Could not find any similar ops to torchvision::_new_empty_tensor_op. This op may not exist or may not be currently supported in TorchScript.
:
File "/home/user/.local/lib/python3.6/site-packages/torchvision/ops/new_empty_tensor.py", line 16
output (Tensor)
"""
return torch.ops.torchvision._new_empty_tensor_op(x, shape)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
Serialized File "code/__torch__/torchvision/ops/new_empty_tensor.py", line 3
def _new_empty_tensor(x: Tensor,
shape: List[int]) -> Tensor:
_0 = ops.torchvision._new_empty_tensor_op(x, shape)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
return _0
'_new_empty_tensor' is being compiled since it was called from 'interpolate'
Serialized File "code/__torch__/torchvision/ops/misc.py", line 123
align_corners: Optional[bool]=None) -> Tensor:
_31 = __torch__.torchvision.ops.misc._output_size
_32 = __torch__.torchvision.ops.new_empty_tensor._new_empty_tensor
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
_33 = uninitialized(Tensor)
if torch.gt(torch.numel(input), 0):
'interpolate' is being compiled since it was called from 'GeneralizedRCNNTransform.resize'
Serialized File "code/__torch__/torchvision/models/detection/transform.py", line 78
target: Optional[Dict[str, Tensor]]) -> Tuple[Tensor, Optional[Dict[str, Tensor]]]:
_18 = __torch__.torchvision.models.detection.transform.resize_boxes
_19 = __torch__.torchvision.ops.misc.interpolate
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
_20 = __torch__.torchvision.models.detection.transform.resize_keypoints
_21 = uninitialized(Tuple[Tensor, Optional[Dict[str, Tensor]]])
'GeneralizedRCNNTransform.resize' is being compiled since it was called from 'GeneralizedRCNNTransform.forward'
File "/home/user/.local/lib/python3.6/site-packages/torchvision/models/detection/transform.py", line 47
"of shape [C, H, W], got {}".format(image.shape))
image = self.normalize(image)
image, target_index = self.resize(image, target_index)
~~~~~~~~~~~ <--- HERE
images[i] = image
if targets is not None and target_index is not None:
Serialized File "code/__torch__/torchvision/models/detection/transform.py", line 28
pass
image0 = (self).normalize(image, )
_2 = (self).resize(image0, target_index, )
~~~~~~~~~~~~ <--- HERE
image1, target_index0, = _2
_3 = torch._set_item(images0, i, image1)
Aborted (core dumped)
All the details:
I tried it with the latest versions of pytorch/libtorch
import torch, torchvision
torch.__version__
'1.4.0.dev20191205+cpu'
torchvision.__version__
'0.5.0.dev20191204+cpu'
The export (and import) in python works:
model = torchvision.models.detection.maskrcnn_resnet50_fpn(pretrained=True)
model.eval()
script_model = torch.jit.script(model)
script_model.save("torchscript.pt")
After which I followed the basic steps of importing a torchscript module into c++ from https://pytorch.org/tutorials/advanced/cpp_export.html .
Also here I've downloaded the latest version of libtorch:
```file=build-hash
/pytorch /pytorch ~/project
3fd4c696d9b869b03c47dae35ba908c07e6fa1b5
```file=build-version
1.4.0.dev20191205+cpu
The c++ code is a just a very basic loading operation:
#include <torch/script.h> // One-stop header.
#include <iostream>
#include <memory>
#include <chrono>
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;
module = torch::jit::load(argv[1]);
std::cout << "ok\n";
}
For completion, the cmakelist (since the c++ version got changed to 14 https://github.com/pytorch/vision/pull/1635 ):
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(custom_ops)
find_package(Torch REQUIRED)
add_executable(run_model run_model.cpp)
target_link_libraries(run_model "${TORCH_LIBRARIES}")
set_property(TARGET run_model PROPERTY CXX_STANDARD 14)
Hi,
For this particular error, I think you can follow the comments in https://github.com/pytorch/vision/pull/1407#issuecomment-562096191 to fix this issue
Hi,
For this particular error, I think you can follow the comments in #1407 (comment) to fix this issue
Thanks! I missed that one.
Let me know if this is enough to fix your issue, and if you manage to fix the List[Tensor] issue, can you comment in there as well?
Thanks! I after some (cmake) issues I managed to get the loading of the networks running with the code in https://github.com/pytorch/vision/pull/1407#issuecomment-562096191 . Not yet tried forward passing or anything
I had to fix some headers though: https://github.com/pytorch/vision/pull/1644
Thanks for the fix in https://github.com/pytorch/vision/pull/1644!
I'm assuming that this issue can be closed now. Let me know if you still face issues
Hi, I am also facing the similar issue by using the latest libtorch. Is there any plan to make the missing torch vision operators available in C++?
Most helpful comment
Thanks! I after some (cmake) issues I managed to get the loading of the networks running with the code in https://github.com/pytorch/vision/pull/1407#issuecomment-562096191 . Not yet tried forward passing or anything
I had to fix some headers though: https://github.com/pytorch/vision/pull/1644