Hello,
I have saved pytorch model using below method after training
torch.save(model.state_dict(), model_path)
My final goal is to deploy the model on mobile. I am using below code for the purpose
model = Net()
model.load_state_dict(torch.load(model_path, map_location=‘cpu’))
traced_script_module = torch.jit.script(model)
traced_script_module_optimized = optimize_for_mobile(traced_script_module)
traced_script_module_optimized._save_for_lite_interpreter(ptl_model_path)
With this code, I am getting below error. Any fix available for this problem?
File “pytorch_experiment_mnist.py”, line 103, in convert_pth_to_ptl
traced_script_module = torch.jit.script(model)
File “/home/harshavardhana/ProjectTools/Python_VEnv/venv_pytorch/lib/python3.6/site-packages/torch/jit/_script.py”, line 1258, in script
obj, torch.jit._recursive.infer_methods_to_compile
File “/home/harshavardhana/ProjectTools/Python_VEnv/venv_pytorch/lib/python3.6/site-packages/torch/jit/_recursive.py”, line 451, in create_script_module
return create_script_module_impl(nn_module, concrete_type, stubs_fn)
File “/home/harshavardhana/ProjectTools/Python_VEnv/venv_pytorch/lib/python3.6/site-packages/torch/jit/_recursive.py”, line 513, in create_script_module_impl
script_module = torch.jit.RecursiveScriptModule._construct(cpp_module, init_fn)
File “/home/harshavardhana/ProjectTools/Python_VEnv/venv_pytorch/lib/python3.6/site-packages/torch/jit/_script.py”, line 587, in _construct
init_fn(script_module)
File “/home/harshavardhana/ProjectTools/Python_VEnv/venv_pytorch/lib/python3.6/site-packages/torch/jit/_recursive.py”, line 491, in init_fn
scripted = create_script_module_impl(orig_value, sub_concrete_type, stubs_fn)
File “/home/harshavardhana/ProjectTools/Python_VEnv/venv_pytorch/lib/python3.6/site-packages/torch/jit/_recursive.py”, line 517, in create_script_module_impl
create_methods_and_properties_from_stubs(concrete_type, method_stubs, property_stubs)
File “/home/harshavardhana/ProjectTools/Python_VEnv/venv_pytorch/lib/python3.6/site-packages/torch/jit/_recursive.py”, line 368, in create_methods_and_properties_from_stubs
concrete_type._create_methods_and_properties(property_defs, property_rcbs, method_defs, method_rcbs, method_defaults)
File “/home/harshavardhana/ProjectTools/Python_VEnv/venv_pytorch/lib/python3.6/site-packages/torch/jit/_recursive.py”, line 838, in try_compile_fn
return torch.jit.script(fn, _rcb=rcb)
File “/home/harshavardhana/ProjectTools/Python_VEnv/venv_pytorch/lib/python3.6/site-packages/torch/jit/_script.py”, line 1307, in script
ast = get_jit_def(obj, obj.name)
File “/home/harshavardhana/ProjectTools/Python_VEnv/venv_pytorch/lib/python3.6/site-packages/torch/jit/frontend.py”, line 264, in get_jit_def
return build_def(parsed_def.ctx, fn_def, type_line, def_name, self_name=self_name, pdt_arg_types=pdt_arg_types)
File “/home/harshavardhana/ProjectTools/Python_VEnv/venv_pytorch/lib/python3.6/site-packages/torch/jit/frontend.py”, line 302, in build_def
param_list = build_param_list(ctx, py_def.args, self_name, pdt_arg_types)
File “/home/harshavardhana/ProjectTools/Python_VEnv/venv_pytorch/lib/python3.6/site-packages/torch/jit/frontend.py”, line 337, 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 “/usr/lib/python3.6/collections/init.py”, line 357
def namedtuple(typename, field_names, *, verbose=False, rename=False, module=None):
~~~~~ <— HERE
“”"Returns a new subclass of tuple with named fields.