I am trying to create an application that runs inference using the deeplabv3 model, in c++ using libTorch. First, i am trying to convert the model for use in c++. Based on the example code, i have:
import torch
import torchvision
from torchvision import models
model = models.segmentation.deeplabv3_resnet101(pretrained=True)
model.eval()
# An example input you would normally provide to your model's forward() method.
example = torch.rand(1, 3, 224, 224)
# Use torch.jit.trace to generate a torch.jit.ScriptModule via tracing.
traced_script_module = torch.jit.trace(model, example)
traced_script_module.save("model.pt")
When i run this, I get the error:
Traceback (most recent call last):
File "convert.py", line 15, in <module>
traced_script_module = torch.jit.trace(model, example)
File "C:\Python37\lib\site-packages\torch\jit\__init__.py", line 636, in trace
var_lookup_fn, _force_outplace)
RuntimeError: Only tensors and (possibly nested) tuples of tensors are supported as inputs or outputs of traced functions (toIValue at C:\a\w\1\s\windows\pytorch\torch/csrc/jit/pybind_utils.h:91)
(no backtrace available)
trace only supports modules that have tensor or tuple of tensor as output.
According to deeplabv3 implementation, its output is OrderedDict. That is problem.
To solve, make wrapper module
class wrapper(torch.nn.Module):
def __init__(self, model):
super(wrapper, self).__init__()
self.model = model
def forward(self, input):
results = []
output = self.model(input)
for k, v in output.items():
results.append(v)
return tuple(results)
model = wrapper(deeplap_model)
#trace...
Hi, thanks for your reply. With the ‘torch.rand’ data as above, I get:
Unhandled exception at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: std::runtime_error at memory location 0x00000098549B7A80.
the output is:
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA678.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA648.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA678.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA648.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA678.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA648.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA678.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA648.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA678.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA648.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA678.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA648.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA678.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA648.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA678.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA648.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA678.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA648.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA678.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA648.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA678.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA648.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA678.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA648.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA678.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA648.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA678.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA648.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA678.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA648.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA678.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA648.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA678.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: torch::jit::constant_not_supported_error at memory location 0x00000098549BA648.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: c10::Error at memory location 0x00000098549BA210.
Exception thrown at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: std::runtime_error at memory location 0x00000098549B7A80.
Unhandled exception at 0x00007FFE50BD3FB8 in Segmentation.exe: Microsoft C++ exception: std::runtime_error at memory location 0x00000098549B7A80.
`torch.dll!torch::jit::InterpreterStateImpl::handleError(std::basic_string<char,std::char_traits,std::allocator > && error_msg, bool is_jit_exception) Line 750 C++