Open aman977381 opened 3 months ago
Indeed, it looks like a deserialization bug. Mind you, it seems your code has some issues as well:
Input
.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.
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 can you please assist regarding this issue it's been a long since rising this issue yet no response
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
to save the model
and, to load the model
tensorflow version 2.16.1 keras version 3.2.1