gpt4 book ai didi

python - 如何从数据集中创建锚定正负对来训练模型?

转载 作者:行者123 更新时间:2023-12-05 07:20:41 27 4
gpt4 key购买 nike

我正在尝试将一次性学习应用于人脸识别。我的数据集目录中有几张不同人的照片,我想训练我的模型,但问题是我无法弄清楚如何从数据集目录中提供正锚定和负锚定对。

我已经构建了一个自定义的 convNet 模型并定义了三元组损失(如 deeplearning.ai 类(class)中所述)。

我的模型

model = models.Sequential()

model.add(layers.Conv2D(16, (3,3), (3,3), activation='relu', input_shape=(384, 384, 1)))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.BatchNormalization())

for t in range(2):
model.add(layers.Conv2D(32, (1,1), (1,1), activation='relu'))
model.add(layers.Conv2D(32, (3,3), (1,1), padding='same', activation='relu'))
model.add(layers.Conv2D(64, (1,1), (1,1), activation='relu'))
model.add(layers.BatchNormalization())
model.add(layers.MaxPooling2D((2,2)))

for t in range(3):
model.add(layers.Conv2D(64, (1,1), (1,1), activation='relu'))
model.add(layers.Conv2D(64, (3,3), (1,1), padding='same', activation='relu'))
model.add(layers.Conv2D(128, (1,1), (1,1), activation='relu'))
model.add(layers.BatchNormalization())
model.add(layers.MaxPooling2D((2,2)))

for t in range(4):
model.add(layers.Conv2D(128, (1,1), (1,1), activation='relu'))
model.add(layers.Conv2D(128, (3,3), (1,1), padding='same', activation='relu'))
model.add(layers.Conv2D(256, (1,1), (1,1), activation='relu'))
model.add(layers.BatchNormalization())
model.add(layers.MaxPooling2D((2,2)))

for t in range(3):
model.add(layers.Conv2D(256, (1,1), (1,1), activation='relu'))
model.add(layers.Conv2D(256, (3,3), (1,1), padding='same', activation='relu'))
model.add(layers.Conv2D(512, (1,1), (1,1), activation='relu'))
model.add(layers.BatchNormalization())
model.add(layers.AveragePooling2D((4,4)))

model.add(layers.Flatten())
model.add(layers.Dense(128))

model.add(layers.Lambda(lambda x: backend.l2_normalize(x,axis=1)))

三重损失

def triplet_loss(y_true, y_pred, alpha = 0.3):
"""
Implementation of the triplet loss as defined by formula (3)

Arguments:
y_pred -- python list containing three objects:
anchor -- the encodings for the anchor images, of shape (None, 128)
positive -- the encodings for the positive images, of shape (None, 128)
negative -- the encodings for the negative images, of shape (None, 128)

Returns:
loss -- real number, value of the loss
"""

anchor, positive, negative = y_pred[0], y_pred[1], y_pred[2]

# Step 1: Compute the (encoding) distance between the anchor and the positive, you will need to sum over axis=-1
pos_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, positive)), axis=-1)
# Step 2: Compute the (encoding) distance between the anchor and the negative, you will need to sum over axis=-1
neg_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, negative)), axis=-1)
# Step 3: subtract the two previous distances and add alpha.
basic_loss = tf.add(tf.subtract(pos_dist, neg_dist), alpha)
# Step 4: Take the maximum of basic_loss and 0.0. Sum over the training examples.
loss = tf.reduce_sum(tf.maximum(basic_loss, 0.0))

return loss

模型编译

model.compile(optimizer='adam',loss='triplet_loss',metrics=['accuracy'])

请帮助我制作用于训练的正锚定和负锚定对。在这方面,我不知道如何处理数据集目录!

最佳答案

可以通过多种方式找到三元组以使用三元组损失函数训练连体神经网络。原文FaceNet论文描述了硬三元组(hard positives,positives 这样 argmax||f(anchor)-f(positive)||^2 和 hard negatives,negatives 这样 argmin||f(anchor)-f(negative)||^2 其中 f 是神经网络的嵌入。

然而,在我的一个 Siamese 网络中,我随机选择了 (anchor,positive,negative) 三元组,结果证明它具有很好的分类精度。因此,您可以先尝试随机三元组选择,因为硬三元组选择通常计算量大且需要 CPU 集群。

我希望你已经标记了数据集中的所有图像,并且标签应该反射(reflect)特定图像所指的人。例如,如果你有 5 张 A 的图像,标签应该看起来像 (A_1.jpg, A_2.jpg,...A_5.jpg) 或者你应该为每个都有一个单独的目录人。您可以从一个目录中随机选择一张图像作为 anchor ,从同一目录中选择一张图像作为正片,从不同目录中选择一张图像作为负片。以三元组格式 (anchor,positive,negative) 捆绑此图像并重复该过程以创建一个批处理。那里有一批训练图像。

我只是介绍了执行此操作的基本过程,但是,如果您正在寻找示例代码,this教程可以帮助您创建一批三元组来训练网络。

关于python - 如何从数据集中创建锚定正负对来训练模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57428524/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com