Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

batch_normalization issue when trying to load model #19577

Open
aman977381 opened this issue Apr 21, 2024 · 2 comments
Open

batch_normalization issue when trying to load model #19577

aman977381 opened this issue Apr 21, 2024 · 2 comments
Assignees
Labels
stat:awaiting keras-eng Awaiting response from Keras engineer type:Bug

Comments

@aman977381
Copy link

aman977381 commented Apr 21, 2024

I'm getting the following issue when I'm trrying to load my model,


ValueError Traceback (most recent call last)
in <cell line: 1>()
----> 1 load_model = load_model("/content/Mobile_model2.keras")

10 frames
/usr/local/lib/python3.10/dist-packages/keras/src/saving/saving_api.py in load_model(filepath, custom_objects, compile, safe_mode)
174
175 if is_keras_zip:
--> 176 return saving_lib.load_model(
177 filepath,
178 custom_objects=custom_objects,

/usr/local/lib/python3.10/dist-packages/keras/src/saving/saving_lib.py in load_model(filepath, custom_objects, compile, safe_mode)
150 )
151 with open(filepath, "rb") as f:
--> 152 return _load_model_from_fileobj(
153 f, custom_objects, compile, safe_mode
154 )

/usr/local/lib/python3.10/dist-packages/keras/src/saving/saving_lib.py 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 )

/usr/local/lib/python3.10/dist-packages/keras/src/saving/serialization_lib.py 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(

/usr/local/lib/python3.10/dist-packages/keras/src/models/sequential.py in from_config(cls, config, custom_objects)
337 custom_objects=custom_objects,
338 )
--> 339 model.add(layer)
340 if (
341 not model._functional

/usr/local/lib/python3.10/dist-packages/keras/src/models/sequential.py in add(self, layer, rebuild)
118 self._layers.append(layer)
119 if rebuild:
--> 120 self._maybe_rebuild()
121 else:
122 self.built = False

/usr/local/lib/python3.10/dist-packages/keras/src/models/sequential.py in _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)
140
141 def _lock_state(self):

/usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py in build_wrapper(*args, **kwargs)
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)

/usr/local/lib/python3.10/dist-packages/keras/src/models/sequential.py in 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.

/usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py in error_handler(*args, **kwargs)
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

/usr/local/lib/python3.10/dist-packages/keras/src/layers/input_spec.py 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."

ValueError: Layer 'batch_normalization' expected 1 input(s). Received 2 instead.

Here's my model code

base_model = EfficientNetV2L(input_shape=(128,128,3),
                         include_top=False, weights='imagenet')

for layer in base_model.layers:
    layer.trainable = False

model2 = Sequential()

model2.add(base_model)
model2.add(GlobalAveragePooling2D())
model2.add(BatchNormalization())  # Add batch normalization layer
model2.add(Dropout(0.5))  # Add dropout layer
model2.add(Dense(256, activation='relu'))  # Add a dense layer with 256 units
model2.add(BatchNormalization())  # Add another batch normalization layer
model2.add(Dropout(0.5))  # Add another dropout layer
model2.add(Dense(len(class_labels), activation='softmax'))


opt = keras.optimizers.Adam(learning_rate=0.0001)

model2.compile(optimizer=opt, loss='sparse_categorical_crossentropy', metrics=['accuracy'])


my_callbacks = [
    keras.callbacks.EarlyStopping(monitor='val_loss', patience=5, verbose=2, restore_best_weights=True),
    keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=4, min_lr=0.001, verbose=1)
]

model2.summary() 

to save the model

model2.save("Mobile_model.keras")

and, to load the model

load_model("/content/Mobile_model2.keras")

tensorflow version 2.16.1
keras version 3.2.1

@fchollet
Copy link
Member

Indeed, it looks like a deserialization bug. Mind you, it seems your code has some issues as well:

  1. The layers added aren't built by the time you save the model, because your Sequential model doesn't have an Input.
  2. The model definition could be made much simpler. Like this:
inputs = keras.Input(shape=(128, 128, 3))
base_model = keras.applications.EfficientNetV2L(
    input_tensor=inputs, include_top=False, weights="imagenet"
)

for layer in base_model.layers:
    layer.trainable = False

x = base_model.output
x = keras.layers.GlobalAveragePooling2D()(x)
x = keras.layers.BatchNormalization()(x)
x = keras.layers.Dropout(0.5)(x)
x = keras.layers.Dense(256, activation="relu")(x)
x = keras.layers.BatchNormalization()(x)
x = keras.layers.Dropout(0.5)(x)
outputs = keras.layers.Dense(len(class_labels), activation="softmax")(x)
model2 = keras.Model(inputs, outputs)

This equivalent model works fine for saving/loading and everything is already built at instantiation time.

@aman977381
Copy link
Author

thanks I will consider your format, althoug loading model in previous version (2.15.0) works fine but it will be great if this bug got resolve asap in this version.

@sachinprasadhs sachinprasadhs added the stat:awaiting keras-eng Awaiting response from Keras engineer label Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stat:awaiting keras-eng Awaiting response from Keras engineer type:Bug
Projects
None yet
Development

No branches or pull requests

3 participants