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

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

Closed
kishan042p opened this issue Apr 26, 2024 · 11 comments

Comments

@kishan042p
Copy link

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
Copy link

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
Copy link

@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
Copy link
Author

@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
Copy link

AssiaBou commented Apr 28, 2024

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
Copy link
Collaborator

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
Copy link

russell919 commented May 3, 2024

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)

Copy link

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 github-actions bot added the stale label May 18, 2024
Copy link

github-actions bot commented Jun 1, 2024

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 github-actions bot closed this as completed Jun 1, 2024
Copy link

github-actions bot commented Jun 1, 2024

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

@helderdaniel
Copy link

helderdaniel commented Jun 20, 2024

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

@diponkor-bala
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants