这是我在RandLA-Net模型上添加了Transformer和图卷积的代码: "class AttentivePooling(nn.Module): def…
- User
这是我在RandLA-Net模型上添加了Transformer和图卷积的代码: "class AttentivePooling(nn.Module): def init(self, d_in, d_out): super().init() self.fc = nn.Conv2d(d_in, d_in, (1, 1), bias=False)
codeself.mlp = nn.Sequential( nn.Conv2d(d_in, d_out, 1), nn.BatchNorm2d(d_out), nn.LeakyReLU(negative_slope=0.2, inplace=True) ) def forward(self, feature_set): att_activation = self.fc(feature_set) att_scores = F.softmax(att_activation, dim=3) f_agg = feature_set * att_scores f_agg = torch.sum(f_agg, dim=3, keepdim=True) f_agg = self.mlp(f_agg) return f_aggdef build_edge_index(xyz, k=16): # xyz shape (batch_size, num_points, 3) B, N, _ = xyz.size() edge_index_list = [] for i in range(B): edge_index = knn_graph(xyz[i], k=k, batch=None, loop=False) edge_index_list.append(edge_index) return edge_index_list # 返回每一个batch的edge_index
class GCNBlock(nn.Module): def init(self, in_channels, out_channels): super().init() self.conv1 = GCNConv(in_channels, out_channels // 2) self.conv2 = GCNConv(out_channels // 2, out_channels)
codedef forward(self, x, edge_index): # x: (B*N, in_channels) # edge_index (2, E) where E is the number of edges x = F.relu(self.conv1(x, edge_index)) x = self.conv2(x, edge_index) return x""" 利用多头注意力机制来获取全局上下文信息 """
class SelfAttention(nn.Module): def init(self, d_model, num_heads): super().init() self.attn = nn.MultiheadAttention(embed_dim=d_model, num_heads=num_heads)
codedef forward(self, x): # x shape (batch, npoints, d_model) x = x.permute(1, 0, 2) attn_output, _ = self.attn(x, x, x) return attn_output.permute(1, 0, 2)""" 挤压激励模块 """ class SqueezeExcitation(nn.Module): def init(self, in_channels, reduced_dim): super().init() self.se = nn.Sequential( nn.AdaptiveAvgPool2d(1), nn.Conv2d(in_channels, reduced_dim, 1), # 降维 nn.SiLU(), nn.Conv2d(reduced_dim, in_channels, 1), nn.Sigmoid() )
- Assistant