-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathloss.py
More file actions
38 lines (27 loc) · 997 Bytes
/
Copy pathloss.py
File metadata and controls
38 lines (27 loc) · 997 Bytes
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
32
33
34
35
36
37
38
import torch
from torch import nn
import torch.nn.functional as F
def _iou(pred, target, size_average=True):
b = pred.shape[0]
IoU = 0.0
for i in range(0, b):
# compute the IoU of the foreground
Iand1 = torch.sum(target[i, :, :, :] * pred[i, :, :, :])
Ior1 = torch.sum(target[i, :, :, :]) + torch.sum(pred[i, :, :, :]) - Iand1
IoU1 = Iand1 / Ior1
# IoU loss is (1-IoU1)
IoU = IoU + (1 - IoU1)
return IoU / b
class IOU(torch.nn.Module):
def __init__(self, size_average=True):
super(IOU, self).__init__()
self.size_average = size_average
def forward(self, pred, target):
return _iou(pred, target, self.size_average)
def bce_iou_loss(pred, target):
bce_loss = nn.BCELoss(size_average=True)
iou_loss_f = IOU(size_average=True)
bce_out = bce_loss(pred, target)
iou_out = iou_loss_f(pred, target)
loss = bce_out + iou_out
return loss