读Shape-Guided代码③训练

关于训练,是先把pretrain也做成npz

bash 复制代码
                            BS: 32
                            LR: 0.0001
                     POINT_NUM: 500
                     ckpt_path: ./checkpoint_debug/2024-03-11-16-38-30-knn500
                       classes: ['*']
                 datasets_path: None
              dict_n_component: 3
                         epoch: 1000
                     grid_path: data_new/
                     group_mul: 5
                    image_size: 224
                      k_number: 1
                   method_name: None
                    output_dir: None
                    rgb_method: Dict
                  sampled_size: 20
                           viz: False

这个训练batch还必须满足batch_size,如果不满足直接跳过

于是每epoch的每个batch,有points和samples,二者都是32,500,3

输入point作为gt,sample作为inputs_points

python 复制代码
SDF_Model(
  (encoder): encoder_BN(
    (conv1): Conv1d(3, 64, kernel_size=(1,), stride=(1,))
    (conv2): Conv1d(64, 128, kernel_size=(1,), stride=(1,))
    (conv3): Conv1d(128, 1024, kernel_size=(1,), stride=(1,))
    (fc1): Linear(in_features=1024, out_features=512, bias=True)
    (fc2): Linear(in_features=512, out_features=128, bias=True)
    (relu): ReLU()
    (bn1): BatchNorm1d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (bn2): BatchNorm1d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (bn3): BatchNorm1d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (bn4): BatchNorm1d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (bn5): BatchNorm1d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  )
  把gt经过encoder得到32,128的feature
  再把它tile成32,500,128 point_feature
  然后point_feature和inputs_points被输入给NIL的get_gradient
  (NIF): local_NIF(
    (fc1): Linear(in_features=128, out_features=512, bias=True)
    (fc2): Linear(in_features=3, out_features=512, bias=True)
    (fc3): Linear(in_features=1024, out_features=512, bias=True)
    (fc40): Linear(in_features=512, out_features=512, bias=True)
    (fc41): Linear(in_features=512, out_features=512, bias=True)
    (fc42): Linear(in_features=512, out_features=512, bias=True)
    (fc43): Linear(in_features=512, out_features=512, bias=True)
    (fc44): Linear(in_features=512, out_features=512, bias=True)
    (fc45): Linear(in_features=512, out_features=512, bias=True)
    (fc46): Linear(in_features=512, out_features=512, bias=True)
    (fc5): Linear(in_features=512, out_features=1, bias=True)
  )
)

get_gradient方法在local_NIF类中用于计算输入点相对于输出SDF(有符号距离字段)值的梯度,并使用这个梯度来更新点的位置。下面是数据流向和处理步骤的详细分析:

输入梯度设置:input_points.requires_grad_(True)确保input_points可以计算梯度。这是为了使得在SDF输出值forward方法中关于input_points的梯度能够被计算。

前向传播:使用forward方法计算SDF值(32,500,1)。这个方法先通过两个独立的全连接层处理points_feature和input_points,然后将这两个特征合并,并通过一系列额外的全连接层进一步处理。

计算梯度(一个32,500,3组成的tuple):torch.autograd.grad用于计算SDF输出相对于input_points的梯度。这里使用torch.ones_like(sdf)作为梯度的权重,因为我们对SDF本身的梯度感兴趣,而不是对它的某个函数的梯度。

梯度归一化:计算梯度的模长normal_p_length,并用它来归一化梯度,得到单位向量grad_norm。这一步确保了梯度向量在每个方向上具有相同的长度,这在计算新的点位置时是有用的。pytorch_safe_norm函数用于计算向量的模长,避免除零错误,通过在求和后添加一个很小的数(epsilon)来实现。

计算更新的点位置:使用梯度信息更新input_points的位置。g_point = input_points - sdf * grad_norm根据梯度方向和大小调整每个点的位置,这里sdf * grad_norm计算每个点沿梯度方向的位移,然后从原始位置减去这个位移得到新的位置(32,500,3这也就是网络的最终输出了)

整体来看,get_gradient方法通过计算SDF相对于输入点的梯度,并使用这个梯度来更新点的位置,这在许多应用中是有用的,例如形状优化、网格重构等。

计算g_point和points的l2范数作为损失

相关推荐
新启航-光学3D测量6 小时前
从 48 小时到 4 小时:三维逆向工程中自动化工具链如何重构扫描建模效率
科技·3d·制造
彩旗工作室12 小时前
腾讯混元3D系列开源模型:从工业级到移动端的本地部署
3d·开源·腾讯混元
CG_MAGIC12 小时前
主流 3D 模型格式(FBX/OBJ/DAE/GLTF)材质支持与转换操作指南
3d·渲染·动画·材质·贴图·3d 模型格式·材质支持与转换操作指南
计算机科研圈19 小时前
ICCV 2025 | 首个3D动作游戏专用VLA模型,打黑神话&只狼超越人类玩家
图像处理·人工智能·3d·黑神话
LetsonH1 天前
⭐CVPR2025 RigGS:从 2D 视频到可编辑 3D 关节物体的建模新范式
3d
郝学胜-神的一滴2 天前
Three.js 材质系统深度解析
javascript·3d·游戏引擎·webgl·材质
AndrewHZ2 天前
【3D重建技术】如何基于遥感图像和DEM等数据进行城市级高精度三维重建?
图像处理·人工智能·深度学习·3d·dem·遥感图像·3d重建
二川bro3 天前
第16节:自定义几何体 - 从顶点构建3D世界
3d
迈火3 天前
ComfyUI-3D-Pack:3D创作的AI神器
人工智能·gpt·3d·ai·stable diffusion·aigc·midjourney
新启航光学频率梳5 天前
【新启航】起落架大型结构件深孔检测探究 - 激光频率梳 3D 轮廓检测
科技·3d·制造