我正在尝试加载我使用Tensorflow和Keras训练和保存的模型,但它给了我一个错误 .
Python版本:3.6.6
Tensorflow版本:1.11.0
输出:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/packages/tensorflow/1.11.0/Python-3.6.6/tensorflow/python/keras/engine/saving.py", line 230, in load_model
model = model_from_config(model_config, custom_objects=custom_objects)
File "/packages/tensorflow/1.11.0/Python-3.6.6/tensorflow/python/keras/engine/saving.py", line 310, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File "/packages/tensorflow/1.11.0/Python-3.6.6/tensorflow/python/keras/layers/serialization.py", line 64, in deserialize
printable_module_name='layer')
File "/packages/tensorflow/1.11.0/Python-3.6.6/tensorflow/python/keras/utils/generic_utils.py", line 173, in deserialize_keras_object
list(custom_objects.items())))
File "/packages/tensorflow/1.11.0/Python-3.6.6/tensorflow/python/keras/engine/sequential.py", line 339, in from_config
custom_objects=custom_objects)
File "/packages/tensorflow/1.11.0/Python-3.6.6/tensorflow/python/keras/layers/serialization.py", line 64, in deserialize
printable_module_name='layer')
File "/packages/tensorflow/1.11.0/Python-3.6.6/tensorflow/python/keras/utils/generic_utils.py", line 175, in deserialize_keras_object
return cls.from_config(config['config'])
File "/packages/tensorflow/1.11.0/Python-3.6.6/tensorflow/python/keras/engine/base_layer.py", line 1617, in from_config
return cls(**config)
File "/packages/tensorflow/1.11.0/Python-3.6.6/tensorflow/python/keras/layers/advanced_activations.py", line 310, in __init__
if max_value is not None and max_value < 0.:
TypeError: '<' not supported between instances of 'dict' and 'float'
我也试过保存权重而不是整个模型,但这似乎不太成功:
回溯(最近一次调用最后一次):文件“predict_from_NN.py”,第44行,in
model.load_weights('/home/me/Data/Out/finished_model_2_weights.hdf5.index')
File "/packages/tensorflow/1.11.0/Python-3.6.6/tensorflow/python/keras/engine/network.py", line 1526, in load_weights
checkpointable_utils.streaming_restore(status=status, session=session)
File "/packages/tensorflow/1.11.0/Python-3.6.6/tensorflow/python/training/checkpointable/util.py", line 880, in streaming_restore
"Streaming restore not supported from name-based checkpoints. File a "
NotImplementedError: Streaming restore not supported from name-based checkpoints. File a feature request if this limitation bothers you.
虽然我不确定为什么/如何进行“流式恢复”,但谷歌在这两种情况下都不是很有用 .
如果它有帮助,这是我的模型的代码:
from tensorflow.python.keras.layers import Conv2D, MaxPooling2D, ReLU
来自tensorflow.keras.models从tensorflow.keras.layers导入顺序导入Flatten,Activation,Dense
def cnn_model(img_rows, img_cols, img_channels):
model = Sequential()
model.add(Conv2D(64, (3, 3),activation='linear',kernel_initializer='he_uniform',
input_shape=(img_rows, img_cols, img_channels)))
model.add(ReLU()) # add an advanced activation
model.add(MaxPooling2D(pool_size=(5, 5)))
model.add(Conv2D(32, (3, 3),activation='linear',kernel_initializer='he_uniform'))
model.add(ReLU()) # add an advanced activation
model.add(MaxPooling2D(pool_size=(3, 3)))
model.add(Conv2D(16, (3, 3),activation='linear',kernel_initializer='he_uniform'))
model.add(ReLU()) # add an advanced activation
model.add(MaxPooling2D(pool_size=(3, 3)))
model.add(Flatten())
model.add(Dense(1024))
model.add(Dense(1024))
model.add(ReLU()) # add an advanced activation
model.add(Dense(4))
model.add(Activation('softmax'))
return model
我保存我的模型:
model.save(os.path.join(output_folder, model_name + '_GPU.hdf5'))
并尝试像这样加载它:
from tensorflow.python.keras.models import load_model
model = load_model(model_file)
1 回答
您是否尝试使用“to_json”功能保存和加载模型,如下所述?
P.S:我从here借了这个代码 .