[英]NameError: name 'keras_applications' is not defined when loading model
I have a custom keras model built in the following way : 我有一个以下列方式构建的自定义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
I trained the model and saved it using model.save()
. 我训练模型并使用
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 我也尝试将模型保存为json文件及其权重,但它没有说
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.
将其存储在.h5文件中,因为它目前适用于我。 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. 希望这对你有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.