for k in range(cfg.RPN.SA_CONFIG.NPOINTS.__len__()): mlps = cfg.RPN.SA_CONFIG.MLPS[k].copy() channel_out = 0 for idx in range(mlps.__len__()): mlps[idx] = [channel_in] + mlps[idx] channel_out += mlps[idx][-1] self.SA_modules.append( PointnetSAModuleMSG( npoint=cfg.RPN.SA_CONFIG.NPOINTS[k], radii=cfg.RPN.SA_CONFIG.RADIUS[k], nsamples=cfg.RPN.SA_CONFIG.NSAMPLE[k], mlps=mlps, use_xyz=use_xyz, bn=cfg.RPN.USE_BN ) ) skip_channel_list.append(channel_out) channel_in = channel_out self.FP_modules = nn.ModuleList() for k in range(cfg.RPN.FP_MLPS.__len__()): pre_channel = cfg.RPN.FP_MLPS[k + 1][-1] if k + 1 < len(cfg.RPN.FP_MLPS) else channel_out self.FP_modules.append( PointnetFPModule(mlp=[pre_channel + skip_channel_list[k]] + cfg.RPN.FP_MLPS[k]) ) def _break_up_pc(self, pc): xyz = pc[..., 0:3].contiguous() features = ( pc[..., 3:].transpose(1, 2).contiguous() if pc.size(-1) > 3 else None ) return xyz, features def forward(self, pointcloud: torch.cuda.FloatTensor): xyz, features = self._break_up_pc(pointcloud) l_xyz, l_features = [xyz], [features] for i in range(len(self.SA_modules)): li_xyz, li_features = self.SA_modules[i](l_xyz[i], l_features[i]) l_xyz.append(li_xyz) l_features.append(li_features) for i in range(-1, -(len(self.FP_modules) + 1), -1): l_features[i - 1] = self.FP_modules[i]( l_xyz[i - 1], l_xyz[i], l_features[i - 1], l_features[i] ) return l_xyz[0], l_features[0]在forward函数中,如果我要使用channel_out变量传入SA_modules中,我该如何在forward函数中计算并得到它,再传入SA_modules中,你可以给我详细的代码吗?
时间: 2023-07-15 14:11:29 浏览: 122
arm.rar_LPC2103_flash.i_app_cfg.h lpc21
在 forward 函数中,可以定义一个变量 channel_out,用于保存当前 SA_module 的输出通道数。然后在每一次循环时更新 channel_out 的值,并将其传入 SA_module 中。代码如下:
```
def forward(self, pointcloud: torch.cuda.FloatTensor):
xyz, features = self._break_up_pc(pointcloud)
l_xyz, l_features = [xyz], [features]
channel_out = 0 # 初始化 channel_out
for i in range(len(self.SA_modules)):
mlps = self.SA_modules[i].mlps # 获取当前 SA_module 的 MLPs
# 计算当前 SA_module 的输出通道数
channel_out = sum(mlp[-1] for mlp in mlps)
li_xyz, li_features = self.SA_modules[i](
l_xyz[i], l_features[i], channel_out=channel_out # 传入 channel_out
)
l_xyz.append(li_xyz)
l_features.append(li_features)
for i in range(-1, -(len(self.FP_modules) + 1), -1):
l_features[i - 1] = self.FP_modules[i](
l_xyz[i - 1], l_xyz[i], l_features[i - 1], l_features[i]
)
return l_xyz[0], l_features[0]
```
在 SA_module 的 forward 函数中,需要修改函数签名,增加一个参数 channel_out,用于指定输出通道数。然后在函数中使用该参数来初始化 MLP 中的第一个元素。代码如下:
```
class PointnetSAModuleMSG(nn.Module):
def __init__(self, npoint, radii, nsamples, mlps, use_xyz=True, bn=True):
super(PointnetSAModuleMSG, self).__init__()
self.npoint = npoint
self.radii = radii
self.nsamples = nsamples
self.bn = bn
self.use_xyz = use_xyz
self.SA_modules = nn.ModuleList()
for j in range(len(mlps)):
in_channel = mlps[j][0]
mlp = nn.Sequential()
for k in range(len(mlps[j])-1):
mlp.add_module('layer{}'.format(k),
pt_utils.Conv1d(in_channel, mlps[j][k+1], bn=self.bn))
in_channel = mlps[j][k+1]
self.SA_modules.append(mlp)
def forward(self, xyz, features, channel_out):
if self.use_xyz:
new_features_list = [xyz.transpose(1, 2)]
else:
new_features_list = []
for i in range(len(self.radii)):
idx, pts_cnt = pt_utils.query_ball_point(self.radii[i], self.nsamples[i], xyz, xyz)
grouped_xyz = pt_utils.grouping_operation(xyz, idx)
grouped_xyz -= xyz.unsqueeze(-1)
if features is not None:
grouped_features = pt_utils.grouping_operation(features, idx)
grouped_features = torch.cat([grouped_features, grouped_xyz], dim=1)
else:
grouped_features = grouped_xyz
new_features = self.SA_modules[i](grouped_features)
new_features = new_features.max(dim=-1)[0]
new_features_list.append(new_features)
new_features = torch.cat(new_features_list, dim=1)
for i in range(len(self.SA_modules)):
new_features[:, i:i+1] = self.SA_modules[i][0](new_features[:, i:i+1]) # 初始化 MLP 中的第一个元素
if channel_out != new_features.size(1): # 如果输出通道数不一致,进行升维或降维
new_features = pt_utils.Conv1d(new_features.size(1), channel_out, bn=self.bn)(new_features)
new_xyz = pt_utils.gather_operation(xyz, pt_utils.furthest_point_sample(xyz, self.npoint))
return new_xyz, new_features
```
阅读全文