Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving meta data/information in Keras model

Is it possible to save meta data/meta information in Keras model? My goal is to save input pre-processing parameters, train/test set used, class label maps etc. which I can use while loading model again.
I went through Keras documentation and did not find anything. I found similar issue on GitHub but it was closed two years back without any resolution.
Currently I am saving all these information in separate file, and using this file while loading the model.
Although probably not relevant but I am using tf.keras functional model and saving my model as h5 file using model.save().

like image 559
Vivek Mehta Avatar asked Jan 06 '20 11:01

Vivek Mehta


People also ask

How do you save weights in keras?

Save Your Neural Network Model to JSON The weights are saved directly from the model using the save_weights() function and later loaded using the symmetrical load_weights() function.

How do you save keras as pickle?

Keras recommends to use model. save(). Scikit recommends joblib. After tuning the params with RandomizedSearchCV, you can just use trial_search.

How do you save a keras best model?

Callback to save the Keras model or model weights at some frequency. ModelCheckpoint callback is used in conjunction with training using model. fit() to save a model or weights (in a checkpoint file) at some interval, so the model or weights can be loaded later to continue the training from the state saved.

How do you save a TF keras model?

SavedModel serialization format Keras SavedModel uses tf. saved_model. save to save the model and all trackable objects attached to the model (e.g. layers and variables). The model config, weights, and optimizer are saved in the SavedModel.


Video Answer


2 Answers

This is working for me:

from tensorflow.python.keras.saving import hdf5_format
import h5py


# Save model
with h5py.File(model_path, mode='w') as f:
    hdf5_format.save_model_to_hdf5(my_keras_model, f)
    f.attrs['param1'] = param1
    f.attrs['param2'] = param2

# Load model
with h5py.File(model_path, mode='r') as f:
    param1 = f.attrs['param1']
    param2 = f.attrs['param2']
    my_keras_model = hdf5_format.load_model_from_hdf5(f)
like image 62
driedler Avatar answered Oct 10 '22 18:10

driedler


I think the closest think you could implement in order to satisfy your needs(at least part of them) is to save a MetaGraph.

You can achieve that by using tf.saved_model method (at least in TensorFlow 2.0).

Your original model can also be trained in Keras, not necessarily in pure tensorflow in order to use tf.saved_model.

You can read more about tf.saved_model here: https://www.tensorflow.org/guide/saved_model

like image 1
Timbus Calin Avatar answered Oct 10 '22 18:10

Timbus Calin