I have a tensorflow model that saves checkpoints, but I need to to load the weights and save the Kereas .h5 model. How can I do that?
I am assuming you need to convert your previous checkpoint into .h5
Given an already trained model, you want to load its weights and save as .h5. I am assuming you have it saved as a .model file. Lets say it was called first.model
In your script, you will want to use load_model, loading your checkpoint with
model = load_model('first.model')
then you will simply need to use
model.save('goal.h5')
to save as a .h5 file.
For future reference, you can avoid this conversion process by saving checkpoints as .h5:
When using the Checkpoints feature, you have the option to save as either a .model .h5, or .hdf5. The line might look something like this:
checkpoint = ModelCheckpoint("**FILE_NAME_HERE**.model",monitor='val_loss',verbose=1,mode='min',save_best_only=True,save_weights_only=False,period=1)
That is how you save your checkpoint as a .model, but to save it as a h5 as you are looking to do:
checkpoint = ModelCheckpoint("**FILE_NAME_HERE**.h5",monitor='val_loss',verbose=1,mode='min',save_best_only=True,save_weights_only=False,period=1)
Related
What I tried so far:
pre-train a model using unsupervised method in PyTorch, and save off the checkpoint file (using torch.save(state, filename))
convert the checkpoint file to onnx format (using torch.onnx.export)
convert the onnx to tensorflow saved model (using onnx-tf)
trying to load the variables in saved_model folder as checkpoint in my tensorflow training code (using tf.train.init_from_checkpoint) for fine-tuning
But now I am getting stuck at step 4 because I notice that variables.index and variables.data#1 files are basically empty (probably because of this: https://github.com/onnx/onnx-tensorflow/issues/994)
Also, specifically, if I try to use tf.train.NewCheckpointReader to load the files and call ckpt_reader.get_variable_to_shape_map(), _CHECKPOINTABLE_OBJECT_GRAPH is empty
Any suggestions/experience are appreciated :-)
I have a saved trained model file in Keras which was saved using model.save(). Is it possible to load only the weights of the model from that file? Because I am getting an error when I try to load the model as a whole.
I have a simple pb file, without any ckpt file. I would like to (randomly)initialize all the weights of the pb file and save the the initialized weights as ckpt file. I could not find any way to do it. global variable initializer just threw no variables to save
The Model can be saved in .pb format in three ways. Using tf.saved_model.simple_save, tf.saved_model.Builder.save or estimator.export_savedmodel.
To answer your question, you can restore the .pb model using the function, tf.saved_model.loader.load(sess,[tag_constants.TRAINING],Export_Dir).
Then, you can restore the Weights (you should remember the name corresponding to Weights Tensor) using the below code:
with graph2.as_default():
with tf.Session(graph=graph2) as sess:
# Restore saved values
print('\nRestoring...')
tf.saved_model.loader.load(sess,[tag_constants.SERVING],path)
#Specify the correct `Weights_Tensor_Name`
Weights_Var = graph2.get_tensor_by_name('Weights_Tensor_Name:0')
After that, you can save the Weights to .checkpoint files using the below code:
saver = tf.train.Saver({"Weights_Var": Weights_Var})
But if you just want to save a Random Tensor (with name Weights) into Check Points File, you can generate a Random Tensor using the function, tf.random.uniform and then can save it using the code below:
saver = tf.train.Saver({"Weights_Var": Weights_Var})
I want to do very simple task. Let us assume that I have executed a model and saved multiple checkpoints and metada for this model using tf.estimator. We can again assume that I have 3 checkpoints. 1, 2 and 3. While I am evaluating the trained results on the tensorboard, I am realizing that checkpoint 2 is providing the better weights for my objective.
Therefore I want to load checkpoint 2 and make my predictions. What I want to ask simply is that, is it possible to delete checkpoint 3 from the model dir and let the estimator load it automatically from checkpoint 2 or is there anything I can do to load a specific checkpoint for.my predictions?
Thank you.
Yes, You can. By default, Estimator will load latest available checkpoint in model_dir. So you can either delete files manually, or specify checkpoint file with
warm_start = tf.estimator.WarmStartSettings(ckpt_to_initialize_from='file.ckpt')
and pass this to estimator
tf.estimator.Estimator(model_fn=model_fn,
config=run_config,
model_dir='dir',
warm_start_from=warm_start)
The latter option will not mess tensorboard summaries, so it's generally cleaner
I want to change a ckpt files's tensor's value by many other ckpt files's tensors, and use the modified ckpt files to restart TF training jobs.
Hope you some advices!
Thanks!
There are standalone utilities for reading checkpoint files (search for CheckpointReader or NewCheckpointReader) but not modifying them. The easiest approach is probably to load the checkpoint into your model, assign a new value to the variable you want to change, and save this new checkpoint.