FasterRCNN Resnet50 JIT Trace

Hi,

I’m trying to trace FasterRCNN to use in Pytorch Mobile on iOS.

I simply trace as shown below:

model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)
model.eval()

input_tensor = torch.rand(1,3,224,224)
script_model = torch.jit.trace(model, input_tensor)
script_model.save("models/fRCNN_resnet50.pt")

I receive a “Only tensors or tuples of tensors can be output from traced functions (getOutput at …/torch/csrc/jit/tracer.cpp:209)” error as shown below

RuntimeError: Only tensors or tuples of tensors can be output from traced functions (getOutput at ../torch/csrc/jit/tracer.cpp:209)
frame #0: c10::Error::Error(c10::SourceLocation, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) + 135 (0x128cc29e7 in libc10.dylib)
frame #1: torch::jit::tracer::TracingState::getOutput(c10::IValue const&) + 1785 (0x120164069 in libtorch.dylib)
frame #2: torch::jit::tracer::exit(std::__1::vector<c10::IValue, std::__1::allocator<c10::IValue> > const&) + 232 (0x120167108 in libtorch.dylib)
frame #3: torch::jit::tracer::createGraphByTracing(pybind11::function const&, torch::jit::tracer::TypedStack, pybind11::function const&, bool, torch::jit::script::Module*) + 916 (0x11c957914 in libtorch_python.dylib)
frame #4: void pybind11::cpp_function::initialize<torch::jit::script::initJitScriptBindings(_object*)::$_16, void, torch::jit::script::Module&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, pybind11::function, pybind11::tuple, pybind11::function, bool, pybind11::name, pybind11::is_method, pybind11::sibling>(torch::jit::script::initJitScriptBindings(_object*)::$_16&&, void (*)(torch::jit::script::Module&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, pybind11::function, pybind11::tuple, pybind11::function, bool), pybind11::name const&, pybind11::is_method const&, pybind11::sibling const&)::'lambda'(pybind11::detail::function_call&)::__invoke(pybind11::detail::function_call&) + 197 (0x11c993185 in libtorch_python.dylib)
frame #5: pybind11::cpp_function::dispatcher(_object*, _object*, _object*) + 3324 (0x11c4cd92c in libtorch_python.dylib)
frame #6: _PyCFunction_FastCallDict + 183 (0x10e3d0167 in Python)
frame #7: call_function + 184 (0x10e452d28 in Python)
frame #8: _PyEval_EvalFrameDefault + 27511 (0x10e44f597 in Python)
frame #9: _PyEval_EvalCodeWithName + 2447 (0x10e45388f in Python)
frame #10: fast_function + 545 (0x10e454141 in Python)
frame #11: call_function + 401 (0x10e452e01 in Python)
frame #12: _PyEval_EvalFrameDefault + 27511 (0x10e44f597 in Python)
frame #13: _PyEval_EvalCodeWithName + 2447 (0x10e45388f in Python)
frame #14: fast_function + 545 (0x10e454141 in Python)
frame #15: call_function + 401 (0x10e452e01 in Python)
frame #16: _PyEval_EvalFrameDefault + 27511 (0x10e44f597 in Python)
frame #17: _PyEval_EvalCodeWithName + 2447 (0x10e45388f in Python)
frame #18: PyEval_EvalCode + 100 (0x10e448954 in Python)
frame #19: builtin_exec + 548 (0x10e445fe4 in Python)
frame #20: _PyCFunction_FastCallDict + 491 (0x10e3d029b in Python)
frame #21: call_function + 439 (0x10e452e27 in Python)
frame #22: _PyEval_EvalFrameDefault + 27511 (0x10e44f597 in Python)
frame #23: gen_send_ex + 183 (0x10e3a7fe7 in Python)
frame #24: _PyEval_EvalFrameDefault + 11552 (0x10e44b740 in Python)
frame #25: gen_send_ex + 183 (0x10e3a7fe7 in Python)
frame #26: _PyEval_EvalFrameDefault + 11552 (0x10e44b740 in Python)
frame #27: gen_send_ex + 183 (0x10e3a7fe7 in Python)
frame #28: _PyCFunction_FastCallDict + 560 (0x10e3d02e0 in Python)
frame #29: call_function + 439 (0x10e452e27 in Python)
frame #30: _PyEval_EvalFrameDefault + 27511 (0x10e44f597 in Python)
frame #31: fast_function + 381 (0x10e45409d in Python)
frame #32: call_function + 401 (0x10e452e01 in Python)
frame #33: _PyEval_EvalFrameDefault + 27511 (0x10e44f597 in Python)
frame #34: fast_function + 381 (0x10e45409d in Python)
frame #35: call_function + 401 (0x10e452e01 in Python)
frame #36: _PyEval_EvalFrameDefault + 27511 (0x10e44f597 in Python)
frame #37: _PyEval_EvalCodeWithName + 2447 (0x10e45388f in Python)
frame #38: _PyFunction_FastCallDict + 763 (0x10e45445b in Python)
frame #39: _PyObject_FastCallDict + 247 (0x10e3873e7 in Python)
frame #40: _PyObject_Call_Prepend + 149 (0x10e387505 in Python)
frame #41: PyObject_Call + 96 (0x10e387220 in Python)
frame #42: _PyEval_EvalFrameDefault + 28250 (0x10e44f87a in Python)
frame #43: _PyEval_EvalCodeWithName + 2447 (0x10e45388f in Python)
frame #44: fast_function + 545 (0x10e454141 in Python)
frame #45: call_function + 401 (0x10e452e01 in Python)
frame #46: _PyEval_EvalFrameDefault + 27670 (0x10e44f636 in Python)
frame #47: gen_send_ex + 183 (0x10e3a7fe7 in Python)
frame #48: builtin_next + 92 (0x10e446bcc in Python)
frame #49: _PyCFunction_FastCallDict + 491 (0x10e3d029b in Python)
frame #50: call_function + 439 (0x10e452e27 in Python)
frame #51: _PyEval_EvalFrameDefault + 27511 (0x10e44f597 in Python)
frame #52: _PyEval_EvalCodeWithName + 2447 (0x10e45388f in Python)
frame #53: fast_function + 545 (0x10e454141 in Python)
frame #54: call_function + 401 (0x10e452e01 in Python)
frame #55: _PyEval_EvalFrameDefault + 27511 (0x10e44f597 in Python)
frame #56: gen_send_ex + 183 (0x10e3a7fe7 in Python)
frame #57: builtin_next + 92 (0x10e446bcc in Python)
frame #58: _PyCFunction_FastCallDict + 491 (0x10e3d029b in Python)
frame #59: call_function + 439 (0x10e452e27 in Python)
frame #60: _PyEval_EvalFrameDefault + 27511 (0x10e44f597 in Python)
frame #61: _PyEval_EvalCodeWithName + 2447 (0x10e45388f in Python)
frame #62: fast_function + 545 (0x10e454141 in Python)
frame #63: call_function + 401 (0x10e452e01 in Python)

Could someone explain to me how to properly trace or script a pretrained object detection model such as this one? I don’t know which steps I might be missing if any!

The MaskRCNN based models don’t support tracing due to the error you saw, but thanks to this PR you can use torch.jit.script to compile the whole network.

You’ll need to get the most current version of torchvision by building it from source, then you can do

model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)
model.eval()
script_model = torch.jit.script(model)
1 Like