tensorflow: Convert savedmodel to h5 Keras format - AttributeError: 'AutoTrackable' object has no attribute 'summary'

System information

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Windows 10
  • TensorFlow version (use command below): 2.3.0
  • Python version: 3.6.1

Describe the current behavior I used Google AI-Platform to train a model for object detection. The result is a model in the savedmodel format. I wan’t to convert it to h5 Keras format so I can import into Matlab.

Standalone code to reproduce the issue Here is the output folder directly from the bucket:

https://drive.google.com/file/d/1mgVGG_WNGC9gxZSgn1G2_YdQTVkJJInM/view?usp=sharing

Other info / logs

I tried to do this:

New_Model = load_model('model'), or this New_Model = tf.keras.models.load_model('model') or this New_Model = tf.saved_model.load(export_dir='model') and lastly this New_Model.summary() and I always get the same error:

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

If the model would had loaded correctly I would try to do tf.keras.models.save_model(New_Model, 'New_Model.h5')

Which will end up throwing this error:

AttributeError: 'AutoTrackable' object has no attribute '_is_graph_network'

Please help, as I said the model is saved in the savedmodel format, so I guess every piece needed is there

Complete log for summary:

2020-08-17 01:48:46.011995: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2020-08-17 01:48:46.016769: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2020-08-17 01:48:53.754437: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found
2020-08-17 01:48:53.758821: W tensorflow/stream_executor/cuda/cuda_driver.cc:312] failed call to cuInit: UNKNOWN ERROR (303)
2020-08-17 01:48:53.780897: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: EC2347417W3
2020-08-17 01:48:53.785702: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: EC2347417W3
2020-08-17 01:48:53.789520: 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
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2020-08-17 01:48:53.820077: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x16799c381d0 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-08-17 01:48:53.832027: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
WARNING:tensorflow:Unable to create a python object for variable <tf.Variable 'resnet50/conv2d/kernel:0' shape=(7, 7, 3, 64) dtype=float32_ref> because it is a reference variable. It may not be visible to training APIs. If this is a problem, consider rebuilding the SavedModel after running tf.compat.v1.enable_resource_variables().
WARNING:tensorflow:Unable to create a python object for variable <tf.Variable 'resnet50/batch_normalization/gamma:0' shape=(64,) dtype=float32_ref> because it is a reference variable. It may not be visible to training APIs. If this is a problem, consider rebuilding the SavedModel after running tf.compat.v1.enable_resource_variables().
WARNING:tensorflow:Unable to create a python object for variable <tf.Variable 'resnet50/batch_normalization/beta:0' shape=(64,) dtype=float32_ref> because it is a reference variable. It may not be visible to training APIs. If this is a problem, consider rebuilding the SavedModel after running tf.compat.v1.enable_resource_variables().
WARNING:tensorflow:Unable to create a python object for variable <tf.Variable 'resnet50/batch_normalization/moving_mean:0' shape=(64,) dtype=float32_ref> because it is a reference variable. It may not be visible to training APIs. If this is a problem, consider rebuilding the SavedModel after running tf.compat.v1.enable_resource_variables().
WARNING:tensorflow:Unable to create a python object for variable <tf.Variable 'resnet50/batch_normalization/moving_variance:0' shape=(64,) dtype=float32_ref> because it is a reference variable. It may not be visible to training APIs. If this is a problem, consider rebuilding the SavedModel after running tf.compat.v1.enable_resource_variables().
WARNING:tensorflow:Unable to create a python object for variable <tf.Variable 'resnet50/conv2d/kernel:0' shape=(7, 7, 3, 64) dtype=float32_ref> because it is a reference variable. It may not be visible to training APIs. If this is a problem, consider rebuilding the SavedModel after running tf.compat.v1.enable_resource_variables().
WARNING:tensorflow:Unable to create a python object for variable <tf.Variable 'resnet50/batch_normalization/gamma:0' shape=(64,) dtype=float32_ref> because it is a reference variable. It may not be visible to training APIs. If this is a problem, consider rebuilding the SavedModel after running tf.compat.v1.enable_resource_variables().
WARNING:tensorflow:Unable to create a python object for variable <tf.Variable 'resnet50/batch_normalization/beta:0' shape=(64,) dtype=float32_ref> because it is a reference variable. It may not be visible to training APIs. If this is a problem, consider rebuilding the SavedModel after running tf.compat.v1.enable_resource_variables().
WARNING:tensorflow:Unable to create a python object for variable <tf.Variable 'resnet50/batch_normalization/moving_mean:0' shape=(64,) dtype=float32_ref> because it is a reference variable. It may not be visible to training APIs. If this is a problem, consider rebuilding the SavedModel after running tf.compat.v1.enable_resource_variables().
WARNING:tensorflow:Unable to create a python object for variable <tf.Variable 'resnet50/batch_normalization/moving_variance:0' shape=(64,) dtype=float32_ref> because it is a reference variable. It may not be visible to training APIs. If this is a problem, consider rebuilding the SavedModel after running tf.compat.v1.enable_resource_variables().
WARNING:tensorflow:Unable to create a python object for variable <tf.Variable 'resnet50/conv2d/kernel:0' shape=(7, 7, 3, 64) dtype=float32_ref> because it is a reference variable. It may not be visible to training APIs. If this is a problem, consider rebuilding the SavedModel after running tf.compat.v1.enable_resource_variables().
WARNING:tensorflow:Unable to create a python object for variable <tf.Variable 'resnet50/batch_normalization/gamma:0' shape=(64,) dtype=float32_ref> because it is a reference variable. It may not be visible to training APIs. If this is a problem, consider rebuilding the SavedModel after running tf.compat.v1.enable_resource_variables().
WARNING:tensorflow:Unable to create a python object for variable <tf.Variable 'resnet50/batch_normalization/beta:0' shape=(64,) dtype=float32_ref> because it is a reference variable. It may not be visible to training APIs. If this is a problem, consider rebuilding the SavedModel after running tf.compat.v1.enable_resource_variables().
WARNING:tensorflow:Unable to create a python object for variable <tf.Variable 'resnet50/batch_normalization/moving_mean:0' shape=(64,) dtype=float32_ref> because it is a reference variable. It may not be visible to training APIs. If this is a problem, consider rebuilding the SavedModel after running tf.compat.v1.enable_resource_variables().
WARNING:tensorflow:Unable to create a python object for variable <tf.Variable 'resnet50/batch_normalization/moving_variance:0' shape=(64,) dtype=float32_ref> because it is a reference variable. It may not be visible to training APIs. If this is a problem, consider rebuilding the SavedModel after running tf.compat.v1.enable_resource_variables().
WARNING:tensorflow:Unable to create a python object for variable <tf.Variable 'resnet50/conv2d/kernel:0' shape=(7, 7, 3, 64) dtype=float32_ref> because it is a reference variable. It may not be visible to training APIs. If this is a problem, consider rebuilding the SavedModel after running tf.compat.v1.enable_resource_variables().
WARNING:tensorflow:Unable to create a python object for variable <tf.Variable 'resnet50/batch_normalization/gamma:0' shape=(64,) dtype=float32_ref> because it is a reference variable. It may not be visible to training APIs. If this is a problem, consider rebuilding the SavedModel after running tf.compat.v1.enable_resource_variables().
WARNING:tensorflow:Unable to create a python object for variable <tf.Variable 'resnet50/batch_normalization/beta:0' shape=(64,) dtype=float32_ref> because it is a reference variable. It may not be visible to training APIs. If this is a problem, consider rebuilding the SavedModel after running tf.compat.v1.enable_resource_variables().
WARNING:tensorflow:Unable to create a python object for variable <tf.Variable 'resnet50/batch_normalization/moving_mean:0' shape=(64,) dtype=float32_ref> because it is a reference variable. It may not be visible to training APIs. If this is a problem, consider rebuilding the SavedModel after running tf.compat.v1.enable_resource_variables().
WARNING:tensorflow:Unable to create a python object for variable <tf.Variable 'resnet50/batch_normalization/moving_variance:0' shape=(64,) dtype=float32_ref> because it is a reference variable. It may not be visible to training APIs. If this is a problem, consider rebuilding the SavedModel after running tf.compat.v1.enable_resource_variables().
Traceback (most recent call last):
  File "savedmodel_to_h5.py", line 15, in <module>
    New_Model.summary()
AttributeError: 'AutoTrackable' object has no attribute 'summary'

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 1
  • Comments: 38 (13 by maintainers)

Most upvoted comments

It’s not the time to close issue.

There are many users faced the same problem crossing various versions.

tf.keras.models.load_model only works with SavedModels saved from Keras. I’m guessing that the SavedModel is saved from another source that is not Keras.

If you want to convert a SavedModel to Keras, you could try something like:

loaded = tf.saved_model.load(path)

class LayerFromSavedModel(tf.keras.layers.Layer):
  def __init__(self):
    super(LayerFromSavedModel, self).__init__()
    self.vars = loaded.variables
  def call(self, inputs):
    return loaded.signatures['serving_default'](inputs)

input = tf.keras.Input(...)
model = tf.keras.Model(input, LayerFromSavedModel()(input))
model.save('saved_model')

Hey, any updates on this issue ? I’ve been dealing with the same problem.

@jvishnuvardhan I did run this:

import os
import tensorflow as tf
from tensorflow.keras.preprocessing import image

loaded = tf.saved_model.load(r"C:\Users\my_user\Documents\out\2\OUTPUT\model")

class LayerFromSavedModel(tf.keras.layers.Layer):
  def __init__(self):
    super(LayerFromSavedModel, self).__init__()
    self.vars = loaded.variables
  def call(self, inputs):
    return loaded.signatures['serving_default'](inputs)

input = tf.keras.Input(...)
model = tf.keras.Model(input, LayerFromSavedModel()(input))
model.save('saved_model')

Where “C:\Users\my_user\Documents\out\2\OUTPUT\model” contains:

image

OUTPUT contains:

image

@dfvr1994 What was the input for tf.keras.Input in your code? Can you share a standalone code to reproduce the issue? Thanks!

Same issue here executing this code:

import tensorflow as tf

class LayerFromSavedModel(tf.keras.layers.Layer):
    def __init__(self):
        super(LayerFromSavedModel, self).__init__()
        self.vars = loaded.variables
    def call(self, inputs):
        return loaded.signatures['serving_default'](inputs)

path = r'path/to/model/folder/'
loaded = tf.saved_model.loader.load(path)

input = tf.keras.Input(...)
model = tf.keras.Model(input, LayerFromSavedModel()(input))

The problem seems to be with the line input = tf.keras.Input(...) leading to

c:\Users\user\Documents\python_codebase_postdoc\MISC\DeepZ\load_model_tf.py in <module>
----> [18](file:///c%3A/Users/user/Documents/python_codebase_postdoc/MISC/DeepZ/load_model_tf.py?line=17) input = tf.keras.Input(...)

c:\Users\user\anaconda3\envs\tf1.7.0\lib\site-packages\tensorflow\python\keras\_impl\keras\engine\input_layer.py in Input(shape, batch_size, name, dtype, sparse, tensor, **kwargs)
    221       dtype=dtype,
    222       sparse=sparse,
--> 223       input_tensor=tensor)
    224   # Return tensor including `_keras_history`.
    225   # Note that in this case train_output and test_output are the same pointer.

c:\Usersuser\anaconda3\envs\tf1.7.0\lib\site-packages\tensorflow\python\keras\_impl\keras\engine\input_layer.py in __init__(self, input_shape, batch_size, dtype, input_tensor, sparse, name, **kwargs)
     89     if input_tensor is None:
     90       if input_shape is not None:
---> 91         batch_input_shape = (batch_size,) + tuple(input_shape)
     92       else:
     93         batch_input_shape = None

TypeError: 'ellipsis' object is not iterable

This error was replicated in both tensorflow 1.7.1 and 2.9.1

Any idea how to resolve this? Thanks!

@LRpz The … is meant to be replaced by the actual input shape, e.g. tf.keras.Input((3,)) for a batch shape of [None, 3]

I’m afraid not. @dfvr1994