NameError:加载模型时未定义名称“ keras_applications”

时间:2019-05-23 17:57:50

标签: python tensorflow keras

我有一个通过以下方式构建的自定义keras模型:

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

我训练了模型并使用model.save()保存了模型。

但是,每次尝试加载模型时,都会出现以下错误:

>>> 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

我也尝试过将模型及其权重另存为json文件,但失败了

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

我可能在哪里出错?

1 个答案:

答案 0 :(得分:0)

我相信您应该更改存储模型的方式。将其存储在.h5文件中,因为该文件目前对我有效。看一下下面的代码,它们可以很好地存储和加载模型:

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

model = load_model('model_num.h5')

和模型摘要:

model.summary()

输出:

enter image description here

希望这对您有用。

相关问题