我定義了一個典型的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 imagesencoded_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 scoreprediction = Dense(1,activation='sigmoid')(L1_distance)# Connect the inputs with the outputssiamese_net = Model(inputs=[left_input,right_input],outputs=prediction)siamese_net.compile(loss='binary_crossentropy', optimizer="adam", metrics=['accuracy'])siamese_net.summary()# return the modelreturn siamese_net “'” ---------------------------------------------------------------------------值錯誤回溯(最近一次調用最后一次)在 1 #final_model = siamese_model() ----> 2 final_model = load_model(“triplet_loss_function_vgg16_siamese_h100_128.h5”)
2 回答

MMTTMM
TA貢獻1869條經驗 獲得超4個贊
嘗試將用于生成編碼的代碼部分更改為
# Generate the encodings (feature vectors) for the two images
encoded_l = temp_model(left_input)
encoded_r = temp_model(right_input)

冉冉說
TA貢獻1877條經驗 獲得超1個贊
這是加載嵌套模型時的常見問題,此問題沒有單一的答案,但是有一些有用的鏈接,您可以在其中獲得解決此類問題的提示。https://github.com/keras-team/keras/pull/11847
在我的情況下,我重新定義了一個架構(與我的訓練相同),將可訓練參數設置為false,而不是使用load_model我使用load_weights,它對我有用。正如我所說,沒有單一的答案,你必須測試并嘗試不同的選擇。
添加回答
舉報
0/150
提交
取消