keras-team / keras-io

Keras documentation, hosted live at keras.io
Apache License 2.0
2.69k stars 2.01k forks source link

ValueError: The filepath provided must end in `.keras` (Keras model format). Received: filepath=Model_ckpt.h5 #1844

Closed kishan042p closed 4 weeks ago

kishan042p commented 2 months ago

Issue Type

Support

Source

source

Keras Version

3.2.1

Custom Code

Yes

OS Platform and Distribution

Linux Ubuntu 22.04.4

Python version

3.11.5

GPU model and memory

16 gm

Current Behavior?

please solve this error

Standalone code to reproduce the issue or tutorial link

CKPT_path = "Model_ckpt.h5"
checkpointing_cb = tf.keras.callbacks.ModelCheckpoint(CKPT_path, save_best_only=True)

# Orginal train

EPOCHS = 30
VALIDATION_SET = (X_valid, y_valid)

history = model_clf.fit(X_train, y_train, epochs=EPOCHS,
                    validation_data=VALIDATION_SET, batch_size=32, callbacks=[tb_cb, early_stopping_cb,checkpointing_cb] )

Relevant log output

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[37], line 2
      1 CKPT_path = "Model_ckpt.h5"
----> 2 checkpointing_cb = tf.keras.callbacks.ModelCheckpoint(CKPT_path, save_best_only=True)

File ~/anaconda3/lib/python3.11/site-packages/keras/src/callbacks/model_checkpoint.py:191, in ModelCheckpoint.__init__(self, filepath, monitor, verbose, save_best_only, save_weights_only, mode, save_freq, initial_value_threshold)
    189 else:
    190     if not self.filepath.endswith(".keras"):
--> 191         raise ValueError(
    192             "The filepath provided must end in `.keras` "
    193             "(Keras model format). Received: "
    194             f"filepath={self.filepath}"
    195         )

ValueError: The filepath provided must end in `.keras` (Keras model format). Received: filepath=Model_ckpt.h5
AssiaBou commented 2 months ago

I have the same problem in Kaggle notebook while executing the following code: history=model1.fit(train_img_datagen, steps_per_epoch=steps_per_epoch, epochs=100, verbose=1, callbacks = [ keras.callbacks.ModelCheckpoint('UNet_100.h5', save_weights_only=False, save_best_only=True, monitor='val_loss', mode='min'), keras.callbacks.ReduceLROnPlateau(), ], validation_data=val_img_datagen, validation_steps=val_steps_per_epoch, )

AssiaBou commented 2 months ago

@kishan042p I have just solved the problem by installing an older versio of keras:

!pip install keras==2.15.0 import keras print(keras.version)

But you have to restart your kernel before using it,

Good luck

kishan042p commented 2 months ago

@kishan042p I have just solved the problem by installing an older versio of keras:

!pip install keras==2.15.0 import keras print(keras.version)

But you have to restart your kernel before using it,

Good luck

But i Want to save model in .h5 file not in .keras so please provide solution regarding it.

AssiaBou commented 2 months ago

Yes I use my old code, where I save my model in .h5 file not .keras You don't have to change anything, you just have to iinstall an older version of keras and everything will be OK

sachinprasadhs commented 1 month ago

In Keras 3, for checkpoint filepath, you need to provide in .keras format only, if you're saving only weights file name should end with .weights.h5.

You can save and load the model in .h5 using below method https://keras.io/guides/migrating_to_keras_3/#saving-a-model-in-the-tf-savedmodel-format

russell919 commented 1 month ago

In Keras 3, if you want to save your model in .h5 file in any case, set "save_weights_only=True", and change your flie name to "xxx.weights.h5".

for your code: CKPT_path = "Model_ckpt.weights.h5" checkpointing_cb = tf.keras.callbacks.ModelCheckpoint(CKPT_path, save_best_only=True, save_weights_only=True)

github-actions[bot] commented 1 month ago

This issue is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you.

github-actions[bot] commented 4 weeks ago

This issue was closed because it has been inactive for 28 days. Please reopen if you'd like to work on this further.

github-actions[bot] commented 4 weeks ago

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

helderdaniel commented 1 week ago

https://keras.io/guides/migrating_to_keras_3/#saving-a-model-in-the-tf-savedmodel-format

"The following snippet of code will reproduce the above error:

sequential_model = keras.Sequential([
    keras.layers.Dense(2)
])
sequential_model.save("saved_model")

How to fix it: use model.export(filepath) instead of model.save(filepath)"