ValueError:您正在尝试将包含6层的权重文件加载到模型中,其值为0

时间:2019-01-01 06:02:22

标签: python keras neural-network deep-learning

我有一个简单的keras模型。保存模型后。我无法加载模型。这是实例化模型并尝试加载权重后得到的错误:

SELECT id, track_id, 1 AS play_count

用于实例化模型并使用model.load_weights进行模型汇总。当我使用print(model)打印模型时,我什么也没得到

Using TensorFlow backend.
Traceback (most recent call last):
  File "test.py", line 4, in <module>
    model = load_model("test.h5")
  File "/usr/lib/python3.7/site-packages/keras/engine/saving.py", line 419, in load_model
  model = _deserialize_model(f, custom_objects, compile)
  File "/usr/lib/python3.7/site-packages/keras/engine/saving.py", line 258, in _deserialize_model
.format(len(layer_names), len(filtered_layers))
 ValueError: You are trying to load a weight file containing 6 layers into a model with 0 layers

这是我的网络:

Traceback (most recent call last):
File "test.py", line 7, in <module>
    print(model.summary())
AttributeError: 'NoneType' object has no attribute 'summary'

培训过程脚本:

from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, InputLayer, Flatten,    Dense, BatchNormalization


def create_model():
    kernel_size = 5
    pool_size = 2
    batchsize = 64
    model = Sequential()
    model.add(InputLayer((36, 120, 1)))
    model.add(Conv2D(filters=20, kernel_size=kernel_size,    activation='relu', padding='same'))
    model.add(BatchNormalization())
    model.add(MaxPooling2D(pool_size))
    model.add(Conv2D(filters=50, kernel_size=kernel_size, activation='relu', padding='same'))
    model.add(BatchNormalization())
    model.add(MaxPooling2D(pool_size))
    model.add(Flatten())
    model.add(Dense(120, activation='relu'))
    model.add(Dense(2, activation='relu'))
    return model

1 个答案:

答案 0 :(得分:4)

  1. 拖放InputLayer并在第一层使用input_shape。您的代码将类似于:

    model = Sequentional()
    model.add(Conv2D(filters=20,..., input_shape=(36, 120, 1)))

    似乎带有InputLayer的模型未正确序列化到HDF5

  2. 将Tensorflow和Keras升级到最新版本

  3. 按照解释的here

  4. 解决解释器问题