-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
31 lines (26 loc) · 1 KB
/
utils.py
File metadata and controls
31 lines (26 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
def param_count(model: nn.Module) -> int:
"""
Determines number of trainable parameters.
:param model: An nn.Module.
:return: The number of trainable parameters.
"""
return sum(param.numel() for param in model.parameters() if param.requires_grad)
class BinaryFocalLoss(nn.Module):
def __init__(self, args):
super(BinaryFocalLoss, self).__init__()
self.alpha = args.alpha
self.gamma = args.gamma
self.smooth = 0
def forward(self, pred, target):
pred_sigmoid = pred.sigmoid()
target = target.type_as(pred)
pt = (1 - pred_sigmoid) * target + pred_sigmoid * (1 - target)
focal_weight = (self.alpha * target + (1 - self.alpha) *
(1 - target)) * pt.pow(self.gamma)
loss = F.binary_cross_entropy_with_logits(
pred, target, reduction='none') * focal_weight
return loss