Question about focal loss implementation.
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.
RegL1Loss has the same problem(maybe not a problem?)
That's a good point. I am not aware of that. Will investigate it later.
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..

@Markusgami What is the effect of your verification?I have also noticed this problem.
@Markusgami is the 'correct version' of loss work?
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
Hi @Markusgami could you share your "corrected loss" implementation ? thanks
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
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!
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.
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!