使用load_model或load_weights时的ValueError

时间:2018-06-15 12:57:48

标签: python keras

我构建了一个简单的lenet模型:

def get_lenet():
    kernel_size = (5, 5)
    model = Sequential()  # each image is 28x28
    model.add(Conv2D(32, kernel_size, activation='relu'))  # now 24x24
    model.add(MaxPool2D())  # 12x12
    model.add(Conv2D(64, kernel_size, activation='relu'))  # 8x8
    model.add(MaxPool2D())  # 4x4
    model.add(Flatten())
    model.add(Dense(128, activation='relu'))
    model.add(Dense(10, activation='softmax'))
    return model

编译和训练一切顺利,然后保存模型:

model.save_weights('w.h5')
with open('model_architecture.json', 'w') as f:
    f.write(model.to_json())

稍后,当试图加载

with open('model_architecture.json', 'r') as f:
    model = model_from_json(f.read())
model.load_weights('w.h5)

我收到以下错误:

ValueError: You are trying to load a weight file containing 4 layers into a model with 0 layers.

使用save_model时会出现同样的错误。 有没有人有想法?

0 个答案:

没有答案