Search code examples
python-3.xtensorflowkeras-2

How do I load the saved keras weights from an .h5 file


I am trying to replicate an example of image classification - training, validation and then finally test on a standalone image sample.

I saved the weights (only) using:

model.save_weights('file_name.h5')

Now, in order to run prediction, I want to load the saved .h5 file for which I am trying:

Model.load_weights('file_name.h5')

The file file_name.h5 is saved in the same root path.

However, I am getting error

TypeError: load_weights() missing 1 required positional argument: 'filepath'

What should I do in order the load the saved weight.

There is a discussion in this respect here at the keras page


Solution

  • Just to close the stupid question (can't delete it) (besides typho_s):

    Here is what I would do.

    1. After creating a model:

      model = Sequential()
      model.add(Activation('relu'))
      ...
      ...
      model.add(Dense(1))
      model.add(Activation('sigmoid'))
      
    2. Load the weights:

      model.load_weights(<path_to_file>)