gpt4 book ai didi

python - 值错误 : You are trying to load a weight file containing 6 layers into a model with 0

转载 作者:太空宇宙 更新时间:2023-11-03 13:57:10 25 4
gpt4 key购买 nike

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

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

用于实例化模型和使用 model.load_weights 并进行模型总结。当我使用 print(model) 打印模型时,我得到 None

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

训练过程脚本:

import numpy as np
from keras import optimizers
from keras import losses
from sklearn.model_selection import train_test_split
from model import create_model


def data_loader(images, pos):
while(True):
for i in range(0, images.shape[0], 64):
if (i+64) < images.shape[0]:
img_batch = images[i:i+64]
pos_batch = pos[i:i+64]
yield img_batch, pos_batch
else:
img_batch = images[i:]
pos_batch = pos[i:]
yield img_batch, pos_batch


def main():
model = create_model()
sgd = optimizers.Adadelta(lr=0.01, rho=0.95, epsilon=None, decay=0.0)
model.compile(loss=losses.mean_squared_error, optimizer=sgd)
print("traning")
data = np.load("data.npz")
images = data['images']
pos = data['pos']
x_train, x_test, y_train, y_test = train_test_split(images, pos, test_size=0.33, random_state=42)
model.fit_generator(data_loader(x_train, y_train), steps_per_epoch=x_train.shape[0]//64, validation_data=data_loader(x_test, y_test), \
validation_steps = x_test.shape[0]//64, epochs=1)
model.save('test.h5')
model.save_weights('test_weights.h5')

print("training done")


if __name__ == '__main__':
main()

最佳答案

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

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

    似乎具有InputLayer 的模型没有正确序列化为HDF5

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

  3. 按照说明修复解释器问题 here

关于python - 值错误 : You are trying to load a weight file containing 6 layers into a model with 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53993348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com