center_loss.py 3.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.

littletomatodonkey's avatar
littletomatodonkey 已提交
15 16
# This code is refer from: https://github.com/KaiyangZhou/pytorch-center-loss

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import pickle

import paddle
import paddle.nn as nn
import paddle.nn.functional as F


class CenterLoss(nn.Layer):
    """
    Reference: Wen et al. A Discriminative Feature Learning Approach for Deep Face Recognition. ECCV 2016.
    """
littletomatodonkey's avatar
littletomatodonkey 已提交
32

33 34 35 36 37 38 39 40 41
    def __init__(self,
                 num_classes=6625,
                 feat_dim=96,
                 init_center=False,
                 center_file_path=None):
        super().__init__()
        self.num_classes = num_classes
        self.feat_dim = feat_dim
        self.centers = paddle.randn(
42
            shape=[self.num_classes, self.feat_dim]).astype("float64")
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

        if init_center:
            assert os.path.exists(
                center_file_path
            ), f"center path({center_file_path}) must exist when init_center is set as True."
            with open(center_file_path, 'rb') as f:
                char_dict = pickle.load(f)
                for key in char_dict.keys():
                    self.centers[key] = paddle.to_tensor(char_dict[key])

    def __call__(self, predicts, batch):
        assert isinstance(predicts, (list, tuple))
        features, predicts = predicts

        feats_reshape = paddle.reshape(
            features, [-1, features.shape[-1]]).astype("float64")
        label = paddle.argmax(predicts, axis=2)
        label = paddle.reshape(label, [label.shape[0] * label.shape[1]])

        batch_size = feats_reshape.shape[0]

64 65 66 67 68
        #calc l2 distance between feats and centers  
        square_feat = paddle.sum(paddle.square(feats_reshape),
                                 axis=1,
                                 keepdim=True)
        square_feat = paddle.expand(square_feat, [batch_size, self.num_classes])
69

70 71 72 73 74 75
        square_center = paddle.sum(paddle.square(self.centers),
                                   axis=1,
                                   keepdim=True)
        square_center = paddle.expand(
            square_center, [self.num_classes, batch_size]).astype("float64")
        square_center = paddle.transpose(square_center, [1, 0])
76

77 78 79 80
        distmat = paddle.add(square_feat, square_center)
        feat_dot_center = paddle.matmul(feats_reshape,
                                        paddle.transpose(self.centers, [1, 0]))
        distmat = distmat - 2.0 * feat_dot_center
81 82 83 84 85 86 87

        #generate the mask
        classes = paddle.arange(self.num_classes).astype("int64")
        label = paddle.expand(
            paddle.unsqueeze(label, 1), (batch_size, self.num_classes))
        mask = paddle.equal(
            paddle.expand(classes, [batch_size, self.num_classes]),
88
            label).astype("float64")
89
        dist = paddle.multiply(distmat, mask)
90

91 92
        loss = paddle.sum(paddle.clip(dist, min=1e-12, max=1e+12)) / batch_size
        return {'loss_center': loss}