Keras: 'imagenet_utils' is not defined when reloading model to predict only

Created on 8 Dec 2017  路  6Comments  路  Source: keras-team/keras

After training successfully Xception with pretrained imagenet weights, I try to tun another script that loads that saved model only to predict. I get the following error, can anyone help me please?

  File "models/test_keras.py", line 85, in <module>
    model = load_model(model)
  File "/afs/l2f.inesc-id.pt/home/jlages/Cdiscount_Kaggle/venv/lib/python3.6/site-packages/keras/models.py", line 240, in load_model
    model = model_from_config(model_config, custom_objects=custom_objects)
  File "/afs/l2f.inesc-id.pt/home/jlages/Cdiscount_Kaggle/venv/lib/python3.6/site-packages/keras/models.py", line 314, in model_from_config
    return layer_module.deserialize(config, custom_objects=custom_objects)
  File "/afs/l2f.inesc-id.pt/home/jlages/Cdiscount_Kaggle/venv/lib/python3.6/site-packages/keras/layers/__init__.py", line 55, in deserialize
    printable_module_name='layer')
  File "/afs/l2f.inesc-id.pt/home/jlages/Cdiscount_Kaggle/venv/lib/python3.6/site-packages/keras/utils/generic_utils.py", line 140, in deserialize_keras_object
    list(custom_objects.items())))
  File "/afs/l2f.inesc-id.pt/home/jlages/Cdiscount_Kaggle/venv/lib/python3.6/site-packages/keras/engine/topology.py", line 2500, in from_config
    process_node(layer, node_data)
  File "/afs/l2f.inesc-id.pt/home/jlages/Cdiscount_Kaggle/venv/lib/python3.6/site-packages/keras/engine/topology.py", line 2457, in process_node
    layer(input_tensors[0], **kwargs)
  File "/afs/l2f.inesc-id.pt/home/jlages/Cdiscount_Kaggle/venv/lib/python3.6/site-packages/keras/engine/topology.py", line 603, in __call__
    output = self.call(inputs, **kwargs)
  File "/afs/l2f.inesc-id.pt/home/jlages/Cdiscount_Kaggle/venv/lib/python3.6/site-packages/keras/layers/core.py", line 651, in call
    return self.function(inputs, **arguments)
  File "/afs/l2f.inesc-id.pt/home/jlages/Cdiscount_Kaggle/venv/lib/python3.6/site-packages/keras/applications/xception.py", line 280, in preprocess_input
    return imagenet_utils.preprocess_input(x, mode='tf')
NameError: name 'imagenet_utils' is not defined

Most helpful comment

you can use this code to load model successfully:
from keras.applications import imagenet_utils
model = load_model(filepath, custom_objects={'imagenet_utils': imagenet_utils})

All 6 comments

I got same problem, did you fix it =)

@DavexPro Nope,I had no idea what was going on.

got the same problem, use load_weights to work around.

I got same problem

same issue, anyone tried to solve this rather than using load_weights?

you can use this code to load model successfully:
from keras.applications import imagenet_utils
model = load_model(filepath, custom_objects={'imagenet_utils': imagenet_utils})

Was this page helpful?
0 / 5 - 0 ratings