Computes the precision@k for the specified values of k
(output, target, topk=(1,))
| 23 | |
| 24 | @torch.no_grad() |
| 25 | def accuracy(output, target, topk=(1,)): |
| 26 | """Computes the precision@k for the specified values of k""" |
| 27 | if target.numel() == 0: |
| 28 | return [torch.zeros([], device=output.device)] |
| 29 | maxk = max(topk) |
| 30 | batch_size = target.size(0) |
| 31 | _, pred = output.topk(maxk, 1, True, True) |
| 32 | pred = pred.t() |
| 33 | correct = pred.eq(target.view(1, -1).expand_as(pred)) |
| 34 | |
| 35 | res = [] |
| 36 | for k in topk: |
| 37 | correct_k = correct[:k].view(-1).float().sum(0) |
| 38 | res.append(correct_k.mul_(100.0 / batch_size)) |
| 39 | return res |
| 40 | |
| 41 | |
| 42 | def sigmoid_focal_loss(inputs, targets, num_boxes, alpha: float = 0.25, gamma: float = 2): |