Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Model' object has no attribute 'load_model' keras

I'm trying to load a model that it was saved with: model.save('myModel.h5')

The model is defined like this:

self.model = VGGFace(input_tensor=input_tensor, include_top=True)

for layer in self.model.layers:
    layer.trainable = False

self.model.get_layer('fc7').trainable = True
last_layer = self.model.get_layer('fc7').output
out = BatchNormalization()(last_layer)
out = Dense(self.n_outputs, activation='softmax', name='fc8')(out)
self.model = Model(input=self.model.input, output=out)

when i try to load myModel.h5 with model.load_model('myModel.h5') it throws me the following error:

AttributeError: 'Model' object has no attribute 'load_model'

I supose it's because i'm not working with Sequential models.

How can i load my model then? since model.save('myModel.h5') seems to work.

Thanks!!!!

like image 412
Eric Avatar asked Apr 14 '17 12:04

Eric


Video Answer


2 Answers

load_model() isn't an attribute of an model obejct indeed. load_model() is a function imported from keras.models that takes a file name and returns a model obejct.

You should use it like this :

from keras.models import load_model

model = load_model(path_to_model)

You can then use keras.models.load_model(filepath) to reinstantiate your model. load_model will also take care of compiling the model using the saved training configuration (unless the model was never compiled in the first place). from source

like image 153
Nassim Ben Avatar answered Sep 22 '22 13:09

Nassim Ben


The reason is sequential doesn't have a way to load the full model along with optimizer etc only weights. To load the complete model after you performed model.save('myModel.h5') you should load the model with

import tensorflow as tf
from tensorflow import keras
#...
myModel = tf.keras.models.load_model("myModel.h5")
like image 42
Shiladitya Sircar Avatar answered Sep 20 '22 13:09

Shiladitya Sircar