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

时间:2018-10-31 08:26:06

标签: python tensorflow keras

我正在尝试加载使用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行
    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.

尽管我不确定我为什么/如何进行“流媒体还原”,并且在这两种情况下google都不是很有用。

如果有帮助,这是我的模型的代码:

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)

3 个答案:

答案 0 :(得分:0)

您是否尝试过使用“ 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:我从here借来了这段代码。

答案 1 :(得分:0)

尝试更新Keras,因为我也遇到类似的错误,因此更新存储库很有帮助。 在终端中使用github链接更新keras git clone https://github.com/fchollet/keras.git。在终端中重定向到位置并运行     sudo python3 setup.py install。希望这对您也有帮助。

答案 2 :(得分:0)

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

Tf version:  2.2.0-rc3
Keras version:  2.3.1

尝试更新您的软件包。 祝你好运!