加载tensorflow训练的Keras模型时出错

问题描述 投票:0回答:3

我正在尝试加载我使用 Tensorflow 和 Keras 训练和保存的模型,但它给了我一个错误。

Python版本:3.6.6

张量流版本: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'

我也尝试过只保存权重而不是整个模型,但这似乎并不更成功:

Traceback (most recent call last):
  File "predict_from_NN.py", line 44, in <module>

        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导入扁平化,激活,密集

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)
python tensorflow keras
3个回答
1
投票

您是否尝试过使用“to_json”函数保存和加载模型,如下所述?

from keras.models import model_from_json
[...]
# serialize model to JSON
model_json = model.to_json()
with open("model.json", "w") as json_file:
    json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("model.h5")
print("Saved model to disk")

# later...

# load json and create model
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("model.h5")
print("Loaded model from disk")

P.S:我从这里借用了这个代码。


1
投票

尝试更新 Keras,因为我也遇到了类似的错误,更新存储库有帮助。 在终端中使用 github 链接更新 keras

git clone https://github.com/fchollet/keras.git
。在终端重定向到位置并运行
sudo python3 setup.py install


0
投票

我在 Google Colab 上复制了你的代码,效果很好。

Tf version:  2.2.0-rc3
Keras version:  2.3.1

尝试更新您的软件包。

© www.soinside.com 2019 - 2024. All rights reserved.