forked from Shathe/SemiSeg-Contrastive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloss.py
147 lines (125 loc) · 5.48 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
import torch
import torch.nn.functional as F
import torch.nn as nn
from torch.autograd import Variable
import numpy as np
class CrossEntropy2d(nn.Module):
def __init__(self, ignore_label=255):
super(CrossEntropy2d, self).__init__()
self.ignore_label = ignore_label
def forward(self, predict, target, weight=None):
"""
Args:
predict:(n, c, h, w)
target:(n, h, w)
weight (Tensor, optional): a manual rescaling weight given to each class.
If given, has to be a Tensor of size "nclasses"
"""
assert not target.requires_grad
assert predict.dim() == 4
assert target.dim() == 3
n, c, h, w = predict.size()
target_mask = (target >= 0) * (target != self.ignore_label)
target = target[target_mask]
if not target.data.dim():
return Variable(torch.zeros(1))
predict = predict.transpose(1, 2).transpose(2, 3).contiguous()
predict = predict[target_mask.view(n, h, w, 1).repeat(1, 1, 1, c)].view(-1, c)
loss = F.cross_entropy(predict, target, weight=weight, reduction='mean')
return loss
class CrossEntropyLoss2dPixelWiseWeighted(nn.Module):
def __init__(self, weight=None, ignore_index=250, reduction='none'):
super(CrossEntropyLoss2dPixelWiseWeighted, self).__init__()
self.CE = nn.CrossEntropyLoss(weight=weight, ignore_index=ignore_index, reduction=reduction)
def forward(self, output, target, pixelWiseWeight):
loss = self.CE(output, target)
loss = torch.mean(loss * pixelWiseWeight)
return loss
class MSELoss2d(nn.Module):
def __init__(self, size_average=None, reduce=None, reduction='mean', ignore_index=255):
super(MSELoss2d, self).__init__()
self.MSE = nn.MSELoss(size_average=size_average, reduce=reduce, reduction=reduction)
def forward(self, output, target):
loss = self.MSE(torch.softmax(output, dim=1), target)
return loss
def customsoftmax(inp, multihotmask):
"""
Custom Softmax
"""
soft = torch.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 = False
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[:]
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[:, :, :, :].half() *
class_weights.unsqueeze(0).unsqueeze(2).unsqueeze(3) *
customsoftmax(inputs, target[:, :, :, :].half())).sum(1)) * \
(1. - mask.half())
else:
loss_matrix = (-1 / border_weights *
(target[:, :, :, :].float() *
class_weights.unsqueeze(0).unsqueeze(2).unsqueeze(3) *
customsoftmax(inputs, target[:, :, :, :].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[:, :, :, :].sum(1).half()
else:
weights = target[:, :, :, :].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])
class_weights = torch.ones((class_weights.shape))
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