qubvel / efficientnet

Implementation of EfficientNet model. Keras and TensorFlow Keras.
https://arxiv.org/abs/1905.11946
Apache License 2.0
2.07k stars 472 forks source link

Pickling errors for `preprocess_input` due to `functools.wraps` #154

Open ORippler opened 1 year ago

ORippler commented 1 year ago

Hey,

executing the below minimum working example will error

import pickle

from efficientnet.keras import preprocess_input

with open("test.pkl", "wb") as fh:
    pickle.dump(preprocess_input, fh)

with _pickle.PicklingError: Can't pickle <function preprocess_input at 0x7f1c565d94d0>: it's not the same object as efficientnet.model.preprocess_input.

Reason for this is that functools.wraps used to inject the preprocessing here per default updates the __module__ etc. of the wrapper function to look like the wrapped function, see here. Quick and dirty solution is to set the __module__ property explicitly, but that is certainly not very elegant.