tensorflow / models

Models and examples built with TensorFlow
Other
76.97k stars 45.79k forks source link

'_UserObject' object has no attribute 'summary' #8990

Closed nicholasguimaraes closed 2 years ago

nicholasguimaraes commented 4 years ago

Hello, I'm trying to load a ssd_resnet50_v1_fpn_640x640_coco17_tpu-8 I just fine tuned but I'm coming across this error:

'_UserObject' object has no attribute 'summary'

Here are the 4 lines of code I have;

import tensorflow as tf

model_dir = 'C:/Users/Windows/Documents/Tensorflow_Obj_Det_API/models/research/object_detection/inference_graph/saved_model'

trained_model = tf.saved_model.load(model_dir)

trained_model.summary()

I've tried including the save_model.pb on the path to the model but then I get this error:

SavedModel file does not exist at: C:\Users\Windows\Documents\Tensorflow_Obj_Det_API\models\research\object_detection\inference_graph\saved_model\saved_model.pb/{saved_model.pbtxt|saved_model.pb}

Anyone knows how to load a trained model to do inference?

nicholasguimaraes commented 4 years ago

I'm using the tf.keras.models.load_model function instead of tf.saved_model.load to load the model.

Now I'm getting a different error;

spec = fn.concrete_functions[0].structured_input_signature[0][0] IndexError: list index out of range

LackesLab commented 4 years ago

Did you already solved your problems? I ran in to the same issue. Using the saved_model.pb from the object detection api. Tried tf.saved_model.load as well as tf.keras.models.load_model

jvishnuvardhan commented 4 years ago

@nicholasguimaraes Can you please share a standalone code to reproduce the issue? Thanks!

google-ml-butler[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you.

yingsiqin commented 3 years ago

I ran in to the same issue. Using the saved_model.pb from the object detection api. Tried tf.saved_model.load as well as tf.keras.models.load_model. Both gave this error:

AttributeError: '_UserObject' object has no attribute 'summary'

hlloydt commented 3 years ago

I am also getting the same error with models:

The config files are the same the ones in: models/research/object_detection/configs/tf2/

I first train with: models/research/object_detection/model_main_tf2.py

And then I export with: models/research/object_detection/exporter_main_v2.py

But then when I try to load the exported model I get the error:

Traceback (most recent call last):
  File "/Users/harrythomas/projects/xrfiber_effdet/pb_to_coreml.py", line 82, in <module>
    main()
  File "/Users/harrythomas/projects/xrfiber_effdet/pb_to_coreml.py", line 22, in main
    model = tf.keras.models.load_model(PATH_TO_MODEL, compile=False)
  File "/Users/harrythomas/opt/anaconda3/envs/effdetvenv/lib/python3.8/site-packages/tensorflow/python/keras/saving/save.py", line 187, in load_model
    return saved_model_load.load(filepath, compile, options)
  File "/Users/harrythomas/opt/anaconda3/envs/effdetvenv/lib/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/load.py", line 120, in load
    model = tf_load.load_internal(
  File "/Users/harrythomas/opt/anaconda3/envs/effdetvenv/lib/python3.8/site-packages/tensorflow/python/saved_model/load.py", line 632, in load_internal
    loader = loader_cls(object_graph_proto, saved_model_proto, export_dir,
  File "/Users/harrythomas/opt/anaconda3/envs/effdetvenv/lib/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/load.py", line 194, in __init__
    super(KerasObjectLoader, self).__init__(*args, **kwargs)
  File "/Users/harrythomas/opt/anaconda3/envs/effdetvenv/lib/python3.8/site-packages/tensorflow/python/saved_model/load.py", line 130, in __init__
    self._load_all()
  File "/Users/harrythomas/opt/anaconda3/envs/effdetvenv/lib/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/load.py", line 221, in _load_all
    self._finalize_objects()
  File "/Users/harrythomas/opt/anaconda3/envs/effdetvenv/lib/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/load.py", line 526, in _finalize_objects
    _finalize_saved_model_layers(layers_revived_from_saved_model)
  File "/Users/harrythomas/opt/anaconda3/envs/effdetvenv/lib/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/load.py", line 706, in _finalize_saved_model_layers
    inputs = infer_inputs_from_restored_call_function(call_fn)
  File "/Users/harrythomas/opt/anaconda3/envs/effdetvenv/lib/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/load.py", line 982, in infer_inputs_from_restored_call_function
    spec = fn.concrete_functions[0].structured_input_signature[0][0]
IndexError: list index out of range

System Information:

Code:

Import tensorflow as tf
import object_detection.models.keras_models.resnet_v1
import object_detection.models.feature_map_generators
PATH_TO_MODEL = ‘./saved_model/‘
model = tf.keras.models.load_model(PATH_TO_MODEL)

I can load and make predictions with

model = tf.saved_model.load(PATH_TO_MODEL)
tf_out = model(inputs)

However I wish to load the saved model so that I can edit it and then export it with coreML.

ghost commented 3 years ago

has this issue been resolved yet? I'm getting the same issue.

eRaul commented 3 years ago

I am also getting the same issue. I was tring to load model from object detection model zone: https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2_detection_zoo.md

epikjjh commented 3 years ago

Same here issue when using object detection model in google colab

MaxPRon commented 3 years ago

Is there any progress on this ?

I'm trying to restore models in from both the Tensorflow hub as well as the object detection model zoo.

However in both cases I only get "<tensorflow.python.saved_model.load.Loader._recreate_base_user_object.._UserObject at 0x7f7431ec2070>" something like this. This has none of the usual model functionalities.

For Example the code I am using is the following:

test = tf.saved_model.load('models/faster_rcnn_resnet152_v1_800x1333_coco17_gpu-8/saved_model/')

The model has been download from here: http://download.tensorflow.org/models/object_detection/tf2/20200711/faster_rcnn_resnet152_v1_800x1333_coco17_gpu-8.tar.gz

I also used other loading methods such as: test_2 = tf.keras.models.load_model('models/faster_rcnn_resnet152_v1_800x1333_coco17_gpu-8/saved_model/')

But in both cases I don't get a fully functional model.

What I was expecting to get was a model with the usual compile(), fit() and predict() functions. However I am not sure if this is actually possible with the model zoo and tensorflow hub models.

I don't have any experiences with those pretrained models and therefore any help and suggestions are welcome.

``

nilskk commented 3 years ago

Does someone have any solution to this?

ghost commented 3 years ago

I think the problem here is that the saved model wasn't created using Keras. so of course, when you load it you don't get a Keras object. It should be possible to create a new model with the same architecture as the one you want to load using Keras and load the weights from there. something like this:

keras_model.set_weights(loaded_model.get_weights())
noirmist commented 3 years ago

I convert model from pytorch to tensorflow through onnx and I've got same issue right now...

twohiccups commented 3 years ago

Is this issue still being worked on?

RArbore commented 3 years ago

I've got the same issue with a model I've converted from onnx... Is this issue going to be resolved ever?

deepdatadive commented 3 years ago

I get the same issue when trying to load a model using trained using the object detection API and google colab when trying to import the .pb as a keras model:

imported = tf.keras.models.load_model(PATH_TO_SAVED_MODEL)

1067 spec = fn.concrete_functions[0].structured_input_signature[0][0] 1068 for concrete in fn.concrete_functions[1:]: 1069 spec2 = concrete.structured_input_signature[0][00]

IndexError: list index out of range

Songyun-Tao commented 3 years ago

I've got the same issue with a model I've converted from onnx... Is this issue going to be resolved ever?

I got the exact same issue, did you manage to solve it?

Yonma commented 3 years ago

There's an approach to solve the "IndexError: list index out of range" Error, hope this could help you, and I'm glad to see your comments. https://github.com/tensorflow/tensorflow/issues/43527#issuecomment-763430516

tianhuat commented 3 years ago

Yonma comments help on fixing the "IndexError: list index out of range" Error, but ultimately still the same error on "_UserObject' object has no attribute 'summary'", anyone who can get the summary() information successfully?

ghost commented 3 years ago

The same error on "_UserObject' object has no attribute 'summary'" - issue with model converted from onnx!

elanzini commented 3 years ago

@vighneshbirodkar I have the same problem when loading the DeepMAC pretrained model with the following code:

import tensorflow as tf

model_path = './deepmac_1024x1024_coco17/saved_model'
model = tf.keras.models.load_model(model_path)

model.summary()

I tried different versions of tf, including the suggested 2.2.0 in the Colab Demo and tf-nightly-gpu. I also tried to load the model as explained here follows:

from tensorflow import keras
model = keras.models.load_model('path/to/location')

but I get the same error. Also, the error is the same for any action that I try to perform on model, compile, fit or anything else.

Is there an alternative way to load the pretrained model to finetune it?

vighneshbirodkar commented 3 years ago

model.summary() is just not supported for the models at the moment, I suggest you just skip that line and move to inference.

Please use tf.saved_model.load for loading your models.

vighneshbirodkar commented 3 years ago

Also, exported models are not keras models, they won't support fit, compile, etc...

vighneshbirodkar commented 3 years ago

@elanzini For fine tuning you would just have to launch a regular training job and load pre-trained checkpoint by setting the fine_tune_checkpoint value in the config.

elanzini commented 3 years ago

I see, thanks @vighneshbirodkar!

sipie800 commented 3 years ago

Got a solution,it's weird though. give it a try. use keras api rather than tf.keras,like keras.models.load_model but not tf.keras.models.load_model that goes for save_model too. I'm using tf 2.5 and keras 2.4.3,both last version...... have no idea what's going on in there.Seems two sets of api of Keras?It' has been the api messing issues of tf-keras framework always anyway.

Ostyk commented 3 years ago

I also converted a pytorch model to a SavedModel via onnx Using: https://analyticsindiamag.com/converting-a-model-from-pytorch-to-tensorflow-guide-to-onnx/

Now facing the exact same issue when loading it in tf 2.5 AttributeError: '_UserObject' object has no attribute 'predict'

Ostyk commented 3 years ago

Ok I managed to run the model in the following manner (tf 2.3.1 cpu but should work for any Tensorflow version 2.0 <= TF < 2.5

According to https://www.tensorflow.org/guide/saved_model

# load model
model = tf.saved_model.load("../model/logs/models/42_500_inception_False_True")

print(list(loaded.signatures.keys()))  # ["serving_default"]

infer = model.signatures["serving_default"]
print(infer.structured_outputs) #{'output_0': TensorSpec(shape=(1, 2), dtype=tf.float32, name='output_0')}

# convert img to tf
x = tf.keras.preprocessing.image.img_to_array(img)
x = tf.keras.applications.mobilenet.preprocess_input(
    x[tf.newaxis,...])
# (1,3,network_size, network_size)

labeling = infer(tf.constant(x))
predict_class = np.argmax(labeling['output_0'].numpy())
print(predict_class ) # int, depends on your task -- mine was img classfication
rossGardiner commented 3 years ago

The underlying issue here is: "How can a Tensorflow pb saved_model be loaded into Keras?" Does anyone know?

Blackclaws commented 3 years ago

I'm seeing similar issues where the following doesn't work:

print(model.summary())
tf.saved_model.save(model, "saved_model")

new_model = tf.saved_model.load("saved_model")
print(new_model.summary())

So basically saving and immediately loading makes summary not work anymore. Maybe I am very confused about what saving in this context actually means.

I think the issue stems from the fact that the low-level tf.saved_moved.save and load functions do only save parts of the keras model in this case. What you want to use instead is this:

print(model.summary())
model.save("save_model_keras")

from tensorflow import keras

new_model = keras.models.load_model("save_model_keras")
print(new_model.summary())

Which does work. See also the documentation at: https://www.tensorflow.org/guide/keras/save_and_serialize

It should be made clearer what the limitations of tf.saved_model actually are

Sgsouham commented 2 years ago

I have got the same issue, i got a pth file, converted it into onnx and from there i converted it into .pb format which gives me a folder. i tried tf.keras.models.load_model but after i try to see a summary, it shows me the _UserObject object has no attribute summary. Please help.

workworkwc3 commented 2 years ago

+1 For this issue occurring after using ONNX to convert a PyTorch model in my case to ONNX, then ONNX to TensorFlow. So I am not entirely sure if this is a TF or ONNX problem. For what it's worth, I can use the "model" for prediction using the reply from @Ostyk involving the "signatures" attribute and "serving_default" key of the loaded "_UserObject".

However my intent/use case was to fully convert a PyTorch model to a TensorFlow model so I can use other libraries compatible with TensorFlow, and it seems they cannot recognize this "_UserObject" which only contains the "signatures" attribute (and is missing "variables", "graph", etc).

thatdc commented 2 years ago

Same issue here. I have a .pth PyTorch model and I exported it with Onnx:

batch_size = 1
x = torch.randn(batch_size, 1, 512, 2381, requires_grad=True)
torch_out = torch_model(x)

torch.onnx.export(torch_model,               # model being run
                  x,                         # model input (or a tuple for multiple inputs)
                  "PE_model.onnx",   # where to save the model (can be a file or file-like object)
                  export_params=True,        # store the trained parameter weights inside the model file
                  opset_version=10,          # the ONNX version to export the model to
                  do_constant_folding=True,  # whether to execute constant folding for optimization
                  input_names = ['input'],   # the model's input names
                  output_names = ['output'], # the model's output names
                  dynamic_axes={'input' : {0 : 'batch_size'},    # variable length axes
                                'output' : {0 : 'batch_size'}})

Then I imported it into a TF representation and exported the graph:

onnx_model = onnx.load("PE_model.onnx")  # load onnx model
tf_rep = prepare(onnx_model)  # prepare tf representation
tf_rep.export_graph("model_graph")  # export the model

When I load it with:

model = keras.models.load_model('/content/model_graph')

I get the usual '_UserObject' object has no attribute 'summary'

danishansari commented 2 years ago

I am getting similar error with TF2.6, cuda-10.2, TRT-7.2

2021-11-04 16:06:55.345573: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2021-11-04 16:06:55.349826: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2021-11-04 16:06:55.350105: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2021-11-04 16:06:55.350528: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. 2021-11-04 16:06:55.350809: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2021-11-04 16:06:55.351071: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2021-11-04 16:06:55.351302: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2021-11-04 16:06:55.766650: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2021-11-04 16:06:55.766916: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2021-11-04 16:06:55.767136: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2021-11-04 16:06:55.767349: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1510] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 2440 MB memory: → device: 0, name: GeForce GTX 1650, pci bus id: 0000:01:00.0, compute capability: 7.5 Traceback (most recent call last): File “scripts/convert_saved_model_trt_engine.py”, line 20, in converter.convert() File “/home/hexa/miniconda3/envs/TRT/lib/python3.8/site-packages/tensorflow/python/compiler/tensorrt/trt_convert.py”, line 1096, in convert self._saved_model = load.load(self._input_saved_model_dir, File “/home/hexa/miniconda3/envs/TRT/lib/python3.8/site-packages/tensorflow/python/saved_model/load.py”, line 864, in load result = load_internal(export_dir, tags, options)[“root”] File “/home/hexa/miniconda3/envs/TRT/lib/python3.8/site-packages/tensorflow/python/saved_model/load.py”, line 902, in load_internal loader = loader_cls(object_graph_proto, saved_model_proto, export_dir, File “/home/hexa/miniconda3/envs/TRT/lib/python3.8/site-packages/tensorflow/python/saved_model/load.py”, line 162, in init self._load_all() File “/home/hexa/miniconda3/envs/TRT/lib/python3.8/site-packages/tensorflow/python/saved_model/load.py”, line 259, in _load_all self._load_nodes() File “/home/hexa/miniconda3/envs/TRT/lib/python3.8/site-packages/tensorflow/python/saved_model/load.py”, line 448, in _load_nodes slot_variable = optimizer_object.add_slot( AttributeError: ‘_UserObject’ object has no attribute ‘add_slot’

Has anyone been able to fix it?

tttzof351 commented 2 years ago

If you work from ONNX you can do (for inference) something like this:

saved_model = tf.saved_model.load(export_dir="./your_saved_model")
inference = saved_model.signatures["serving_default"]
print(inference)
#ConcreteFunction signature_wrapper(*, input)
#  Args:
#    input: float32 Tensor, shape=(None, 100, 80)
#  Returns:
#   {'output_0': <1>}
#    <1>: float32 Tensor, shape=(None, 3)
tensor = tf.convert_to_tensor(np.zeros((1, 100, 80)).astype(np.float32))

print(inference(input=tensor))

And if you want use it with keras you probably can wrap this concrete function in custom layer

ArthurCbn commented 2 years ago

See this to convert a Pytorch model into a Keras one : I had the exact same issue.

danpprince commented 2 years ago

I had this issue (unable to access the predict() method) with both Tensorflow 2.7 and 2.8 (but not 2.2) when trying to load a SavedModel specifically in a Windows Github Action runner, even though a local Windows environment or an Ubuntu Github Action runner worked fine. I was able to work around the issue by saving as an HDF5 file instead of SavedModel, which works OK for me since I am not using any custom models.

youcef4k commented 2 years ago

I have encountered the same problem with SavedModel models downloaded from TFHUB (example: InceptionV3), even loading it with tf.keras.models.load_model() returns a plain model (a sort of a basic generic model to allow back-compatibility) that does not have keras API (predict, fit, summary, build, etc) on top of it, the object type is: <tensorflow.python.saved_model.load.Loader._recreate_base_user_object.<locals>._UserObject object at 0x14a42ac2bcf8>. If you want to use just the inference call (predict), you can call your model directly on data (__call__ method is defined) as follow:

model(input) # or model.__call__(padded_seq)

One workaround I have found to get the Keras API again is wrapping it inside a KerasLayer in a Sequential model as follow:

import tensorflow as tf
import tensorflow_hub as hub

model = tf.keras.Sequential([
    hub.KerasLayer("saved/model/path")
])
model.build(<input_shape>)

Tensorflow_hub module can be installed simply using: pip install tensorflow_hub Now the model supports all Keras API like predict, summary, etc, and this now should work:

model.predict(input)

MarkDaoust commented 2 years ago

Hi,

There are many problems and solutions on this thread.

Issues should be specific and reproducible.

Many of the issues stem from the fact that there are two flavors of saved-model: "generic" and "keras".

Model.save() will save a "keras" flavored model. tf.keras.models.load_model will load it back as a keras model. Standard keras methods like summary and perdict will all work.

tf.saved_model.save OTOH will save a generic model. tf.saved_model.load will load it back either type, as a generic _UserObject model.

If you load a generic _UserObject model It will only have two sets of methods available:

  1. The tf.function methods that were attached to the model before you saved it
  2. The .signatures that were attached for serving.

See https://www.tensorflow.org/guide/saved_model for more details.

If you have a specific problem (not addressed by my comments above) and code to reproduce it:

google-ml-butler[bot] commented 2 years ago

Are you satisfied with the resolution of your issue? Yes No

sachinkmohan commented 2 years ago

Thanks @MarkDaoust for your valuable insight! I was able to print using the below code finally.

        loaded = tf.saved_model.load("/home/user/git/models_zoo/ssd_mobilenet_v2_320x320_coco17_tpu-8/saved_model/")
        #print(list(loaded.signatures.keys()))  # ["serving_default"]
        infer = loaded.signatures["serving_default"]
        for v in infer.trainable_variables:
            print(v.name)
egolfbr commented 2 years ago

Hi,

There are many problems and solutions on this thread.

Issues should be specific and reproducible.

Many of the issues stem from the fact that there are two flavors of saved-model: "generic" and "keras".

Model.save() will save a "keras" flavored model. tf.keras.models.load_model will load it back as a keras model. Standard keras methods like summary and perdict will all work.

tf.saved_model.save OTOH will save a generic model. tf.saved_model.load will load it back either type, as a generic _UserObject model.

If you load a generic _UserObject model It will only have two sets of methods available:

  1. The tf.function methods that were attached to the model before you saved it
  2. The .signatures that were attached for serving.

See https://www.tensorflow.org/guide/saved_model for more details.

If you have a specific problem (not addressed by my comments above) and code to reproduce it:

I created, trained and saved a model form colab, and saved using model.save() and tried loading it into a VS code python script on my computer and I used the correct function (tf.keras.models.load_model()) and it still gives this error.

MarkDaoust commented 2 years ago

I created, trained and saved a model form colab, and saved using model.save() and tried loading it into a VS code python script on my computer and I used the correct function (tf.keras.models.load_model()) and it still gives this error.

Reloading a saved_model as a keras model requires the python code that defines the internal layers & models. It may be that you defined a custom class in python, built, trained and saved that, and then tried to reload it in an environment without access to that python code. It may just be falling back to "Generic" because it can't rebuild the object.

Does that make sense @egolfbr ?

Jiayuli-CU commented 2 years ago

Got the same error after converting the model to TensorRT format.

Model definition: model = tf.keras.models.Sequential([some layers...])

Convert to TensorRT conversion_params = trt.TrtConversionParams(precision_mode=trt.TrtPrecisionMode.FP32) converter = trt.TrtGraphConverterV2(input_saved_model_dir='raw_model',conversion_params=conversion_params) converter.convert() converter.save('converted_model')

After loading the converted model back with booth tf.keras.models.load_model() and tf.saved_model.load(), and evaluate with model.evaluate(), I got the error AttributeError: '_UserObject' object has no attribute 'evaluate' Any idea about this issue? Thanks

saurabhkumar8112 commented 1 year ago

How come is this still an issue in tf2.12?

anmolkohli18 commented 9 months ago

Got the same issue in tensorflow 2.13 AttributeError: '_UserObject' object has no attribute 'predict'