9

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().

Vivek Mehta
  • 2,612
  • 2
  • 18
  • 30
  • 1
    i don't think `keras` model save file is expected to save anything but model parameters(layer weights, layer activation functions, etc) – Oleg Vorobiov Jan 06 '20 at 11:13
  • A save file like that normally only contains the model parameters (also mentioned by okawo). You could implement your own metadata logging with `logging` and save that to a log file instead. – Cobalt Jan 06 '20 at 11:24
  • That's what I am currently doing @Cobalt – Vivek Mehta Jan 06 '20 at 11:31

3 Answers3

8

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)
driedler
  • 3,750
  • 33
  • 26
  • 1
    It works also for json dicts with int64 values! `f.attrs['categorical_values'] = json.dumps(categorical_values, cls=NpEncoder)` – steller Jul 10 '22 at 19:54
  • For tensorflow 2.13.0 I'm not importing tensorflow.python.keras.saving, but using `my_keras_model.save(f, save_format='h5')` and `tensorflow.keras.models.load_model(f)` – Peer Sommerlund Aug 21 '23 at 22:08
1

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

Timbus Calin
  • 13,809
  • 5
  • 41
  • 59
  • Is there option to save meta information in `tf.saved_model`? I went through documentation but could not find anywhere. – Vivek Mehta Jan 08 '20 at 06:51
  • I think the saved_model format in essence, it contains other information apart from the weights. – Timbus Calin Jan 08 '20 at 07:39
  • I think this is the right answer, but I would love more detail here! Do you add a signature that just returns a constant tensor with the info you want? – Carson McNeil Aug 24 '20 at 22:15
1

This is vile, but it works for me.

I have a threshold parameter 'thr' that is used in the preprocessing. I build it into the input name...

input_img = layers.Input((None, None, 1), name="input-thr{0:f}".format(thr))

In another program, when I read the model and use it, I scan the input name for the value...

try:
    thr = float(re.match('input-thr(.*)', model.layers[0].name).group(1))
except:
    thr = args.thr # default value   

Perhaps this is not as nasty as it seems, for the input name describes the pre-processing the model expects for that input.

It would be nicer if the Keras model had a public metadata dictionary where we could stash stuff like this.

Postscript: I have removed this from my code.

It is only a few lines to save all the training parameters to a separate file. Once you set this up, it is easy to saved all the arguments and not the ones for your immediate needs. If you are really paranoid about syncing this data to the trained model, save the model name and creation time too.

Richard Kirk
  • 281
  • 1
  • 12