-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloss_utils.py
150 lines (128 loc) · 5.48 KB
/
loss_utils.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
import torch
import numpy as np
from torch import nn
from torch.nn import functional as F
GRASP_MAX_WIDTH = 0.1
GRASP_MAX_TOLERANCE = 0.05
THRESH_GOOD = 0.7
THRESH_BAD = 0.1
def transform_point_cloud(cloud, transform, format='4x4'):
if not (format == '3x3' or format == '4x4' or format == '3x4'):
raise ValueError('Unknown transformation format, only support \'3x3\' or \'4x4\' or \'3x4\'.')
if format == '3x3':
cloud_transformed = torch.matmul(transform, cloud.T).T
elif format == '4x4' or format == '3x4':
ones = cloud.new_ones(cloud.size(0), device=cloud.device).unsqueeze(-1)
cloud_ = torch.cat([cloud, ones], dim=1)
cloud_transformed = torch.matmul(transform, cloud_.T).T
cloud_transformed = cloud_transformed[:, :3]
return cloud_transformed
def generate_grasp_views(N=300, phi=(np.sqrt(5)-1)/2, center=np.zeros(3), r=1):
views = []
for i in range(N):
zi = (2 * i + 1) / N - 1
xi = np.sqrt(1 - zi**2) * np.cos(2 * i * np.pi * phi)
yi = np.sqrt(1 - zi**2) * np.sin(2 * i * np.pi * phi)
views.append([xi, yi, zi])
views = r * np.array(views) + center
return torch.from_numpy(views.astype(np.float32))
def batch_viewpoint_params_to_matrix(batch_towards, batch_angle):
axis_x = batch_towards
ones = torch.ones(axis_x.shape[0], dtype=axis_x.dtype, device=axis_x.device)
zeros = torch.zeros(axis_x.shape[0], dtype=axis_x.dtype, device=axis_x.device)
axis_y = torch.stack([-axis_x[:,1], axis_x[:,0], zeros], dim=-1)
mask_y = (torch.norm(axis_y, dim=-1) == 0)
axis_y[mask_y,1] = 1
axis_x = axis_x / torch.norm(axis_x, dim=-1, keepdim=True)
axis_y = axis_y / torch.norm(axis_y, dim=-1, keepdim=True)
axis_z = torch.cross(axis_x, axis_y)
sin = torch.sin(batch_angle)
cos = torch.cos(batch_angle)
R1 = torch.stack([ones, zeros, zeros, zeros, cos, -sin, zeros, sin, cos], dim=-1)
R1 = R1.reshape([-1,3,3])
R2 = torch.stack([axis_x, axis_y, axis_z], dim=-1)
batch_matrix = torch.matmul(R2, R1)
return batch_matrix
def huber_loss(error, delta=1.0):
abs_error = torch.abs(error)
quadratic = torch.clamp(abs_error, max=delta)
linear = (abs_error - quadratic)
loss = 0.5 * quadratic**2 + delta * linear
return loss
def l1_loss_clamp(error, thresh=0.01):
abs_error = torch.abs(error)
loss = F.relu(abs_error-thresh)
return loss
class FocalLoss_Ori(nn.Module):
def __init__(self, num_class, alpha=None, gamma=2, ignore_index=None, reduction='mean'):
super(FocalLoss_Ori, self).__init__()
self.num_class = num_class
self.gamma = gamma
self.reduction = reduction
self.smooth = 1e-4
self.ignore_index = ignore_index
self.alpha = alpha
if alpha is None:
self.alpha = torch.ones(num_class, )
elif isinstance(alpha, (int, float)):
self.alpha = torch.as_tensor([alpha] * num_class)
elif isinstance(alpha, (list, np.ndarray)):
self.alpha = torch.as_tensor(alpha)
if self.alpha.shape[0] != num_class:
raise RuntimeError('the length not equal to number of class')
def forward(self, logit, target):
N, C = logit.shape[:2]
alpha = self.alpha.to(logit.device)
prob = F.softmax(logit, dim=1)
if prob.dim() > 2:
prob = prob.view(N, C, -1)
prob = prob.transpose(1, 2).contiguous()
prob = prob.view(-1, prob.size(-1))
ori_shp = target.shape
target = target.view(-1, 1)
valid_mask = None
if self.ignore_index is not None:
valid_mask = target != self.ignore_index
target = target * valid_mask
prob = prob.gather(1, target).view(-1) + self.smooth # avoid nan
logpt = torch.log(prob)
alpha_class = alpha[target.squeeze().long()]
class_weight = -alpha_class * torch.pow(torch.sub(1.0, prob), self.gamma)
loss = class_weight * logpt
if valid_mask is not None:
loss = loss * valid_mask.squeeze()
if self.reduction == 'mean':
if valid_mask is not None:
loss = loss.sum() / (valid_mask.sum()+1e-6)
else:
loss = loss.mean()
elif self.reduction == 'none':
loss = loss.view(ori_shp)
return loss
class BinaryFocalLoss(nn.Module):
def __init__(self, alpha=3, gamma=2, ignore_index=None, reduction='mean', **kwargs):
super(BinaryFocalLoss, self).__init__()
self.alpha = alpha
self.gamma = gamma
self.smooth = 1e-6
self.ignore_index = ignore_index
self.reduction = reduction
assert self.reduction in ['none', 'mean', 'sum']
def forward(self, output, target):
prob = torch.sigmoid(output)
prob = torch.clamp(prob, self.smooth, 1.0 - self.smooth)
valid_mask = None
if self.ignore_index is not None:
valid_mask = (target != self.ignore_index).float()
pos_mask = (target == 1).float()
neg_mask = (target == 0).float()
if valid_mask is not None:
pos_mask = pos_mask * valid_mask
neg_mask = neg_mask * valid_mask
pos_weight = (pos_mask * torch.pow(1 - prob, self.gamma)).detach()
pos_loss = -pos_weight * torch.log(prob)
neg_weight = (neg_mask * torch.pow(prob, self.gamma)).detach()
neg_loss = -self.alpha * neg_weight * F.logsigmoid(-output)
loss = pos_loss + neg_loss
loss = loss.mean()
return loss