I took the official torchvision C++ example project and changed it so that it uses the an object detection model ssdlite320_mobilenet_v3_large
instead of the image recognition model resnet18
. This causes the following error when running the built executable:
⋊> /w/o/v/e/c/h/build on main ⨯ ./hello-world 14:12:27
terminate called after throwing an instance of 'c10::Error'
what(): forward() Expected a value of type 'List[Tensor]' for argument 'images' but instead found type 'Tensor'.
Position: 1
Declaration: forward(__torch__.torchvision.models.detection.ssd.SSD self, Tensor[] images, Dict(str, Tensor)[]? targets=None) -> ((Dict(str, Tensor), Dict(str, Tensor)[]))
Exception raised from checkArg at ../aten/src/ATen/core/function_schema_inl.h:339 (most recent call first):
frame #0: c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) + 0x6b (0x7f0cb87da05b in /work/Downloads/libtorch/lib/libc10.so)
frame #1: c10::detail::torchCheckFail(char const*, char const*, unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) + 0xbf (0x7f0cb87d4f6f in /work/Downloads/libtorch/lib/libc10.so)
frame #2: void c10::FunctionSchema::checkArg<c10::Type>(c10::IValue const&, c10::Argument const&, c10::optional<unsigned long>) const + 0x151 (0x7f0cb9de0361 in /work/Downloads/libtorch/lib/libtorch_cpu.so)
frame #3: void c10::FunctionSchema::checkAndNormalizeInputs<c10::Type>(std::vector<c10::IValue, std::allocator<c10::IValue> >&, std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, c10::IValue, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, c10::IValue> > > const&) const + 0x217 (0x7f0cb9de1ba7 in /work/Downloads/libtorch/lib/libtorch_cpu.so)
frame #4: torch::jit::Method::operator()(std::vector<c10::IValue, std::allocator<c10::IValue> >, std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, c10::IValue, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, c10::IValue> > > const&) const + 0x173 (0x7f0cbcde5b53 in /work/Downloads/libtorch/lib/libtorch_cpu.so)
frame #5: <unknown function> + 0x151da (0x56495747d1da in ./hello-world)
frame #6: <unknown function> + 0x11c90 (0x564957479c90 in ./hello-world)
frame #7: <unknown function> + 0x29d90 (0x7f0cb830dd90 in /lib/x86_64-linux-gnu/libc.so.6)
frame #8: __libc_start_main + 0x80 (0x7f0cb830de40 in /lib/x86_64-linux-gnu/libc.so.6)
frame #9: <unknown function> + 0x11765 (0x564957479765 in ./hello-world)
fish: Job 1, './hello-world' terminated by signal SIGABRT (Abort)
The modified code looks as follows:
trace_model.py
import os.path as osp
import torch
import torchvision
HERE = osp.dirname(osp.abspath(__file__))
ASSETS = osp.dirname(osp.dirname(HERE))
model = torchvision.models.detection.ssdlite320_mobilenet_v3_large()
model.eval()
traced_model = torch.jit.script(model)
traced_model.save("ssdlite320_mobilenet_v3_large.pt")
main.cpp
#include <torch/script.h>
#include <torchvision/vision.h>
int main()
{
torch::jit::script::Module model = torch::jit::load("ssdlite320_mobilenet_v3_large.pt");
auto inputs = std::vector<torch::jit::IValue> {torch::rand({1, 3, 10, 10})};
auto out = model.forward(inputs);
std::cout << out << "\n";
}
Do you have any idea what’s going on here?