I just tried your method and found the generated .keras file cannot be loaded properly
ValueError Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/keras/src/ops/operation.py in from_config(cls, config)
233 try:
→ 234 return cls(**config)
235 except Exception as e:
13 frames
/usr/local/lib/python3.10/dist-packages/keras/src/layers/convolutional/conv2d.py in init(self, filters, kernel_size, strides, padding, data_format, dilation_rate, groups, activation, use_bias, kernel_initializer, bias_initializer, kernel_regularizer, bias_regularizer, activity_regularizer, kernel_constraint, bias_constraint, **kwargs)
108 ):
→ 109 super().init(
110 rank=2,
/usr/local/lib/python3.10/dist-packages/keras/src/layers/convolutional/base_conv.py in init(self, rank, filters, kernel_size, strides, padding, data_format, dilation_rate, groups, activation, use_bias, kernel_initializer, bias_initializer, kernel_regularizer, bias_regularizer, activity_regularizer, kernel_constraint, bias_constraint, lora_rank, **kwargs)
106 ):
→ 107 super().init(activity_regularizer=activity_regularizer, **kwargs)
108 self.rank = rank
/usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py in init(self, activity_regularizer, trainable, dtype, autocast, name, **kwargs)
265 if kwargs:
→ 266 raise ValueError(
267 "Unrecognized keyword arguments "
ValueError: Unrecognized keyword arguments passed to Conv2D: {‘batch_input_shape’: [None, 150, 150, 3]}
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/keras/src/saving/serialization_lib.py in deserialize_keras_object(config, custom_objects, safe_mode, **kwargs)
717 try:
→ 718 instance = cls.from_config(inner_config)
719 except TypeError as e:
/usr/local/lib/python3.10/dist-packages/keras/src/ops/operation.py in from_config(cls, config)
235 except Exception as e:
→ 236 raise TypeError(
237 f"Error when deserializing class ‘{cls.name}’ using "
TypeError: Error when deserializing class ‘Conv2D’ using config={‘name’: ‘conv2d_20’, ‘trainable’: True, ‘dtype’: ‘float32’, ‘batch_input_shape’: [None, 150, 150, 3], ‘filters’: 32, ‘kernel_size’: [3, 3], ‘strides’: [1, 1], ‘padding’: ‘valid’, ‘data_format’: ‘channels_last’, ‘dilation_rate’: [1, 1], ‘groups’: 1, ‘activation’: ‘relu’, ‘use_bias’: True, ‘kernel_initializer’: {‘module’: ‘keras.initializers’, ‘class_name’: ‘GlorotUniform’, ‘config’: {‘seed’: None}, ‘registered_name’: None}, ‘bias_initializer’: {‘module’: ‘keras.initializers’, ‘class_name’: ‘Zeros’, ‘config’: {}, ‘registered_name’: None}, ‘kernel_regularizer’: None, ‘bias_regularizer’: None, ‘activity_regularizer’: None, ‘kernel_constraint’: None, ‘bias_constraint’: None}.
Exception encountered: Unrecognized keyword arguments passed to Conv2D: {‘batch_input_shape’: [None, 150, 150, 3]}
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/keras/src/saving/serialization_lib.py in deserialize_keras_object(config, custom_objects, safe_mode, **kwargs)
717 try:
→ 718 instance = cls.from_config(inner_config)
719 except TypeError as e:
/usr/local/lib/python3.10/dist-packages/keras/src/models/sequential.py in from_config(cls, config, custom_objects)
346 else:
→ 347 layer = serialization_lib.deserialize_keras_object(
348 layer_config,
/usr/local/lib/python3.10/dist-packages/keras/src/saving/serialization_lib.py in deserialize_keras_object(config, custom_objects, safe_mode, **kwargs)
719 except TypeError as e:
→ 720 raise TypeError(
721 f"{cls} could not be deserialized properly. Please"
TypeError: <class ‘keras.src.layers.convolutional.conv2d.Conv2D’> could not be deserialized properly. Please ensure that components that are Python object instances (layers, models, etc.) returned by get_config() are explicitly deserialized in the model’s from_config() method.
config={‘module’: ‘keras.layers’, ‘class_name’: ‘Conv2D’, ‘config’: {‘name’: ‘conv2d_20’, ‘trainable’: True, ‘dtype’: ‘float32’, ‘batch_input_shape’: [None, 150, 150, 3], ‘filters’: 32, ‘kernel_size’: [3, 3], ‘strides’: [1, 1], ‘padding’: ‘valid’, ‘data_format’: ‘channels_last’, ‘dilation_rate’: [1, 1], ‘groups’: 1, ‘activation’: ‘relu’, ‘use_bias’: True, ‘kernel_initializer’: {‘module’: ‘keras.initializers’, ‘class_name’: ‘GlorotUniform’, ‘config’: {‘seed’: None}, ‘registered_name’: None}, ‘bias_initializer’: {‘module’: ‘keras.initializers’, ‘class_name’: ‘Zeros’, ‘config’: {}, ‘registered_name’: None}, ‘kernel_regularizer’: None, ‘bias_regularizer’: None, ‘activity_regularizer’: None, ‘kernel_constraint’: None, ‘bias_constraint’: None}, ‘registered_name’: None, ‘build_config’: {‘input_shape’: [None, 150, 150, 3]}}.
Exception encountered: Error when deserializing class ‘Conv2D’ using config={‘name’: ‘conv2d_20’, ‘trainable’: True, ‘dtype’: ‘float32’, ‘batch_input_shape’: [None, 150, 150, 3], ‘filters’: 32, ‘kernel_size’: [3, 3], ‘strides’: [1, 1], ‘padding’: ‘valid’, ‘data_format’: ‘channels_last’, ‘dilation_rate’: [1, 1], ‘groups’: 1, ‘activation’: ‘relu’, ‘use_bias’: True, ‘kernel_initializer’: {‘module’: ‘keras.initializers’, ‘class_name’: ‘GlorotUniform’, ‘config’: {‘seed’: None}, ‘registered_name’: None}, ‘bias_initializer’: {‘module’: ‘keras.initializers’, ‘class_name’: ‘Zeros’, ‘config’: {}, ‘registered_name’: None}, ‘kernel_regularizer’: None, ‘bias_regularizer’: None, ‘activity_regularizer’: None, ‘kernel_constraint’: None, ‘bias_constraint’: None}.
Exception encountered: Unrecognized keyword arguments passed to Conv2D: {‘batch_input_shape’: [None, 150, 150, 3]}
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
in <cell line: 4>()
2
3 imbalanced_history = pd.read_csv(‘history-imbalanced/history-imbalanced.csv’)
----> 4 imbalanced_model = tf.keras.models.load_model(‘model-imbalanced.keras’)
/usr/local/lib/python3.10/dist-packages/keras/src/saving/saving_api.py in load_model(filepath, custom_objects, compile, safe_mode)
180
181 if is_keras_zip or is_keras_dir:
→ 182 return saving_lib.load_model(
183 filepath,
184 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)
235 )
236 with open(filepath, “rb”) as f:
→ 237 return _load_model_from_fileobj(
238 f, custom_objects, compile, safe_mode
239 )
/usr/local/lib/python3.10/dist-packages/keras/src/saving/saving_lib.py in _load_model_from_fileobj(fileobj, custom_objects, compile, safe_mode)
312 config_json = f.read()
313
→ 314 model = _model_from_config(
315 config_json, custom_objects, compile, safe_mode
316 )
/usr/local/lib/python3.10/dist-packages/keras/src/saving/saving_lib.py in _model_from_config(config_json, custom_objects, compile, safe_mode)
301 # Construct the model from the configuration file in the archive.
302 with ObjectSharingScope():
→ 303 model = deserialize_keras_object(
304 config_dict, custom_objects, safe_mode=safe_mode
305 )
/usr/local/lib/python3.10/dist-packages/keras/src/saving/serialization_lib.py in deserialize_keras_object(config, custom_objects, safe_mode, **kwargs)
718 instance = cls.from_config(inner_config)
719 except TypeError as e:
→ 720 raise TypeError(
721 f"{cls} could not be deserialized properly. Please"
722 " ensure that components that are Python object"
TypeError: <class ‘keras.src.models.sequential.Sequential’> could not be deserialized properly. Please ensure that components that are Python object instances (layers, models, etc.) returned by get_config() are explicitly deserialized in the model’s from_config() method.
config={‘module’: ‘keras’, ‘class_name’: ‘Sequential’, ‘config’: {‘name’: ‘sequential_5’, ‘layers’: [{‘module’: ‘keras.layers’, ‘class_name’: ‘InputLayer’, ‘config’: {‘batch_input_shape’: [None, 150, 150, 3], ‘dtype’: ‘float32’, ‘sparse’: False, ‘ragged’: False, ‘name’: ‘conv2d_20_input’}, ‘registered_name’: None}, {‘module’: ‘keras.layers’, ‘class_name’: ‘Conv2D’, ‘config’: {‘name’: ‘conv2d_20’, ‘trainable’: True, ‘dtype’: ‘float32’, ‘batch_input_shape’: [None, 150, 150, 3], ‘filters’: 32, ‘kernel_size’: [3, 3], ‘strides’: [1, 1], ‘padding’: ‘valid’, ‘data_format’: ‘channels_last’, ‘dilation_rate’: [1, 1], ‘groups’: 1, ‘activation’: ‘relu’, ‘use_bias’: True, ‘kernel_initializer’: {‘module’: ‘keras.initializers’, ‘class_name’: ‘GlorotUniform’, ‘config’: {‘seed’: None}, ‘registered_name’: None}, ‘bias_initializer’: {‘module’: ‘keras.initializers’, ‘class_name’: ‘Zeros’, ‘config’: {}, ‘registered_name’: None}, ‘kernel_regularizer’: None, ‘bias_regularizer’: None, ‘activity_regularizer’: None, ‘kernel_constraint’: None, ‘bias_constraint’: None}, ‘registered_name’: None, ‘build_config’: {‘input_shape’: [None, 150, 150, 3]}}, {‘module’: ‘keras.layers’, ‘class_name’: ‘MaxPooling2D’, ‘config’: {‘name’: ‘max_pooling2d_20’, ‘trainable’: True, ‘dtype’: ‘float32’, ‘pool_size’: [2, 2], ‘padding’: ‘valid’, ‘strides’: [2, 2], ‘data_format’: ‘channels_last’}, ‘registered_name’: None}, {‘module’: ‘keras.layers’, ‘class_name’: ‘Conv2D’, ‘config’: {‘name’: ‘conv2d_21’, ‘trainable’: True, 'd…
Exception encountered: <class ‘keras.src.layers.convolutional.conv2d.Conv2D’> could not be deserialized properly. Please ensure that components that are Python object instances (layers, models, etc.) returned by get_config() are explicitly deserialized in the model’s from_config() method.
config={‘module’: ‘keras.layers’, ‘class_name’: ‘Conv2D’, ‘config’: {‘name’: ‘conv2d_20’, ‘trainable’: True, ‘dtype’: ‘float32’, ‘batch_input_shape’: [None, 150, 150, 3], ‘filters’: 32, ‘kernel_size’: [3, 3], ‘strides’: [1, 1], ‘padding’: ‘valid’, ‘data_format’: ‘channels_last’, ‘dilation_rate’: [1, 1], ‘groups’: 1, ‘activation’: ‘relu’, ‘use_bias’: True, ‘kernel_initializer’: {‘module’: ‘keras.initializers’, ‘class_name’: ‘GlorotUniform’, ‘config’: {‘seed’: None}, ‘registered_name’: None}, ‘bias_initializer’: {‘module’: ‘keras.initializers’, ‘class_name’: ‘Zeros’, ‘config’: {}, ‘registered_name’: None}, ‘kernel_regularizer’: None, ‘bias_regularizer’: None, ‘activity_regularizer’: None, ‘kernel_constraint’: None, ‘bias_constraint’: None}, ‘registered_name’: None, ‘build_config’: {‘input_shape’: [None, 150, 150, 3]}}.
Exception encountered: Error when deserializing class ‘Conv2D’ using config={‘name’: ‘conv2d_20’, ‘trainable’: True, ‘dtype’: ‘float32’, ‘batch_input_shape’: [None, 150, 150, 3], ‘filters’: 32, ‘kernel_size’: [3, 3], ‘strides’: [1, 1], ‘padding’: ‘valid’, ‘data_format’: ‘channels_last’, ‘dilation_rate’: [1, 1], ‘groups’: 1, ‘activation’: ‘relu’, ‘use_bias’: True, ‘kernel_initializer’: {‘module’: ‘keras.initializers’, ‘class_name’: ‘GlorotUniform’, ‘config’: {‘seed’: None}, ‘registered_name’: None}, ‘bias_initializer’: {‘module’: ‘keras.initializers’, ‘class_name’: ‘Zeros’, ‘config’: {}, ‘registered_name’: None}, ‘kernel_regularizer’: None, ‘bias_regularizer’: None, ‘activity_regularizer’: None, ‘kernel_constraint’: None, ‘bias_constraint’: None}.
Exception encountered: Unrecognized keyword arguments passed to Conv2D: {‘batch_input_shape’: [None, 150, 150, 3]}