Getting Typeerror: Can't Pickle _thread.rlock Objects
Read a number of similar questions, most of them mentioned that you shouldn't try to serialize an unserializable object. I am not able to understand the issue. I am able to save th
Solution 1:
I was able to replicate your issue in TF 2.3.0 using Google Colab
import pickle
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
model = Sequential()
model.add(Dense(1, input_dim=42, activation='sigmoid'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
withopen('model.pkl', 'wb') as f:
pickle.dump(model, f)
Output:
---------------------------------------------------------------------------
TypeError Traceback (most recent calllast)
<ipython-input-1-afb2bf58a891>in<module>()
89withopen('model.pkl', 'wb') as f:
---> 10 pickle.dump(model, f)
TypeError: can't pickle _thread.RLock objects
@adriangb, proposed hot fix to this issue in github for more details please refer this
import pickle
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Dense
from tensorflow.python.keras.layers import deserialize, serialize
from tensorflow.python.keras.saving import saving_utils
defunpack(model, training_config, weights):
restored_model = deserialize(model)
if training_config isnotNone:
restored_model.compile(
**saving_utils.compile_args_from_training_config(
training_config
)
)
restored_model.set_weights(weights)
return restored_model
# Hotfix functiondefmake_keras_picklable():
def__reduce__(self):
model_metadata = saving_utils.model_metadata(self)
training_config = model_metadata.get("training_config", None)
model = serialize(self)
weights = self.get_weights()
return (unpack, (model, training_config, weights))
cls = Model
cls.__reduce__ = __reduce__
# Run the function
make_keras_picklable()
# Create the model
model = Sequential()
model.add(Dense(1, input_dim=42, activation='sigmoid'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# Savewithopen('model.pkl', 'wb') as f:
pickle.dump(model, f)
Solution 2:
An improved version of the hotfix linked in the accepted answer can be found here. Although it is a bit more complicated, it is more likely to work in future version of TensorFlow. This version also fixes tensorflow/tensorflow#44670.
Source: I am the author of the hotfix linked above as well as this improved version.
Post a Comment for "Getting Typeerror: Can't Pickle _thread.rlock Objects"