Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read keras model weights without a model

Tags:

python

keras

A keras model can be saved in two files. One file is with a model architecture. And the other one is with model weights, weights are saved by the method model.save_weights().

Then weights can be loaded with model.load_weights(file_path). It assumes that the model exists.

I need to load only weights without a model. I tried to use pickle.load().

with open(file_path, 'rb') as fp:
    w = pickle.load(fp)

But it gives the error:

_pickle.UnpicklingError: invalid load key, 'H'.

I suppose that weights file was saved in the way not compatible. Is it possible to load only weights from file created by model.save_weights()?

like image 502
Antonina Avatar asked Jul 04 '18 12:07

Antonina


People also ask

How do I view a weights file?

If you cannot open your WEIGHT file correctly, try to right-click or long-press the file. Then click "Open with" and choose an application. You can also display a WEIGHT file directly in the browser: Just drag the file onto this browser window and drop it.

How to save a keras model with weights?

A keras model can be saved in two files. One file is with a model architecture. And the other one is with model weights, weights are saved by the method model.save_weights (). Then weights can be loaded with model.load_weights (file_path).

What are the different types of Keras models?

There are three ways to create Keras models: The Sequential model, which is very straightforward (a simple list of layers), but is limited to single-input, single-output stacks of layers (as the name gives away). The Functional API, which is an easy-to-use, fully-featured API that supports arbitrary model architectures.

What is the use of callback in keras 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 to load a model from weights only?

You can’t load a model from weights only. In this case, you can’t use load_model method. You have to set and define the architecture of your model and then use model.load_weights ('CIFAR1006.h5').


3 Answers

The data format is h5 so you can directly use the h5py library to inspect and load the weights. From the quickstart guide:

import h5py
f = h5py.File('weights.h5', 'r')
print(list(f.keys()))
# will get a list of layer names which you can use as index
d = f['dense']['dense_1']['kernel:0']
# <HDF5 dataset "kernel:0": shape (128, 1), type "<f4">
d.shape == (128, 1)
d[0] == array([-0.14390108], dtype=float32)
# etc.

The file contains properties including weights of layers and you can explore in detail what is stored and how. If you would like a visual version there is h5pyViewer as well.

like image 160
nuric Avatar answered Oct 21 '22 22:10

nuric


Ref: https://github.com/keras-team/keras/issues/91 Code Snippet for your ask below

from __future__ import print_function

import h5py

def print_structure(weight_file_path):
    """
    Prints out the structure of HDF5 file.

    Args:
      weight_file_path (str) : Path to the file to analyze
    """
    f = h5py.File(weight_file_path)
    try:
        if len(f.attrs.items()):
            print("{} contains: ".format(weight_file_path))
            print("Root attributes:")

        print("  f.attrs.items(): ")
        for key, value in f.attrs.items():           
            print("  {}: {}".format(key, value))

        if len(f.items())==0:
            print("  Terminate # len(f.items())==0: ")
            return 

        print("  layer, g in f.items():")
        for layer, g in f.items():            
            print("  {}".format(layer))
            print("    g.attrs.items(): Attributes:")
            for key, value in g.attrs.items():
                print("      {}: {}".format(key, value))

            print("    Dataset:")
            for p_name in g.keys():
                param = g[p_name]
                subkeys = param.keys()
                print("    Dataset: param.keys():")
                for k_name in param.keys():
                    print("      {}/{}: {}".format(p_name, k_name, param.get(k_name)[:]))
    finally:
        f.close()
print_structure('weights.h5.keras')
like image 44
user2458922 Avatar answered Oct 21 '22 21:10

user2458922


You need to create a Keras Model, then you can load your architecture and afterwards the model weights

See the code below,

model = keras.models.Sequential()          # create a Keras Model
model.load_weights('my_model_weights.h5')  # load model weights

More information in the Keras docs

like image 20
JK_16 Avatar answered Oct 21 '22 21:10

JK_16