-
Notifications
You must be signed in to change notification settings - Fork 36
/
loss.py
335 lines (280 loc) · 12.2 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
"""
Loss.py
"""
import logging
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from config import cfg
def get_loss(args):
"""
Get the criterion based on the loss function
args: commandline arguments
return: criterion, criterion_val
"""
if args.cls_wt_loss:
ce_weight = torch.Tensor([0.8373, 0.9180, 0.8660, 1.0345, 1.0166, 0.9969, 0.9754,
1.0489, 0.8786, 1.0023, 0.9539, 0.9843, 1.1116, 0.9037,
1.0865, 1.0955, 1.0865, 1.1529, 1.0507])
else:
ce_weight = None
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.jointwtborder:
criterion = ImgWtLossSoftNLL(classes=args.dataset_cls.num_classes,
ignore_index=args.dataset_cls.ignore_label,
upper_bound=args.wt_bound).cuda()
else:
print("standard cross entropy")
criterion = nn.CrossEntropyLoss(weight=ce_weight, reduction='mean',
ignore_index=args.dataset_cls.ignore_label).cuda()
criterion_val = nn.CrossEntropyLoss(reduction='mean',
ignore_index=args.dataset_cls.ignore_label).cuda()
return criterion, criterion_val
def get_loss_by_epoch(args):
"""
Get the criterion based on the loss function
args: commandline arguments
return: criterion, criterion_val
"""
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.jointwtborder:
criterion = ImgWtLossSoftNLL_by_epoch(classes=args.dataset_cls.num_classes,
ignore_index=args.dataset_cls.ignore_label,
upper_bound=args.wt_bound).cuda()
else:
criterion = CrossEntropyLoss2d(size_average=True,
ignore_index=args.dataset_cls.ignore_label).cuda()
criterion_val = CrossEntropyLoss2d(size_average=True,
weight=None,
ignore_index=args.dataset_cls.ignore_label).cuda()
return criterion, criterion_val
def get_loss_aux(args):
"""
Get the criterion based on the loss function
args: commandline arguments
return: criterion, criterion_val
"""
if args.cls_wt_loss:
ce_weight = torch.Tensor([0.8373, 0.9180, 0.8660, 1.0345, 1.0166, 0.9969, 0.9754,
1.0489, 0.8786, 1.0023, 0.9539, 0.9843, 1.1116, 0.9037,
1.0865, 1.0955, 1.0865, 1.1529, 1.0507])
else:
ce_weight = None
print("standard cross entropy")
criterion = nn.CrossEntropyLoss(weight=ce_weight, reduction='mean',
ignore_index=args.dataset_cls.ignore_label).cuda()
return criterion
def get_loss_bcelogit(args):
if args.cls_wt_loss:
pos_weight = torch.Tensor([0.8373, 0.9180, 0.8660, 1.0345, 1.0166, 0.9969, 0.9754,
1.0489, 0.8786, 1.0023, 0.9539, 0.9843, 1.1116, 0.9037,
1.0865, 1.0955, 1.0865, 1.1529, 1.0507])
else:
pos_weight = None
print("standard bce with logit cross entropy")
criterion = nn.BCEWithLogitsLoss(reduction='mean').cuda()
return criterion
def weighted_binary_cross_entropy(output, target):
weights = torch.Tensor([0.1, 0.9])
loss = weights[1] * (target * torch.log(output)) + \
weights[0] * ((1 - target) * torch.log(1 - output))
return torch.neg(torch.mean(loss))
class ImageBasedCrossEntropyLoss2d(nn.Module):
"""
Image Weighted Cross Entropy Loss
"""
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.NLLLoss(weight=weight, reduction='mean', ignore_index=ignore_index)
self.norm = norm
self.upper_bound = upper_bound
self.batch_weights = cfg.BATCH_WEIGHTING
self.logsoftmax = nn.LogSoftmax(dim=1)
def calculate_weights(self, target):
"""
Calculate weights of classes based on the training crop
"""
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.calculate_weights(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.calculate_weights(target_cpu[i])
self.nll_loss.weight = torch.Tensor(weights).cuda()
loss += self.nll_loss(self.logsoftmax(inputs[i].unsqueeze(0)),
targets[i].unsqueeze(0))
return loss
class CrossEntropyLoss2d(nn.Module):
"""
Cross Entroply NLL Loss
"""
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.NLLLoss(weight=weight, reduction='mean', ignore_index=ignore_index)
self.logsoftmax = nn.LogSoftmax(dim=1)
# self.weight = weight
def forward(self, inputs, targets):
return self.nll_loss(self.logsoftmax(inputs), targets)
def customsoftmax(inp, multihotmask):
"""
Custom Softmax
"""
soft = F.softmax(inp, dim=1)
# This takes the mask * softmax ( sums it up hence summing up the classes in border
# then takes of summed up version vs no summed version
return torch.log(
torch.max(soft, (multihotmask * (soft * multihotmask).sum(1, keepdim=True)))
)
class ImgWtLossSoftNLL(nn.Module):
"""
Relax Loss
"""
def __init__(self, classes, ignore_index=255, weights=None, upper_bound=1.0,
norm=False):
super(ImgWtLossSoftNLL, self).__init__()
self.weights = weights
self.num_classes = classes
self.ignore_index = ignore_index
self.upper_bound = upper_bound
self.norm = norm
self.batch_weights = cfg.BATCH_WEIGHTING
def calculate_weights(self, target):
"""
Calculate weights of the classes based on training crop
"""
if len(target.shape) == 3:
hist = np.sum(target, axis=(1, 2)) * 1.0 / target.sum()
else:
hist = np.sum(target, axis=(0, 2, 3)) * 1.0 / target.sum()
if self.norm:
hist = ((hist != 0) * self.upper_bound * (1 / hist)) + 1
else:
hist = ((hist != 0) * self.upper_bound * (1 - hist)) + 1
return hist[:-1]
def custom_nll(self, inputs, target, class_weights, border_weights, mask):
"""
NLL Relaxed Loss Implementation
"""
if (cfg.REDUCE_BORDER_ITER != -1 and cfg.ITER > cfg.REDUCE_BORDER_ITER):
border_weights = 1 / border_weights
target[target > 1] = 1
loss_matrix = (-1 / border_weights *
(target[:, :-1, :, :].float() *
class_weights.unsqueeze(0).unsqueeze(2).unsqueeze(3) *
customsoftmax(inputs, target[:, :-1, :, :].float())).sum(1)) * \
(1. - mask.float())
# loss_matrix[border_weights > 1] = 0
loss = loss_matrix.sum()
# +1 to prevent division by 0
loss = loss / (target.shape[0] * target.shape[2] * target.shape[3] - mask.sum().item() + 1)
return loss
def forward(self, inputs, target):
weights = target[:, :-1, :, :].sum(1).float()
ignore_mask = (weights == 0)
weights[ignore_mask] = 1
loss = 0
target_cpu = target.data.cpu().numpy()
if self.batch_weights:
class_weights = self.calculate_weights(target_cpu)
for i in range(0, inputs.shape[0]):
if not self.batch_weights:
class_weights = self.calculate_weights(target_cpu[i])
loss = loss + self.custom_nll(inputs[i].unsqueeze(0),
target[i].unsqueeze(0),
class_weights=torch.Tensor(class_weights).cuda(),
border_weights=weights[i], mask=ignore_mask[i])
loss = loss / inputs.shape[0]
return loss
class ImgWtLossSoftNLL_by_epoch(nn.Module):
"""
Relax Loss
"""
def __init__(self, classes, ignore_index=255, weights=None, upper_bound=1.0,
norm=False):
super(ImgWtLossSoftNLL_by_epoch, self).__init__()
self.weights = weights
self.num_classes = classes
self.ignore_index = ignore_index
self.upper_bound = upper_bound
self.norm = norm
self.batch_weights = cfg.BATCH_WEIGHTING
self.fp16 = False
def calculate_weights(self, target):
"""
Calculate weights of the classes based on training crop
"""
if len(target.shape) == 3:
hist = np.sum(target, axis=(1, 2)) * 1.0 / target.sum()
else:
hist = np.sum(target, axis=(0, 2, 3)) * 1.0 / target.sum()
if self.norm:
hist = ((hist != 0) * self.upper_bound * (1 / hist)) + 1
else:
hist = ((hist != 0) * self.upper_bound * (1 - hist)) + 1
return hist[:-1]
def custom_nll(self, inputs, target, class_weights, border_weights, mask):
"""
NLL Relaxed Loss Implementation
"""
if (cfg.REDUCE_BORDER_EPOCH != -1 and cfg.EPOCH > cfg.REDUCE_BORDER_EPOCH):
border_weights = 1 / border_weights
target[target > 1] = 1
if self.fp16:
loss_matrix = (-1 / border_weights *
(target[:, :-1, :, :].half() *
class_weights.unsqueeze(0).unsqueeze(2).unsqueeze(3) *
customsoftmax(inputs, target[:, :-1, :, :].half())).sum(1)) * \
(1. - mask.half())
else:
loss_matrix = (-1 / border_weights *
(target[:, :-1, :, :].float() *
class_weights.unsqueeze(0).unsqueeze(2).unsqueeze(3) *
customsoftmax(inputs, target[:, :-1, :, :].float())).sum(1)) * \
(1. - mask.float())
# loss_matrix[border_weights > 1] = 0
loss = loss_matrix.sum()
# +1 to prevent division by 0
loss = loss / (target.shape[0] * target.shape[2] * target.shape[3] - mask.sum().item() + 1)
return loss
def forward(self, inputs, target):
if self.fp16:
weights = target[:, :-1, :, :].sum(1).half()
else:
weights = target[:, :-1, :, :].sum(1).float()
ignore_mask = (weights == 0)
weights[ignore_mask] = 1
loss = 0
target_cpu = target.data.cpu().numpy()
if self.batch_weights:
class_weights = self.calculate_weights(target_cpu)
for i in range(0, inputs.shape[0]):
if not self.batch_weights:
class_weights = self.calculate_weights(target_cpu[i])
loss = loss + self.custom_nll(inputs[i].unsqueeze(0),
target[i].unsqueeze(0),
class_weights=torch.Tensor(class_weights).cuda(),
border_weights=weights, mask=ignore_mask[i])
return loss