mltu icon indicating copy to clipboard operation
mltu copied to clipboard

ValueError: The filepath provided must end in `.keras` (Keras model format). Received: filepath=Models/02_captcha_to_text/202403291006/model.h5

Open keosaly opened this issue 1 year ago • 5 comments

Hello, Can you help me to fix this?

ValueError: The filepath provided must end in .keras (Keras model format). Received: filepath=Models/02_captcha_to_text/202403291006/model.h5

keosaly avatar Mar 29 '24 10:03 keosaly

my code:

Define callbacks

earlystopper = EarlyStopping(monitor="val_CER", patience=50, verbose=1, mode="min") checkpoint = ModelCheckpoint(f"{configs.model_path}/model.h5", monitor="val_CER", verbose=1, save_best_only=True, mode="min") trainLogger = TrainLogger(configs.model_path) tb_callback = TensorBoard(f"{configs.model_path}/logs", update_freq=1) reduceLROnPlat = ReduceLROnPlateau(monitor="val_CER", factor=0.9, min_delta=1e-10, patience=20, verbose=1, mode="min") model2onnx = Model2onnx(f"{configs.model_path}/model.h5")

Train the model

model.fit( train_data_provider, validation_data=val_data_provider, epochs=configs.train_epochs, callbacks=[earlystopper, checkpoint, trainLogger, reduceLROnPlat, tb_callback, model2onnx], workers=configs.train_workers )

Save training and validation datasets as csv files

train_data_provider.to_csv(os.path.join(configs.model_path, "train.csv")) val_data_provider.to_csv(os.path.join(configs.model_path, "val.csv"))

keosaly avatar Mar 29 '24 10:03 keosaly

did you find any solutions on that? I'am still working on that

mbahadirk avatar May 07 '24 21:05 mbahadirk

It seems you try to use newest tensorflow version (different from tutorial). Recommend to downgrade tensorflow for compatibility. Otherwise you need to do changes manualy. For example change model.h5 to model.keras in all places. But you may face more serious incompatibilities further

pythonlessons avatar May 16 '24 11:05 pythonlessons

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, so 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". https://keras.io/guides/migrating_to_keras_3/#saving-a-model-in-the-tf-savedmodel-format

https://github.com/keras-team/keras-io/issues/1844

Here's changes for your code:

earlystopper = EarlyStopping(monitor="val_CER", patience=50, verbose=1, mode="min")
checkpoint = ModelCheckpoint(f"{configs.model_path}/model.h5", monitor="val_CER", verbose=1, save_best_only=True,, save_weights_only=True, mode="min")
trainLogger = TrainLogger(configs.model_path)
tb_callback = TensorBoard(f"{configs.model_path}/logs", update_freq=1)
reduceLROnPlat = ReduceLROnPlateau(monitor="val_CER", factor=0.9, min_delta=1e-10, patience=20, verbose=1, mode="min")
model2onnx = Model2onnx(f"{configs.model_path}/model.h5")

model.fit(
train_data_provider,
validation_data=val_data_provider,
epochs=configs.train_epochs,
callbacks=[earlystopper, checkpoint, trainLogger, reduceLROnPlat, tb_callback, model2onnx]
)

train_data_provider.to_csv(os.path.join(configs.model_path, "train.csv"))
val_data_provider.to_csv(os.path.join(configs.model_path, "val.csv"))

Mudr0x avatar Aug 08 '24 08:08 Mudr0x

I have the same problem. I want to save in "tensorflow" format - which is set of files in a directory. I do not want to use either .keras nor .h5

I want to save everything not just weights. And I want to use it for both - future training and inference.

If i save it in .keras format - when I load it for further training or inference, sometimes it throws - optimiser variables mismatch, and doesnt use the saved adam vars.

Which I want to avoid (tell me this is wrong idea).

Versions:
TF Version: 2.18.0 Python Version: 3.11.2 (main, Nov 30 2024, 21:22:50) [GCC 12.2.0]

I use tf.keras everywhere not keras directly (if it makes any difference).

What happened to the "tensorflow" checkpoints? have you removed this feature completely? I dont see anything in the docs indicating it and the samples that supposed to use tensorflow format are throwing the same error (want .keras suffix - in either - checkpoint callback and direct model.save)

maxima120 avatar Feb 02 '25 10:02 maxima120