Я определил типичную сиамскую сетевую архитектуру, чтобы получить кодировки, я использовал temp_model (модель VGG с весом, предварительно обученным с функцией потери триплета), в приведенном ниже коде, наконец, я обучил модель и сохранил на диск как файл h5, но когда я загружаю Модель для прогнозирования, я получил ошибку (ValueError: Неверный аргумент input_shape [(Нет, 224, 224, 3), (Нет, 224, 224, 3), (Нет, 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
' '' -------------------------- ------------------------------------------------- ValueError Обратная трассировка (последний вызов был последним) в 1 ''
model_siamese = siamese_model()
from keras.callbacks import ModelCheckpoint
checkpoint = ModelCheckpoint('triplet_loss_function_vgg16_siamese_h512_10_128.h5', verbose=1, monitor='val_loss',save_best_only=True, mode='auto')
hist = model_siamese.fit_generator(generate_batch_siamese(batch_size=128, validation=False),epochs=1000, steps_per_epoch=int((total * 0.8) / 32),
validation_steps=int((total * 0.10) / 32),
validation_data=generate_batch_siamese(batch_size=128, validation=True), callbacks=[checkpoint], use_multiprocessing=True)
final_model = load_model("triplet_loss_function_vgg16_siamese_h100_128.h5")
'' '