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()
.
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.
Keras recommends to use model. save(). Scikit recommends joblib. After tuning the params with RandomizedSearchCV, you can just use trial_search.
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.
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.
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With