PointPillars算法 点云检测 自动驾驶 深度学习 OpenPCDet系列 | 5.4.2 DenseHead中的ResidualCoder回归信息编码模块 - from_numpy(self
PointPillars算法 点云检测 自动驾驶 深度学习 OpenPCDet系列 | 5.4.2 DenseHead中的ResidualCoder回归信息编码模块
文章目录
ResidualCoder模块
ResidualCoder模块
这里主要涉及的是PointPillars算法对预测的编码处理来计算loss,在OpenPCDet中的实现基本和论文的一致,不过在角度编码上没有使用sin来进行编码处理,而是直接利用差值进行编码。自然也不需要argsin函数来进行解码操作。(这里说法是错误的,其实在代码中比较巧妙的实现了sin(θ1-θ2)的编码方式,差值具体是在loss中才进行相减的)
论文中的编码方式如下所示:
代码实现如下所示:
# 功能:在AnchorHeadTemplate中调用
class ResidualCoder(object):
def __init__(self, code_size=7, encode_angle_by_sincos=False, **kwargs):
"""
Args:
code_size: 默认=7
encode_angle_by_sincos: False
num_dir_bins: 传参=6
Func:
loss中anchor和gt的编码和解码
"""
super().__init__()
self.code_size = code_size # 7
self.encode_angle_by_sincos = encode_angle_by_sincos # False
if self.encode_angle_by_sincos:
self.code_size += 1
# 一般是训练过程进行编码,测试过程进行解码
def encode_torch(self, boxes, anchors):
"""
Args:
boxes: (N, 7 + C) [x, y, z, dx, dy, dz, heading, ...] (104, 7)
anchors: (N, 7 + C) [x, y, z, dx, dy, dz, heading or *[cos, sin], ...] (104, 7)
Returns:
"""
anchors[:, 3:6] = torch.clamp_min(anchors[:, 3:6], min=1e-5) # clamp是阶段函数,确保anchor和gt的尺寸dxdydz非0
boxes[:, 3:6] = torch.clamp_min(boxes[:, 3:6], min=1e-5)
xa, ya, za, dxa, dya, dza, ra, *cas = torch.split(anchors, 1, dim=-1) # 分别切分出各参数,shape均为 (104, 1)
xg, yg, zg, dxg, dyg, dzg, rg, *cgs = torch.split(boxes, 1, dim=-1)
# 根据论文中的编码方式进行,计算Δx,Δy,Δz,Δw,Δl,Δh,Δθ
diagonal = torch.sqrt(dxa ** 2 + dya ** 2) # 计算anchor尺寸大小的对角线长度
xt = (xg - xa) / diagonal # Δx
yt = (yg - ya) / diagonal # Δy
zt = (zg - za) / dza # Δz
dxt = torch.log(dxg / dxa) # Δw
dyt = torch.log(dyg / dya) # Δl
dzt = torch.log(dzg / dza) # Δh
if self.encode_angle_by_sincos: # 用sin+cos函数值来对角度进行编码,因为这两者可以确定一个角度值
rt_cos = torch.cos(rg) - torch.cos(ra)
rt_sin = torch.sin(rg) - torch.sin(ra)
rts = [rt_cos, rt_sin]
else:
rts = [rg - ra] # Δθ 这里没有使用论文中的torch.sin(rg - ra),而且直接编码其差值
cts = [g - a for g, a in zip(cgs, cas)] # None
return torch.cat([xt, yt, zt, dxt, dyt, dzt, *rts, *cts], dim=-1) # 重新拼接在一起
# 一般是训练过程进行编码,测试过程进行解码
def decode_torch(self, box_encodings, anchors):
"""
Args:
box_encodings: (B, N, 7 + C) or (N, 7 + C) [x, y, z, dx, dy, dz, heading or *[cos, sin], ...]
anchors: (B, N, 7 + C) or (N, 7 + C) [x, y, z, dx, dy, dz, heading, ...]
Returns:
"""
xa, ya, za, dxa, dya, dza, ra, *cas = torch.split(anchors, 1, dim=-1)
if not self.encode_angle_by_sincos:
xt, yt, zt, dxt, dyt, dzt, rt, *cts = torch.split(box_encodings, 1, dim=-1)
else:
xt, yt, zt, dxt, dyt, dzt, cost, sint, *cts = torch.split(box_encodings, 1, dim=-1)
diagonal = torch.sqrt(dxa ** 2 + dya ** 2)
xg = xt * diagonal + xa
yg = yt * diagonal + ya
zg = zt * dza + za
dxg = torch.exp(dxt) * dxa
dyg = torch.exp(dyt) * dya
dzg = torch.exp(dzt) * dza
if self.encode_angle_by_sincos:
rg_cos = cost + torch.cos(ra)
rg_sin = sint + torch.sin(ra)
rg = torch.atan2(rg_sin, rg_cos)
else:
rg = rt + ra
cgs = [t + a for t, a in zip(cts, cas)]
return torch.cat([xg, yg, zg, dxg, dyg, dzg, rg, *cgs], dim=-1)
回归损失的loss计算类WeightedSmoothL1Loss代码如下:
# 回归损失函数: reg_loss
class WeightedSmoothL1Loss(nn.Module):
"""
Code-wise Weighted Smooth L1 Loss modified based on fvcore.nn.smooth_l1_loss
https://github.com/facebookresearch/fvcore/blob/master/fvcore/nn/smooth_l1_loss.py
| 0.5 * x ** 2 / beta if abs(x) < beta
smoothl1(x) = |
| abs(x) - 0.5 * beta otherwise, 拼多多帮助力入口
where x = input - target.
"""
def __init__(self, beta: float = 1.0 / 9.0, code_weights: list = None):
"""
Args:
beta: Scalar float.
L1 to L2 change point.
For beta values < 1e-5, L1 loss is computed.
code_weights: (#codes) float list if not None.
Code-wise weights.
"""
super(WeightedSmoothL1Loss, self).__init__()
self.beta = beta # 0.11111
if code_weights is not None:
self.code_weights = np.array(code_weights, dtype=np.float32)
self.code_weights = torch.from_numpy(self.code_weights).cuda() # cuda: [1,1,1,1,1,1,1]
@staticmethod
def smooth_l1_loss(diff, beta):
if beta < 1e-5:
loss = torch.abs(diff)
else:
n = torch.abs(diff)
loss = torch.where(n < beta, 0.5 * n ** 2 / beta, n - 0.5 * beta)
return loss
def forward(self, input: torch.Tensor, target: torch.Tensor, weights: torch.Tensor = None):
"""
Args:
input: (B, #anchors, #codes) float tensor.
Ecoded predicted locations of objects.
target: (B, #anchors, #codes) float tensor.
Regression targets.
weights: (B, #anchors) float tensor if not None.
Returns:
loss: (B, #anchors) float tensor.
Weighted smooth l1 loss without reduction.
"""
target = torch.where(torch.isnan(target), input, target) # ignore nan targets
diff = input - target # 差值计算,包含了角度
# code-wise weighting
if self.code_weights is not None:
diff = diff * self.code_weights.view(1, 1, -1)
loss = self.smooth_l1_loss(diff, self.beta) # (16, 321408, 7)
# anchor-wise weighting
if weights is not None:
assert weights.shape[0] == loss.shape[0] and weights.shape[1] == loss.shape[1]
loss = loss * weights.unsqueeze(-1)
return loss
PointPillars算法 点云检测 自动驾驶 深度学习 OpenPCDet系列 | 5.4.2 DenseHead中的ResidualCoder回归信息编码模块
推荐文章