def test_h5dict_attrs(h5_path='test.h5'): # test both HDF5 and dict implementations paths = [h5_path, dict()] for path in paths: f = h5dict(path, mode='w') # str f['x'] = 'abcd' # list<bytes> f['y'] = [b'efg', b'hij', b'klmn'] # ndarray array = np.random.random((3, 4, 5)) f['z'] = array f.close() f = h5dict(path, mode='r') assert f['x'] == 'abcd' assert f['y'] == [b'efg', b'hij', b'klmn'] assert_allclose(f['z'], array) f.close()
def test_h5dict_attrs(): _, h5_path = tempfile.mkstemp('.h5') # test both HDF5 and dict implementations paths = [h5_path, dict()] for path in paths: f = h5dict(path, mode='w') # str f['x'] = 'abcd' # list<bytes> f['y'] = [b'efg', b'hij', b'klmn'] # ndarray array = np.random.random((4, 5, 512)) f['z'] = array f.close() f = h5dict(path, mode='r') assert f['x'] == 'abcd' assert f['y'] == [b'efg', b'hij', b'klmn'] assert_allclose(f['z'], array) f.close() os.remove(h5_path)
def test_h5dict_attrs(): _, h5_path = tempfile.mkstemp('.h5') # test both HDF5 and dict implementations paths = [h5_path, dict()] for path in paths: f = h5dict(path, mode='w') # str f['x'] = 'abcd' # list<bytes> f['y'] = [b'efg', b'hij', b'klmn'] # ndarray array = np.random.random((4, 5, 512)) f['z'] = array f.close() f = h5dict(path, mode='r') assert f['x'] == 'abcd' assert f['y'] == [b'efg', b'hij', b'klmn'] assert_allclose(f['z'], array) f.close() os.remove(h5_path)
def test_h5dict_groups(): _, h5_path = tempfile.mkstemp('.h5') # test both HDF5 and dict implementations paths = [h5_path, dict()] for path in paths: f = h5dict(path, mode='w') group1 = f['group1'] group2 = group1['group2'] group2['x'] = 'abcd' group3 = group2['group3'] group3['y'] = [b'efg', b'hij', b'klmn'] group4 = group3['group4'] array = np.random.random((4, 5, 512)) group4['z'] = array f.close() f = h5dict(path, mode='r') assert 'group1' in f group1 = f['group1'] assert 'group2' in group1 group2 = group1['group2'] assert group2['x'] == 'abcd' assert 'group3' in group2 group3 = group2['group3'] assert group3['y'] == [b'efg', b'hij', b'klmn'] assert 'group4' in group3 group4 = group3['group4'] assert_allclose(group4['z'], array) f.close() os.remove(h5_path)
def test_h5dict_groups(): _, h5_path = tempfile.mkstemp('.h5') # test both HDF5 and dict implementations paths = [h5_path, dict()] for path in paths: f = h5dict(path, mode='w') group1 = f['group1'] group2 = group1['group2'] group2['x'] = 'abcd' group3 = group2['group3'] group3['y'] = [b'efg', b'hij', b'klmn'] group4 = group3['group4'] array = np.random.random((4, 5, 512)) group4['z'] = array f.close() f = h5dict(path, mode='r') assert 'group1' in f group1 = f['group1'] assert 'group2' in group1 group2 = group1['group2'] assert group2['x'] == 'abcd' assert 'group3' in group2 group3 = group2['group3'] assert group3['y'] == [b'efg', b'hij', b'klmn'] assert 'group4' in group3 group4 = group3['group4'] assert_allclose(group4['z'], array) f.close() os.remove(h5_path)
def save_model(model, filepath, overwrite=True, include_optimizer=True): """Save a model to a HDF5 file. Note: Please also see [How can I install HDF5 or h5py to save my models in Keras?]( /getting-started/faq/ #how-can-i-install-HDF5-or-h5py-to-save-my-models-in-Keras) in the FAQ for instructions on how to install `h5py`. The saved model contains: - the model's configuration (topology) - the model's weights - the model's optimizer's state (if any) Thus the saved model can be reinstantiated in the exact same state, without any of the code used for model definition or training. # Arguments model: Keras model instance to be saved. filepath: one of the following: - string, path where to save the model, or - h5py.File or h5py.Group object where to save the model overwrite: Whether we should overwrite any existing model at the target location, or instead ask the user with a manual prompt. include_optimizer: If True, save optimizer's state together. # Raises ImportError: if h5py is not available. """ if h5py is None: raise ImportError('`save_model` requires h5py.') if not isinstance(filepath, h5py.Group): # If file exists and should not be overwritten. if not overwrite and os.path.isfile(filepath): proceed = ask_to_proceed_with_overwrite(filepath) if not proceed: return opened_new_file = True else: opened_new_file = False f = h5dict(filepath, mode='w') try: _serialize_model(model, f, include_optimizer) finally: if opened_new_file: f.close()
def _load_model(filepath, model_class): """Load a model, its state and its hyperparameters from file. Args: filepath (TYPE): Path to model to load """ from keras.utils.io_utils import h5dict from keras.engine.saving import load_weights_from_hdf5_group_by_name #, _deserialize_model from json import loads from keras.models import model_from_json f = h5dict(filepath, mode='r') parameters = loads(str(f['parameters'])) model = model_class(**parameters) model._maybe_create_model() model.model_.load_weights(filepath, by_name=True) f.close() return model
def load_model(filepath, custom_objects=None, compile=True): """Loads a model saved via `save_model`. # Arguments filepath: one of the following: - string, path to the saved model, or - h5py.File or h5py.Group object from which to load the model custom_objects: Optional dictionary mapping names (strings) to custom classes or functions to be considered during deserialization. compile: Boolean, whether to compile the model after loading. # Returns A Keras model instance. If an optimizer was found as part of the saved model, the model is already compiled. Otherwise, the model is uncompiled and a warning will be displayed. When `compile` is set to False, the compilation is omitted without any warning. # Raises ImportError: if h5py is not available. ValueError: In case of an invalid savefile. """ if h5py is None: raise ImportError('`load_model` requires h5py.') model = None opened_new_file = not isinstance(filepath, h5py.Group) f = h5dict(filepath, 'r') try: model = _deserialize_model(f, custom_objects, compile) finally: if opened_new_file: f.close() return model
def unpickle_model(state): f = h5dict(state, mode='r') return _deserialize_model(f)
def pickle_model(model): d = {} f = h5dict(d) _serialize_model(model, f) return d