14

I have a saved a model using model.save(). I'm trying to reload the model and add a few layers and tune some hyper-parameters, however, it throws the AttributeError.

Model is loaded using load_model().

I guess I'm missing understanding how to add layers to saved layers. If someone can guide me here, it will be great. I'm a novice to deep learning and using keras, so probably my request would be silly.

Snippet:

prev_model = load_model('final_model.h5') # loading the previously saved model.

prev_model.add(Dense(256,activation='relu'))
prev_model.add(Dropout(0.5))
prev_model.add(Dense(1,activation='sigmoid'))

model = Model(inputs=prev_model.input, outputs=prev_model(prev_model.output))

And the error it throws:

Traceback (most recent call last):
  File "image_classifier_3.py", line 39, in <module>
    prev_model.add(Dense(256,activation='relu'))
AttributeError: 'Model' object has no attribute 'add'

I know adding layers works on new Sequential() model, but how do we add to existing saved models?

Abhijit Nathwani
  • 398
  • 1
  • 5
  • 17

2 Answers2

24

The add method is present only in sequential models (Sequential class), which is a simpler interface to the more powerful but complicated functional model (Model class). load_model will always return a Model instance, which is the most generic class.

You can look at the example to see how you can compose different models, but the idea is that, in the end, a Model behaves pretty much like any other layer. So you should be able to do:

prev_model = load_model('final_model.h5') # loading the previously saved model.

new_model = Sequential()
new_model.add(prev_model)
new_model.add(Dense(256,activation='relu'))
new_model.add(Dropout(0.5))
new_model.add(Dense(1,activation='sigmoid'))

new_model.compile(...)
jdehesa
  • 58,456
  • 7
  • 77
  • 121
11

That is due to the fact, that the loaded model is of a functional type instead of a Sequential model. Therefore, you will have to make use of the functional API as described here:(https://keras.io/getting-started/functional-api-guide/).

At the end of the day the correct function is something like this:

fc = Dense(256,activation='relu')(prev_model)
drop = Dropout(0.5)(fc)
fc2 = Dense(1,activation='sigmoid')(drop)

model = Model(inputs=prev_model.input, outputs=fc2)
Thomas Pinetz
  • 6,948
  • 2
  • 27
  • 46
  • 10
    Thanks for your brilliant workaround! But I think `fc = Dense(256,activation='relu')(prev_model)` should be changed to `fc = Dense(256,activation='relu')(prev_model.output)` otherwise there will be `ValueError: Layer conv2d_308 was called with an input that isn't a symbolic tensor. Received type: . Full input: []. All inputs to the layer should be tensors.` – keineahnung2345 Jan 15 '19 at 08:41
  • @keineahnung2345 indeed, was looking for this solution for hours – Cabal Jun 03 '21 at 16:46