3

我正在使用 Keras 为 224x224x3 大小的图像开发一个用于人脸识别的连体网络。Siamese Network 的架构是这样的:

连体网络结构

对于 CNN 模型,我正在考虑使用已经在 Keras.applications 模块中预训练的 InceptionV3 模型。

#Assume all the other modules are imported correctly

from keras.applications.inception_v3 import InceptionV3

IMG_SHAPE=(224,224,3)

def return_siamese_net():

  left_input=Input(IMG_SHAPE)
  right_input=Input(IMG_SHAPE)

  model1=InceptionV3(include_top=False, weights="imagenet", input_tensor=left_input) #Left SubConvNet
  model2=InceptionV3(include_top=False, weights="imagenet", input_tensor=right_input) #Right SubConvNet

  #Do Something here

  distance_layer = #Do Something
  prediction = Dense(1,activation='sigmoid')(distance_layer) # Outputs 1 if the images match and 0 if it does not

  siamese_net = #Do Something  
  return siamese_net

model=return_siamese_net()
  

由于模型是预训练的,我得到了错误,我现在被困在为孪生网络实现距离层。

为了使这个连体网络正常工作,我应该在两者之间添加什么?

4

1 回答 1

1

在使用距离层之前,一个非常重要的注意事项是考虑到您只有一个卷积神经网络。

共享权重实际上仅指一个卷积神经网络,并且权重是共享的,因为在传递一对图像时使用相同的权重(取决于使用的损失函数)以计算特征以及随后每个输入的嵌入图片。

您将只有一个神经网络,并且块逻辑需要如下所示:

def euclidean_distance(vectors):
    (features_A, features_B) = vectors
    sum_squared = K.sum(K.square(features_A - features_B), axis=1, keepdims=True)
    return K.sqrt(K.maximum(sum_squared, K.epsilon()))


image_A = Input(shape=...)
image_B = Input(shape=...)
feature_extractor_model = get_feature_extractor_model(shape=...)
features_A = feature_extractor(image_A)
features_B = feature_extractor(image_B)
distance = Lambda(euclidean_distance)([features_A, features_B])
outputs = Dense(1, activation="sigmoid")(distance)
siamese_model = Model(inputs=[image_A, image_B], outputs=outputs)

当然,特征提取器模型可以是来自 Keras/TensorFlow 的预训练网络,并改进了输出分类层。

主要逻辑应该和上面一样,当然,如果你想使用三元组损失,那将需要三个输入(Anchor、Positive、Negative),但一开始我建议坚持基础。

此外,最好查阅此文档:

  1. https://www.pyimagesearch.com/2020/11/30/siamese-networks-with-keras-tensorflow-and-deep-learning/
  2. https://towardsdatascience.com/one-shot-learning-with-siamese-networks-using-keras-17f34e75bb3d
于 2021-01-07T08:28:31.293 回答