-
Notifications
You must be signed in to change notification settings - Fork 8
/
loss.py
63 lines (40 loc) · 1.76 KB
/
loss.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import numpy as np
import torch
from torch.autograd import Variable
from metrics import dice_coef
CE = torch.nn.BCELoss()
# labels for adversarial training
pred_label = 0
gt_label = 1
def make_Dis_label(label, gts):
D_label = np.ones(gts.shape) * label
D_label = Variable(torch.FloatTensor(D_label)).cuda()
return D_label
def calc_loss(pred, target, bce_weight=0.5):
bce = CE(pred, target)
dl = 1 - dice_coef(pred, target)
loss = bce * bce_weight + dl * bce_weight
return loss
def loss_sup(logit_S1, logit_S2, labels_S1, labels_S2):
loss1 = calc_loss(logit_S1, labels_S1)
loss2 = calc_loss(logit_S2, labels_S2)
return loss1 + loss2
def loss_diff(u_prediction_1, u_prediction_2, batch_size):
a = CE(u_prediction_1, Variable(u_prediction_2, requires_grad=False))
a = a.item()
b = CE(u_prediction_2, Variable(u_prediction_1, requires_grad=False))
b = b.item()
loss_diff_avg = (a + b)
return loss_diff_avg / batch_size
def loss_adversarial_1(D_fake_1, D_fake_2, labels_S1, labels_S2):
D_loss_fake_0_1 = CE(D_fake_1, make_Dis_label(pred_label, labels_S1))
D_loss_fake_0_2 = CE(D_fake_2, make_Dis_label(pred_label, labels_S2))
loss = D_loss_fake_0_1 + D_loss_fake_0_2
return loss
def loss_adversarial_2(D_fake_1, D_real_1, D_fake_2, D_real_2, labels_S1, labels_S2):
D_loss_fake_0_1 = CE(D_fake_1, make_Dis_label(pred_label, labels_S1))
D_loss_fake_0_2 = CE(D_fake_2, make_Dis_label(pred_label, labels_S2))
D_loss_real_1 = CE(D_real_1, make_Dis_label(gt_label, labels_S1))
D_loss_real_2 = CE(D_real_2, make_Dis_label(gt_label, labels_S2))
loss = D_loss_fake_0_1 + D_loss_fake_0_2 + D_loss_real_1 + D_loss_real_2
return loss