How to fix"NameError: name 'load_model' is not defined"

17,671

What you are missing here is, load_model function is inside models class so you have to reference models class.

from keras import models    
model = models.load_model('filename.h5')

In order to do it your way, you have to use import as following

from keras.models import load_model

PS: This next line might help you in future. If you are trying to load weights, use function:

model.load_weight('weights_file.h5')
Share:
17,671

Related videos on Youtube

Karin
Author by

Karin

Updated on June 04, 2022

Comments

  • Karin
    Karin over 1 year

    I am reading the book"Deep Learning with Python" and I have come across a problem. I have used Pycharm to save models in anther file and when I tried to load it by the function 'load_model()', the system supports that 'NameError: name 'load_model' is not defined'. pycharm 2019.1.3 keras 2.2.4 tensorflow 1.13.0

    from keras import models
    model = load_model('cats_and_dogs_small_2.h5')
    

    Using TensorFlow backend. Traceback (most recent call last): File "F:/python program/visualizDemo/main.py", line 2, in model = load_model('cats_and_dogs_small_2.h5') NameError: name 'load_model' is not defined

    Process finished with exit code 1

    • Li-Pin Juan
      Li-Pin Juan about 3 years
      @krain I think you probably missed the specification of custom_object like loss function, metric function, etc, when you save your model. I had the same issue before and the resolution is simply to add what I said in mymodel.save().