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)

Most upvoted comments

@mvidyasagar-sc If you can provide some reproducible code, we can reopen this ticket.

cc. @kfrancischen

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:

from tensorflow import keras

model = keras.models.load_model(path_to_saved_model, compile=False)

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:

from tensorflow.keras import layers
from tensorflow import keras

Before, I was importing in this order/fashion. Don’t do this!

from tensorflow import keras
from keras import layers

@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-keras

Clear pip cache

pip cache purge

Install specific versions in order

pip install tensorflow==2.16.1
pip install 'keras<3.0.0' --no-deps

and import the libraries like that;

import os
os.environ["TF_USE_LEGACY_KERAS"] = "1"
os.environ["KERAS_BACKEND"] = "tensorflow"
os.environ["TF_KERAS"] = "1"

import tensorflow.compat.v2 as tf

def patch_tf_internal():
    if not hasattr(tf.__internal__, "register_load_context_function"):

        def register_load_context_function(func):
            return tf.__internal__.register_call_context_function(func)

        tf.__internal__.register_load_context_function = register_load_context_function

patch_tf_internal()

from tensorflow.python import keras
from tensorflow.python.keras import layers

import numpy as np
import json
import matplotlib.pyplot as plt


BTW thanks @cameronbronstein 

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_model format.

When trying to load the model with tf.keras.models.load_model(SAVED_MODEL_PATH, compile=False), I was getting the following error:

ValueError: File format not supported: filepath=/tmp/saved_model. Keras 3 only supports V3 `.keras` files and legacy H5 format files (`.h5` extension). Note that the legacy SavedModel format is not supported by `load_model()` in Keras 3. In order to reload a TensorFlow SavedModel as an inference-only layer in Keras 3, use `keras.layers.TFSMLayer(/tmp/saved_model, call_endpoint='serving_default')` (note that your `call_endpoint` might have a different name).

However, attempting to load with TFSMLayer resulted in AttributeError: '_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-keras needs to be installed for that to work?

Installing tf-keras solves 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_model doesn’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.