Pickle Error with resnext101_32x4d Pretrained models

Somehow a lambda is sneaking into the code and causing a pickle error on learn.load but I haven’t figured out to solve it. Does anyone have some clever workarounds?

The error is:

I am certain it is related to purge due to the traceback below. I am less certain that the lamba problem is in torch/serialization.py or how to solve it.

PicklingError                             Traceback (most recent call last)
<ipython-input-22-1870fe106b65> in <module>
----> 1 learn.load('bestdog')

~/anaconda3/lib/python3.7/site-packages/fastai/basic_train.py in load(self, file, device, strict, with_opt, purge, remove_module)
    262              with_opt:bool=None, purge:bool=True, remove_module:bool=False):
    263         "Load model and optimizer state (if `with_opt`) `file` from `self.model_dir` using `device`. `file` can be file-like (file or buffer)"
--> 264         if purge: self.purge(clear_opt=ifnone(with_opt, False))
    265         if device is None: device = self.data.device
    266         elif isinstance(device, int): device = torch.device('cuda', device)

~/anaconda3/lib/python3.7/site-packages/fastai/basic_train.py in purge(self, clear_opt)
    315         tmp_file = get_tmp_file(self.path/self.model_dir)
--> 316         torch.save(state, open(tmp_file, 'wb'))
    317         for a in attrs_del: delattr(self, a)
    318         gc.collect()

~/anaconda3/lib/python3.7/site-packages/torch/serialization.py in save(obj, f, pickle_module, pickle_protocol)
    217         >>> torch.save(x, buffer)
    218     """
--> 219     return _with_file_like(f, "wb", lambda f: _save(obj, f, pickle_module, pickle_protocol))

~/anaconda3/lib/python3.7/site-packages/torch/serialization.py in _with_file_like(f, mode, body)
    142         f = open(f, mode)
    143     try:
--> 144         return body(f)
    145     finally:
    146         if new_fd:

~/anaconda3/lib/python3.7/site-packages/torch/serialization.py in <lambda>(f)
    217         >>> torch.save(x, buffer)
    218     """
--> 219     return _with_file_like(f, "wb", lambda f: _save(obj, f, pickle_module, pickle_protocol))

~/anaconda3/lib/python3.7/site-packages/torch/serialization.py in _save(obj, f, pickle_module, pickle_protocol)
    290     pickler = pickle_module.Pickler(f, protocol=pickle_protocol)
    291     pickler.persistent_id = persistent_id
--> 292     pickler.dump(obj)
    294     serialized_storage_keys = sorted(serialized_storages.keys())

PicklingError: Can't pickle <function <lambda> at 0x7fef07919158>: attribute lookup <lambda> on pretrainedmodels.models.resnext_features.resnext101_32x4d_features failed
1 Like

Yes pytorch serializes with pickle which doesn’t like lambda functions. You can avoid the purge by passing purge=False but you’ll never be able to export a learner with that model. It should be written without lambda functions.


Perfect! Fortunately, no need to export and now I can use the the pretrained models!