简体   繁体   中英

NameError: name 'keras_applications' is not defined when loading model

I have a custom keras model built in the following way :

def gen_base_model(n_class):
    cnn_model = InceptionResNetV2(include_top=False, input_shape=(width, width, 3), weights='imagenet')
    inputs = Input((width, width, 3))

    x = inputs
    x = Lambda(preprocess_input, name='preprocessing')(x)
    x = cnn_model(x)
    x = GlobalAveragePooling2D()(x)
    x = Dropout(0.5)(x)
    x = Dense(n_class, activation='softmax', name='softmax')(x)

    model = Model(inputs, x)
    return model

I trained the model and saved it using model.save() .

However, everytime I try to load the model, I get the following error :

>>> model = load_model('coat.hdf5')
WARNING:tensorflow:From /home/aniruddh/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
2019-05-23 23:24:38.613487: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2019-05-23 23:24:38.637936: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 1992000000 Hz
2019-05-23 23:24:38.638313: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x55951c96f170 executing computations on platform Host. Devices:
2019-05-23 23:24:38.638370: I tensorflow/compiler/xla/service/service.cc:158]   StreamExecutor device (0): <undefined>, <undefined>
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/aniruddh/anaconda3/lib/python3.6/site-packages/keras/models.py", line 243, in load_model
    model = model_from_config(model_config, custom_objects=custom_objects)
  File "/home/aniruddh/anaconda3/lib/python3.6/site-packages/keras/models.py", line 317, in model_from_config
    return layer_module.deserialize(config, custom_objects=custom_objects)
  File "/home/aniruddh/anaconda3/lib/python3.6/site-packages/keras/layers/__init__.py", line 55, in deserialize
    printable_module_name='layer')
  File "/home/aniruddh/anaconda3/lib/python3.6/site-packages/keras/utils/generic_utils.py", line 144, in deserialize_keras_object
    list(custom_objects.items())))
  File "/home/aniruddh/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py", line 2520, in from_config
    process_node(layer, node_data)
  File "/home/aniruddh/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py", line 2477, in process_node
    layer(input_tensors[0], **kwargs)
  File "/home/aniruddh/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py", line 617, in __call__
    output = self.call(inputs, **kwargs)
  File "/home/aniruddh/anaconda3/lib/python3.6/site-packages/keras/layers/core.py", line 663, in call
    return self.function(inputs, **arguments)
  File "/usr/local/lib/python3.6/dist-packages/keras/applications/__init__.py", line 23, in wrapper
NameError: name 'keras_applications' is not defined

I have also tried saving the model as a json file along with its weights, but it fails saying

TypeError: ('Not JSON Serializable:', <function preprocess_input at 0x7fa12b5e79d8>)

Where might I be going wrong?

I believe you should change the way you store your model. Store it in an .h5 file as that currently works for me. Have a look at the following code which works fine storing and loading the model:

#serialize weights to HDF5
model.save("model_num.h5")

model = load_model('model_num.h5')

And model summary:

model.summary()

The Output:

在此输入图像描述

Hopefully this will work for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM