我已经训练了CNN,并相应地保存了它:
model = Sequential()
model.add(Flatten(input_shape=train_data.shape[1:]))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop',
loss='binary_crossentropy', metrics=['accuracy'])
model.fit(train_data, train_labels,
epochs=epochs,
batch_size=batch_size,
validation_data=(validation_data, validation_labels))
model.save('full_model.h5')
我现在尝试使用以下命令在另一个python脚本中加载模型:
model = tf.keras.models.load_model('full_model.h5')
,并收到以下错误:
Traceback (most recent call last):
File "/media/spt/Data/tensorflow_server/get_model.py", line 12, in <module>
model = tf.keras.models.load_model('full_model.h5')
File "/home/spt/.conda/envs/dev_env/lib/python3.6/site-packages/tensorflow/python/keras/engine/saving.py", line 229, in load_model
model = model_from_config(model_config, custom_objects=custom_objects)
File "/home/spt/.conda/envs/dev_env/lib/python3.6/site-packages/tensorflow/python/keras/engine/saving.py", line 306, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File "/home/spt/.conda/envs/dev_env/lib/python3.6/site-packages/tensorflow/python/keras/layers/serialization.py", line 64, in deserialize
printable_module_name='layer')
File "/home/spt/.conda/envs/dev_env/lib/python3.6/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 173, in deserialize_keras_object
list(custom_objects.items())))
File "/home/spt/.conda/envs/dev_env/lib/python3.6/site-packages/tensorflow/python/keras/engine/sequential.py", line 286, in from_config
layer = layer_module.deserialize(conf, custom_objects=custom_objects)
File "/home/spt/.conda/envs/dev_env/lib/python3.6/site-packages/tensorflow/python/keras/layers/serialization.py", line 64, in deserialize
printable_module_name='layer')
File "/home/spt/.conda/envs/dev_env/lib/python3.6/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 193, in deserialize_keras_object
function_name)
ValueError: Unknown layer:name
我遇到了多个描述相同/相似问题的网站,例如stack overflow,github。通常,问题是Keras的过时版本。但就我而言,所有与Keras相关的软件包都是最新的(所有与keras相关的软件包的conda列表输出):
keras-applications 1.0.6 py36_0
keras-base 2.2.4 py36_0
keras-gpu 2.2.4 0
keras-preprocessing 1.0.5 py36_0
有人可以建议我如何解决/解决此问题吗?
答案 0 :(得分:1)
如果您使用的是自定义图层,则可以使用以下图层加载keras模型:
model = keras.models.load_model(model_path, custom_objects={'MyCustomLayer': InstanceOfMyCustomLayer})
答案 1 :(得分:0)
我遇到了同样的问题,并且在我更新Tensorflow和Keras版本时已经解决了