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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
| import torch import torch.nn as nn import torch.nn.functional as F
class EffectivePointMask(nn.Module): """ 有效点Mask机制 论文Section 3.1核心创新 问题: mmWave点云稀疏,帧间点数波动大。 对齐(padding/truncation)产生的无效点会污染KNN图和特征聚合。 解决: 学习每个点的有效性权重(0-1), 抑制无效点对邻域图和特征聚合的影响。 """ def __init__(self, in_channels: int, k: int = 16): super().__init__() self.k = k self.mask_predictor = nn.Sequential( nn.Linear(in_channels, 64), nn.ReLU(), nn.Linear(64, 32), nn.ReLU(), nn.Linear(32, 1), nn.Sigmoid() ) def forward(self, points: torch.Tensor, padding_mask: torch.Tensor = None) -> tuple: """ Args: points: (B, N, C) 点特征 [x, y, z, intensity, ...] padding_mask: (B, N) True表示padding点 Returns: masked_points: (B, N, C) 加权后的点 mask: (B, N) 有效性权重 """ mask = self.mask_predictor(points) mask = mask.squeeze(-1) if padding_mask is not None: mask = mask * (~padding_mask).float() masked_points = points * mask.unsqueeze(-1) return masked_points, mask
class DGCNNWithMask(nn.Module): """ Mask-Aware DGCNN (Dynamic Graph CNN) 论文Section 3.2: 在KNN图构建和特征聚合时使用mask """ def __init__(self, in_channels: int, hidden_dim: int = 64, k: int = 16): super().__init__() self.k = k self.mask_module = EffectivePointMask(in_channels, k) self.edge_mlp = nn.Sequential( nn.Linear(2 * in_channels + 3, hidden_dim), nn.ReLU(), nn.BatchNorm1d(hidden_dim), nn.Linear(hidden_dim, hidden_dim), nn.ReLU(), nn.BatchNorm1d(hidden_dim) ) self.agg_mlp = nn.Sequential( nn.Linear(hidden_dim * 2, hidden_dim), nn.ReLU(), nn.Linear(hidden_dim, in_channels) ) def knn(self, points: torch.Tensor, mask: torch.Tensor) -> torch.Tensor: """ K近邻图构建(mask感知) Args: points: (B, N, C) mask: (B, N) 有效性权重 Returns: edge_index: (B, N, K) 邻居索引 """ B, N, C = points.shape dist = torch.cdist(points, points) invalid_mask = mask < 0.5 dist = dist.masked_fill( invalid_mask.unsqueeze(1).expand(-1, N, -1), float('inf') ) _, indices = torch.topk(-dist, self.k, dim=-1) return indices def forward(self, points: torch.Tensor, padding_mask: torch.Tensor = None) -> torch.Tensor: """ Args: points: (B, N, C) [x, y, z, intensity] padding_mask: (B, N) True=padding Returns: features: (B, N, hidden_dim) """ B, N, C = points.shape masked_points, mask = self.mask_module(points, padding_mask) knn_idx = self.knn(masked_points, mask) neighbors = self._gather_neighbors(masked_points, knn_idx) center_expanded = masked_points.unsqueeze(2).expand(-1, -1, self.k, -1) edge_feat = torch.cat([ center_expanded, neighbors, center_expanded - neighbors ], dim=-1) mask_expanded = mask.unsqueeze(-1).expand(-1, -1, self.k).unsqueeze(-1) edge_feat = edge_feat * mask_expanded edge_feat = edge_feat.reshape(B * N * self.k, -1) edge_feat = self.edge_mlp(edge_feat) edge_feat = edge_feat.reshape(B, N, self.k, -1) mask_for_agg = mask.unsqueeze(-1).expand(-1, -1, self.k).unsqueeze(-1) edge_feat = edge_feat.masked_fill(mask_for_agg < 0.5, -1e9) agg_feat, _ = torch.max(edge_feat, dim=2) output = self.agg_mlp(torch.cat([agg_feat, masked_points], dim=-1)) output = masked_points + output return output, mask def _gather_neighbors(self, points: torch.Tensor, indices: torch.Tensor) -> torch.Tensor: """收集邻居点特征""" B, N, C = points.shape K = indices.shape[-1] indices_expanded = indices.unsqueeze(-1).expand(-1, -1, -1, C) points_expanded = points.unsqueeze(1).expand(-1, N, -1, -1) neighbors = torch.gather(points_expanded, 2, indices_expanded) return neighbors
|