Giter Club home page Giter Club logo

Comments (18)

gustavozf avatar gustavozf commented on April 27, 2024 5

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?

from tensorflow.

AaronEggert avatar AaronEggert commented on April 27, 2024 3

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})

from tensorflow.

H0TB0X420 avatar H0TB0X420 commented on April 27, 2024 3

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.

from tensorflow.

sergvbash avatar sergvbash commented on April 27, 2024 2

Have exactly the same issue on Mac. Setting the input shape does't help

from tensorflow.

squallssck avatar squallssck commented on April 27, 2024 2

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)

from tensorflow.

gustavozf avatar gustavozf commented on April 27, 2024 1

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.

from tensorflow.

gustavozf avatar gustavozf commented on April 27, 2024 1

@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.

from tensorflow.

91Abdullah avatar 91Abdullah commented on April 27, 2024 1

Any update on this issue?

from tensorflow.

91Abdullah avatar 91Abdullah commented on April 27, 2024 1

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

from tensorflow.

ba049 avatar ba049 commented on April 27, 2024 1

Thanks @91Abdullah. It worked for me!

from tensorflow.

Venkat6871 avatar Venkat6871 commented on April 27, 2024

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 with updated code for your reference. Please go through it once.

Thank you!

from tensorflow.

AaronEggert avatar AaronEggert commented on April 27, 2024

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

from tensorflow.

google-ml-butler avatar google-ml-butler commented on April 27, 2024

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

from tensorflow.

Rebelioner avatar Rebelioner commented on April 27, 2024

есть ли фид бек по тому когда эта ошибка появилась , потому что раньше все работало отлично и никаких проблем не наблюдалось

from tensorflow.

XXTongC avatar XXTongC commented on April 27, 2024

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.

from tensorflow.

gmtanner-cord avatar gmtanner-cord commented on April 27, 2024

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.

from tensorflow.

91Abdullah avatar 91Abdullah commented on April 27, 2024

@gmtanner-cord try to use version 2.15 it should work as expected.

from tensorflow.

XXTongC avatar XXTongC commented on April 27, 2024

from tensorflow.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.