tensorflow: "ValueError: Layer 'dense' expected 1 input(s). Received 2 instead" on tf.keras.models.load_model
Issue type
Bug
Have you reproduced the bug with TensorFlow Nightly?
No
Source
source
TensorFlow version
v2.16.0-rc0-18-g5bc9d26649c 2.16.1
Custom code
Yes
OS platform and distribution
Windows 11 (KB5035853)
Mobile device
No response
Python version
3.11.8
Bazel version
No response
GCC/compiler version
No response
CUDA/cuDNN version
No response
GPU model and memory
No response
Current behavior?
I want to load a saved model directly after training. But when it executes tf.keras.models.load_model it says ValueError: Layer 'dense' expected 1 input(s). Received 2 instead.
Standalone code to reproduce the issue
base_model = tf.keras.applications.ResNet50(include_top=False, weights='imagenet')
global_average_layer = tf.keras.layers.GlobalAveragePooling2D()
output_layer = tf.keras.layers.Dense(1)
model = tf.keras.Sequential([
base_model,
global_average_layer,
output_layer
])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
history = model.fit(train_dataset,
epochs=epochs,
validation_data=val_dataset)
loss, accuracy = model.evaluate(val_dataset)
# Save the model
model.save('model-v1.keras')
model.summary()
n_model = tf.keras.models.load_model('model-v1.keras')
n_model.summary()
Relevant log output
Model: "sequential"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓
┃ Layer (type) ┃ Output Shape ┃ Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩
│ resnet50 (Functional) │ (None, 12, 12, 2048) │ 23,587,712 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ global_average_pooling2d │ (None, 2048) │ 0 │
│ (GlobalAveragePooling2D) │ │ │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ dense (Dense) │ (None, 1) │ 2,049 │
└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘
Total params: 70,663,045 (269.56 MB)
Trainable params: 23,536,641 (89.79 MB)
Non-trainable params: 53,120 (207.50 KB)
Optimizer params: 47,073,284 (179.57 MB)
Traceback (most recent call last):
File "C:\Users\Aaron-Desktop\OneDrive\Dokumente\Meine Projekte\Deep Learning\Hund erkennung\code\train.py", line 63, in <module>
n_model = tf.keras.models.load_model('model-v1.keras')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Aaron-Desktop\AppData\Roaming\Python\Python311\site-packages\keras\src\saving\saving_api.py", line 176, in load_model
return saving_lib.load_model(
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Aaron-Desktop\AppData\Roaming\Python\Python311\site-packages\keras\src\saving\saving_lib.py", line 155, in load_model
model = deserialize_keras_object(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Aaron-Desktop\AppData\Roaming\Python\Python311\site-packages\keras\src\saving\serialization_lib.py", line 711, in deserialize_keras_object
instance = cls.from_config(inner_config)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Aaron-Desktop\AppData\Roaming\Python\Python311\site-packages\keras\src\models\sequential.py", line 336, in from_config
model.add(layer)
File "C:\Users\Aaron-Desktop\AppData\Roaming\Python\Python311\site-packages\keras\src\models\sequential.py", line 117, in add
self._maybe_rebuild()
File "C:\Users\Aaron-Desktop\AppData\Roaming\Python\Python311\site-packages\keras\src\models\sequential.py", line 136, in _maybe_rebuild
self.build(input_shape)
File "C:\Users\Aaron-Desktop\AppData\Roaming\Python\Python311\site-packages\keras\src\layers\layer.py", line 224, in build_wrapper
original_build_method(*args, **kwargs)
File "C:\Users\Aaron-Desktop\AppData\Roaming\Python\Python311\site-packages\keras\src\models\sequential.py", line 177, in build
x = layer(x)
^^^^^^^^
File "C:\Users\Aaron-Desktop\AppData\Roaming\Python\Python311\site-packages\keras\src\utils\traceback_utils.py", line 123, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\Aaron-Desktop\AppData\Roaming\Python\Python311\site-packages\keras\src\layers\input_spec.py", line 156, in assert_input_compatibility
raise ValueError(
ValueError: Layer 'dense' expected 1 input(s). Received 2 instead.
About this issue
- Original URL
- State: open
- Created 3 months ago
- Comments: 18
I have the exact same error. Although the platform is different (MacBook Pro w/ M3), the TF version is the same (2.16.1). My models are also really similar to the ones described here (CNN Backbone + GlobalAveragePooling2D + Dense). The proposed fix also did not seem to work properly.
Do we have any updates on this issue?
I found a solution to that: You need to define the input_size on loading the model
This Code whould help you:
I am having the shape issue on a Windows 10 machine with TF 2.16.1 and similar model. The proposed solution yields the same ValueError as before.
same here with tf 2.16.1, I’ve also tried tf2.15 with keras 3.0&3.1.1 but with no luck. My trained and saved .keras model (trained with 2.16.1)is not able to load with keras.saving.load_model()
a walkround is to save the trained model with .weights.h5 and build model , load weights(but you need to retrain the model and save weights only which is sometimes not acceptable)
model.save_weights("mobilenetv2_weights_only.weights.h5")then
Have exactly the same issue on Mac. Setting the input shape does’t help
Thanks @91Abdullah. It worked for me!
Version 2.15.0.post1 is working fine for *.keras extensions. The model should also be trained on this version. It’s working fine on 2.15.0.post1
Any update on this issue?
@squallssck I think that we reached the same workaround. The only thing working for now is saving the weights and loading them to the model:
Not the optimal solution, but it works.
Well, it doesn’t seem to work properly. Am I missing something?