taohan10200 / IIM

PyTorch implementations of the paper: "Learning Independent Instance Maps for Crowd Localization"
MIT License
169 stars 41 forks source link

模型保存为pt时报错RuntimeError: Could not export Python function call 'BinarizedF' #23

Open wangle-wang opened 3 years ago

wangle-wang commented 3 years ago

您好,在尝试使用torch.jit.trace保存为pt模型文件时 traced_script_module = torch.jit.trace(net,torch.rand(1, 3, 224, 224)) traced_script_module.save("modelIIMX.pt") 报如下错误: Traceback (most recent call last): File "gpt.py", line 28, in torch.jit.save(model,"modelIIMX.pt") File "/home/wjq/anaconda3/envs/IIM/lib/python3.7/site-packages/torch/jit/_serialization.py", line 81, in save m.save(f, _extra_files=_extra_files) File "/home/wjq/anaconda3/envs/IIM/lib/python3.7/site-packages/torch/jit/_script.py", line 490, in save return self._c.save(str(f), **kwargs) RuntimeError: Could not export Python function call 'BinarizedF'. Remove calls to Python functions before export. Did you forget to add @script or @script_method annotation? If this is a nn.ModuleList, add it to constants: /home/wjq/IIM/IIM-main/model/PBM.py(77): forward /home/wjq/anaconda3/envs/IIM/lib/python3.7/site-packages/torch/nn/modules/module.py(860): _slow_forward /home/wjq/anaconda3/envs/IIM/lib/python3.7/site-packages/torch/nn/modules/module.py(887): _call_impl /home/wjq/IIM/IIM-main/model/locator.py(39): forward /home/wjq/anaconda3/envs/IIM/lib/python3.7/site-packages/torch/nn/modules/module.py(860): _slow_forward /home/wjq/anaconda3/envs/IIM/lib/python3.7/site-packages/torch/nn/modules/module.py(887): _call_impl /home/wjq/anaconda3/envs/IIM/lib/python3.7/site-packages/torch/jit/_trace.py(940): trace_module /home/wjq/anaconda3/envs/IIM/lib/python3.7/site-packages/torch/jit/_trace.py(742): trace gpt.py(27): 尝试了一些方法仍未解决,麻烦有空帮忙看下要如何解决,谢谢

taohan10200 commented 3 years ago

我们没尝试过用torch.jit.trace来转换我们的代码,但是根据你的报错,应该是在转换我们PBM.py的代码的时候出问题了,具体的是BinarizedF这个模块, Could not export Python function call 'BinarizedF'. Remove calls to Python functions before export. Did you forget to add @script or @script_method annotation? If this is a nn.ModuleList, add it to constants: 建议你根据提示修改一下类里面的方法申明试一下。