Is it possible to convert a neural network saved as a checkpoint to h5 format?

There is a trained neural network (Object Detection Model) that is saved as a Checkpoint, that is, there are 4 files:

  • labels.pbtxt
  • ckpt-1.data-00000-of-00001
  • ckpt-1.index
  • pipeline.config

Is it possible to convert and save this neural network in Keras *.h5 format?

Maybe I’m wrong, but it seems it is possible to restore the specified model as follows:
PATH_TO_MODEL_DIR = “/content/saved_model”
PATH_TO_CFG = PATH_TO_MODEL_DIR + “/pipeline.config”
PATH_TO_CKPT = PATH_TO_MODEL_DIR + “/checkpoint”
where dir “checkpoint” include:
- labels.pbtxt
- ckpt-1.data-00000-of-00001
- ckpt-1.index

we pre-install the Object Detection API and then
#Load pipeline config and build a detection model:
configs = config_util.get_configs_from_pipeline_file(PATH_TO_CFG)
model_config = configs[‘model’]
detection_model = model_builder.build(model_config=model_config, is_training=False)

Restore checkpoint

ckpt = tf.compat.v2.train.Checkpoint(model=detection_model)
ckpt.restore(os.path.join(PATH_TO_CKPT, ‘ckpt-1’)).expect_partial()
tf.saved_model.save(ckpt, export_dir=‘/content/model’)

but I can’t save it later as model.save()==>
ckpt.save(‘myModel.h5’, save_format=‘h5’)

I get: “TypeError:Checkpoint.save() got an unexpected keyword argument ‘save_format’”, but I can save as:

tf.saved_model.save(ckpt, path_to_saved_dir)

is it possible to somehow convert the model into a single *.h5 file after that?

Simply put, how can I get a model from these four files so that I can save it as:

model = … # Get model ()

model.save(‘path/to/model’)

Have you read the TensorFlow documentation on this subject? Here’s a page that seems like a good place to start.

If you have a directory containing files and you want to convert it to an h5 file, I would try googling “python create h5 file”. Here’s what I get.

But then the next question is, can the “load()” command take an h5 file as input?

Thank you for your attention to my question. Yes, of course, I read the Tensorflow documentation related to saving and loading models, I didn’t find anything that could help me, so I asked this question on the forum.

But then the next question is, can the “load()” command take an h5 file as input?
Yes, by saving the entire model in the ‘*.h5’ format, you can then easily download it from file, like this:
model = keras.models.load_model(“my_model.h5”)
https://www.tensorflow.org/api_docs/python/tf/keras/models/load_model

Hello @Yustas

There is a separate repo in Github on how to save h5 format file in tensorflow Data and deployment available, have you gone through it.

Also if you can share a complete image of the error you are getting, probably we can try tracking the issue.

as far as I check you codes, your codes are incorrect on how to save the model.

Regards
DP

1 Like

Hello @Yustas

Below is the link on tfjs converter as well as on how to save mode based on input format. Kindly go through this in detail and see if you can resolve your issue. let us know, if you need help

Regards
DP

1 Like