CenterNet icon indicating copy to clipboard operation
CenterNet copied to clipboard

Question about focal loss implementation.

Open Markusgami opened this issue 7 years ago • 11 comments

CenterNet is a very good detection algorithm. It acheives 10% more accuracy on our dataset than our previous algorithm. Thanks for the work! But I have a question on the focal loss implementation:

def _neg_loss(pred, gt):
    ''' Modified focal loss. Exactly the same as CornerNet.
        Runs faster and costs a little bit more memory
      Arguments:
        pred (batch x c x h x w)
        gt_regr (batch x c x h x w)
    '''
    pos_inds = gt.eq(1).float()
    neg_inds = gt.lt(1).float()

    neg_weights = torch.pow(1 - gt, 4)

    loss = 0

    pos_loss = torch.log(pred) * torch.pow(1 - pred, 2) * pos_inds
    neg_loss = torch.log(1 - pred) * torch.pow(pred, 2) * neg_weights * neg_inds

    num_pos = pos_inds.float().sum()
    pos_loss = pos_loss.sum()
    neg_loss = neg_loss.sum()

    if num_pos == 0:
        loss = loss - neg_loss
    else:
        loss = loss - (pos_loss + neg_loss) / num_pos

    return loss
~~~~~~~~~~~~~~

if num_pos == 0:
        loss = loss - neg_loss --->lossA
else:
        loss = loss - (pos_loss + neg_loss) / num_pos  --->lossB

If there is any positive in this batch of data, this loss function will return lossB even though maybe most of the data in batch has no positive..
So for a img, it will have different affect on loss accordding to different batch composition.
I don't understand if this will have any good or bad effect on training.


Markusgami avatar Jul 31 '19 10:07 Markusgami

RegL1Loss has the same problem(maybe not a problem?)

Markusgami avatar Jul 31 '19 10:07 Markusgami

That's a good point. I am not aware of that. Will investigate it later.

xingyizhou avatar Aug 01 '19 17:08 xingyizhou

I'm training the Network on "Correct version" of loss now. We will see if it's getting better or just nothing happend.. I noticed this implementation is from CornerNet..So their code is not same with their paper..

image

Markusgami avatar Aug 02 '19 05:08 Markusgami

@Markusgami What is the effect of your verification?I have also noticed this problem.

shuoshuoa avatar Aug 06 '19 02:08 shuoshuoa

@Markusgami is the 'correct version' of loss work?

realwill avatar Aug 08 '19 06:08 realwill

Sorry.. Only thing I know now is that "corrected loss" gives comparable results...I trained with some other changes to the CenterNet and GPU is always busy, so I still don't know the isolated effect on the loss function

Markusgami avatar Aug 13 '19 00:08 Markusgami

Hi @Markusgami could you share your "corrected loss" implementation ? thanks

rafikg avatar Aug 13 '19 13:08 rafikg

def _neg_loss(pred, gt): ''' Modified focal loss. Exactly the same as CornerNet. Runs faster and costs a little bit more memory Arguments: pred (batch x c x h x w) gt_regr (batch x c x h x w) ''' pos_inds = gt.eq(1).float() neg_inds = gt.lt(1).float()

neg_weights = torch.pow(1 - gt, 4)

lossA = 0 lossB = 0

pos_loss = torch.log(pred) * torch.pow(1 - pred, 2) * pos_inds neg_loss = torch.log(1 - pred) * torch.pow(pred, 2) * neg_weights * neg_inds

num_pos = pos_inds.float().sum((1, 2, 3)) pos_loss = pos_loss.sum((1, 2, 3)) neg_loss = neg_loss.sum((1, 2, 3))

mask = 1- (num_pos==0)

lossA = lossA - neg_loss lossB = lossB - (pos_loss + neg_loss) / (num_pos+1e-4)

loss = mask.float() * lossB + (1-mask).float() * lossA return loss

class RegL1Loss(nn.Module): def init(self): super(RegL1Loss, self).init()

def forward(self, output, mask, ind, target): pred = _tranpose_and_gather_feat(output, ind) mask = mask.unsqueeze(2).expand_as(pred).float() loss2 = torch.abs((pred * mask - target * mask)).sum((1, 2)) loss2 = loss2 / (mask.sum((1, 2)) + 1e-4) return loss2

CenterNet will take mean for the loss in base_trainer.py

Markusgami avatar Aug 16 '19 08:08 Markusgami

Thanks @Markusgami. here if I understand correctly, you did not sum over the batch axis, so this function will return a batch of loss (a batch of scalar ??), is it right? and after that you take the mean of 3 losses: loss = mean(L_keypoints + L_offset + 0.1* L_size)? If is it correct, I don't know how to do it in keras because by definition in keras, a loss function return a scalar!

rafikg avatar Aug 17 '19 03:08 rafikg

Yeah..Before the change, the loss function return one loss value for the whole batch directly. After my change, the loss function will return a vector which contains one loss value for every single image in the batch..You need to take the mean across the batch before do the backward.

Markusgami avatar Aug 18 '19 00:08 Markusgami

Thanks @Markusgami Let's take an example:

target = array([[[[3, 1, 3],
         [3, 1, 3]],

        [[4, 4, 4],
         [0, 0, 4]]],


       [[[1, 1, 3],
         [1, 3, 4]],

        [[0, 1, 0],
         [2, 3, 1]]]])
pred = array([[[[4, 4, 4],
         [4, 1, 0]],

        [[0, 2, 1],
         [2, 3, 4]]],


       [[[4, 0, 2],
         [0, 1, 2]],

        [[3, 0, 2],
         [3, 3, 2]]]])
target.shape = (2, 2, 2, 3) # (batch, w, h, channel)
pred.shape=(2, 2, 2, 3) # (batch, w, h, channel)
Let's calculate your  `RegL1Loss` (we omit the mask)
abs_value = np.abs((pred * mask - target * mask)).sum((1, 2))
print(abs_value)
array([[8, 8, 7],
       [8, 4, 6]])
print(abs_value.shape)
(2, 3) # (batch, channel)
# take the mean over the batch 
print(abs_value.mean(0))
array([8. , 6. , 6.5])
We got a vector ! Sure I miss something!

rafikg avatar Aug 18 '19 03:08 rafikg