2017-04-14 201 views
1

我試圖加載它被保存的模型:model.save('myModel.h5')「模型」對象有沒有屬性「load_model」 keras

模型的定義是這樣的:

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) 

當我嘗試加載myModel.h5model.load_model('myModel.h5')它拋出我下面的錯誤:

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

我supose這是因爲我不Sequential模型工作。

那麼我該如何加載我的模型呢?因爲model.save('myModel.h5')似乎工作。

謝謝!

回答

3

load_model()的確不是模型對象的屬性。 load_model()是一個從keras.models導入的函數,它接受一個文件名並返回一個模型對象。

你應該使用這樣的:

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

+0

AAH沒關係!現在它的工作,謝謝;) – Eric

+0

非常歡迎:) –

+0

我可以訪問'model.history'加載整個模型的權利? – Eric

相關問題