我是Fast-ai的新手。我为数据绑定定义了自己的转换pad3d,并使用自己的自动编码器模型创建并训练了学习者。但是,当我们要导出学习者时,我遇到了一个错误:
PicklingError: Can’t pickle <function pad3d at 0x7fd07dee8d90>: it’s not the same object as main.pad3d
The traceback looks like this:
----> 2 learner.export(‘testexport.pkl’)
~/anaconda2/envs/my37/lib/python3.7/site-packages/fastai/basic_train.py in export(self, file, destroy)
240 state[‘data’] = self.data.valid_ds.get_state(**xtra)
241 state[‘cls’] = self.class
–> 242 try_save(state, self.path, file)
243 if destroy: self.destroy()
244
~/anaconda2/envs/my37/lib/python3.7/site-packages/fastai/torch_core.py in try_save(state, path, file)
404 def try_save(state:Dict, path:Path=None, file:PathLikeOrBinaryStream=None):
405 target = open(path/file, ‘wb’) if is_pathlike(file) else file
–> 406 try: torch.save(state, target)
407 except OSError as e:
408 raise Exception(f"{e}\n Can’t write {path/file}. Pass an absolute writable pathlib obj fname.")
~/anaconda2/envs/my37/lib/python3.7/site-packages/torch/serialization.py in save(obj, f, pickle_module, pickle_protocol)
222 >>> torch.save(x, buffer)
223 “”"
–> 224 return _with_file_like(f, “wb”, lambda f: _save(obj, f, pickle_module, pickle_protocol))
225
226
~/anaconda2/envs/my37/lib/python3.7/site-packages/torch/serialization.py in _with_file_like(f, mode, body)
147 f = open(f, mode)
148 try:
–> 149 return body(f)
150 finally:
151 if new_fd:
~/anaconda2/envs/my37/lib/python3.7/site-packages/torch/serialization.py in (f)
222 >>> torch.save(x, buffer)
223 “”"
–> 224 return _with_file_like(f, “wb”, lambda f: _save(obj, f, pickle_module, pickle_protocol))
225
226
~/anaconda2/envs/my37/lib/python3.7/site-packages/torch/serialization.py in _save(obj, f, pickle_module, pickle_protocol)
295 pickler = pickle_module.Pickler(f, protocol=pickle_protocol)
296 pickler.persistent_id = persistent_id
–> 297 pickler.dump(obj)
298
299 serialized_storage_keys = sorted(serialized_storages.keys())
PicklingError: Can’t pickle <function pad3d at 0x7fd07dee8d90>: it’s not the same object as main.pad3d
我看过这两个帖子
https://forums.fast.ai/t/learn-export-not-working-with-gans/42195/12
https://github.com/fastai/fastai/issues/1540
在后一篇文章中,我感觉是因为我的代码包含以下自定义函数:
@faiv.TfmPixel
@singledispatch
def pad3d(x, padding)->torch.Tensor:
#s = x.shape
npad = (padding[0],padding[0],padding[1],padding[1],padding[2],padding[2])
m = nn.ConstantPad3d(npad, 0)
r = m(x)
return r.contiguous()
tfms1 = pad3d(padding=(50,20,50))
我在导出之前重新运行了该函数的定义,但是它也可以正常工作。 那里似乎没有一个能帮助我解决问题。
非常感谢任何人。