我正在尝试用 Keras 来实现 Siamese Network 来实现一次性人脸识别模型。但我遇到了一个我无法理解的错误,需要一些帮助。
我使用的模型是一个编码器模型,它接收(299,299,3)图像(锚定图像、正图像以及负图像)并输出1000每个图像的维度编码向量。这类似于带有分类头的 InceptionV3 模型。我还使用自定义三元组损失函数来实现同样的目的。我的模型如下:
class SiameseNet(tf.keras.layers.Layer):
def __init__(self, model):
self.model = model # This is the image feature extraction model (similar to InceptionV3)
super().__init__()
def call(self, feat):
feats = self.model(feat[0])
nfeats = self.model(feat[1])
return [feats, nfeats]
Run Code Online (Sandbox Code Playgroud)
损失函数如下
def triplet_loss(y_true, y_pred, alpha=1e-2):
return max(tf.reduce_sum((y_pred[0]-y_true)**2 - (y_pred[0]-y_pred[1])**2) + alpha, 0)
Run Code Online (Sandbox Code Playgroud)
共有三个数组,分别命名为images(锚图像) 和negatives(负图像),形状均为(500,299,299,3)(其中 500 是训练示例的数量) 和positives(正图像特征),形状均为(500,1000)。所有这些都是 numpy 数组。
我的模型代码如下所示
image_input = tf.keras.layers.Input(shape=(299,299,3), name='image_input')
negative_input …Run Code Online (Sandbox Code Playgroud)