猿问

获取错误:在 Keras 中加载模型时:

我定义了一个典型的siamese网络架构,以获得我使用的编码temp_model(VGG模型,权重预训练有三重损失函数),在下面的代码中,最后我训练模型并作为h5文件保存到我的磁盘上,但是当我加载模型进行预测时,我得到了一个错误(ValueError:无效input_shape参数[(None,224, 224, 3), (None, 224, 224, 3), (None, 224, 224, 3)]: 模型有 1 个张量输入。


'''


left_input = Input(shape = (224, 224, 3))

right_input = Input(shape = (224, 224, 3))


# Generate the encodings (feature vectors) for the two images

encoded_l = temp_model([left_input,left_input,left_input])

encoded_r = temp_model([right_input,right_input,right_input])


# Add a customized layer to compute the absolute difference between the encodings 

L1_layer = Lambda(lambda tensors:K.abs(tensors[0] - tensors[1]))

L1_distance = L1_layer([encoded_l, encoded_r])


L1_distance = Dense(512,activation='relu')(L1_distance)

L1_distance = Dropout(0.2)(L1_distance)


L1_distance = Dense(10,activation='relu')(L1_distance)

L1_distance = Dropout(0.2)(L1_distance)


# Add a dense layer with a sigmoid unit to generate the similarity score

prediction = Dense(1,activation='sigmoid')(L1_distance)


# Connect the inputs with the outputs

siamese_net = Model(inputs=[left_input,right_input],outputs=prediction)


siamese_net.compile(loss='binary_crossentropy', optimizer="adam",

      metrics=['accuracy'])


siamese_net.summary()


# return the model

return siamese_net 

“'” ---------------------------------------------------------------------------值错误回溯(最近一次调用最后一次)在 1 #final_model = siamese_model() ----> 2 final_model = load_model(“triplet_loss_function_vgg16_siamese_h100_128.h5”)


森林海
浏览 144回答 2
2回答

MMTTMM

尝试将用于生成编码的代码部分更改为# Generate the encodings (feature vectors) for the two imagesencoded_l = temp_model(left_input)encoded_r = temp_model(right_input)

冉冉说

这是加载嵌套模型时的常见问题,此问题没有单一的答案,但是有一些有用的链接,您可以在其中获得解决此类问题的提示。https://github.com/keras-team/keras/pull/11847在我的情况下,我重新定义了一个架构(与我的训练相同),将可训练参数设置为false,而不是使用load_model我使用load_weights,它对我有用。正如我所说,没有单一的答案,你必须测试并尝试不同的选择。
随时随地看视频慕课网APP

相关分类

Python
我要回答