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 173 174 175 176 177
| import torch import torch.nn as nn import torch.nn.functional as F from transformers import ViTModel
class InCaRPose(nn.Module): """ InCaRPose: 座舱内相对位姿估计模型 架构: 1. Frozen Backbone: DINOv3 ViT (预训练视觉基础模型) 2. Feature Fusion: 双视角特征融合 3. Transformer Decoder: 几何关系建模 4. Pose Head: 位姿预测 输入: - 参考图像(标定状态) - 目标图像(当前状态) 输出: - 相对旋转 R (3x3) - 相对平移 t (3,) - 度量级 """ def __init__(self, config): super().__init__() self.backbone = ViTModel.from_pretrained('dinov3-small') for param in self.backbone.parameters(): param.requires_grad = False self.feature_dim = self.backbone.config.hidden_size self.feature_fusion = nn.Sequential( nn.Linear(self.feature_dim * 2, 512), nn.LayerNorm(512), nn.GELU(), nn.Linear(512, 512) ) self.transformer_decoder = nn.TransformerDecoder( nn.TransformerDecoderLayer( d_model=512, nhead=8, dim_feedforward=2048, dropout=0.1, batch_first=True ), num_layers=6 ) self.rotation_head = nn.Sequential( nn.Linear(512, 256), nn.ReLU(), nn.Linear(256, 9) ) self.translation_head = nn.Sequential( nn.Linear(512, 256), nn.ReLU(), nn.Linear(256, 3) ) def forward(self, reference_image, target_image): """ Args: reference_image: (B, 3, H, W) 参考图像 target_image: (B, 3, H, W) 目标图像 Returns: pose_dict: { 'rotation': (B, 3, 3) 相对旋转矩阵, 'translation': (B, 3) 相对平移向量(米) } """ B = reference_image.shape[0] ref_features = self.backbone(reference_image).last_hidden_state tgt_features = self.backbone(target_image).last_hidden_state fused_features = self.feature_fusion( torch.cat([ref_features, tgt_features], dim=-1) ) decoded = self.transformer_decoder( tgt=fused_features, memory=ref_features ) global_features = decoded.mean(dim=1) rotation_flat = self.rotation_head(global_features) rotation = rotation_flat.view(B, 3, 3) rotation = self._orthogonalize(rotation) translation = self.translation_head(global_features) return { 'rotation': rotation, 'translation': translation } def _orthogonalize(self, rotation): """ 使用SVD正交化旋转矩阵 确保预测的旋转矩阵满足正交性约束: R^T R = I det(R) = 1 """ B = rotation.shape[0] orth_rotation = [] for i in range(B): U, _, Vt = torch.linalg.svd(rotation[i]) R = U @ Vt if torch.det(R) < 0: R = -R orth_rotation.append(R) return torch.stack(orth_rotation)
class InCaRPoseLoss(nn.Module): """ InCaRPose损失函数 组成: 1. 旋转损失(角度距离) 2. 平移损失(L2距离,度量级) """ def __init__(self, rotation_weight=1.0, translation_weight=10.0): super().__init__() self.rotation_weight = rotation_weight self.translation_weight = translation_weight def forward(self, pred_pose, gt_pose): """ Args: pred_pose: {'rotation': (B,3,3), 'translation': (B,3)} gt_pose: {'rotation': (B,3,3), 'translation': (B,3)} """ rotation_diff = pred_pose['rotation'] @ gt_pose['rotation'].transpose(-1, -2) trace = rotation_diff.diagonal(dim1=-2, dim2=-1).sum(-1) angle_distance = torch.acos(torch.clamp((trace - 1) / 2, -1, 1)) rotation_loss = angle_distance.mean() translation_loss = F.mse_loss( pred_pose['translation'], gt_pose['translation'] ) total_loss = ( self.rotation_weight * rotation_loss + self.translation_weight * translation_loss ) return total_loss, { 'rotation_loss': rotation_loss.item(), 'translation_loss': translation_loss.item() }
|