实验实践 / 点云配准

RPF 模型架构分析文档

RPF 模型架构分析文档

概述

本文档详细分析 train_utonia_indoor_from_base_weights.sh 训练脚本所使用模型的架构,包括:

  1. Utonia Encoder (PointTransformerV3) - 点云特征提取器
  2. GeoRegTransformer - 注册变换器,用于预测相机姿态

1. Utonia Encoder (PointTransformerV3) 架构详解

1.1 整体架构参数

Utonia 基于 PointTransformerV3 实现,其架构参数如下:

参数 值 说明
in_channels 9 输入特征维度 (x, y, z, color, normal 共9维)
order ("z", "z-trans") 序列化顺序
stride (2, 2, 2, 2) 4个下采样阶段的步长
enc_depths (3, 3, 3, 12, 3) 5个编码器阶段的Block数量
enc_channels (54, 108, 216, 432, 576) 5个阶段的特征通道数
enc_num_head (3, 6, 12, 24, 32) 5个阶段的注意力头数
enc_patch_size (1024, 1024, 1024, 1024, 1024) 每个阶段的patch大小
mlp_ratio 4 MLP隐藏层扩展比例
drop_path 0.3 Drop path 概率
pre_norm True 是否使用预归一化
enable_flash True 是否启用Flash Attention

1.2 各阶段详细结构

Stage 0 (Encoder Stage 0)

  • 输入: (N, 9) - N个点,每个点9维特征 (coord + color + normal)
  • Embedding: Linear(9 → 54) + LayerNorm + GELU
  • Block数量: 3
  • 输出通道: 54
  • 注意力头数: 3
  • Patch Size: 1024
  • 下采样: 无 (第一阶段)

Stage 1 (Encoder Stage 1)

  • 输入: Stage 0 输出
  • GridPooling: 通道变换 54 → 108,步长2
  • Block数量: 3
  • 输出通道: 108
  • 注意力头数: 6
  • Patch Size: 1024

Stage 2 (Encoder Stage 2)

  • 输入: Stage 1 输出
  • GridPooling: 通道变换 108 → 216,步长2
  • Block数量: 3
  • 输出通道: 216
  • 注意力头数: 12
  • Patch Size: 1024

Stage 3 (Encoder Stage 3)

  • 输入: Stage 2 输出
  • GridPooling: 通道变换 216 → 432,步长2
  • Block数量: 12
  • 输出通道: 432
  • 注意力头数: 24
  • Patch Size: 1024

Stage 4 (Encoder Stage 4)

  • 输入: Stage 3 输出
  • GridPooling: 通道变换 432 → 576,步长2
  • Block数量: 3
  • 输出通道: 576
  • 注意力头数: 32
  • Patch Size: 1024

1.3 核心组件

1.3.1 Embedding 层

Embedding(
in_channels=9,
embed_channels=54, # enc_channels[0]
norm_layer=nn.LayerNorm,
act_layer=nn.GELU,
)
  • 输入: (N, 9) - 点坐标(3) + 颜色(3) + 法线(3)
  • 输出: (N, 54)

1.3.2 Block 结构

每个 Block 包含:

  1. CPE (Contextual Position Encoding):
    • SubMConv3d(54, 54, kernel_size=3) + Linear(54, 54) + LayerNorm(54)
  2. LayerNorm1 + SerializedAttention + LayerScale + DropPath
  3. LayerNorm2 + MLP + LayerScale + DropPath

MLP 结构:

  • Linear(channels, channels * mlp_ratio)
  • GELU
  • Linear(channels * mlp_ratio, channels)

1.3.3 SerializedAttention

  • 输入: (N, C) - 序列化后的点特征
  • 输出: (N, C)
  • 注意力机制:
    • Flash Attention (启用时)
    • 3D RoPE 位置编码 (Point3DRoPE)
    • 头数: num_heads
    • Head dimension: channels // num_heads

1.3.4 Point3DRoPE (3D旋转位置编码)

Point3DRoPE(
head_dim=channels // num_heads,
base=10 # rope_base
)
  • 将3D坐标 (x, y, z) 编码为频率向量
  • 对每个维度分别进行正弦/余弦编码

1.4 Utonia 输出

super_point = {
"coord": (N, 3), # 归一化后的点坐标
"feat": (N, 576), # 提取的特征 (最后一层输出)
"batch": (N,), # batch索引 (用于多帧场景)
"offset": (B+1,), # 每帧点的offset
...
}

2. GeoRegTransformer (注册变换器) 架构详解

2.1 整体架构参数

参数 值 说明
in_dim 576 Utonia 输出的特征维度
dec_embed_dim 1024 变换器隐藏层维度
dec_num_heads 16 注意力头数
dec_depth 36 Transformer Block 数量
mlp_ratio 4 MLP 扩展比例
predict_global_point True 是否预测全局点
num_dec_blk_not_to_checkpoint 8 不使用checkpoint的block数

2.2 核心组件

2.2.1 Embedding3D (3D位置编码)

Embedding3D(
embed_dim=1024,
multires=10
)

结构:

  • PointCloudEmbedding:
    • input_dims=3 (x, y, z)
    • max_freq_log2=9
    • num_freqs=10
    • 输出维度: 3 + 3 * 10 * 2 = 63 (包含原始输入 + 20个频率编码)
  • Projection: Linear(63 + 1024, 1024) = Linear(1087, 1024)

输入: (N, 1024) 特征 + (N, 3) 坐标 输出: (N, 1024)

2.2.2 Projects (特征投影)

Projects(
dim_in=576, # Utonia输出特征维度
dim_out=1024 # 目标维度
)

结构:

nn.Sequential(
nn.Linear(576, 1024),
nn.LayerNorm(1024),
nn.GELU(),

# 残差分支
nn.Linear(1024, 1024),
nn.LayerNorm(1024),
nn.GELU(),
nn.Linear(1024, 1024),
nn.LayerNorm(1024),

nn.Linear(1024, 1024)
)

输入: (N, 576) 输出: (N, 1024)

2.2.3 Decoder Blocks (36层)

每个 BlockRope 包含:

  1. LayerNorm
  2. FlashAttentionRope (带3D RoPE)
  3. LayerScale (init_values=0.01)
  4. DropPath
  5. LayerNorm
  6. MLP (Linear(1024, 4096) + GELU + Linear(4096, 1024))
  7. LayerScale
  8. DropPath

注意力机制:

  • 交替使用两种注意力模式:
    • Frame-wise Attention: 每帧内的点相互注意
    • Batch-wise Attention: 整个batch的所有点相互注意

2.2.4 CameraDecoder

TransformerDecoder(
in_dim=2048, # 最后一层decoder输出 * 2 (两个输出拼接)
dec_embed_dim=1024,
dec_num_heads=16,
out_dim=512,
depth=5,
rope=Embedding3D
)

2.2.5 CameraHead

CameraHead(dim=512)

结构:

nn.ModuleList([
ResConvBlock(512, 512), # 2个
ResConvBlock(512, 512)
])

nn.Sequential(
nn.Linear(512, 512),
nn.ReLU(),
nn.Linear(512, 512),
nn.ReLU()
)

nn.Linear(512, 3) # 平移向量
nn.Linear(512, 9) # 旋转矩阵 (9D表示)

输出: (B, 4, 4) - 相机姿态矩阵

2.2.6 Global Points Decoder (可选)

TransformerDecoder(
in_dim=2048,
dec_embed_dim=1024,
dec_num_heads=16,
out_dim=1024,
depth=5,
rope=Embedding3D
)

LinearPts3d(
dec_embed_dim=1024,
output_dim=3,
head_type="occupancy",
occ_bounds=(-1.0, 1.0),
occ_voxel_size=0.05
)

Occupancy Head 输出:

  • occ_logits: (num_voxels, 1) - 每个voxel的occupancy概率
  • pred_points: 预测的全局点

3. Forward 流程与尺度变化

3.1 输入数据

data_dict = {
'points': (B * S, N_points, 3), # B: batch大小, S: 每batch帧数, N_points: 每帧点数
'poses': (B * S, 4, 4), # 相机姿态 (c2w)
'point_offset': (B * S + 1,), # 每帧点云的offset
'frame_num': (B,), # 每batch的帧数
'grid_coord': (B * S, N_points, 3), # 网格坐标
'map': (B, N_map_points, 3), # 采样地图点
'scale': (B,) # 尺度因子
}

假设输入: B=2, S=8, N_points=3000

  • points: (16, 3000, 3)
  • poses: (16, 4, 4)
  • point_offset: (17,)
  • frame_num: (2,)

3.2 Utonia Encoder Forward

输入: points (B*S, N_points, 3)
+ color zeros (B*S, N_points, 3)
+ normal zeros (B*S, N_points, 3)
= (B*S, N_points, 9)

→ Embedding (Linear 9→54)
输出: (B*S, N_points, 54)

→ Serialization + Sparsify
点云被序列化和稀疏化

→ Stage 0: 3 Blocks (54 channels, 3 heads)
输出: (N0, 54)

→ Stage 1: GridPooling (54→108, stride=2) + 3 Blocks (108 channels, 6 heads)
输出: (N1, 108)

→ Stage 2: GridPooling (108→216, stride=2) + 3 Blocks (216 channels, 12 heads)
输出: (N2, 216)

→ Stage 3: GridPooling (216→432, stride=2) + 12 Blocks (432 channels, 24 heads)
输出: (N3, 432)

→ Stage 4: GridPooling (432→576, stride=2) + 3 Blocks (576 channels, 32 heads)
输出: (N_final, 576)

最终输出 super_point:
{
"coord": (N_final, 3),
"feat": (N_final, 576), # 特征维度 576
"batch": (N_final,), # batch索引
"offset": (B+1,), # 每帧的offset
...
}

注意: N_final < BSN_points,因为经过稀疏化和下采样

3.3 特征准备

hidden = super_point['feat'].clone()      # (N_final, 576)
pos = super_point["coord"].clone() # (N_final, 3)
batch_id = super_point['batch'].clone() # (N_final,)

# 计算每帧的offset
frame_seqlen = torch.bincount(batch_id) # (S,) 每帧点数
offset = nn.functional.pad(torch.cumsum(frame_seqlen, 0), (1, 0)) # (S+1,)

# 创建anchor indices (第一帧为anchor)
anchor_indices = torch.zeros(N_final, dtype=torch.bool)
for i in range(B):
start_idx = offset[batch_start_indices[i]]
end_idx = offset[batch_start_indices[i]+1]
anchor_indices[start_idx:end_idx] = True

3.4 GeoRegTransformer Forward

3.4.1 序列长度计算

frame_seqlen = torch.bincount(batch_id)  # (S,) 每帧点数
batch_seqlen = torch.zeros(B, dtype=torch.int64)

for i in range(B):
start_idx = batch_start_indices[i]
end_idx = start_idx + frame_num[i]
batch_seqlen[i] = frame_seqlen[start_idx:end_idx].sum()

# Frame-wise attention 参数
max_seqlen_frame = frame_seqlen.max() # 最大单帧点数
cu_seqlens_frame = pad(cumsum(frame_seqlen, 0), (1, 0)) # (S+1,)

# Batch-wise attention 参数
max_seqlen_batch = batch_seqlen.max() # 最大batch点数
cu_seqlens_batch = pad(cumsum(batch_seqlen, 0), (1, 0)) # (B+1,)

3.4.2 特征投影

hidden = self.proj(hidden)  # (N_final, 576) → (N_final, 1024)

3.4.3 Decoder Blocks (36层)

for i in range(36):
if i % 2 == 0:
# Frame-wise attention
cu_seqlens = cu_seqlens_frame
max_seqlen = max_seqlen_frame
else:
# Batch-wise attention
cu_seqlens = cu_seqlens_batch
max_seqlen = max_seqlen_batch

# BlockRope forward:
# 1. norm1 → FlashAttentionRope → ls1 → drop_path
# 2. norm2 → MLP → ls2 → drop_path

hidden = blk(hidden, pos, anchor_indices, cu_seqlens, max_seqlen)
# 输入: (N_final, 1024)
# 输出: (N_final, 1024)

# 收集最后两层输出
final_output = [hidden_at_layer_35, hidden_at_layer_36]
hidden = concat([final_output[0], final_output[1]], dim=-1) # (N_final, 2048)

每层 BlockRope 内部:

# Attention with 3D RoPE
x = self.norm1(x)
x = self.rope(x, pos, anchor_indices) # 应用3D位置编码
qkv = self.qkv(x).reshape(-1, 3, num_heads, head_dim)
q, k, v = qkv.unbind(1)
q, k = self.q_norm(q), self.k_norm(k)
qkv = stack([q, k, v], dim=1)
out = flash_attn.flash_attn_varlen_qkvpacked(qkv, cu_seqlens, max_seqlen)
out = self.proj(out)

# MLP
x = self.norm2(x)
x = self.mlp(x) # Linear(1024, 4096) → GELU → Linear(4096, 1024)

3.4.4 Camera Decoder (5层)

camera_hidden = self.camera_decoder(hidden, pos, anchor_indices, cu_seqlens_frame, max_seqlen_frame)
# 输入: (N_final, 2048)
# 输出: (N_final, 512)

3.4.5 Camera Head

camera_poses = self.camera_head(camera_hidden, cu_seqlens_frame)
# 输入: (N_final, 512)
# 输出: (B, 4, 4) - 预测的相机姿态

CameraHead 内部:

# 1. ResConvBlocks
for i in range(2):
feat = self.res_conv[i](feat) # (N_final, 512) → (N_final, 512)

# 2. 按帧平均池化
feat_list = []
for i in range(B):
start = cu_seqlens_frame[i]
end = cu_seqlens_frame[i+1]
feat_single = feat[start:end].mean(dim=0) # (512,)
feat_list.append(feat_single)
feat_pooled = cat(feat_list, dim=0) # (B, 512)

# 3. 额外MLP
feat = self.more_mlps(feat_pooled) # (B, 512) → (B, 512)

# 4. 预测平移和旋转
out_t = self.fc_t(feat) # (B, 3)
out_r = self.fc_rot(feat) # (B, 9)

# 5. 转换为4x4姿态矩阵
pose = convert_pose_to_4x4(out_r, out_t) # (B, 4, 4)

3.4.6 Global Points Decoder (可选)

if self.predict_global_point:
global_point_hidden = self.global_points_decoder(hidden, pos, anchor_indices, ...)
# 输入: (N_final, 2048)
# 输出: (N_tokens, 1024)

occ_logits, token_offset, grid, raw_points_gt = self.global_point_head(global_point_hidden, super_point, poses)
# 输出:
# - occ_logits: (num_voxels, 1) occupancy概率
# - pred_points: 预测的全局点

3.5 完整 Forward 流程总结

输入数据:
├── points: (B*S, N_points, 3)
├── poses: (B*S, 4, 4)
├── point_offset: (B*S + 1,)
├── frame_num: (B,)
├── grid_coord: (B*S, N_points, 3)
└── map: (B, N_map, 3)

↓ Utonia Encoder

super_point:
├── coord: (N_enc, 3)
├── feat: (N_enc, 576) ← 特征维度 576
├── batch: (N_enc,)
└── offset: (B+1,)

↓ 特征准备

hidden: (N_enc, 576)
pos: (N_enc, 3)
batch_id: (N_enc,)
anchor_indices: (N_enc,) bool
frame_seqlen: (S,)
cu_seqlens_frame: (S+1,)
cu_seqlens_batch: (B+1,)

↓ GeoRegTransformer

1. Projects: (N_enc, 576) → (N_enc, 1024)

2. Decoder Blocks (36层):
- 交替使用 frame-wise / batch-wise attention
- 每层输入/输出: (N_enc, 1024)
- 最后两层拼接: (N_enc, 2048)

3. Camera Decoder (5层):
- 输入: (N_enc, 2048)
- 输出: (N_enc, 512)

4. Camera Head:
- 输入: (N_enc, 512)
- 按帧平均池化: (B, 512)
- 输出姿态: (B, 4, 4)

↓ 输出

output_dict:
├── pred_camera_poses: (B, 4, 4) ← 预测的相机姿态
├── pred_occ_logits: (num_voxels, 1) ← (可选) occupancy预测
├── pred_points: ... ← (可选) 预测的全局点
├── raw_camera_poses: (B*S, 4, 4) ← 原始GT姿态
├── raw_points: (N_enc, 3) ← 原始点坐标
├── frame_num: (B,) ← 每batch帧数
└── ...

4. 关键维度汇总表

模块 输入维度 输出维度 说明
Utonia Encoder
- Embedding (N, 9) (N, 54) 9→54
- Stage 0 (N, 54) (N0, 54) 3 blocks
- Stage 1 (N0, 54) (N1, 108) Pool + 3 blocks
- Stage 2 (N1, 108) (N2, 216) Pool + 3 blocks
- Stage 3 (N2, 216) (N3, 432) Pool + 12 blocks
- Stage 4 (N3, 432) (N_enc, 576) Pool + 3 blocks
GeoRegTransformer
- Projects (N_enc, 576) (N_enc, 1024) 特征投影
- Embedding3D (N_enc, 1024) + (N_enc, 3) (N_enc, 1024) 3D位置编码
- Decoder Block (N_enc, 1024) (N_enc, 1024) 36层每层
- Final concat (N_enc, 1024) × 2 (N_enc, 2048) 最后两层拼接
- Camera Decoder (N_enc, 2048) (N_enc, 512) 5层
- Camera Head (N_enc, 512) (B, 4, 4) 姿态预测

5. 训练配置

从 train_utonia_indoor_from_base_weights.sh 提取的关键配置:

# 模型权重
UTONIA_CKPT=/home/zhangliyuan/HouPossion/RPF/weights/utonia/utonia.pth
FLOW_MODEL_CKPT=/home/zhangliyuan/HouPossion/RPF/weights/pi3/model.safetensors

# 数据配置
DATA_MAX_POINTS=24000
MAX_FRAMES_PER_GPU=192
TRAIN_MAX_VIEWS=16
EVAL_MAX_VIEWS=16

# 训练策略
TRAIN_ACCUMULATE_GRAD_BATCHES=1
FLOW_NUM_DEC_BLK_NOT_TO_CHECKPOINT=8
INCLUDE_SCANNETPP=1

6. 参数量估算

模块 参数量估计
Utonia Encoder (PointTransformerV3) ~30M
GeoRegTransformer ~400M
- Projects ~1.5M
- 36 Decoder Blocks ~350M
- Camera Decoder + Head ~50M
总计 ~430M+

— 本篇完 —

个人学习与研究记录。引用论文请以原文为准,欢迎交流与指正。

搜索研究笔记

输入关键词,探索笔记中的内容。