Converting YOLOv3 Darknet Implementation to Torchscript

username: Helaly96
cooked: <p>Hello All,<br>
I Used this implementation of YOLOv3 in my code,<br>
<a href="https://github.com/eriklindernoren/PyTorch-YOLOv3" rel="noopener nofollow ugc">eriklindernoren/PyTorch-YOLOv3: Minimal PyTorch implementation of YOLOv3 (github.com)</a><br>
i wanted to then Convert it to torch scipt and JIT , but i am represented with multiple errors.</p>
<p>Code Sample:</p>
<pre><code class="lang-auto">device = torch.device("cpu")
# Set up model
model = Darknet("config/yolov3.cfg", img_size=416).to(device)
# load the weights
model.load_darknet_weights("weights/yolov3.weights")

Tensor = torch.FloatTensor

img=cv2.imread("temp.jpg")
PILimg = np.array(Image.fromarray(cv2.cvtColor(img,cv2.COLOR_BGR2RGB)))
imgTensor = transforms.ToTensor()(PILimg)
imgTensor, _ = pad_to_square(imgTensor, 0)
imgTensor = resize(imgTensor, 416)
#add the batch size
imgTensor = imgTensor.unsqueeze(0)
imgTensor = Variable(imgTensor.type(Tensor))
sm = torch.jit.script(model)
sm.save("traced_resnet_model.pt")
</code></pre>
<p>Output:</p>
<pre><code class="lang-auto">  File ".\TSTest.py", line 51, in &lt;module&gt;
    sm = torch.jit.script(model)
  File "C:\Users\Helaly\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\jit\_script.py", line 898, in script
    obj, torch.jit._recursive.infer_methods_to_compile
  File "C:\Users\Helaly\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\jit\_recursive.py", line 352, in create_script_module
    return create_script_module_impl(nn_module, concrete_type, stubs_fn)
  File "C:\Users\Helaly\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\jit\_recursive.py", line 406, in create_script_module_impl
    script_module = torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn)
  File "C:\Users\Helaly\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\jit\_script.py", line 388, in _construct
    init_fn(script_module)
  File "C:\Users\Helaly\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\jit\_recursive.py", line 388, in init_fn
    scripted = create_script_module_impl(orig_value, sub_concrete_type, stubs_fn)
  File "C:\Users\Helaly\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\jit\_recursive.py", line 406, in create_script_module_impl
    script_module = torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn)
  File "C:\Users\Helaly\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\jit\_script.py", line 388, in _construct
    init_fn(script_module)
  File "C:\Users\Helaly\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\jit\_recursive.py", line 388, in init_fn
    scripted = create_script_module_impl(orig_value, sub_concrete_type, stubs_fn)
  File "C:\Users\Helaly\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\jit\_recursive.py", line 410, in create_script_module_impl
    create_methods_and_properties_from_stubs(concrete_type, method_stubs, property_stubs)
  File "C:\Users\Helaly\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\jit\_recursive.py", line 304, in create_methods_and_properties_from_stubs
    concrete_type._create_methods_and_properties(property_defs, property_rcbs, method_defs, method_rcbs, method_defaults)
  File "C:\Users\Helaly\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\jit\_recursive.py", line 676, in compile_unbound_method
    stub = make_stub(fn, fn.__name__)
  File "C:\Users\Helaly\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\jit\_recursive.py", line 37, in make_stub
    ast = get_jit_def(func, name, self_name="RecursiveScriptModule")
  File "C:\Users\Helaly\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\jit\frontend.py", line 221, in get_jit_def
    return build_def(ctx, fn_def, type_line, def_name, self_name=self_name)
  File "C:\Users\Helaly\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\jit\frontend.py", line 243, in build_def
    param_list = build_param_list(ctx, py_def.args, self_name)
  File "C:\Users\Helaly\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\jit\frontend.py", line 270, in build_param_list
    raise NotSupportedError(ctx_range, _vararg_kwarg_err)
torch.jit.frontend.NotSupportedError: Compiled functions can't take variable number of arguments or use keyword-only arguments with defaults:
  File "C:\Users\Helaly\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\nn\modules\module.py", line 164
def _forward_unimplemented(self, *input: Any) -&gt; None:
                                 ~~~~~~ &lt;--- HERE
    r"""Defines the computation performed at every call.
</code></pre>
<p>and if i chose to replace jit.script with jit.trace , an huge log is dumped on screen and nothing is saved</p>

username: Prrr
cooked: <p>Hey <a class="mention" href="/u/ptrblck">@ptrblck</a> any idea how to solve this issue ?<br>
As the function here goes to  _forward_unimplemented even though there is a forward function?<br>
Also <a class="mention" href="/u/helaly96">@Helaly96</a> were you able to solve the issue ?</p>