[98e649]: / libs / losses / mag_angle_loss.py

Download this file

144 lines (110 with data), 4.9 kB

  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
import torch
import torch.nn as nn
import math
def cart2polar(coord):
""" coord: (N, 2, ...)
"""
x = coord[:, 0, ...]
y = coord[:, 1, ...]
theta = torch.atan(y / (x + 1e-12))
theta = theta + (x < 0).to(coord.dtype) * math.pi
theta = theta + ((x > 0).to(coord.dtype) * (y < 0).to(coord.dtype)) * 2 * math.pi
return theta / (2 * math.pi)
class EuclideanAngleLossWithOHEM(nn.Module):
def __init__(self, npRatio=3):
super(EuclideanAngleLossWithOHEM, self).__init__()
self.npRatio = npRatio
def __cal_weight(self, gt):
_, H, W = gt.shape # N=1
labels = torch.unique(gt, sorted=True)[1:]
weight = torch.zeros((H, W), dtype=torch.float, device=gt.device)
posRegion = gt[0, ...] > 0
posCount = torch.sum(posRegion)
if posCount != 0:
segRemain = 0
for segi in labels:
overlap_segi = gt[0, ...] == segi
overlapCount_segi = torch.sum(overlap_segi)
if overlapCount_segi == 0: continue
segRemain = segRemain + 1
segAve = float(posCount) / segRemain
for segi in labels:
overlap_segi = gt[0, ...] == segi
overlapCount_segi = torch.sum(overlap_segi, dtype=torch.float)
if overlapCount_segi == 0: continue
pixAve = segAve / overlapCount_segi
weight = weight * (~overlap_segi).to(torch.float) + pixAve * overlap_segi.to(torch.float)
# weight = weight[None]
return weight
def forward(self, pred, gt_df, gt, weight=None):
""" pred: (N, C, H, W)
gt_df: (N, C, H, W)
gt: (N, 1, H, W)
"""
# L1 and L2 distance
N, _, H, W = pred.shape
distL1 = pred - gt_df
distL2 = distL1 ** 2
theta_p = cart2polar(pred)
theta_g = cart2polar(gt_df)
angleDistL1 = theta_g - theta_p
if weight is None:
weight = torch.zeros((N, H, W), device=pred.device)
for i in range(N):
weight[i] = self.__cal_weight(gt[i])
# the amount of positive and negtive pixels
regionPos = (weight > 0).to(torch.float)
regionNeg = (weight == 0).to(torch.float)
sumPos = torch.sum(regionPos, dim=(1,2)) # (N,)
sumNeg = torch.sum(regionNeg, dim=(1,2))
# the amount of hard negative pixels
sumhardNeg = torch.min(self.npRatio * sumPos, sumNeg).to(torch.int) # (N,)
# angle loss on ~(top - sumhardNeg) negative pixels to 0
angleLossNeg = (angleDistL1 ** 2) * regionNeg
angleLossNegFlat = torch.flatten(angleLossNeg, start_dim=1) # (N, ...)
# set loss on ~(top - sumhardNeg) negative pixels to 0
lossNeg = (distL2[:,0,...] + distL2[:, 1, ...]) * regionNeg
lossFlat = torch.flatten(lossNeg, start_dim=1) # (N, ...)
# l2-norm distance and angle distance
lossFlat = lossFlat + angleLossNegFlat
arg = torch.argsort(lossFlat, dim=1)
for i in range(N):
lossFlat[i, arg[i, :-sumhardNeg[i]]] = 0
lossHard = lossFlat.view(lossNeg.shape)
# weight for positive and negative pixels
weightPos = torch.zeros_like(gt, dtype=pred.dtype)
weightNeg = torch.zeros_like(gt, dtype=pred.dtype)
weightPos = weight.clone()
weightNeg[:,0,...] = (lossHard != 0).to(torch.float32)
# total loss
total_loss = torch.sum(((distL2[:,0,...] + distL2[:, 1, ...]) + angleDistL1 ** 2) *
(weightPos + weightNeg)) / N / 2. / torch.sum(weightPos + weightNeg)
return total_loss
if __name__ == "__main__":
import os
import torch.nn as nn
import torch.optim as optim
os.environ["CUDA_VISIBLE_DEVICES"] = "4"
criterion = EuclideanAngleLossWithOHEM()
# models = nn.Sequential(nn.Conv2d(2, 2, 1),
# nn.ReLU())
# models.to(device="cuda")
# epoch_n = 200
# learning_rate = 1e-4
# optimizer = optim.Adam(params=models.parameters(), lr=learning_rate)
# for i in range(100):
# pred = torch.randn((32, 2, 224, 224)).cuda()
# gt_df = torch.randn((32, 2, 224, 224)).cuda()
# gt = torch.randint(0, 4, (32, 1, 224, 224)).cuda()
# pred = models(gt_df)
# loss = criterion(pred, gt_df, gt)
# optimizer.zero_grad()
# loss.backward()
# optimizer.step()
# print("{:6} loss:{}".format(i, loss))
for i in range(100):
pred = torch.randn((32, 2, 224, 224)).cuda()
gt_df = torch.randn((32, 2, 224, 224)).cuda()
gt = torch.randint(0, 4, (32, 1, 224, 224)).cuda()
loss = criterion(-gt_df, gt_df, gt)
print("{:6} loss:{}".format(i, loss))