0

我在 Siamese 网络上使用 Triplet Loss 创建了一个人脸识别模型。

  1. 该模型在使用小数据集进行训练时过拟合。(看起来模型工作正常)
  2. 损失正在完美减少。

问题是当我在测试数据上使用这个模型时它不能正常工作。也许问题出在训练数据集上。我的训练数据的总大小是 11,000,其中只有 2000 - 2500 是唯一图像。单个数据包含(锚、正、负)图像。

我可以做些什么来提高模型的性能吗?(请帮帮我)

损失类

class TripletLoss(nn.Module):
    def __init__(self, device, margin):
        super(TripletLoss, self).__init__()
        self.margin = margin
        self.device = device

    def forward(self, anchor, positive, negative):
        # anchor, positive, negative are the activation values of last hidden 
        # layer of the siamese network. layer of size 200.
        
        # anchor, positive, negative shape -> (batch_size, 200)

        loss = torch.clamp(torch.sum(torch.square(anchor - positive), dim=1)
                           - torch.sum(torch.square(anchor - negative), dim=1)
                           + self.margin, min=0)
        return torch.mean(loss)

    def distance(self, output1, output2):
        diff = F.pairwise_distance(output1, output2)
        return diff
4

0 回答 0