tensorflow: AttributeError: '_UserObject' object has no attribute 'add_slot'
Issue Type: Bug Source: binary Tensorflow Version: 2.6 Custom Code: Yes
Current Behaviour?
I have model, saved using SavedModel format. The model is trained on Kaggle TPU and saved in the following ways:
# in kaggle tpu, it must be saved in the following way!
save_locally = tf.saved_model.SaveOptions(experimental_io_device='/job:localhost')
final_model.save('./final_model', options=save_locally)
Now, it seems like, if I try to load this model in the following way,
model = tf.saved_model.load("final_model")
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/tmp/ipykernel_17/3033429325.py in <module>
----> 1 model = tf.saved_model.load("final_model")
...
...
AttributeError: '_UserObject' object has no attribute 'add_slot'
But if I do as follows, it works.
model = tf.keras.models.load_model("final_model")
But for some reason, I need to make tf.saved_model.load API work instead tf.keras.models.load_model API. What approach should we take here?
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 3
- Comments: 16 (2 by maintainers)
Here’s a good reproducible example project https://github.com/cjbayron/autochord. The author uses the legacy .pb extension for the model, however in Keras 3 this isn’t supported and it recommends you to use TFSMLayer(model_dir) instead. If you change line 74 on init.py from load_model to keras.layers.TFSMLayer(_CHORD_MODEL_DIR), you get exactly the error that the author is discussing here.
@hassan883 I ended up using TF 2.16.1, with tf-keras installed in the environment. Also, I’m using keras 2 in the backend, which requires setting this environment variable before importing tensorflow:
os.environ["TF_USE_LEGACY_KERAS"] = "1"As far as I know, the TFSMLayer option can work to load an old model, but then the model is static and the Layers can’t be accessed, which in my case wouldn’t work, since I need to be able to load/add/freeze layers in the model for transfer learning.
What I found worked for me was loading the model this way:
This was an important change compared to loading via
tf.keras.load_model().Another issue I ran into affected model deserialization. Without this, all of my custom model layers were being interpreted as either TFOpLambdas or SlicingOpLambda, and didn’t retain the keras properties/methods I was used to, which broke my code. In the end, this is what fixed that second issue:
Before, I was importing in this order/fashion. Don’t do this!
@cameronbronstein Thank you so much, i just fixed my issue, to get rid of this install the tensorflow using this command:
First, remove all existing installations
pip uninstall -y tensorflow tensorflow-cpu keras tf-kerasClear pip cache
pip cache purgeInstall specific versions in order
and import the libraries like that;
Thanks @minevvgoogle ! That also worked for me 😃
For context, I am trying to upgrade to TF 2.18, and was trying to load a model trained using an old version of TF/Keras 2 (i.e. TF 2.4.1), which was saved using the
saved_modelformat.When trying to load the model with
tf.keras.models.load_model(SAVED_MODEL_PATH, compile=False), I was getting the following error:However, attempting to load with
TFSMLayerresulted inAttributeError: '_UserObject' object has no attribute 'add_slot'.It’d be nice to have clearer documentation of what steps to take for compatibility with old models. Or did I miss that
tf-kerasneeds to be installed for that to work?Installing
tf-kerassolves the issue in our case. But it doesn’t look like a good solution, just a workaround that can help with finding a root cause@kfrancischen in my case the
tf.keras.models.load_modeldoesn’t solve the pb, for saving the model and loading it with tf 2.9 everything works fine but when I switch to tf 2.6 it is not the case … for more info I am using the subclassing API to build the model.