Attaching class labels to a Keras model

So I tried my hand at a solution myself and this seems to work. I was hoping for something simpler though.

Opening the model file a second time is not really optimal I think. If anyone can do better, by all means, do.

import h5py

from keras.models import load_model
from keras.models import save_model


def load_model_ext(filepath, custom_objects=None):
    model = load_model(filepath, custom_objects=None)
    f = h5py.File(filepath, mode='r')
    meta_data = None
    if 'my_meta_data' in f.attrs:
        meta_data = f.attrs.get('my_meta_data')
    f.close()
    return model, meta_data


def save_model_ext(model, filepath, overwrite=True, meta_data=None):
    save_model(model, filepath, overwrite)
    if meta_data is not None:
        f = h5py.File(filepath, mode='a')
        f.attrs['my_meta_data'] = meta_data
        f.close()

Tags:

Python

Keras