"ValueError: Layer 'dense' expected 1 input(s). Received 2 instead" on tf.keras.models.load_model
Hi **@AaronEggert** ,
Sorry for the delay, This error typically occurs when the input shape of the loaded model does not match the expected input shape of the layers.
Here i am providing [gist](https://colab.sandbox.google.com/gist/Venkat6871/7bef42319504f21bd20690f05dbebaac/63853_2-16-1.ipynb) with updated code for your reference. Please go through it once.

Thank you!
 Thank you for your help. 
When I define the input size while loading the model, I get the same Error.

input_shape = (224, 224, 3)
model = tf.keras.models.load_model('model-v1.keras', compile=False, custom_objects={'input_shape': input_shape})

Traceback (most recent call last):
  File "C:\Users\Aaron-Desktop\OneDrive\Dokumente\Meine Projekte\Deep Learning\Hund erkennung\code\train.py", line 64, in <module>
    n_model = tf.keras.models.load_model('model-v1.keras', compile=False, custom_objects={'input_shape': input_shape})
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  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. 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:
```
input_shape = (224, 224, 3)
model = tf.keras.models.load_model('model.keras', compile=False, custom_objects={'input_shape': input_shape})
``` Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/63853">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/63853">No</a>
 Well, it doesn't seem to work properly. Am I missing something?

```
>>> tf.keras.models.load_model('model.keras', compile=False, custom_objects={'input_shape': (48, 48, 3)})
...
ValueError: Layer 'dense' expected 1 input(s). Received 2 instead.
``` 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.  Have exactly the same issue on Mac. Setting the input shape does't help 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()


```
base_model = keras.applications.MobileNetV2(input_shape=(224,224,3),include_top=False,weights="imagenet")
model = keras.Sequential([
    base_model,
    keras.layers.GlobalAveragePooling2D(),
    keras.layers.Dropout(rate=0.5),
    keras.layers.Dense(8,activation='softmax'),
])

model.compile(...)
model.fit(...)

model.summary() #OK

model.save("test_mobilenetv2_save.keras")

model_2 = keras.saving.load_model("test_mobilenetv2_save.keras") #Error
model_2 = keras.saving.load_model("test_mobilenetv2_save.keras",compile=False, custom_objects={'input_shape': input_shape}) #Error

model_2.summary() 
```


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
```

base_model2 = keras.applications.MobileNetV2(input_shape=(224,224,3),include_top=False,weights="imagenet")

base_model2.trainable = True

model2 = keras.Sequential([
    base_model2,
    keras.layers.GlobalAveragePooling2D(),
    keras.layers.Dropout(rate=0.5),
    keras.layers.Dense(8,activation='softmax'),
])

model2.compile(
    loss=loss,
    # optimizer=keras.optimizers.SGD(learning_rate=0.01),
    optimizer = optimizer,
    metrics=[
        metrics.CategoricalAccuracy(),
        metrics.TopKCategoricalAccuracy(k=3),
    ]
)

model2.summary()

model2.predict(np.ones((32,224,224,3))) #must build model first or you not able to load weights

model2.load_weights("mobilenetv2_weights_only.weights.h5", skip_mismatch=False)
``` @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:

```
model = build_model()
model.build(input_shape=(None, *INPUT_SHAPE))
model.load_weights('model.weights.h5')
```

Not the optimal solution, but it works. Any update on this issue? есть ли фид бек по тому когда эта ошибка появилась , потому что раньше все работало отлично и никаких проблем не наблюдалось 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 Thanks @91Abdullah. It worked for me! I am also having the shape issue on a Ubuntu 20.4 machine with TF 2.16.1 
this how i trained my model, here is when i load my model happens
```
import pathlib
import pickle
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
from keras.preprocessing.image import ImageDataGenerator

'''
import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))
'''
format_1 = '/kaggle/input/flower-dataset/train'
format_2 = '/kaggle/input/flower-dataset/valid'
train_dir = pathlib.Path(format_1)
valid_dir = pathlib.Path(format_2)
train_count = len(list(train_dir.glob('*/*.jpg')))
valid_count = len(list(valid_dir.glob('*/*.jpg')))

print(train_count)
print(valid_count)

CLASS_NAMES = np.array([item.name for item in train_dir.glob('*') if item.is_dir()])

image_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1. / 255)
BATCH_SIZE = 32
IMG_HEIGHT = 224
IMG_WIDTH = 224
train_data_gen = image_generator.flow_from_directory(directory=str(train_dir),
                                                     batch_size=BATCH_SIZE,
                                                     target_size=(IMG_HEIGHT, IMG_WIDTH),
                                                     shuffle=True,
                                                     classes=list(CLASS_NAMES))
valid_data_gen = image_generator.flow_from_directory(directory=str(valid_dir),
                                                     batch_size=BATCH_SIZE,
                                                     target_size=(IMG_HEIGHT, IMG_WIDTH),
                                                     shuffle=True,
                                                     classes=list(CLASS_NAMES))
print(CLASS_NAMES)
ResNet50 = tf.keras.applications.resnet_v2.ResNet50V2(include_top=False, weights='imagenet', input_shape=(224, 224, 3))
ResNet50.trainable = True
my_net = tf.keras.models.Sequential()
my_net.add(ResNet50)
my_net.add(tf.keras.layers.GlobalAveragePooling2D())
my_net.add(tf.keras.layers.Dense(100, activation='softmax'))
my_net.summary()
my_net.compile(optimizer=tf.keras.optimizers.Adamax(0.001),
            loss='categorical_crossentropy',
            metrics=['accuracy'])
epoch_steps = train_count // BATCH_SIZE
val_steps = valid_count // BATCH_SIZE
my_net.fit_generator(
    train_data_gen,
    steps_per_epoch=epoch_steps,
    epochs=5,
    validation_data=valid_data_gen,
    validation_steps=val_steps
)
my_net.save('flower.h5')
```
here is error
```
Traceback (most recent call last):
  File "/root/keras-flask-deploy-webapp/app.py", line 42, in <module>
    model = tf.keras.models.load_model(MODEL_PATH, compile=False, custom_objects={'input_shape': input_shape})
  File "/usr/local/lib/python3.10/dist-packages/keras/src/saving/saving_api.py", line 183, in load_model
    return legacy_h5_format.load_model_from_hdf5(filepath)
  File "/usr/local/lib/python3.10/dist-packages/keras/src/legacy/saving/legacy_h5_format.py", line 133, in load_model_from_hdf5
    model = saving_utils.model_from_config(
  File "/usr/local/lib/python3.10/dist-packages/keras/src/legacy/saving/saving_utils.py", line 85, in model_from_config
    return serialization.deserialize_keras_object(
  File "/usr/local/lib/python3.10/dist-packages/keras/src/legacy/saving/serialization.py", line 495, in deserialize_keras_object
    deserialized_obj = cls.from_config(
  File "/usr/local/lib/python3.10/dist-packages/keras/src/models/sequential.py", line 335, in from_config
    model.add(layer)
  File "/usr/local/lib/python3.10/dist-packages/keras/src/models/sequential.py", line 116, in add
    self._maybe_rebuild()
  File "/usr/local/lib/python3.10/dist-packages/keras/src/models/sequential.py", line 135, in _maybe_rebuild
    self.build(input_shape)
  File "/usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py", line 223, in build_wrapper
    original_build_method(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/keras/src/models/sequential.py", line 176, in build
    x = layer(x)
  File "/usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 122, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/usr/local/lib/python3.10/dist-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.
``` Also having the same issue. Windows 10 machine with TF 2.16.1. Here is how I trained my model:
```
import tensorflow as tf
from tensorflow.keras.applications import EfficientNetV2S

V2S_model = EfficientNetV2S(weights='imagenet',
                        include_top=False,
                        input_shape=(224, 224, 3))

for layer in V2S_model.layers:
  layer.trainable = False
from tensorflow.keras import layers

image_preprocess = tf.keras.Sequential([
    tf.keras.Input((None,None,3)),
    layers.Resizing(224,224, crop_to_aspect_ratio = True),
    layers.RandomFlip("horizontal_and_vertical"),
    layers.RandomRotation(0.2)
], name = "image_aug")

transfer_model = tf.keras.Sequential([
    tf.keras.Input((None,None,3)),
    image_preprocess,
    V2S_model,
    layers.GlobalAveragePooling2D(),
    layers.Dense(512, activation='relu'),
    layers.Dropout(0.2),
    layers.Dense(1, activation = 'sigmoid')
])

transfer_model.summary()

metrics = [tf.keras.metrics.BinaryAccuracy(threshold=0.5, name='accuracy'),
           tf.keras.metrics.AUC(),
           tf.keras.metrics.TruePositives(),
           tf.keras.metrics.TrueNegatives(),
           tf.keras.metrics.FalsePositives(),
           tf.keras.metrics.FalseNegatives()
          ]

transfer_model.compile(loss=tf.keras.losses.BinaryCrossentropy(),
                       optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
                       metrics=metrics)

BATCH_SIZE = 16
IMAGE_SIZE = (224,224)
SEED = 42

# This sets up a training and validation set from our ../data/ directory
train_dataset = tf.keras.utils.image_dataset_from_directory(
    '../data/',
    class_names = ['not_niblet','niblet'],
    color_mode='rgb',
    batch_size=BATCH_SIZE,
    image_size=IMAGE_SIZE,
    shuffle=True,
    validation_split=0.2,
    subset='training',
    seed=SEED)

# This is the validation set. Notice `shuffle = FALSE` and `subset = validation`
val_dataset = tf.keras.utils.image_dataset_from_directory(
    '../data/',
    class_names = ['not_niblet','niblet'],
    color_mode='rgb',
    batch_size=BATCH_SIZE,
    image_size=IMAGE_SIZE,
    shuffle=True,
    validation_split=0.2,
    subset='validation',
    seed=SEED)

from tensorflow.keras.callbacks import EarlyStopping

es = EarlyStopping(patience=15, monitor='val_loss')

history = transfer_model.fit(train_dataset, epochs=100,
                             validation_data=val_dataset,
                             callbacks=[es])

transfer_model.save('../models/transfer_model_gt_2024_04_23.keras')

transfer_model_loaded = tf.keras.models.load_model('../models/transfer_model_gt_2024_04_23.keras')
```

and here is my error:

```
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[16], line 2
      1 input_shape = (None,None,3)
----> 2 transfer_model_loaded = tf.keras.models.load_model('../models/transfer_model_gt_2024_04_23.keras', compile=False, custom_objects={'input_shape': input_shape})

File [~\anaconda3\envs\CS380_final\Lib\site-packages\keras\src\saving\saving_api.py:176](http://localhost:8888/lab/tree/Documents/GitHub/CSC380/scripts/~/anaconda3/envs/CS380_final/Lib/site-packages/keras/src/saving/saving_api.py#line=175), in load_model(filepath, custom_objects, compile, safe_mode)
    173         is_keras_zip = True
    175 if is_keras_zip:
--> 176     return saving_lib.load_model(
    177         filepath,
    178         custom_objects=custom_objects,
    179         compile=compile,
    180         safe_mode=safe_mode,
    181     )
    182 if str(filepath).endswith((".h5", ".hdf5")):
    183     return legacy_h5_format.load_model_from_hdf5(filepath)

File [~\anaconda3\envs\CS380_final\Lib\site-packages\keras\src\saving\saving_lib.py:152](http://localhost:8888/lab/tree/Documents/GitHub/CSC380/scripts/~/anaconda3/envs/CS380_final/Lib/site-packages/keras/src/saving/saving_lib.py#line=151), in load_model(filepath, custom_objects, compile, safe_mode)
    147     raise ValueError(
    148         "Invalid filename: expected a `.keras` extension. "
    149         f"Received: filepath={filepath}"
    150     )
    151 with open(filepath, "rb") as f:
--> 152     return _load_model_from_fileobj(
    153         f, custom_objects, compile, safe_mode
    154     )

File [~\anaconda3\envs\CS380_final\Lib\site-packages\keras\src\saving\saving_lib.py:170](http://localhost:8888/lab/tree/Documents/GitHub/CSC380/scripts/~/anaconda3/envs/CS380_final/Lib/site-packages/keras/src/saving/saving_lib.py#line=169), in _load_model_from_fileobj(fileobj, custom_objects, compile, safe_mode)
    168 # Construct the model from the configuration file in the archive.
    169 with ObjectSharingScope():
--> 170     model = deserialize_keras_object(
    171         config_dict, custom_objects, safe_mode=safe_mode
    172     )
    174 all_filenames = zf.namelist()
    175 if _VARS_FNAME + ".h5" in all_filenames:

File [~\anaconda3\envs\CS380_final\Lib\site-packages\keras\src\saving\serialization_lib.py:711](http://localhost:8888/lab/tree/Documents/GitHub/CSC380/scripts/~/anaconda3/envs/CS380_final/Lib/site-packages/keras/src/saving/serialization_lib.py#line=710), in deserialize_keras_object(config, custom_objects, safe_mode, **kwargs)
    709 with custom_obj_scope, safe_mode_scope:
    710     try:
--> 711         instance = cls.from_config(inner_config)
    712     except TypeError as e:
    713         raise TypeError(
    714             f"{cls} could not be deserialized properly. Please"
    715             " ensure that components that are Python object"
   (...)
    719             f"\n\nconfig={config}[.\n\nException](http://localhost:8888/lab/tree/Documents/GitHub/CSC380/scripts/n/nException) encountered: {e}"
    720         )

File [~\anaconda3\envs\CS380_final\Lib\site-packages\keras\src\models\sequential.py:339](http://localhost:8888/lab/tree/Documents/GitHub/CSC380/scripts/~/anaconda3/envs/CS380_final/Lib/site-packages/keras/src/models/sequential.py#line=338), in Sequential.from_config(cls, config, custom_objects)
    334     else:
    335         layer = serialization_lib.deserialize_keras_object(
    336             layer_config,
    337             custom_objects=custom_objects,
    338         )
--> 339     model.add(layer)
    340 if (
    341     not model._functional
    342     and build_input_shape
    343     and isinstance(build_input_shape, (tuple, list))
    344 ):
    345     model.build(build_input_shape)

File [~\anaconda3\envs\CS380_final\Lib\site-packages\keras\src\models\sequential.py:120](http://localhost:8888/lab/tree/Documents/GitHub/CSC380/scripts/~/anaconda3/envs/CS380_final/Lib/site-packages/keras/src/models/sequential.py#line=119), in Sequential.add(self, layer, rebuild)
    118 self._layers.append(layer)
    119 if rebuild:
--> 120     self._maybe_rebuild()
    121 else:
    122     self.built = False

File [~\anaconda3\envs\CS380_final\Lib\site-packages\keras\src\models\sequential.py:139](http://localhost:8888/lab/tree/Documents/GitHub/CSC380/scripts/~/anaconda3/envs/CS380_final/Lib/site-packages/keras/src/models/sequential.py#line=138), in Sequential._maybe_rebuild(self)
    137 if isinstance(self._layers[0], InputLayer) and len(self._layers) > 1:
    138     input_shape = self._layers[0].batch_shape
--> 139     self.build(input_shape)

File [~\anaconda3\envs\CS380_final\Lib\site-packages\keras\src\layers\layer.py:222](http://localhost:8888/lab/tree/Documents/GitHub/CSC380/scripts/~/anaconda3/envs/CS380_final/Lib/site-packages/keras/src/layers/layer.py#line=221), in Layer.__new__.<locals>.build_wrapper(*args, **kwargs)
    219 @wraps(original_build_method)
    220 def build_wrapper(*args, **kwargs):
    221     with obj._open_name_scope():
--> 222         original_build_method(*args, **kwargs)
    223     # Record build config.
    224     signature = inspect.signature(original_build_method)

File [~\anaconda3\envs\CS380_final\Lib\site-packages\keras\src\models\sequential.py:180](http://localhost:8888/lab/tree/Documents/GitHub/CSC380/scripts/~/anaconda3/envs/CS380_final/Lib/site-packages/keras/src/models/sequential.py#line=179), in Sequential.build(self, input_shape)
    178 for layer in self._layers[1:]:
    179     try:
--> 180         x = layer(x)
    181     except NotImplementedError:
    182         # Can happen if shape inference is not implemented.
    183         # TODO: consider reverting inbound nodes on layers processed.
    184         return

File [~\anaconda3\envs\CS380_final\Lib\site-packages\keras\src\utils\traceback_utils.py:122](http://localhost:8888/lab/tree/Documents/GitHub/CSC380/scripts/~/anaconda3/envs/CS380_final/Lib/site-packages/keras/src/utils/traceback_utils.py#line=121), in filter_traceback.<locals>.error_handler(*args, **kwargs)
    119     filtered_tb = _process_traceback_frames(e.__traceback__)
    120     # To get the full stack trace, call:
    121     # `keras.config.disable_traceback_filtering()`
--> 122     raise e.with_traceback(filtered_tb) from None
    123 finally:
    124     del filtered_tb

File [~\anaconda3\envs\CS380_final\Lib\site-packages\keras\src\layers\input_spec.py:156](http://localhost:8888/lab/tree/Documents/GitHub/CSC380/scripts/~/anaconda3/envs/CS380_final/Lib/site-packages/keras/src/layers/input_spec.py#line=155), in assert_input_compatibility(input_spec, inputs, layer_name)
    154 inputs = tree.flatten(inputs)
    155 if len(input_spec) != len(inputs):
--> 156     raise ValueError(
    157         f"Layer '{layer_name}' expected {len(input_spec)} input(s). "
    158         f"Received {len(inputs)} instead."
    159     )
    160 for x in inputs:
    161     # Having a shape/dtype is the only commonality of the various
    162     # tensor-like objects that may be passed. The most common kind of
    163     # invalid type we are guarding for is a Layer instance (Functional API),
    164     # which does not have a `shape` attribute.
    165     if not hasattr(x, "shape"):

ValueError: Layer 'dense' expected 1 input(s). Received 2 instead.
``` @gmtanner-cord try to use version 2.15 it should work as expected.  i have solved this problem by lower my tensorflow version to 2.12.0, maybe u can have a try




------------------&nbsp;原始邮件&nbsp;------------------
发件人: ***@***.***&gt;; 
发送时间: 2024年4月24日(星期三) 上午10:10
收件人: ***@***.***&gt;; 
抄送: ***@***.***&gt;; ***@***.***&gt;; 
主题: Re: [tensorflow/tensorflow] &quot;ValueError: Layer &#39;dense&#39; expected 1 input(s). Received 2 instead&quot; on tf.keras.models.load_model (Issue #63853)














 
Also having the same issue. Windows 10 machine with TF 2.16.1. Here is how I trained my model:
 import tensorflow as tf from tensorflow.keras.applications import EfficientNetV2S V2S_model = EfficientNetV2S(weights='imagenet',                         include_top=False,                         input_shape=(224, 224, 3)) for layer in V2S_model.layers:   layer.trainable = False from tensorflow.keras import layers image_preprocess = tf.keras.Sequential([     tf.keras.Input((None,None,3)),     layers.Resizing(224,224, crop_to_aspect_ratio = True),     layers.RandomFlip("horizontal_and_vertical"),     layers.RandomRotation(0.2) ], name = "image_aug") transfer_model = tf.keras.Sequential([     tf.keras.Input((None,None,3)),     image_preprocess,     V2S_model,     layers.GlobalAveragePooling2D(),     layers.Dense(512, activation='relu'),     layers.Dropout(0.2),     layers.Dense(1, activation = 'sigmoid') ]) transfer_model.summary() metrics = [tf.keras.metrics.BinaryAccuracy(threshold=0.5, name='accuracy'),            tf.keras.metrics.AUC(),            tf.keras.metrics.TruePositives(),            tf.keras.metrics.TrueNegatives(),            tf.keras.metrics.FalsePositives(),            tf.keras.metrics.FalseNegatives()           ] transfer_model.compile(loss=tf.keras.losses.BinaryCrossentropy(),                        optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),                        metrics=metrics) BATCH_SIZE = 16 IMAGE_SIZE = (224,224) SEED = 42 # This sets up a training and validation set from our ../data/ directory train_dataset = tf.keras.utils.image_dataset_from_directory(     '../data/',     class_names = ['not_niblet','niblet'],     color_mode='rgb',     batch_size=BATCH_SIZE,     image_size=IMAGE_SIZE,     shuffle=True,     validation_split=0.2,     subset='training',     seed=SEED) # This is the validation set. Notice `shuffle = FALSE` and `subset = validation` val_dataset = tf.keras.utils.image_dataset_from_directory(     '../data/',     class_names = ['not_niblet','niblet'],     color_mode='rgb',     batch_size=BATCH_SIZE,     image_size=IMAGE_SIZE,     shuffle=True,     validation_split=0.2,     subset='validation',     seed=SEED) from tensorflow.keras.callbacks import EarlyStopping es = EarlyStopping(patience=15, monitor='val_loss') history = transfer_model.fit(train_dataset, epochs=100,                              validation_data=val_dataset,                              callbacks=[es]) transfer_model.save('../models/transfer_model_gt_2024_04_23.keras') transfer_model_loaded = tf.keras.models.load_model('../models/transfer_model_gt_2024_04_23.keras')  
and here is my error:
 --------------------------------------------------------------------------- ValueError                                Traceback (most recent call last) Cell In[16], line 2       1 input_shape = (None,None,3) ----&gt; 2 transfer_model_loaded = tf.keras.models.load_model('../models/transfer_model_gt_2024_04_23.keras', compile=False, custom_objects={'input_shape': input_shape}) File [~\anaconda3\envs\CS380_final\Lib\site-packages\keras\src\saving\saving_api.py:176](http://localhost:8888/lab/tree/Documents/GitHub/CSC380/scripts/~/anaconda3/envs/CS380_final/Lib/site-packages/keras/src/saving/saving_api.py#line=175), in load_model(filepath, custom_objects, compile, safe_mode)     173         is_keras_zip = True     175 if is_keras_zip: --&gt; 176     return saving_lib.load_model(     177         filepath,     178         custom_objects=custom_objects,     179         compile=compile,     180         safe_mode=safe_mode,     181     )     182 if str(filepath).endswith((".h5", ".hdf5")):     183     return legacy_h5_format.load_model_from_hdf5(filepath) File [~\anaconda3\envs\CS380_final\Lib\site-packages\keras\src\saving\saving_lib.py:152](http://localhost:8888/lab/tree/Documents/GitHub/CSC380/scripts/~/anaconda3/envs/CS380_final/Lib/site-packages/keras/src/saving/saving_lib.py#line=151), in load_model(filepath, custom_objects, compile, safe_mode)     147     raise ValueError(     148         "Invalid filename: expected a `.keras` extension. "     149         f"Received: filepath={filepath}"     150     )     151 with open(filepath, "rb") as f: --&gt; 152     return _load_model_from_fileobj(     153         f, custom_objects, compile, safe_mode     154     ) File [~\anaconda3\envs\CS380_final\Lib\site-packages\keras\src\saving\saving_lib.py:170](http://localhost:8888/lab/tree/Documents/GitHub/CSC380/scripts/~/anaconda3/envs/CS380_final/Lib/site-packages/keras/src/saving/saving_lib.py#line=169), in _load_model_from_fileobj(fileobj, custom_objects, compile, safe_mode)     168 # Construct the model from the configuration file in the archive.     169 with ObjectSharingScope(): --&gt; 170     model = deserialize_keras_object(     171         config_dict, custom_objects, safe_mode=safe_mode     172     )     174 all_filenames = zf.namelist()     175 if _VARS_FNAME + ".h5" in all_filenames: File [~\anaconda3\envs\CS380_final\Lib\site-packages\keras\src\saving\serialization_lib.py:711](http://localhost:8888/lab/tree/Documents/GitHub/CSC380/scripts/~/anaconda3/envs/CS380_final/Lib/site-packages/keras/src/saving/serialization_lib.py#line=710), in deserialize_keras_object(config, custom_objects, safe_mode, **kwargs)     709 with custom_obj_scope, safe_mode_scope:     710     try: --&gt; 711         instance = cls.from_config(inner_config)     712     except TypeError as e:     713         raise TypeError(     714             f"{cls} could not be deserialized properly. Please"     715             " ensure that components that are Python object"    (...)     719             f"\n\nconfig={config}[.\n\nException](http://localhost:8888/lab/tree/Documents/GitHub/CSC380/scripts/n/nException) encountered: {e}"     720         ) File [~\anaconda3\envs\CS380_final\Lib\site-packages\keras\src\models\sequential.py:339](http://localhost:8888/lab/tree/Documents/GitHub/CSC380/scripts/~/anaconda3/envs/CS380_final/Lib/site-packages/keras/src/models/sequential.py#line=338), in Sequential.from_config(cls, config, custom_objects)     334     else:     335         layer = serialization_lib.deserialize_keras_object(     336             layer_config,     337             custom_objects=custom_objects,     338         ) --&gt; 339     model.add(layer)     340 if (     341     not model._functional     342     and build_input_shape     343     and isinstance(build_input_shape, (tuple, list))     344 ):     345     model.build(build_input_shape) File [~\anaconda3\envs\CS380_final\Lib\site-packages\keras\src\models\sequential.py:120](http://localhost:8888/lab/tree/Documents/GitHub/CSC380/scripts/~/anaconda3/envs/CS380_final/Lib/site-packages/keras/src/models/sequential.py#line=119), in Sequential.add(self, layer, rebuild)     118 self._layers.append(layer)     119 if rebuild: --&gt; 120     self._maybe_rebuild()     121 else:     122     self.built = False File [~\anaconda3\envs\CS380_final\Lib\site-packages\keras\src\models\sequential.py:139](http://localhost:8888/lab/tree/Documents/GitHub/CSC380/scripts/~/anaconda3/envs/CS380_final/Lib/site-packages/keras/src/models/sequential.py#line=138), in Sequential._maybe_rebuild(self)     137 if isinstance(self._layers[0], InputLayer) and len(self._layers) &gt; 1:     138     input_shape = self._layers[0].batch_shape --&gt; 139     self.build(input_shape) File [~\anaconda3\envs\CS380_final\Lib\site-packages\keras\src\layers\layer.py:222](http://localhost:8888/lab/tree/Documents/GitHub/CSC380/scripts/~/anaconda3/envs/CS380_final/Lib/site-packages/keras/src/layers/layer.py#line=221), in Layer.__new__.<locals&gt;.build_wrapper(*args, **kwargs)     219 @wraps(original_build_method)     220 def build_wrapper(*args, **kwargs):     221     with obj._open_name_scope(): --&gt; 222         original_build_method(*args, **kwargs)     223     # Record build config.     224     signature = inspect.signature(original_build_method) File [~\anaconda3\envs\CS380_final\Lib\site-packages\keras\src\models\sequential.py:180](http://localhost:8888/lab/tree/Documents/GitHub/CSC380/scripts/~/anaconda3/envs/CS380_final/Lib/site-packages/keras/src/models/sequential.py#line=179), in Sequential.build(self, input_shape)     178 for layer in self._layers[1:]:     179     try: --&gt; 180         x = layer(x)     181     except NotImplementedError:     182         # Can happen if shape inference is not implemented.     183         # TODO: consider reverting inbound nodes on layers processed.     184         return File [~\anaconda3\envs\CS380_final\Lib\site-packages\keras\src\utils\traceback_utils.py:122](http://localhost:8888/lab/tree/Documents/GitHub/CSC380/scripts/~/anaconda3/envs/CS380_final/Lib/site-packages/keras/src/utils/traceback_utils.py#line=121), in filter_traceback.<locals&gt;.error_handler(*args, **kwargs)     119     filtered_tb = _process_traceback_frames(e.__traceback__)     120     # To get the full stack trace, call:     121     # `keras.config.disable_traceback_filtering()` --&gt; 122     raise e.with_traceback(filtered_tb) from None     123 finally:     124     del filtered_tb File [~\anaconda3\envs\CS380_final\Lib\site-packages\keras\src\layers\input_spec.py:156](http://localhost:8888/lab/tree/Documents/GitHub/CSC380/scripts/~/anaconda3/envs/CS380_final/Lib/site-packages/keras/src/layers/input_spec.py#line=155), in assert_input_compatibility(input_spec, inputs, layer_name)     154 inputs = tree.flatten(inputs)     155 if len(input_spec) != len(inputs): --&gt; 156     raise ValueError(     157         f"Layer '{layer_name}' expected {len(input_spec)} input(s). "     158         f"Received {len(inputs)} instead."     159     )     160 for x in inputs:     161     # Having a shape/dtype is the only commonality of the various     162     # tensor-like objects that may be passed. The most common kind of     163     # invalid type we are guarding for is a Layer instance (Functional API),     164     # which does not have a `shape` attribute.     165     if not hasattr(x, "shape"): ValueError: Layer 'dense' expected 1 input(s). Received 2 instead.  
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you commented.Message ID: ***@***.***&gt; > الإصدار 2.15.0.post1 يعمل بشكل جيد مع امتدادات *.keras. يجب أيضًا تدريب النموذج على هذا الإصدار. إنه يعمل بشكل جيد على 2.15.0.post1

Thank you it wored .
### 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

```shell
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

```shell
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.
```
