6

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

Karin
  • 61
  • 1
  • 1
  • 2
  • @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 my`model.save()`. – Li-Pin Juan Aug 24 '20 at 15:07

1 Answers1

12

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')
PaxPrz
  • 1,778
  • 1
  • 13
  • 29