Esempio n. 1
0
    def forward(self, outputs, labels):
        cross_entropy_loss = CrossEntropyLoss(num_classes=751)
        triplet_loss = TripletLoss(margin=1.2)

        Triplet_Loss = triplet_loss(outputs[:1], labels)
        Triplet_Loss = sum(Triplet_Loss) / len(Triplet_Loss)

        CrossEntropy_Loss = [cross_entropy_loss(output, labels) for output in outputs[4:]]
        CrossEntropy_Loss = sum(CrossEntropy_Loss) / len(CrossEntropy_Loss)

        loss_sum = Triplet_Loss + 2 * CrossEntropy_Loss

        # print('\rtotal loss:%.2f  Triplet_Loss:%.2f  CrossEntropy_Loss:%.2f' % (
        #     loss_sum.data.cpu().numpy(),
        #     Triplet_Loss.data.cpu().numpy(),
        #     CrossEntropy_Loss.data.cpu().numpy()),
        #       end=' ')
        return loss_sum
Esempio n. 2
0
    def __init__(self,
                 datamanager,
                 model,
                 optimizer,
                 margin=0.3,
                 weight_t=1,
                 weight_x=1,
                 scheduler=None,
                 use_gpu=True,
                 label_smooth=True):
        super(ImageTripletEngine, self).__init__(datamanager, model, optimizer,
                                                 scheduler, use_gpu)

        self.weight_t = weight_t
        self.weight_x = weight_x

        self.criterion_t = TripletLoss(margin=margin)
        self.criterion_x = CrossEntropyLoss(
            num_classes=self.datamanager.num_train_pids,
            use_gpu=self.use_gpu,
            label_smooth=label_smooth)