tensorflow: KeyError: 'name' when try to load saved model with tf.keras.models.load_model()
Please make sure that this is a bug. As per our GitHub Policy, we only address code/doc bugs, performance issues, feature requests and build/installation issues on GitHub. tag:bug_template
System information
- Have I written custom code (as opposed to using a stock example script provided in TensorFlow):
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Win10
- Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on a mobile device: None
- TensorFlow installed from (source or binary): Binary
- TensorFlow version (use command below): 2.2.0
- Python version: 3.7.7
- Bazel version (if compiling from source):
- GCC/Compiler version (if compiling from source):
- CUDA/cuDNN version: 10.1
- GPU model and memory: NVIDIA GeForce GTX 750 Ti 10GB (2GB Dedicated Memory; 8GB Shared Memory)
Describe the current behavior
I keep getting an KeyError: 'name'
when trying to load a saved model (.pb) with tf.keras.models.load_model(path)
Describe the expected behavior Load the model successfully.
Standalone code to reproduce the issue
Snippet where the error happens.
import tensorflow as tf
# Load model
model = tf.keras.models.load_model("models/bs-32-ep-10-1595106737.3691123/") # Error happens here
# Do stuff
...
Other info / logs
My model is being saved within a class that inherits from tf.keras.models.Sequential()
.
I haven’t got any errors while training or saving my model.
def train(self):
self.compile(
loss=self.loss,
optimizer=self.optimizer(lr=0.0001, decay=1e-6),
metrics=[*self.mtcs]
)
self.fit(
x=self.train_data[0],
y=self.train_data[1],
batch_size=self.batch_size,
epochs=self.epochs,
validation_data=(*self.test_data,),
callbacks=[*self.callbacks]
)
if self.save_after_training:
self.save("models/{}".format(self.NAME)) # Save Model
The Error:
Traceback (most recent call last):
File "RNN.py", line 63, in <module>
model = tf.keras.models.load_model("models/bs-32-ep-10-1595106737.3691123/")
File "C:\Users\EWC\github\GLAI_\venv\lib\site-packages\tensorflow\python\keras\saving\save.py", line 190, in load_model
return saved_model_load.load(filepath, compile)
File "C:\Users\EWC\github\GLAI_\venv\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py", line 116, in load
model = tf_load.load_internal(path, loader_cls=KerasObjectLoader)
File "C:\Users\EWC\github\GLAI_\venv\lib\site-packages\tensorflow\python\saved_model\load.py", line 604, in load_internal
export_dir)
File "C:\Users\EWC\github\GLAI_\venv\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py", line 188, in __init__
super(KerasObjectLoader, self).__init__(*args, **kwargs)
File "C:\Users\EWC\github\GLAI_\venv\lib\site-packages\tensorflow\python\saved_model\load.py", line 123, in __init__
self._load_all()
File "C:\Users\EWC\github\GLAI_\venv\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py", line 215, in _load_all
self._finalize_objects()
File "C:\Users\EWC\github\GLAI_\venv\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py", line 510, in _finalize_objects
self._reconstruct_all_models()
File "C:\Users\EWC\github\GLAI_\venv\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py", line 528, in _reconstruct_all_models
self._reconstruct_model(model_id, model, layers)
File "C:\Users\EWC\github\GLAI_\venv\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py", line 564, in _reconstruct_model
config, created_layers={layer.name: layer for layer in layers})
File "C:\Users\EWC\github\GLAI_\venv\lib\site-packages\tensorflow\python\keras\engine\network.py", line 2019, in reconstruct_from_config
process_layer(layer_data)
File "C:\Users\EWC\github\GLAI_\venv\lib\site-packages\tensorflow\python\keras\engine\network.py", line 1993, in process_layer
layer_name = layer_data['name']
KeyError: 'name'
If more code is required, please let me know. Thank you
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 18 (5 by maintainers)
I went digging and found that when keras tries to reload a model which is a subclass of sequential, it attempts to reload it as a functional model instead of a sequential model.
Digging into the load/save code I found this - which seems to be perhaps a proximal cause.
An subclass of sequential will not have the class name
Sequential
which forces saving and loading as a Functional model. I’m not sure why this causes the ‘name’ KeyError, but at the very least there is a work around which is to avoid sub-classingkeras.models.Sequential
Hi @rickstaa ! This issue replicated in 2.3 even after saving the loading of the model. The same pattern was not observed in 2.8.
Anyway , I removed the self.save_after_training condition from below snippet in the train block and added a print line to show the model path name too. I was able to run without error then. (Refresh from folder in Colab once before you load the model)
Attaching fresh gist for reference.
Please raise a fresh ticket in Keras repo if you are still looking for assistance. Thanks!
My experience with
KeyError: 'name'
withtf.keras.models.load_model()
was because of theget_config()
implementation of a custom layer that did not include the base config from the parent (tested with tensorflow 2.2.0).More details: https://github.com/tensorflow/models/issues/8692#issuecomment-727033061
Was able to reproduce the error with tf 2.3.0 and tf-nightly-gpu. Heres the gist for tf 2.3 and tf-nightly
I have saved