-
Notifications
You must be signed in to change notification settings - Fork 200
/
loss.py
executable file
·161 lines (129 loc) · 6.12 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""
Copyright (C) 2019 NVIDIA Corporation. All rights reserved.
Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode).
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
import logging
import numpy as np
from config import cfg
from my_functionals.DualTaskLoss import DualTaskLoss
def get_loss(args):
'''
Get the criterion based on the loss function
args:
return: criterion
'''
if args.img_wt_loss:
criterion = ImageBasedCrossEntropyLoss2d(
classes=args.dataset_cls.num_classes, size_average=True,
ignore_index=args.dataset_cls.ignore_label,
upper_bound=args.wt_bound).cuda()
elif args.joint_edgeseg_loss:
criterion = JointEdgeSegLoss(classes=args.dataset_cls.num_classes,
ignore_index=args.dataset_cls.ignore_label, upper_bound=args.wt_bound,
edge_weight=args.edge_weight, seg_weight=args.seg_weight, att_weight=args.att_weight, dual_weight=args.dual_weight).cuda()
else:
criterion = CrossEntropyLoss2d(size_average=True,
ignore_index=args.dataset_cls.ignore_label).cuda()
criterion_val = JointEdgeSegLoss(classes=args.dataset_cls.num_classes, mode='val',
ignore_index=args.dataset_cls.ignore_label, upper_bound=args.wt_bound,
edge_weight=args.edge_weight, seg_weight=args.seg_weight).cuda()
return criterion, criterion_val
class JointEdgeSegLoss(nn.Module):
def __init__(self, classes, weight=None, reduction='mean', ignore_index=255,
norm=False, upper_bound=1.0, mode='train',
edge_weight=1, seg_weight=1, att_weight=1, dual_weight=1, edge='none'):
super(JointEdgeSegLoss, self).__init__()
self.num_classes = classes
if mode == 'train':
self.seg_loss = ImageBasedCrossEntropyLoss2d(
classes=classes, ignore_index=ignore_index, upper_bound=upper_bound).cuda()
elif mode == 'val':
self.seg_loss = CrossEntropyLoss2d(size_average=True,
ignore_index=ignore_index).cuda()
self.edge_weight = edge_weight
self.seg_weight = seg_weight
self.att_weight = att_weight
self.dual_weight = dual_weight
self.dual_task = DualTaskLoss()
def bce2d(self, input, target):
n, c, h, w = input.size()
log_p = input.transpose(1, 2).transpose(2, 3).contiguous().view(1, -1)
target_t = target.transpose(1, 2).transpose(2, 3).contiguous().view(1, -1)
target_trans = target_t.clone()
pos_index = (target_t ==1)
neg_index = (target_t ==0)
ignore_index=(target_t >1)
target_trans[pos_index] = 1
target_trans[neg_index] = 0
pos_index = pos_index.data.cpu().numpy().astype(bool)
neg_index = neg_index.data.cpu().numpy().astype(bool)
ignore_index=ignore_index.data.cpu().numpy().astype(bool)
weight = torch.Tensor(log_p.size()).fill_(0)
weight = weight.numpy()
pos_num = pos_index.sum()
neg_num = neg_index.sum()
sum_num = pos_num + neg_num
weight[pos_index] = neg_num*1.0 / sum_num
weight[neg_index] = pos_num*1.0 / sum_num
weight[ignore_index] = 0
weight = torch.from_numpy(weight)
weight = weight.cuda()
loss = F.binary_cross_entropy_with_logits(log_p, target_t, weight, size_average=True)
return loss
def edge_attention(self, input, target, edge):
n, c, h, w = input.size()
filler = torch.ones_like(target) * 255
return self.seg_loss(input,
torch.where(edge.max(1)[0] > 0.8, target, filler))
def forward(self, inputs, targets):
segin, edgein = inputs
segmask, edgemask = targets
losses = {}
losses['seg_loss'] = self.seg_weight * self.seg_loss(segin, segmask)
losses['edge_loss'] = self.edge_weight * 20 * self.bce2d(edgein, edgemask)
losses['att_loss'] = self.att_weight * self.edge_attention(segin, segmask, edgein)
losses['dual_loss'] = self.dual_weight * self.dual_task(segin, segmask)
return losses
#Img Weighted Loss
class ImageBasedCrossEntropyLoss2d(nn.Module):
def __init__(self, classes, weight=None, size_average=True, ignore_index=255,
norm=False, upper_bound=1.0):
super(ImageBasedCrossEntropyLoss2d, self).__init__()
logging.info("Using Per Image based weighted loss")
self.num_classes = classes
self.nll_loss = nn.NLLLoss2d(weight, size_average, ignore_index)
self.norm = norm
self.upper_bound = upper_bound
self.batch_weights = cfg.BATCH_WEIGHTING
def calculateWeights(self, target):
hist = np.histogram(target.flatten(), range(
self.num_classes + 1), normed=True)[0]
if self.norm:
hist = ((hist != 0) * self.upper_bound * (1 / hist)) + 1
else:
hist = ((hist != 0) * self.upper_bound * (1 - hist)) + 1
return hist
def forward(self, inputs, targets):
target_cpu = targets.data.cpu().numpy()
if self.batch_weights:
weights = self.calculateWeights(target_cpu)
self.nll_loss.weight = torch.Tensor(weights).cuda()
loss = 0.0
for i in range(0, inputs.shape[0]):
if not self.batch_weights:
weights = self.calculateWeights(target_cpu[i])
self.nll_loss.weight = torch.Tensor(weights).cuda()
loss += self.nll_loss(F.log_softmax(inputs[i].unsqueeze(0)),
targets[i].unsqueeze(0))
return loss
#Cross Entroply NLL Loss
class CrossEntropyLoss2d(nn.Module):
def __init__(self, weight=None, size_average=True, ignore_index=255):
super(CrossEntropyLoss2d, self).__init__()
logging.info("Using Cross Entropy Loss")
self.nll_loss = nn.NLLLoss2d(weight, size_average, ignore_index)
def forward(self, inputs, targets):
return self.nll_loss(F.log_softmax(inputs), targets)